From 11ec3345e7a722fa73918e2aaf43aaaf9e726541 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 26 Dec 2021 17:30:56 +0530 Subject: node-api: add node_api_symbol_for() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/41294 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/41329 Reviewed-By: Anna Henningsen Reviewed-By: Michael Dawson Reviewed-By: Tobias Nießen Reviewed-By: James M Snell --- test/js-native-api/test_properties/test.js | 11 +++--- .../test_properties/test_properties.c | 13 ++++++++ test/js-native-api/test_reference/test.js | 19 +++++++++++ test/js-native-api/test_reference/test_reference.c | 39 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 6 deletions(-) (limited to 'test/js-native-api') diff --git a/test/js-native-api/test_properties/test.js b/test/js-native-api/test_properties/test.js index 704002dfc7..7311c615cb 100644 --- a/test/js-native-api/test_properties/test.js +++ b/test/js-native-api/test_properties/test.js @@ -35,12 +35,11 @@ assert.ok(!propertyNames.includes('readwriteAccessor2')); assert.ok(!propertyNames.includes('readonlyAccessor1')); assert.ok(!propertyNames.includes('readonlyAccessor2')); -// Validate property created with symbol -const start = 'Symbol('.length; -const end = start + 'NameKeySymbol'.length; -const symbolDescription = - String(Object.getOwnPropertySymbols(test_object)[0]).slice(start, end); -assert.strictEqual(symbolDescription, 'NameKeySymbol'); +// Validate properties created with symbol +const propertySymbols = Object.getOwnPropertySymbols(test_object); +assert.strictEqual(propertySymbols[0].toString(), 'Symbol(NameKeySymbol)'); +assert.strictEqual(propertySymbols[1].toString(), 'Symbol()'); +assert.strictEqual(propertySymbols[2], Symbol.for('NameKeySymbolFor')); // The napi_writable attribute should be ignored for accessors. const readwriteAccessor1Descriptor = diff --git a/test/js-native-api/test_properties/test_properties.c b/test/js-native-api/test_properties/test_properties.c index c778601aa7..2c1a513449 100644 --- a/test/js-native-api/test_properties/test_properties.c +++ b/test/js-native-api/test_properties/test_properties.c @@ -1,3 +1,4 @@ +#define NAPI_EXPERIMENTAL #include #include "../common.h" @@ -77,6 +78,16 @@ napi_value Init(napi_env env, napi_value exports) { NODE_API_CALL(env, napi_create_symbol(env, symbol_description, &name_symbol)); + napi_value name_symbol_descriptionless; + NODE_API_CALL(env, + napi_create_symbol(env, NULL, &name_symbol_descriptionless)); + + napi_value name_symbol_for; + NODE_API_CALL(env, node_api_symbol_for(env, + "NameKeySymbolFor", + NAPI_AUTO_LENGTH, + &name_symbol_for)); + napi_property_descriptor properties[] = { { "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 }, { "readwriteValue", 0, 0, 0, 0, number, napi_enumerable | napi_writable, 0 }, @@ -84,6 +95,8 @@ napi_value Init(napi_env env, napi_value exports) { { "hiddenValue", 0, 0, 0, 0, number, napi_default, 0}, { NULL, name_value, 0, 0, 0, number, napi_enumerable, 0}, { NULL, name_symbol, 0, 0, 0, number, napi_enumerable, 0}, + { NULL, name_symbol_descriptionless, 0, 0, 0, number, napi_enumerable, 0}, + { NULL, name_symbol_for, 0, 0, 0, number, napi_enumerable, 0}, { "readwriteAccessor1", 0, 0, GetValue, SetValue, 0, napi_default, 0}, { "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0}, { "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0}, diff --git a/test/js-native-api/test_reference/test.js b/test/js-native-api/test_reference/test.js index 0dc9e55330..6f128b7887 100644 --- a/test/js-native-api/test_reference/test.js +++ b/test/js-native-api/test_reference/test.js @@ -21,6 +21,25 @@ async function runTests() { })(); test_reference.deleteReference(); + (() => { + const symbol = test_reference.createSymbolFor('testSymFor'); + test_reference.createReference(symbol, 0); + assert.strictEqual(test_reference.referenceValue, symbol); + assert.strictEqual(test_reference.referenceValue, Symbol.for('testSymFor')); + })(); + test_reference.deleteReference(); + + (() => { + const symbol = test_reference.createSymbolForEmptyString(); + test_reference.createReference(symbol, 0); + assert.strictEqual(test_reference.referenceValue, symbol); + assert.strictEqual(test_reference.referenceValue, Symbol.for('')); + })(); + test_reference.deleteReference(); + + assert.throws(() => test_reference.createSymbolForIncorrectLength(), + /Invalid argument/); + (() => { 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 7b770cb876..4a224efbd8 100644 --- a/test/js-native-api/test_reference/test_reference.c +++ b/test/js-native-api/test_reference/test_reference.c @@ -1,3 +1,4 @@ +#define NAPI_EXPERIMENTAL #include #include #include @@ -49,6 +50,41 @@ static napi_value CreateSymbol(napi_env env, napi_callback_info info) { return result_symbol; } +static napi_value CreateSymbolFor(napi_env env, napi_callback_info info) { + + size_t argc = 1; + napi_value args[1]; + + char description[256]; + size_t description_length; + + 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_CALL(env, napi_get_value_string_utf8(env, args[0], description, sizeof(description), &description_length)); + NODE_API_ASSERT(env, description_length <= 255, "Cannot accommodate descriptions longer than 255 bytes"); + + napi_value result_symbol; + + NODE_API_CALL(env, node_api_symbol_for(env, + description, + description_length, + &result_symbol)); + return result_symbol; +} + +static napi_value CreateSymbolForEmptyString(napi_env env, napi_callback_info info) { + napi_value result_symbol; + NODE_API_CALL(env, node_api_symbol_for(env, NULL, 0, &result_symbol)); + return result_symbol; +} + +static napi_value CreateSymbolForIncorrectLength(napi_env env, napi_callback_info info) { + napi_value result_symbol; + NODE_API_CALL(env, node_api_symbol_for(env, NULL, 5, &result_symbol)); + return result_symbol; +} + static napi_value CreateExternalWithFinalize(napi_env env, napi_callback_info info) { napi_value result; @@ -190,6 +226,9 @@ napi_value Init(napi_env env, napi_value exports) { DECLARE_NODE_API_PROPERTY("checkExternal", CheckExternal), DECLARE_NODE_API_PROPERTY("createReference", CreateReference), DECLARE_NODE_API_PROPERTY("createSymbol", CreateSymbol), + DECLARE_NODE_API_PROPERTY("createSymbolFor", CreateSymbolFor), + DECLARE_NODE_API_PROPERTY("createSymbolForEmptyString", CreateSymbolForEmptyString), + DECLARE_NODE_API_PROPERTY("createSymbolForIncorrectLength", CreateSymbolForIncorrectLength), DECLARE_NODE_API_PROPERTY("deleteReference", DeleteReference), DECLARE_NODE_API_PROPERTY("incrementRefcount", IncrementRefcount), DECLARE_NODE_API_PROPERTY("decrementRefcount", DecrementRefcount), -- cgit v1.2.1