3
3
4
4
using namespace Napi ;
5
5
6
+ class FuncRefObject : public Napi ::ObjectWrap<FuncRefObject> {
7
+ public:
8
+ FuncRefObject (const Napi::CallbackInfo& info)
9
+ : Napi::ObjectWrap<FuncRefObject>(info) {
10
+ Napi::Env env = info.Env ();
11
+ int argLen = info.Length ();
12
+ if (argLen <= 0 || !info[0 ].IsNumber ()) {
13
+ Napi::TypeError::New (env, " First param should be a number" )
14
+ .ThrowAsJavaScriptException ();
15
+ return ;
16
+ }
17
+ Napi::Number value = info[0 ].As <Napi::Number>();
18
+ this ->_value = value.Int32Value ();
19
+ }
20
+
21
+ Napi::Value GetValue (const Napi::CallbackInfo& info) {
22
+ int value = this ->_value ;
23
+ return Napi::Number::New (info.Env (), value);
24
+ }
25
+
26
+ private:
27
+ int _value;
28
+ };
29
+
6
30
namespace {
31
+
32
+ Value ConstructRefFromExisitingRef (const CallbackInfo& info) {
33
+ HandleScope scope (info.Env ());
34
+ FunctionReference ref;
35
+ FunctionReference movedRef;
36
+ ref.Reset (info[0 ].As <Function>());
37
+ movedRef = std::move (ref);
38
+
39
+ return MaybeUnwrap (movedRef ({}));
40
+ }
41
+
42
+ Value CallWithVectorArgs (const CallbackInfo& info) {
43
+ HandleScope scope (info.Env ());
44
+ std::vector<napi_value> newVec;
45
+ FunctionReference ref;
46
+ ref.Reset (info[0 ].As <Function>());
47
+
48
+ for (int i = 1 ; i < (int )info.Length (); i++) {
49
+ newVec.push_back (info[i]);
50
+ }
51
+ return MaybeUnwrap (ref.Call (newVec));
52
+ }
53
+
54
+ Value CallWithInitList (const CallbackInfo& info) {
55
+ HandleScope scope (info.Env ());
56
+ FunctionReference ref;
57
+ ref.Reset (info[0 ].As <Function>());
58
+
59
+ return MaybeUnwrap (ref.Call ({info[1 ], info[2 ], info[3 ]}));
60
+ }
61
+
62
+ Value CallWithRecvInitList (const CallbackInfo& info) {
63
+ HandleScope scope (info.Env ());
64
+ FunctionReference ref;
65
+ ref.Reset (info[0 ].As <Function>());
66
+
67
+ return MaybeUnwrap (ref.Call (info[1 ], {info[2 ], info[3 ], info[4 ]}));
68
+ }
69
+
70
+ Value CallWithRecvVector (const CallbackInfo& info) {
71
+ HandleScope scope (info.Env ());
72
+ FunctionReference ref;
73
+ std::vector<napi_value> newVec;
74
+ ref.Reset (info[0 ].As <Function>());
75
+
76
+ for (int i = 2 ; i < (int )info.Length (); i++) {
77
+ newVec.push_back (info[i]);
78
+ }
79
+ return MaybeUnwrap (ref.Call (info[1 ], newVec));
80
+ }
81
+
82
+ Value CallWithRecvArgc (const CallbackInfo& info) {
83
+ HandleScope scope (info.Env ());
84
+ FunctionReference ref;
85
+ int argLength = info.Length () - 2 ;
86
+ napi_value* args = new napi_value[argLength];
87
+ ref.Reset (info[0 ].As <Function>());
88
+
89
+ int argIdx = 0 ;
90
+ for (int i = 2 ; i < (int )info.Length (); i++, argIdx++) {
91
+ args[argIdx] = info[i];
92
+ }
93
+
94
+ return MaybeUnwrap (ref.Call (info[1 ], argLength, args));
95
+ }
96
+
97
+ Value MakeAsyncCallbackWithInitList (const Napi::CallbackInfo& info) {
98
+ Napi::FunctionReference ref;
99
+ ref.Reset (info[0 ].As <Function>());
100
+
101
+ Napi::AsyncContext context (info.Env (), " func_ref_resources" , {});
102
+
103
+ return MaybeUnwrap (
104
+ ref.MakeCallback (Napi::Object::New (info.Env ()), {}, context));
105
+ }
106
+
107
+ Value MakeAsyncCallbackWithVector (const Napi::CallbackInfo& info) {
108
+ Napi::FunctionReference ref;
109
+ ref.Reset (info[0 ].As <Function>());
110
+ std::vector<napi_value> newVec;
111
+ Napi::AsyncContext context (info.Env (), " func_ref_resources" , {});
112
+
113
+ for (int i = 1 ; i < (int )info.Length (); i++) {
114
+ newVec.push_back (info[i]);
115
+ }
116
+
117
+ return MaybeUnwrap (
118
+ ref.MakeCallback (Napi::Object::New (info.Env ()), newVec, context));
119
+ }
120
+
121
+ Value MakeAsyncCallbackWithArgv (const Napi::CallbackInfo& info) {
122
+ Napi::FunctionReference ref;
123
+ ref.Reset (info[0 ].As <Function>());
124
+ int argLength = info.Length () - 1 ;
125
+ napi_value* args = new napi_value[argLength];
126
+
127
+ int argIdx = 0 ;
128
+ for (int i = 1 ; i < (int )info.Length (); i++, argIdx++) {
129
+ args[argIdx] = info[i];
130
+ }
131
+
132
+ Napi::AsyncContext context (info.Env (), " func_ref_resources" , {});
133
+ return MaybeUnwrap (ref.MakeCallback (
134
+ Napi::Object::New (info.Env ()), argLength, args, context));
135
+ }
136
+
137
+ Value CreateFunctionReferenceUsingNew (const Napi::CallbackInfo& info) {
138
+ Napi::Function func = ObjectWrap<FuncRefObject>::DefineClass (
139
+ info.Env (),
140
+ " MyObject" ,
141
+ {ObjectWrap<FuncRefObject>::InstanceMethod (" getValue" ,
142
+ &FuncRefObject::GetValue)});
143
+ Napi::FunctionReference* constructor = new Napi::FunctionReference ();
144
+ *constructor = Napi::Persistent (func);
145
+
146
+ return MaybeUnwrapOr (constructor->New ({info[0 ].As <Number>()}), Object ());
147
+ }
148
+
149
+ Value CreateFunctionReferenceUsingNewVec (const Napi::CallbackInfo& info) {
150
+ Napi::Function func = ObjectWrap<FuncRefObject>::DefineClass (
151
+ info.Env (),
152
+ " MyObject" ,
153
+ {ObjectWrap<FuncRefObject>::InstanceMethod (" getValue" ,
154
+ &FuncRefObject::GetValue)});
155
+ Napi::FunctionReference* constructor = new Napi::FunctionReference ();
156
+ *constructor = Napi::Persistent (func);
157
+ std::vector<napi_value> newVec;
158
+ newVec.push_back (info[0 ]);
159
+
160
+ return MaybeUnwrapOr (constructor->New (newVec), Object ());
161
+ }
162
+
7
163
Value Call (const CallbackInfo& info) {
8
164
HandleScope scope (info.Env ());
9
165
FunctionReference ref;
@@ -23,7 +179,22 @@ Value Construct(const CallbackInfo& info) {
23
179
24
180
Object InitFunctionReference (Env env) {
25
181
Object exports = Object::New (env);
26
-
182
+ exports[" CreateFuncRefWithNew" ] =
183
+ Function::New (env, CreateFunctionReferenceUsingNew);
184
+ exports[" CreateFuncRefWithNewVec" ] =
185
+ Function::New (env, CreateFunctionReferenceUsingNewVec);
186
+ exports[" CallWithRecvArgc" ] = Function::New (env, CallWithRecvArgc);
187
+ exports[" CallWithRecvVector" ] = Function::New (env, CallWithRecvVector);
188
+ exports[" CallWithRecvInitList" ] = Function::New (env, CallWithRecvInitList);
189
+ exports[" CallWithInitList" ] = Function::New (env, CallWithInitList);
190
+ exports[" CallWithVec" ] = Function::New (env, CallWithVectorArgs);
191
+ exports[" ConstructWithMove" ] =
192
+ Function::New (env, ConstructRefFromExisitingRef);
193
+ exports[" AsyncCallWithInitList" ] =
194
+ Function::New (env, MakeAsyncCallbackWithInitList);
195
+ exports[" AsyncCallWithVector" ] =
196
+ Function::New (env, MakeAsyncCallbackWithVector);
197
+ exports[" AsyncCallWithArgv" ] = Function::New (env, MakeAsyncCallbackWithArgv);
27
198
exports[" call" ] = Function::New (env, Call);
28
199
exports[" construct" ] = Function::New (env, Construct);
29
200
0 commit comments