Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the case where one cannot execute JavaScript #37446

Closed
gabrielschulhof opened this issue Feb 19, 2021 · 4 comments
Closed

Handle the case where one cannot execute JavaScript #37446

gabrielschulhof opened this issue Feb 19, 2021 · 4 comments
Labels
node-api Issues and PRs related to the Node-API.

Comments

@gabrielschulhof
Copy link
Contributor

gabrielschulhof commented Feb 19, 2021

Re #30327 #37236

One (somewhat elaborate?) API for semver-minorly handling the inability to execute JS would be the following (somewhat introspective) API:

typedef enum {
  node_api_reason_call_function,
  node_api_reason_throw_exception,
  node_api_reason_open_handle_scope,
  node_api_reason_open_escapable_handle_scope,
  node_api_reason_open_callback_scope,
  ...   // etc.
} node_api_js_exec_failure_reason;

typedef napi_status (*node_api_js_exec_failure_callback)(
    napi_env env,
    node_api_js_exec_failure_reason reason,
    void* reason_data,
    void* user_data);

NAPI_EXTERN napi_status
node_api_handle_js_execution_failure(
    napi_env env,
    node_api_js_exec_failure_callback exec_fail_cb,
    void* user_data);

The add-on maintainer would attach a callback similarly to the napi_set_instance_data finalizer, which would get called whenever we would otherwise return napi_cannot_call_into_js. The callback would analyze the reason and the associated reason_data (which would be a pointer to a stack-allocated structure the type of which depends on reason – a little bit like ioctl(2)) and would return a napi_status that would then be passed as the return value to the original Node-API call.

This could be used to correct a return value of napi_pending_exception from a call to, say, napi_call_function into a return value of napi_ok.

@gabrielschulhof gabrielschulhof added the node-api Issues and PRs related to the Node-API. label Feb 19, 2021
@gabrielschulhof
Copy link
Contributor Author

@nodejs/node-api ^

@mhdawson
Copy link
Member

We discussed another option were we:

  • have a method to opt-in to better return codes
    • this could store the setting on the environment
  • have a new return code for "not able to call into JavaScript" which is returned if
    the better return codes have been requested.

@mhdawson
Copy link
Member

@legendecas also suggested we should investigate if there is any way we can change the sequence of operations so you can executed JS. For example move up in the env teardown sequence.

@gabrielschulhof
Copy link
Contributor Author

We solved this in #47986 using the new add-on runtime version reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

No branches or pull requests

2 participants