File tree Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 14
14
#include < cstring>
15
15
#include < mutex>
16
16
#include < type_traits>
17
+ #include < utility>
17
18
18
19
namespace Napi {
19
20
@@ -2156,7 +2157,7 @@ inline Function Function::New(napi_env env,
2156
2157
void * data) {
2157
2158
using ReturnType = decltype (cb (CallbackInfo (nullptr , nullptr )));
2158
2159
using CbData = details::CallbackData<Callable, ReturnType>;
2159
- auto callbackData = new CbData ({ cb , data }) ;
2160
+ auto callbackData = new CbData{ std::move (cb) , data} ;
2160
2161
2161
2162
napi_value value;
2162
2163
napi_status status = CreateFunction (env,
Original file line number Diff line number Diff line change
1
+ #include < memory>
1
2
#include " napi.h"
2
3
#include " test_helper.h"
3
4
@@ -271,5 +272,24 @@ Object InitFunction(Env env) {
271
272
exports[" callWithFunctionOperator" ] =
272
273
Function::New<CallWithFunctionOperator>(env);
273
274
result[" templated" ] = exports;
275
+
276
+ exports = Object::New (env);
277
+ exports[" lambdaWithNoCapture" ] =
278
+ Function::New (env, [](const CallbackInfo& info) {
279
+ auto env = info.Env ();
280
+ return Boolean::New (env, true );
281
+ });
282
+ exports[" lambdaWithCapture" ] =
283
+ Function::New (env, [data = 42 ](const CallbackInfo& info) {
284
+ auto env = info.Env ();
285
+ return Boolean::New (env, data == 42 );
286
+ });
287
+ exports[" lambdaWithMoveOnlyCapture" ] = Function::New (
288
+ env, [data = std::make_unique<int >(42 )](const CallbackInfo& info) {
289
+ auto env = info.Env ();
290
+ return Boolean::New (env, *data == 42 );
291
+ });
292
+ result[" lambda" ] = exports;
293
+
274
294
return result;
275
295
}
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ const assert = require('assert');
5
5
module . exports = require ( './common' ) . runTest ( binding => {
6
6
test ( binding . function . plain ) ;
7
7
test ( binding . function . templated ) ;
8
+ testLambda ( binding . function . lambda ) ;
8
9
} ) ;
9
10
10
11
function test ( binding ) {
@@ -112,3 +113,9 @@ function test(binding) {
112
113
binding . makeCallbackWithInvalidReceiver ( ( ) => { } ) ;
113
114
} ) ;
114
115
}
116
+
117
+ function testLambda ( binding ) {
118
+ assert . ok ( binding . lambdaWithNoCapture ( ) ) ;
119
+ assert . ok ( binding . lambdaWithCapture ( ) ) ;
120
+ assert . ok ( binding . lambdaWithMoveOnlyCapture ( ) ) ;
121
+ }
You can’t perform that action at this time.
0 commit comments