2018-11-17 12:34:54 -08:00
|
|
|
#include <js_native_api.h>
|
2017-03-24 01:26:09 -07:00
|
|
|
#include "../common.h"
|
2023-09-06 21:11:09 +08:00
|
|
|
#include "../entry_point.h"
|
2017-03-20 14:55:26 -07:00
|
|
|
|
|
|
|
static bool exceptionWasPending = false;
|
2022-04-03 06:57:33 +08:00
|
|
|
static int num = 0x23432;
|
2017-03-20 14:55:26 -07:00
|
|
|
|
2018-03-22 17:01:37 -04:00
|
|
|
static napi_value returnException(napi_env env, napi_callback_info info) {
|
2017-03-24 01:26:09 -07:00
|
|
|
size_t argc = 1;
|
|
|
|
napi_value args[1];
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
|
2017-03-20 14:55:26 -07:00
|
|
|
|
|
|
|
napi_value global;
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_get_global(env, &global));
|
2017-03-20 14:55:26 -07:00
|
|
|
|
|
|
|
napi_value result;
|
2017-03-24 01:26:09 -07:00
|
|
|
napi_status status = napi_call_function(env, global, args[0], 0, 0, &result);
|
|
|
|
if (status == napi_pending_exception) {
|
2017-03-20 14:55:26 -07:00
|
|
|
napi_value ex;
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex));
|
2017-03-24 01:26:09 -07:00
|
|
|
return ex;
|
2017-03-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:26:09 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-20 14:55:26 -07:00
|
|
|
|
2021-05-24 23:39:36 +08:00
|
|
|
static napi_value constructReturnException(napi_env env, napi_callback_info info) {
|
|
|
|
size_t argc = 1;
|
|
|
|
napi_value args[1];
|
|
|
|
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
|
|
|
|
|
|
|
|
napi_value result;
|
|
|
|
napi_status status = napi_new_instance(env, args[0], 0, 0, &result);
|
|
|
|
if (status == napi_pending_exception) {
|
|
|
|
napi_value ex;
|
|
|
|
NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex));
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:01:37 -04:00
|
|
|
static napi_value allowException(napi_env env, napi_callback_info info) {
|
2017-03-24 01:26:09 -07:00
|
|
|
size_t argc = 1;
|
|
|
|
napi_value args[1];
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
|
2017-03-20 14:55:26 -07:00
|
|
|
|
|
|
|
napi_value global;
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_get_global(env, &global));
|
2017-03-20 14:55:26 -07:00
|
|
|
|
|
|
|
napi_value result;
|
2017-03-24 01:26:09 -07:00
|
|
|
napi_call_function(env, global, args[0], 0, 0, &result);
|
2017-03-20 14:55:26 -07:00
|
|
|
// Ignore status and check napi_is_exception_pending() instead.
|
|
|
|
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending));
|
2017-03-24 01:26:09 -07:00
|
|
|
return NULL;
|
2017-03-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
2021-05-24 23:39:36 +08:00
|
|
|
static napi_value constructAllowException(napi_env env, napi_callback_info info) {
|
|
|
|
size_t argc = 1;
|
|
|
|
napi_value args[1];
|
|
|
|
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
|
|
|
|
|
|
|
|
napi_value result;
|
|
|
|
napi_new_instance(env, args[0], 0, 0, &result);
|
|
|
|
// Ignore status and check napi_is_exception_pending() instead.
|
|
|
|
|
|
|
|
NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:01:37 -04:00
|
|
|
static napi_value wasPending(napi_env env, napi_callback_info info) {
|
2017-03-20 14:55:26 -07:00
|
|
|
napi_value result;
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_get_boolean(env, exceptionWasPending, &result));
|
2017-03-20 14:55:26 -07:00
|
|
|
|
2017-03-24 01:26:09 -07:00
|
|
|
return result;
|
2017-03-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
2018-03-22 17:01:37 -04:00
|
|
|
static void finalizer(napi_env env, void *data, void *hint) {
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL_RETURN_VOID(env,
|
2018-03-22 17:01:37 -04:00
|
|
|
napi_throw_error(env, NULL, "Error during Finalize"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static napi_value createExternal(napi_env env, napi_callback_info info) {
|
|
|
|
napi_value external;
|
|
|
|
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env,
|
2022-04-03 06:57:33 +08:00
|
|
|
napi_create_external(env, &num, finalizer, NULL, &external));
|
2018-03-22 17:01:37 -04:00
|
|
|
|
|
|
|
return external;
|
|
|
|
}
|
|
|
|
|
2018-11-17 12:34:54 -08:00
|
|
|
EXTERN_C_START
|
|
|
|
napi_value Init(napi_env env, napi_value exports) {
|
2017-03-20 14:55:26 -07:00
|
|
|
napi_property_descriptor descriptors[] = {
|
2021-02-03 12:01:28 -08:00
|
|
|
DECLARE_NODE_API_PROPERTY("returnException", returnException),
|
|
|
|
DECLARE_NODE_API_PROPERTY("allowException", allowException),
|
2021-05-24 23:39:36 +08:00
|
|
|
DECLARE_NODE_API_PROPERTY("constructReturnException", constructReturnException),
|
|
|
|
DECLARE_NODE_API_PROPERTY("constructAllowException", constructAllowException),
|
2021-02-03 12:01:28 -08:00
|
|
|
DECLARE_NODE_API_PROPERTY("wasPending", wasPending),
|
|
|
|
DECLARE_NODE_API_PROPERTY("createExternal", createExternal),
|
2017-03-20 14:55:26 -07:00
|
|
|
};
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_define_properties(
|
2017-08-30 02:48:41 -07:00
|
|
|
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
|
|
|
|
|
2018-03-22 17:01:37 -04:00
|
|
|
napi_value error, code, message;
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_create_string_utf8(env, "Error during Init",
|
2018-03-22 17:01:37 -04:00
|
|
|
NAPI_AUTO_LENGTH, &message));
|
2021-02-03 12:01:28 -08:00
|
|
|
NODE_API_CALL(env, napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &code));
|
|
|
|
NODE_API_CALL(env, napi_create_error(env, code, message, &error));
|
|
|
|
NODE_API_CALL(env, napi_set_named_property(env, error, "binding", exports));
|
|
|
|
NODE_API_CALL(env, napi_throw(env, error));
|
2018-03-22 17:01:37 -04:00
|
|
|
|
2017-08-30 02:48:41 -07:00
|
|
|
return exports;
|
2017-03-20 14:55:26 -07:00
|
|
|
}
|
2018-11-17 12:34:54 -08:00
|
|
|
EXTERN_C_END
|