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_twice
napi_handle_scope_mismatch
napi_callback_scope_mismatch
For handle scope error conditions.
napi_queue_full
napi_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, butarray
anddate
are not,name
is 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_expected
status.
-
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_exception
in some conditions:- Macro
THROW_RANGE_ERROR_IF_FALSE
returnsnapi_generic_failure
with throwing JavaScript RangeError. napi_create_bigint_words
returnsnapi_pending_exception
with throwing JavaScript TypeError.napi_instanceof
returnsnapi_function_expected
with throwing JavaScript TypeError.napi_create_dataview
returnsnapi_pending_exception
with 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_ok
status should throw a JavaScript exception, andnapi_pending_exception
should 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_failure
ornapi_pending_exception
?
Refs: #29768 (comment)
Related: #29847
Activity