You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
In the following example variable x will have 0 value if info[0] is not a number:
voidFoo(const Napi::CallbackInfo& info) {
auto x = info[0].As<Napi::Number>().Int32Value(); // expected x type is Maybe<int32_t>, // actual type is int32_t
}
The text was updated successfully, but these errors were encountered:
We discussed in the team meeting today and originally these methods were intentionally left out of those that returns Maybes. @legendecas will take another look and comment.
When a method returns Maybe<> type, it indicates that it may call into JavaScript. However, those number value getters don't call into JavaScript at all. Non napi_ok status simply means that the type assumption of Napi::Value::As() is failed.
I'd propose a new type cast method Napi::Value::Cast() to enforce the type assumption, or add a new definition NAPI_ENABLE_TYPE_CHECK to allow Napi::Value::As() to enforce the type assumption.
If preprocessor directives
NAPI_DISABLE_CPP_EXCEPTIONS
andNODE_ADDON_API_ENABLE_MAYBE
are enabled,Napi
API calls should returnMaybe
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()
) andNapi::BigInt
conversion 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
x
will have0
value ifinfo[0]
is not a number:The text was updated successfully, but these errors were encountered: