From 1ba9a54fb5373ac1b37263d61270306c74ea081d Mon Sep 17 00:00:00 2001 From: JckXia Date: Sat, 28 Aug 2021 20:59:16 -0400 Subject: [PATCH 1/2] src: allow napi_create_reference to recv symbol values --- src/js_native_api_v8.cc | 6 +++--- test/js-native-api/test_reference/test.js | 7 +++++++ .../js-native-api/test_reference/test_reference.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 5d18980fc76a7b..754d71f51d6ac6 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2460,9 +2460,9 @@ napi_status napi_create_reference(napi_env env, CHECK_ARG(env, result); v8::Local v8_value = v8impl::V8LocalValueFromJsValue(value); - - if (!(v8_value->IsObject() || v8_value->IsFunction())) { - return napi_set_last_error(env, napi_object_expected); + if (!(v8_value->IsObject() || v8_value->IsFunction() || + v8_value->IsSymbol())) { + return napi_set_last_error(env, napi_invalid_arg); } v8impl::Reference* reference = diff --git a/test/js-native-api/test_reference/test.js b/test/js-native-api/test_reference/test.js index 0c9d13075f2aa0..0dc9e553300060 100644 --- a/test/js-native-api/test_reference/test.js +++ b/test/js-native-api/test_reference/test.js @@ -14,6 +14,13 @@ assert.strictEqual(test_reference.finalizeCount, 0); // Run each test function in sequence, // with an async delay and GC call between each. async function runTests() { + (() => { + const symbol = test_reference.createSymbol('testSym'); + test_reference.createReference(symbol, 0); + assert.strictEqual(test_reference.referenceValue, symbol); + })(); + test_reference.deleteReference(); + (() => { const value = test_reference.createExternal(); assert.strictEqual(test_reference.finalizeCount, 0); diff --git a/test/js-native-api/test_reference/test_reference.c b/test/js-native-api/test_reference/test_reference.c index 9d69baa0257a05..f2662f41a4d9c2 100644 --- a/test/js-native-api/test_reference/test_reference.c +++ b/test/js-native-api/test_reference/test_reference.c @@ -35,6 +35,20 @@ static napi_value CreateExternal(napi_env env, napi_callback_info info) { return result; } +static napi_value CreateSymbol(napi_env env, napi_callback_info info) { + + size_t argc = 1; + napi_value args[1]; + + NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL,NULL)); + NODE_API_ASSERT(env, argc == 1, "Expect one argument only(symbol description)"); + + napi_value result_symbol; + + NODE_API_CALL(env, napi_create_symbol(env, args[0], &result_symbol)); + return result_symbol; +} + static napi_value CreateExternalWithFinalize(napi_env env, napi_callback_info info) { napi_value result; @@ -175,6 +189,7 @@ napi_value Init(napi_env env, napi_value exports) { CreateExternalWithFinalize), DECLARE_NODE_API_PROPERTY("checkExternal", CheckExternal), DECLARE_NODE_API_PROPERTY("createReference", CreateReference), + DECLARE_NODE_API_PROPERTY("createSymbol", CreateSymbol), DECLARE_NODE_API_PROPERTY("deleteReference", DeleteReference), DECLARE_NODE_API_PROPERTY("incrementRefcount", IncrementRefcount), DECLARE_NODE_API_PROPERTY("decrementRefcount", DecrementRefcount), From 3b6a69881398468c3774a7276cb80d526920bd3b Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 3 Sep 2021 15:18:55 -0400 Subject: [PATCH 2/2] test: update test/js-native-api/test_reference/test_reference.c --- test/js-native-api/test_reference/test_reference.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/js-native-api/test_reference/test_reference.c b/test/js-native-api/test_reference/test_reference.c index f2662f41a4d9c2..7b770cb8766775 100644 --- a/test/js-native-api/test_reference/test_reference.c +++ b/test/js-native-api/test_reference/test_reference.c @@ -41,7 +41,7 @@ static napi_value CreateSymbol(napi_env env, napi_callback_info info) { napi_value args[1]; NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL,NULL)); - NODE_API_ASSERT(env, argc == 1, "Expect one argument only(symbol description)"); + NODE_API_ASSERT(env, argc == 1, "Expect one argument only (symbol description)"); napi_value result_symbol;