From 3ed3f9ed7eea534846d3269d756c2872fb619a05 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Sun, 18 Mar 2018 18:10:45 -0400 Subject: [PATCH] n-api: allow escape of undefined The node-addon-api module was calling escape on undefined which would fail. Instead of forcing a check in all consumers of napi_escape_handle accept undefined and simply return it as it does not need to be escaped. Refs: https://github.com/nodejs/node-addon-api/issues/233 --- src/node_api.cc | 9 +++++++-- test/addons-napi/test_handle_scope/test.js | 2 ++ .../test_handle_scope/test_handle_scope.c | 13 +++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 2ee241badfda97..2b0dc113fecfac 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -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 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); diff --git a/test/addons-napi/test_handle_scope/test.js b/test/addons-napi/test_handle_scope/test.js index 53abfe178c8d7d..5bb9c4b20d655a 100644 --- a/test/addons-napi/test_handle_scope/test.js +++ b/test/addons-napi/test_handle_scope/test.js @@ -17,3 +17,5 @@ assert.throws( testHandleScope.NewScopeWithException(() => { throw new RangeError(); }); }, RangeError); + +assert.ok(testHandleScope.NewScopeEscapeUndefined() === undefined); diff --git a/test/addons-napi/test_handle_scope/test_handle_scope.c b/test/addons-napi/test_handle_scope/test_handle_scope.c index 31efbcf3dd4d7b..ef225c490619f8 100644 --- a/test/addons-napi/test_handle_scope/test_handle_scope.c +++ b/test/addons-napi/test_handle_scope/test_handle_scope.c @@ -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(