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

n-api: allow escape of undefined #19434

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2615,8 +2615,13 @@ napi_status napi_escape_handle(napi_env env,
v8impl::EscapableHandleScopeWrapper* s =
v8impl::V8EscapableHandleScopeFromJsEscapableHandleScope(scope);
if (!s->escape_called()) {
*result = v8impl::JsValueFromV8LocalValue(
s->Escape(v8impl::V8LocalValueFromJsValue(escapee)));
v8::Local<v8::Value> escapee_object =
v8impl::V8LocalValueFromJsValue(escapee);
if (escapee_object->IsUndefined()) {
*result = escapee;
} else {
*result = v8impl::JsValueFromV8LocalValue(s->Escape(escapee_object));
}
return napi_clear_last_error(env);
}
return napi_set_last_error(env, napi_escape_called_twice);
Expand Down
2 changes: 2 additions & 0 deletions test/addons-napi/test_handle_scope/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ assert.throws(
testHandleScope.NewScopeWithException(() => { throw new RangeError(); });
},
RangeError);

assert.ok(testHandleScope.NewScopeEscapeUndefined() === undefined);
13 changes: 13 additions & 0 deletions test/addons-napi/test_handle_scope/test_handle_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,25 @@ napi_value NewScopeWithException(napi_env env, napi_callback_info info) {
return NULL;
}

napi_value NewScopeEscapeUndefined(napi_env env, napi_callback_info info) {
napi_escapable_handle_scope scope;
napi_value escapee = NULL;
napi_value undefined_object = NULL;

NAPI_CALL(env, napi_open_escapable_handle_scope(env, &scope));
NAPI_CALL(env, napi_get_undefined(env, &undefined_object));
NAPI_CALL(env, napi_escape_handle(env, scope, undefined_object, &escapee));
NAPI_CALL(env, napi_close_escapable_handle_scope(env, scope));
return escapee;
}

napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NAPI_PROPERTY("NewScope", NewScope),
DECLARE_NAPI_PROPERTY("NewScopeEscape", NewScopeEscape),
DECLARE_NAPI_PROPERTY("NewScopeEscapeTwice", NewScopeEscapeTwice),
DECLARE_NAPI_PROPERTY("NewScopeWithException", NewScopeWithException),
DECLARE_NAPI_PROPERTY("NewScopeEscapeUndefined", NewScopeEscapeUndefined),
};

NAPI_CALL(env, napi_define_properties(
Expand Down