11cb0ef41Sopenharmony_ci#include <js_native_api.h>
21cb0ef41Sopenharmony_ci#include "../common.h"
31cb0ef41Sopenharmony_ci#include "../entry_point.h"
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cistatic bool exceptionWasPending = false;
61cb0ef41Sopenharmony_cistatic int num = 0x23432;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cistatic napi_value returnException(napi_env env, napi_callback_info info) {
91cb0ef41Sopenharmony_ci  size_t argc = 1;
101cb0ef41Sopenharmony_ci  napi_value args[1];
111cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  napi_value global;
141cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_global(env, &global));
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  napi_value result;
171cb0ef41Sopenharmony_ci  napi_status status = napi_call_function(env, global, args[0], 0, 0, &result);
181cb0ef41Sopenharmony_ci  if (status == napi_pending_exception) {
191cb0ef41Sopenharmony_ci    napi_value ex;
201cb0ef41Sopenharmony_ci    NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex));
211cb0ef41Sopenharmony_ci    return ex;
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  return NULL;
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cistatic napi_value constructReturnException(napi_env env, napi_callback_info info) {
281cb0ef41Sopenharmony_ci  size_t argc = 1;
291cb0ef41Sopenharmony_ci  napi_value args[1];
301cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  napi_value result;
331cb0ef41Sopenharmony_ci  napi_status status = napi_new_instance(env, args[0], 0, 0, &result);
341cb0ef41Sopenharmony_ci  if (status == napi_pending_exception) {
351cb0ef41Sopenharmony_ci    napi_value ex;
361cb0ef41Sopenharmony_ci    NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex));
371cb0ef41Sopenharmony_ci    return ex;
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  return NULL;
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cistatic napi_value allowException(napi_env env, napi_callback_info info) {
441cb0ef41Sopenharmony_ci  size_t argc = 1;
451cb0ef41Sopenharmony_ci  napi_value args[1];
461cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  napi_value global;
491cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_global(env, &global));
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  napi_value result;
521cb0ef41Sopenharmony_ci  napi_call_function(env, global, args[0], 0, 0, &result);
531cb0ef41Sopenharmony_ci  // Ignore status and check napi_is_exception_pending() instead.
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending));
561cb0ef41Sopenharmony_ci  return NULL;
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cistatic napi_value constructAllowException(napi_env env, napi_callback_info info) {
601cb0ef41Sopenharmony_ci  size_t argc = 1;
611cb0ef41Sopenharmony_ci  napi_value args[1];
621cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  napi_value result;
651cb0ef41Sopenharmony_ci  napi_new_instance(env, args[0], 0, 0, &result);
661cb0ef41Sopenharmony_ci  // Ignore status and check napi_is_exception_pending() instead.
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending));
691cb0ef41Sopenharmony_ci  return NULL;
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cistatic napi_value wasPending(napi_env env, napi_callback_info info) {
731cb0ef41Sopenharmony_ci  napi_value result;
741cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_boolean(env, exceptionWasPending, &result));
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  return result;
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_cistatic void finalizer(napi_env env, void *data, void *hint) {
801cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(env,
811cb0ef41Sopenharmony_ci      napi_throw_error(env, NULL, "Error during Finalize"));
821cb0ef41Sopenharmony_ci}
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_cistatic napi_value createExternal(napi_env env, napi_callback_info info) {
851cb0ef41Sopenharmony_ci  napi_value external;
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  NODE_API_CALL(env,
881cb0ef41Sopenharmony_ci      napi_create_external(env, &num, finalizer, NULL, &external));
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  return external;
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ciEXTERN_C_START
941cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) {
951cb0ef41Sopenharmony_ci  napi_property_descriptor descriptors[] = {
961cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("returnException", returnException),
971cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("allowException", allowException),
981cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("constructReturnException", constructReturnException),
991cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("constructAllowException", constructAllowException),
1001cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("wasPending", wasPending),
1011cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("createExternal", createExternal),
1021cb0ef41Sopenharmony_ci  };
1031cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_define_properties(
1041cb0ef41Sopenharmony_ci      env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  napi_value error, code, message;
1071cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_string_utf8(env, "Error during Init",
1081cb0ef41Sopenharmony_ci      NAPI_AUTO_LENGTH, &message));
1091cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &code));
1101cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_error(env, code, message, &error));
1111cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_set_named_property(env, error, "binding", exports));
1121cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_throw(env, error));
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  return exports;
1151cb0ef41Sopenharmony_ci}
1161cb0ef41Sopenharmony_ciEXTERN_C_END
117