Skip to content

Commit

Permalink
make node-addon-api examples context-sensistive (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielschulhof authored Jun 10, 2020
1 parent b024ee9 commit dc86a66
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 13.x]
node-version: [10.21.0, 12.18.0, 13.x, 14.x]
operating-system: [ubuntu-latest, windows-latest, macos-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: node_js
node_js:
- "8"
- "10"
- "12"
- "13"
- "14"
cache:
npm
before_install:
Expand Down
9 changes: 4 additions & 5 deletions 6_object_wrap/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "myobject.h"

Napi::FunctionReference MyObject::constructor;

Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Expand All @@ -12,8 +10,9 @@ Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
InstanceMethod("value", &MyObject::GetValue),
InstanceMethod("multiply", &MyObject::Multiply)});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);

exports.Set("MyObject", func);
return exports;
Expand Down Expand Up @@ -55,7 +54,7 @@ Napi::Value MyObject::Multiply(const Napi::CallbackInfo& info) {
multiple = info[0].As<Napi::Number>();
}

Napi::Object obj = constructor.New(
Napi::Object obj = info.Env().GetInstanceData<Napi::FunctionReference>()->New(
{Napi::Number::New(info.Env(), this->value_ * multiple.DoubleValue())});

return obj;
Expand Down
2 changes: 0 additions & 2 deletions 6_object_wrap/node-addon-api/myobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class MyObject : public Napi::ObjectWrap<MyObject> {
MyObject(const Napi::CallbackInfo& info);

private:
static Napi::FunctionReference constructor;

Napi::Value GetValue(const Napi::CallbackInfo& info);
Napi::Value PlusOne(const Napi::CallbackInfo& info);
Napi::Value Multiply(const Napi::CallbackInfo& info);
Expand Down
5 changes: 4 additions & 1 deletion 6_object_wrap/node-addon-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"main": "addon.js",
"private": true,
"gypfile": true,
"engines": {
"node": "~10 >=10.20 || >=12.17"
},
"dependencies": {
"bindings": "~1.2.1",
"node-addon-api": "^1.0.0"
"node-addon-api": "^3.0.0"
}
}
9 changes: 4 additions & 5 deletions 7_factory_wrap/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

using namespace Napi;

Napi::FunctionReference MyObject::constructor;

Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Napi::Function func = DefineClass(
env, "MyObject", {InstanceMethod("plusOne", &MyObject::PlusOne)});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);

exports.Set("MyObject", func);
return exports;
Expand All @@ -29,7 +28,7 @@ MyObject::MyObject(const Napi::CallbackInfo& info)

Napi::Object MyObject::NewInstance(Napi::Env env, Napi::Value arg) {
Napi::EscapableHandleScope scope(env);
Napi::Object obj = constructor.New({arg});
Napi::Object obj = env.GetInstanceData<Napi::FunctionReference>()->New({arg});
return scope.Escape(napi_value(obj)).ToObject();
}

Expand Down
1 change: 0 additions & 1 deletion 7_factory_wrap/node-addon-api/myobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class MyObject : public Napi::ObjectWrap<MyObject> {
MyObject(const Napi::CallbackInfo& info);

private:
static Napi::FunctionReference constructor;
Napi::Value PlusOne(const Napi::CallbackInfo& info);
double counter_;
};
Expand Down
5 changes: 4 additions & 1 deletion 7_factory_wrap/node-addon-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"main": "addon.js",
"private": true,
"gypfile": true,
"engines": {
"node": "~10 >=10.20 || >=12.17"
},
"dependencies": {
"bindings": "~1.2.1",
"node-addon-api": "^1.0.0"
"node-addon-api": "^3.0.0"
}
}
2 changes: 1 addition & 1 deletion 8_passing_wrapped/node-addon-api/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using namespace Napi;

Napi::Object CreateObject(const Napi::CallbackInfo& info) {
return MyObject::NewInstance(info[0]);
return MyObject::NewInstance(info.Env(), info[0]);
}

Napi::Number Add(const Napi::CallbackInfo& info) {
Expand Down
11 changes: 5 additions & 6 deletions 8_passing_wrapped/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ MyObject::MyObject(const Napi::CallbackInfo& info)
this->val_ = info[0].As<Napi::Number>().DoubleValue();
};

Napi::FunctionReference MyObject::constructor;

void MyObject::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Napi::Function func = DefineClass(env, "MyObject", {});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);

exports.Set("MyObject", func);
}

Napi::Object MyObject::NewInstance(Napi::Value arg) {
Napi::Object obj = constructor.New({arg});
Napi::Object MyObject::NewInstance(Napi::Env env, Napi::Value arg) {
Napi::Object obj = env.GetInstanceData<Napi::FunctionReference>()->New({arg});
return obj;
}
3 changes: 1 addition & 2 deletions 8_passing_wrapped/node-addon-api/myobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
class MyObject : public Napi::ObjectWrap<MyObject> {
public:
static void Init(Napi::Env env, Napi::Object exports);
static Napi::Object NewInstance(Napi::Value arg);
static Napi::Object NewInstance(Napi::Env env, Napi::Value arg);
double Val() const { return val_; }
MyObject(const Napi::CallbackInfo& info);

private:
static Napi::FunctionReference constructor;
double val_;
};

Expand Down
5 changes: 4 additions & 1 deletion 8_passing_wrapped/node-addon-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"main": "addon.js",
"private": true,
"gypfile": true,
"engines": {
"node": "~10 >=10.20 || >=12.17"
},
"dependencies": {
"bindings": "~1.2.1",
"node-addon-api": "^1.0.0"
"node-addon-api": "^3.0.0"
}
}

0 comments on commit dc86a66

Please sign in to comment.