• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <js_native_api.h>
2 #include "../common.h"
3 #include "../entry_point.h"
4 
5 static bool exceptionWasPending = false;
6 static int num = 0x23432;
7 
returnException(napi_env env,napi_callback_info info)8 static napi_value returnException(napi_env env, napi_callback_info info) {
9   size_t argc = 1;
10   napi_value args[1];
11   NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
12 
13   napi_value global;
14   NODE_API_CALL(env, napi_get_global(env, &global));
15 
16   napi_value result;
17   napi_status status = napi_call_function(env, global, args[0], 0, 0, &result);
18   if (status == napi_pending_exception) {
19     napi_value ex;
20     NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex));
21     return ex;
22   }
23 
24   return NULL;
25 }
26 
constructReturnException(napi_env env,napi_callback_info info)27 static napi_value constructReturnException(napi_env env, napi_callback_info info) {
28   size_t argc = 1;
29   napi_value args[1];
30   NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
31 
32   napi_value result;
33   napi_status status = napi_new_instance(env, args[0], 0, 0, &result);
34   if (status == napi_pending_exception) {
35     napi_value ex;
36     NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex));
37     return ex;
38   }
39 
40   return NULL;
41 }
42 
allowException(napi_env env,napi_callback_info info)43 static napi_value allowException(napi_env env, napi_callback_info info) {
44   size_t argc = 1;
45   napi_value args[1];
46   NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
47 
48   napi_value global;
49   NODE_API_CALL(env, napi_get_global(env, &global));
50 
51   napi_value result;
52   napi_call_function(env, global, args[0], 0, 0, &result);
53   // Ignore status and check napi_is_exception_pending() instead.
54 
55   NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending));
56   return NULL;
57 }
58 
constructAllowException(napi_env env,napi_callback_info info)59 static napi_value constructAllowException(napi_env env, napi_callback_info info) {
60   size_t argc = 1;
61   napi_value args[1];
62   NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
63 
64   napi_value result;
65   napi_new_instance(env, args[0], 0, 0, &result);
66   // Ignore status and check napi_is_exception_pending() instead.
67 
68   NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending));
69   return NULL;
70 }
71 
wasPending(napi_env env,napi_callback_info info)72 static napi_value wasPending(napi_env env, napi_callback_info info) {
73   napi_value result;
74   NODE_API_CALL(env, napi_get_boolean(env, exceptionWasPending, &result));
75 
76   return result;
77 }
78 
finalizer(napi_env env,void * data,void * hint)79 static void finalizer(napi_env env, void *data, void *hint) {
80   NODE_API_CALL_RETURN_VOID(env,
81       napi_throw_error(env, NULL, "Error during Finalize"));
82 }
83 
createExternal(napi_env env,napi_callback_info info)84 static napi_value createExternal(napi_env env, napi_callback_info info) {
85   napi_value external;
86 
87   NODE_API_CALL(env,
88       napi_create_external(env, &num, finalizer, NULL, &external));
89 
90   return external;
91 }
92 
93 EXTERN_C_START
Init(napi_env env,napi_value exports)94 napi_value Init(napi_env env, napi_value exports) {
95   napi_property_descriptor descriptors[] = {
96     DECLARE_NODE_API_PROPERTY("returnException", returnException),
97     DECLARE_NODE_API_PROPERTY("allowException", allowException),
98     DECLARE_NODE_API_PROPERTY("constructReturnException", constructReturnException),
99     DECLARE_NODE_API_PROPERTY("constructAllowException", constructAllowException),
100     DECLARE_NODE_API_PROPERTY("wasPending", wasPending),
101     DECLARE_NODE_API_PROPERTY("createExternal", createExternal),
102   };
103   NODE_API_CALL(env, napi_define_properties(
104       env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
105 
106   napi_value error, code, message;
107   NODE_API_CALL(env, napi_create_string_utf8(env, "Error during Init",
108       NAPI_AUTO_LENGTH, &message));
109   NODE_API_CALL(env, napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &code));
110   NODE_API_CALL(env, napi_create_error(env, code, message, &error));
111   NODE_API_CALL(env, napi_set_named_property(env, error, "binding", exports));
112   NODE_API_CALL(env, napi_throw(env, error));
113 
114   return exports;
115 }
116 EXTERN_C_END
117