• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <node_api.h>
2 #include "../../js-native-api/common.h"
3 
4 // Testing calling into JavaScript
ThreadSafeFunctionFinalize(napi_env env,void * finalize_data,void * finalize_hint)5 static void ThreadSafeFunctionFinalize(napi_env env,
6                                        void* finalize_data,
7                                        void* finalize_hint) {
8   napi_ref js_func_ref = (napi_ref)finalize_data;
9   napi_value js_func;
10   napi_value recv;
11   NODE_API_CALL_RETURN_VOID(
12       env, napi_get_reference_value(env, js_func_ref, &js_func));
13   NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &recv));
14   NODE_API_CALL_RETURN_VOID(
15       env, napi_call_function(env, recv, js_func, 0, NULL, NULL));
16   NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, js_func_ref));
17 }
18 
ThreadSafeFunctionNogcFinalize(node_api_nogc_env env,void * data,void * hint)19 static void ThreadSafeFunctionNogcFinalize(node_api_nogc_env env,
20                                            void* data,
21                                            void* hint) {
22 #ifdef NAPI_EXPERIMENTAL
23   NODE_API_NOGC_CALL_RETURN_VOID(
24       env,
25       node_api_post_finalizer(env, ThreadSafeFunctionFinalize, data, hint));
26 #else
27   ThreadSafeFunctionFinalize(env, data, hint);
28 #endif
29 }
30 
31 // Testing calling into JavaScript
CallIntoModule(napi_env env,napi_callback_info info)32 static napi_value CallIntoModule(napi_env env, napi_callback_info info) {
33   size_t argc = 4;
34   napi_value argv[4];
35   NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
36 
37   napi_ref finalize_func;
38   NODE_API_CALL(env, napi_create_reference(env, argv[3], 1, &finalize_func));
39 
40   napi_threadsafe_function tsfn;
41   NODE_API_CALL(env,
42                 napi_create_threadsafe_function(env,
43                                                 argv[0],
44                                                 argv[1],
45                                                 argv[2],
46                                                 0,
47                                                 1,
48                                                 finalize_func,
49                                                 ThreadSafeFunctionNogcFinalize,
50                                                 NULL,
51                                                 NULL,
52                                                 &tsfn));
53   NODE_API_CALL(env,
54                 napi_call_threadsafe_function(tsfn, NULL, napi_tsfn_blocking));
55   NODE_API_CALL(env, napi_release_threadsafe_function(tsfn, napi_tsfn_release));
56   return NULL;
57 }
58 
59 // Module init
Init(napi_env env,napi_value exports)60 static napi_value Init(napi_env env, napi_value exports) {
61   napi_property_descriptor properties[] = {
62       DECLARE_NODE_API_PROPERTY("CallIntoModule", CallIntoModule),
63   };
64 
65   NODE_API_CALL(
66       env,
67       napi_define_properties(env,
68                              exports,
69                              sizeof(properties) / sizeof(properties[0]),
70                              properties));
71 
72   return exports;
73 }
74 NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
75