@@ -22,49 +22,57 @@ your C++ class methods.
22
22
class Example : public Napi ::ObjectWrap<Example > {
23
23
public:
24
24
static Napi::Object Init(Napi::Env env, Napi::Object exports);
25
- Example(const Napi::CallbackInfo &info);
25
+ Example(const Napi::CallbackInfo& info);
26
+ static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
26
27
27
28
private:
28
- static Napi::FunctionReference constructor;
29
29
double _ value;
30
- Napi::Value GetValue(const Napi::CallbackInfo & info);
31
- Napi::Value SetValue(const Napi::CallbackInfo & info);
30
+ Napi::Value GetValue(const Napi::CallbackInfo& info);
31
+ Napi::Value SetValue(const Napi::CallbackInfo& info);
32
32
};
33
33
34
34
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
35
35
// This method is used to hook the accessor and method callbacks
36
36
Napi::Function func = DefineClass(env, "Example", {
37
37
InstanceMethod<&Example::GetValue>("GetValue"),
38
- InstanceMethod<&Example::SetValue>("SetValue")
38
+ InstanceMethod<&Example::SetValue>("SetValue"),
39
+ StaticMethod<&Example::CreateNewItem>("CreateNewItem"),
39
40
});
40
41
42
+ Napi::FunctionReference* constructor = new Napi::FunctionReference();
43
+
41
44
// Create a peristent reference to the class constructor. This will allow
42
45
// a function called on a class prototype and a function
43
46
// called on instance of a class to be distinguished from each other.
44
- constructor = Napi::Persistent(func);
45
- // Call the SuppressDestruct() method on the static data prevent the calling
46
- // to this destructor to reset the reference when the environment is no longer
47
- // available.
48
- constructor.SuppressDestruct();
47
+ *constructor = Napi::Persistent(func);
49
48
exports.Set("Example", func);
49
+
50
+ // Store the constructor as the add-on instance data. This will allow this
51
+ // add-on to support multiple instances of itself running on multiple worker
52
+ // threads, as well as multiple instances of itself running in different
53
+ // contexts on the same thread.
54
+ //
55
+ // By default, the value set on the environment here will be destroyed when
56
+ // the add-on is unloaded using the `delete` operator, but it is also
57
+ // possible to supply a custom deleter.
58
+ env.SetInstanceData<Napi::FunctionReference>(constructor);
59
+
50
60
return exports;
51
61
}
52
62
53
- Example::Example(const Napi::CallbackInfo & info) : Napi::ObjectWrap<Example >(info) {
63
+ Example::Example(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Example >(info) {
54
64
Napi::Env env = info.Env();
55
65
// ...
56
66
Napi::Number value = info[ 0] .As< Napi::Number > ();
57
67
this->_ value = value.DoubleValue();
58
68
}
59
69
60
- Napi::FunctionReference Example::constructor;
61
-
62
- Napi::Value Example::GetValue(const Napi::CallbackInfo &info){
70
+ Napi::Value Example::GetValue(const Napi::CallbackInfo& info){
63
71
Napi::Env env = info.Env();
64
72
return Napi::Number::New(env, this->_ value);
65
73
}
66
74
67
- Napi::Value Example::SetValue(const Napi::CallbackInfo & info){
75
+ Napi::Value Example::SetValue(const Napi::CallbackInfo& info){
68
76
Napi::Env env = info.Env();
69
77
// ...
70
78
Napi::Number value = info[ 0] .As< Napi::Number > ();
@@ -78,6 +86,16 @@ Napi::Object Init (Napi::Env env, Napi::Object exports) {
78
86
return exports;
79
87
}
80
88
89
+ // Create a new item using the constructor stored during Init.
90
+ Napi::Value Example::CreateNewItem(const Napi::CallbackInfo& info) {
91
+ // Retrieve the instance data we stored during ` Init() ` . We only stored the
92
+ // constructor there, so we retrieve it here to create a new instance of the
93
+ // JS class the constructor represents.
94
+ Napi::FunctionReference* constructor =
95
+ info.Env().GetInstanceData< Napi::FunctionReference > ();
96
+ return constructor->New({ Napi::Number::New(info.Env(), 42) });
97
+ }
98
+
81
99
// Register and initialize native add-on
82
100
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
83
101
```
0 commit comments