4
4
5
5
namespace {
6
6
7
- // the test needs to fake out the async structure, so we need to use
8
- // the raw structure here and then cast as done behind the scenes
9
- // in napi calls.
10
- struct async_context {
11
- double async_id;
12
- double trigger_async_id;
13
- };
14
-
15
-
16
7
napi_value RunInCallbackScope (napi_env env, napi_callback_info info) {
17
8
size_t argc;
18
- napi_value args[4 ];
9
+ napi_value args[3 ];
19
10
20
11
NAPI_CALL (env, napi_get_cb_info (env, info, &argc, nullptr , nullptr , nullptr ));
21
- NAPI_ASSERT (env, argc == 4 , " Wrong number of arguments" );
12
+ NAPI_ASSERT (env, argc == 3 , " Wrong number of arguments" );
22
13
23
14
NAPI_CALL (env, napi_get_cb_info (env, info, &argc, args, nullptr , nullptr ));
24
15
@@ -28,35 +19,29 @@ napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
28
19
" Wrong type of arguments. Expects an object as first argument." );
29
20
30
21
NAPI_CALL (env, napi_typeof (env, args[1 ], &valuetype));
31
- NAPI_ASSERT (env, valuetype == napi_number ,
32
- " Wrong type of arguments. Expects a number as second argument." );
22
+ NAPI_ASSERT (env, valuetype == napi_string ,
23
+ " Wrong type of arguments. Expects a string as second argument." );
33
24
34
25
NAPI_CALL (env, napi_typeof (env, args[2 ], &valuetype));
35
- NAPI_ASSERT (env, valuetype == napi_number,
36
- " Wrong type of arguments. Expects a number as third argument." );
37
-
38
- NAPI_CALL (env, napi_typeof (env, args[3 ], &valuetype));
39
26
NAPI_ASSERT (env, valuetype == napi_function,
40
27
" Wrong type of arguments. Expects a function as third argument." );
41
28
42
- struct async_context context;
43
- NAPI_CALL (env, napi_get_value_double (env, args[1 ], &context.async_id ));
44
- NAPI_CALL (env,
45
- napi_get_value_double (env, args[2 ], &context.trigger_async_id ));
29
+ napi_async_context context;
30
+ NAPI_CALL (env, napi_async_init (env, args[0 ], args[1 ], &context));
46
31
47
32
napi_callback_scope scope = nullptr ;
48
33
NAPI_CALL (
49
34
env,
50
35
napi_open_callback_scope (env,
51
36
args[0 ],
52
- reinterpret_cast <napi_async_context>(& context) ,
37
+ context,
53
38
&scope));
54
39
55
40
// if the function has an exception pending after the call that is ok
56
41
// so we don't use NAPI_CALL as we must close the callback scope regardless
57
42
napi_value result = nullptr ;
58
43
napi_status function_call_result =
59
- napi_call_function (env, args[0 ], args[3 ], 0 , nullptr , &result);
44
+ napi_call_function (env, args[0 ], args[2 ], 0 , nullptr , &result);
60
45
if (function_call_result != napi_ok) {
61
46
GET_AND_THROW_LAST_ERROR ((env));
62
47
}
0 commit comments