@@ -6,6 +6,13 @@ namespace {
6
6
7
7
int testData = 1 ;
8
8
9
+ Boolean EmptyConstructor (const CallbackInfo& info) {
10
+ auto env = info.Env ();
11
+ bool isEmpty = info[0 ].As <Boolean>();
12
+ Function function = isEmpty ? Function () : Function (env, Object::New (env));
13
+ return Boolean::New (env, function.IsEmpty ());
14
+ }
15
+
9
16
void VoidCallback (const CallbackInfo& info) {
10
17
auto env = info.Env ();
11
18
Object obj = info[0 ].As <Object>();
@@ -45,8 +52,9 @@ Value ValueCallbackWithData(const CallbackInfo& info) {
45
52
}
46
53
47
54
Value CallWithArgs (const CallbackInfo& info) {
48
- Function func = info[0 ].As <Function>();
49
- return func ({ info[1 ], info[2 ], info[3 ] });
55
+ Function func = info[0 ].As <Function>();
56
+ return func.Call (
57
+ std::initializer_list<napi_value>{info[1 ], info[2 ], info[3 ]});
50
58
}
51
59
52
60
Value CallWithVector (const CallbackInfo& info) {
@@ -59,6 +67,27 @@ Value CallWithVector(const CallbackInfo& info) {
59
67
return func.Call (args);
60
68
}
61
69
70
+ Value CallWithCStyleArray (const CallbackInfo& info) {
71
+ Function func = info[0 ].As <Function>();
72
+ std::vector<napi_value> args;
73
+ args.reserve (3 );
74
+ args.push_back (info[1 ]);
75
+ args.push_back (info[2 ]);
76
+ args.push_back (info[3 ]);
77
+ return func.Call (args.size (), args.data ());
78
+ }
79
+
80
+ Value CallWithReceiverAndCStyleArray (const CallbackInfo& info) {
81
+ Function func = info[0 ].As <Function>();
82
+ Value receiver = info[1 ];
83
+ std::vector<napi_value> args;
84
+ args.reserve (3 );
85
+ args.push_back (info[2 ]);
86
+ args.push_back (info[3 ]);
87
+ args.push_back (info[4 ]);
88
+ return func.Call (receiver, args.size (), args.data ());
89
+ }
90
+
62
91
Value CallWithReceiverAndArgs (const CallbackInfo& info) {
63
92
Function func = info[0 ].As <Function>();
64
93
Value receiver = info[1 ];
@@ -96,17 +125,81 @@ Value CallConstructorWithVector(const CallbackInfo& info) {
96
125
return func.New (args);
97
126
}
98
127
128
+ Value CallConstructorWithCStyleArray (const CallbackInfo& info) {
129
+ Function func = info[0 ].As <Function>();
130
+ std::vector<napi_value> args;
131
+ args.reserve (3 );
132
+ args.push_back (info[1 ]);
133
+ args.push_back (info[2 ]);
134
+ args.push_back (info[3 ]);
135
+ return func.New (args.size (), args.data ());
136
+ }
137
+
99
138
void IsConstructCall (const CallbackInfo& info) {
100
139
Function callback = info[0 ].As <Function>();
101
140
bool isConstructCall = info.IsConstructCall ();
102
141
callback ({Napi::Boolean::New (info.Env (), isConstructCall)});
103
142
}
104
143
144
+ void MakeCallbackWithArgs (const CallbackInfo& info) {
145
+ Env env = info.Env ();
146
+ Function callback = info[0 ].As <Function>();
147
+ Object resource = info[1 ].As <Object>();
148
+
149
+ AsyncContext context (env, " function_test_context" , resource);
150
+
151
+ callback.MakeCallback (
152
+ resource,
153
+ std::initializer_list<napi_value>{info[2 ], info[3 ], info[4 ]},
154
+ context);
155
+ }
156
+
157
+ void MakeCallbackWithVector (const CallbackInfo& info) {
158
+ Env env = info.Env ();
159
+ Function callback = info[0 ].As <Function>();
160
+ Object resource = info[1 ].As <Object>();
161
+
162
+ AsyncContext context (env, " function_test_context" , resource);
163
+
164
+ std::vector<napi_value> args;
165
+ args.reserve (3 );
166
+ args.push_back (info[2 ]);
167
+ args.push_back (info[3 ]);
168
+ args.push_back (info[4 ]);
169
+ callback.MakeCallback (resource, args, context);
170
+ }
171
+
172
+ void MakeCallbackWithCStyleArray (const CallbackInfo& info) {
173
+ Env env = info.Env ();
174
+ Function callback = info[0 ].As <Function>();
175
+ Object resource = info[1 ].As <Object>();
176
+
177
+ AsyncContext context (env, " function_test_context" , resource);
178
+
179
+ std::vector<napi_value> args;
180
+ args.reserve (3 );
181
+ args.push_back (info[2 ]);
182
+ args.push_back (info[3 ]);
183
+ args.push_back (info[4 ]);
184
+ callback.MakeCallback (resource, args.size (), args.data (), context);
185
+ }
186
+
187
+ void MakeCallbackWithInvalidReceiver (const CallbackInfo& info) {
188
+ Function callback = info[0 ].As <Function>();
189
+ callback.MakeCallback (Value (), std::initializer_list<napi_value>{});
190
+ }
191
+
192
+ Value CallWithFunctionOperator (const CallbackInfo& info) {
193
+ Function func = info[0 ].As <Function>();
194
+ return func ({info[1 ], info[2 ], info[3 ]});
195
+ }
196
+
105
197
} // end anonymous namespace
106
198
107
199
Object InitFunction (Env env) {
108
200
Object result = Object::New (env);
109
201
Object exports = Object::New (env);
202
+ exports[" emptyConstructor" ] = Function::New (env, EmptyConstructor);
110
203
exports[" voidCallback" ] = Function::New (env, VoidCallback, " voidCallback" );
111
204
exports[" valueCallback" ] = Function::New (env, ValueCallback, std::string (" valueCallback" ));
112
205
exports[" voidCallbackWithData" ] =
@@ -115,15 +208,30 @@ Object InitFunction(Env env) {
115
208
Function::New (env, ValueCallbackWithData, nullptr , &testData);
116
209
exports[" callWithArgs" ] = Function::New (env, CallWithArgs);
117
210
exports[" callWithVector" ] = Function::New (env, CallWithVector);
211
+ exports[" callWithCStyleArray" ] = Function::New (env, CallWithCStyleArray);
212
+ exports[" callWithReceiverAndCStyleArray" ] =
213
+ Function::New (env, CallWithReceiverAndCStyleArray);
118
214
exports[" callWithReceiverAndArgs" ] = Function::New (env, CallWithReceiverAndArgs);
119
215
exports[" callWithReceiverAndVector" ] = Function::New (env, CallWithReceiverAndVector);
120
216
exports[" callWithInvalidReceiver" ] = Function::New (env, CallWithInvalidReceiver);
121
217
exports[" callConstructorWithArgs" ] = Function::New (env, CallConstructorWithArgs);
122
218
exports[" callConstructorWithVector" ] = Function::New (env, CallConstructorWithVector);
219
+ exports[" callConstructorWithCStyleArray" ] =
220
+ Function::New (env, CallConstructorWithCStyleArray);
123
221
exports[" isConstructCall" ] = Function::New (env, IsConstructCall);
222
+ exports[" makeCallbackWithArgs" ] = Function::New (env, MakeCallbackWithArgs);
223
+ exports[" makeCallbackWithVector" ] =
224
+ Function::New (env, MakeCallbackWithVector);
225
+ exports[" makeCallbackWithCStyleArray" ] =
226
+ Function::New (env, MakeCallbackWithCStyleArray);
227
+ exports[" makeCallbackWithInvalidReceiver" ] =
228
+ Function::New (env, MakeCallbackWithInvalidReceiver);
229
+ exports[" callWithFunctionOperator" ] =
230
+ Function::New (env, CallWithFunctionOperator);
124
231
result[" plain" ] = exports;
125
232
126
233
exports = Object::New (env);
234
+ exports[" emptyConstructor" ] = Function::New (env, EmptyConstructor);
127
235
exports[" voidCallback" ] = Function::New<VoidCallback>(env, " voidCallback" );
128
236
exports[" valueCallback" ] =
129
237
Function::New<ValueCallback>(env, std::string (" valueCallback" ));
@@ -133,6 +241,9 @@ Object InitFunction(Env env) {
133
241
Function::New<ValueCallbackWithData>(env, nullptr , &testData);
134
242
exports[" callWithArgs" ] = Function::New<CallWithArgs>(env);
135
243
exports[" callWithVector" ] = Function::New<CallWithVector>(env);
244
+ exports[" callWithCStyleArray" ] = Function::New<CallWithCStyleArray>(env);
245
+ exports[" callWithReceiverAndCStyleArray" ] =
246
+ Function::New<CallWithReceiverAndCStyleArray>(env);
136
247
exports[" callWithReceiverAndArgs" ] =
137
248
Function::New<CallWithReceiverAndArgs>(env);
138
249
exports[" callWithReceiverAndVector" ] =
@@ -143,7 +254,18 @@ Object InitFunction(Env env) {
143
254
Function::New<CallConstructorWithArgs>(env);
144
255
exports[" callConstructorWithVector" ] =
145
256
Function::New<CallConstructorWithVector>(env);
257
+ exports[" callConstructorWithCStyleArray" ] =
258
+ Function::New<CallConstructorWithCStyleArray>(env);
146
259
exports[" isConstructCall" ] = Function::New<IsConstructCall>(env);
260
+ exports[" makeCallbackWithArgs" ] = Function::New<MakeCallbackWithArgs>(env);
261
+ exports[" makeCallbackWithVector" ] =
262
+ Function::New<MakeCallbackWithVector>(env);
263
+ exports[" makeCallbackWithCStyleArray" ] =
264
+ Function::New<MakeCallbackWithCStyleArray>(env);
265
+ exports[" makeCallbackWithInvalidReceiver" ] =
266
+ Function::New<MakeCallbackWithInvalidReceiver>(env);
267
+ exports[" callWithFunctionOperator" ] =
268
+ Function::New<CallWithFunctionOperator>(env);
147
269
result[" templated" ] = exports;
148
270
return result;
149
271
}
0 commit comments