• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdlib.h>
2 #include "node_api.h"
3 #include "uv.h"
4 #include "../../js-native-api/common.h"
5 
RunInCallbackScope(napi_env env,napi_callback_info info)6 static napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
7   size_t argc;
8   napi_value args[3];
9 
10   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
11   NAPI_ASSERT(env, argc == 3 , "Wrong number of arguments");
12 
13   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
14 
15   napi_valuetype valuetype;
16   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
17   NAPI_ASSERT(env, valuetype == napi_object,
18       "Wrong type of arguments. Expects an object as first argument.");
19 
20   NAPI_CALL(env, napi_typeof(env, args[1], &valuetype));
21   NAPI_ASSERT(env, valuetype == napi_string,
22       "Wrong type of arguments. Expects a string as second argument.");
23 
24   NAPI_CALL(env, napi_typeof(env, args[2], &valuetype));
25   NAPI_ASSERT(env, valuetype == napi_function,
26       "Wrong type of arguments. Expects a function as third argument.");
27 
28   napi_async_context context;
29   NAPI_CALL(env, napi_async_init(env, args[0], args[1], &context));
30 
31   napi_callback_scope scope = NULL;
32   NAPI_CALL(
33       env,
34       napi_open_callback_scope(env,
35                                args[0],
36                                context,
37                                &scope));
38 
39   // if the function has an exception pending after the call that is ok
40   // so we don't use NAPI_CALL as we must close the callback scope regardless
41   napi_value result = NULL;
42   napi_status function_call_result =
43       napi_call_function(env, args[0], args[2], 0, NULL, &result);
44   if (function_call_result != napi_ok) {
45     GET_AND_THROW_LAST_ERROR((env));
46   }
47 
48   NAPI_CALL(env, napi_close_callback_scope(env, scope));
49   NAPI_CALL(env, napi_async_destroy(env, context));
50 
51   return result;
52 }
53 
54 static napi_env shared_env = NULL;
55 static napi_deferred deferred = NULL;
56 
Callback(uv_work_t * req,int ignored)57 static void Callback(uv_work_t* req, int ignored) {
58   napi_env env = shared_env;
59 
60   napi_handle_scope handle_scope = NULL;
61   NAPI_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope));
62 
63   napi_value resource_name;
64   NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(
65       env, "test", NAPI_AUTO_LENGTH, &resource_name));
66   napi_async_context context;
67   NAPI_CALL_RETURN_VOID(env,
68                         napi_async_init(env, NULL, resource_name, &context));
69 
70   napi_value resource_object;
71   NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object));
72 
73   napi_value undefined_value;
74   NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value));
75 
76   napi_callback_scope scope = NULL;
77   NAPI_CALL_RETURN_VOID(env, napi_open_callback_scope(env,
78                                                       resource_object,
79                                                       context,
80                                                       &scope));
81 
82   NAPI_CALL_RETURN_VOID(env,
83                         napi_resolve_deferred(env, deferred, undefined_value));
84 
85   NAPI_CALL_RETURN_VOID(env, napi_close_callback_scope(env, scope));
86 
87   NAPI_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope));
88   NAPI_CALL_RETURN_VOID(env, napi_async_destroy(env, context));
89   free(req);
90 }
91 
NoopWork(uv_work_t * work)92 static void NoopWork(uv_work_t* work) { (void) work; }
93 
TestResolveAsync(napi_env env,napi_callback_info info)94 static napi_value TestResolveAsync(napi_env env, napi_callback_info info) {
95   napi_value promise = NULL;
96   if (deferred == NULL) {
97     shared_env = env;
98     NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
99 
100     uv_loop_t* loop = NULL;
101     NAPI_CALL(env, napi_get_uv_event_loop(env, &loop));
102 
103     uv_work_t* req = malloc(sizeof(*req));
104     uv_queue_work(loop,
105                   req,
106                   NoopWork,
107                   Callback);
108   }
109   return promise;
110 }
111 
Init(napi_env env,napi_value exports)112 static napi_value Init(napi_env env, napi_value exports) {
113   napi_property_descriptor descriptors[] = {
114     DECLARE_NAPI_PROPERTY("runInCallbackScope", RunInCallbackScope),
115     DECLARE_NAPI_PROPERTY("testResolveAsync", TestResolveAsync)
116   };
117 
118   NAPI_CALL(env, napi_define_properties(
119       env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
120 
121   return exports;
122 }
123 
124 NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
125