If preprocessor directives NAPI_DISABLE_CPP_EXCEPTIONS and NODE_ADDON_API_ENABLE_MAYBE are enabled, Napi API calls should return Maybe type https://github.com/nodejs/node-addon-api/blob/HEAD/doc/error_handling.md#handling-errors-with-maybe-type-and-c-exceptions-disabled.
But many Napi methods violate this rule. For example all Napi::Number conversion methods (Int32Value(), Uint32Value(), FloatValue(), DoubleValue()) and Napi::BigInt conversion methods return unwrapped value as is:
|
inline int32_t Number::Int32Value() const { |
|
int32_t result; |
|
napi_status status = napi_get_value_int32(_env, _value, &result); |
|
NAPI_THROW_IF_FAILED(_env, status, 0); |
|
return result; |
|
} |
|
#define NAPI_THROW_IF_FAILED(env, status, ...) \ |
|
if ((status) != napi_ok) { \ |
|
Napi::Error::New(env).ThrowAsJavaScriptException(); \ |
|
return __VA_ARGS__; \ |
|
} |
In the following example variable x will have 0 value if info[0] is not a number:
void Foo(const Napi::CallbackInfo& info) {
auto x = info[0].As<Napi::Number>().Int32Value(); // expected x type is Maybe<int32_t>,
// actual type is int32_t
}
If preprocessor directives
NAPI_DISABLE_CPP_EXCEPTIONSandNODE_ADDON_API_ENABLE_MAYBEare enabled,NapiAPI calls should returnMaybetype https://github.com/nodejs/node-addon-api/blob/HEAD/doc/error_handling.md#handling-errors-with-maybe-type-and-c-exceptions-disabled.But many Napi methods violate this rule. For example all
Napi::Numberconversion methods (Int32Value(),Uint32Value(),FloatValue(),DoubleValue()) andNapi::BigIntconversion methods return unwrapped value as is:node-addon-api/napi-inl.h
Lines 829 to 834 in 39267ba
node-addon-api/napi.h
Lines 90 to 94 in 39267ba
In the following example variable
xwill have0value ifinfo[0]is not a number: