-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Description
- Version: master
- Platform: -
- Subsystem: n-api
We could find out that there are several napi_status existing and been used in various n-api functions.
napi_ok:
For any n-api call that is successful.
napi_cancelled
For canceled async works.
napi_escape_called_twicenapi_handle_scope_mismatchnapi_callback_scope_mismatch
For handle scope error conditions.
napi_queue_fullnapi_closing
For ThreadSafeFunction error conditions.
-
napi_invalid_arg
This is what I'd like to discuss about. If I got it right, mostly, invalid args of calling of n-api should get into this status, except following type exceptions (mostly JavaScript primitive values, butarrayanddateare not,nameis a virtual type that is defined in ECMA spec): -
napi_object_expected -
napi_string_expected -
napi_name_expected -
napi_function_expected- Exceptions we have now:
napi_instanceof: would throw JavaScript TypeError if constructor is not a function, and return anapi_function_expectedstatus.
-
napi_number_expected -
napi_boolean_expected -
napi_array_expected -
napi_bigint_expected -
napi_date_expected
Except for all above statuses, following two is additional generic error status:
-
napi_generic_failure
This could be used in most places IMO, but it has been mixed up withnapi_pending_exceptionin some conditions:- Macro
THROW_RANGE_ERROR_IF_FALSEreturnsnapi_generic_failurewith throwing JavaScript RangeError. napi_create_bigint_wordsreturnsnapi_pending_exceptionwith throwing JavaScript TypeError.napi_instanceofreturnsnapi_function_expectedwith throwing JavaScript TypeError.napi_create_dataviewreturnsnapi_pending_exceptionwith throwing JavaScript RangeError.
- Macro
-
napi_pending_exception
For any n-api call that has pending JavaScript Exception. If I understand n-api document right, every non-napi_okstatus should throw a JavaScript exception, andnapi_pending_exceptionshould be used if the previous JavaScript exception was not handled while calling sequent n-apis.
The return value will be napi_ok if the request was successful and no uncaught JavaScript exception was thrown. If an error occurred AND an exception was thrown, the napi_status value for the error will be returned. If an exception was thrown, and no error occurred, napi_pending_exception will be returned. Link
So here comes the unresolved questions:
- How do we define new type expectation status constraints: should they be limited to JavaScript primitive values? Or any non-primitive types that have an equivalent
napi_is_<typename>function? - In which conditions N-APIs should throw JavaScript exceptions? and for those already throwing, which status should be returned?
napi_generic_failureornapi_pending_exception?
Refs: #29768 (comment)
Related: #29847