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