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

node-api: add napi_get_symbol_to_string_tag() #41370

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
24 changes: 24 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,30 @@ This API creates a JavaScript `symbol` value from a UTF8-encoded C string.
The JavaScript `symbol` type is described in [Section 19.4][]
of the ECMAScript Language Specification.

#### `napi_get_symbol_to_string_tag`

<!-- YAML
added: REPLACEME
napiVersion: REPLACEME
-->

> Stability: 1 - Experimental

```c
napi_status napi_get_symbol_to_string_tag(napi_env env,
napi_value* result)
```

* `[in] env`: The environment that the API is invoked under.
* `[out] result`: A `napi_value` representing a JavaScript `symbol`.

Returns `napi_ok` if the API succeeded.

This API returns the JavaScript well-known symbol, `Symbol.toStringTag`.

The JavaScript `symbol` type is described in [Section 19.4][]
of the ECMAScript Language Specification.

#### `napi_create_typedarray`

<!-- YAML
Expand Down
4 changes: 4 additions & 0 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ NAPI_EXTERN napi_status napi_create_string_utf16(napi_env env,
NAPI_EXTERN napi_status napi_create_symbol(napi_env env,
napi_value description,
napi_value* result);
#ifdef NAPI_EXPERIMENTAL
NAPI_EXTERN napi_status napi_get_symbol_to_string_tag(napi_env env,
napi_value* result);
#endif // NAPI_EXPERIMENTAL
NAPI_EXTERN napi_status napi_create_function(napi_env env,
const char* utf8name,
size_t length,
Expand Down
11 changes: 11 additions & 0 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,17 @@ napi_status napi_create_symbol(napi_env env,
return napi_clear_last_error(env);
}

napi_status napi_get_symbol_to_string_tag(napi_env env,
napi_value* result) {
CHECK_ENV(env);
CHECK_ARG(env, result);

*result = v8impl::JsValueFromV8LocalValue(
v8::Symbol::GetToStringTag(env->isolate));

return napi_clear_last_error(env);
}

static inline napi_status set_error_code(napi_env env,
v8::Local<v8::Value> error,
napi_value code,
Expand Down
10 changes: 4 additions & 6 deletions test/js-native-api/test_properties/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ 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(String(propertySymbols[0]), 'Symbol(NameKeySymbol)');
assert.strictEqual(String(propertySymbols[1]), 'Symbol(Symbol.toStringTag)');

// The napi_writable attribute should be ignored for accessors.
const readwriteAccessor1Descriptor =
Expand Down
6 changes: 6 additions & 0 deletions test/js-native-api/test_properties/test_properties.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define NAPI_EXPERIMENTAL
#include <js_native_api.h>
#include "../common.h"

Expand Down Expand Up @@ -77,13 +78,18 @@ napi_value Init(napi_env env, napi_value exports) {
NODE_API_CALL(env,
napi_create_symbol(env, symbol_description, &name_symbol));

napi_value to_string_tag_symbol;
NODE_API_CALL(env,
napi_get_symbol_to_string_tag(env, &to_string_tag_symbol));

napi_property_descriptor properties[] = {
{ "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 },
{ "readwriteValue", 0, 0, 0, 0, number, napi_enumerable | napi_writable, 0 },
{ "readonlyValue", 0, 0, 0, 0, number, napi_enumerable, 0},
{ "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, to_string_tag_symbol, 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},
Expand Down
7 changes: 7 additions & 0 deletions test/js-native-api/test_reference/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ async function runTests() {
})();
test_reference.deleteReference();

(() => {
const symbol = test_reference.createToStringTagSymbol();
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);
Expand Down
8 changes: 8 additions & 0 deletions test/js-native-api/test_reference/test_reference.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define NAPI_EXPERIMENTAL
#include <stdlib.h>
#include <assert.h>
#include <js_native_api.h>
Expand Down Expand Up @@ -49,6 +50,12 @@ static napi_value CreateSymbol(napi_env env, napi_callback_info info) {
return result_symbol;
}

static napi_value CreateToStringTagSymbol(napi_env env, napi_callback_info info) {
napi_value result_symbol;
NODE_API_CALL(env, napi_get_symbol_to_string_tag(env, &result_symbol));
return result_symbol;
}

static napi_value
CreateExternalWithFinalize(napi_env env, napi_callback_info info) {
napi_value result;
Expand Down Expand Up @@ -190,6 +197,7 @@ 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("createToStringTagSymbol", CreateToStringTagSymbol),
DECLARE_NODE_API_PROPERTY("deleteReference", DeleteReference),
DECLARE_NODE_API_PROPERTY("incrementRefcount", IncrementRefcount),
DECLARE_NODE_API_PROPERTY("decrementRefcount", DecrementRefcount),
Expand Down
39 changes: 39 additions & 0 deletions test/js-native-api/test_symbol/test4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../../common');
const {
deepStrictEqual,
strictEqual,
} = require('assert');

// Testing api calls for test_symbol.ToStringTag
const { ToStringTag } = require(`./build/${common.buildType}/test_symbol`);

const toStringTagSymbol = ToStringTag();
strictEqual(typeof toStringTagSymbol, 'symbol');
strictEqual(toStringTagSymbol, ToStringTag());
strictEqual(toStringTagSymbol, Symbol.toStringTag);
strictEqual(toStringTagSymbol.toString(), 'Symbol(Symbol.toStringTag)');

{
const tag = 'MyTag';
const object = { [toStringTagSymbol]: tag };

deepStrictEqual(Object.keys(object), []);
deepStrictEqual(Object.getOwnPropertyNames(object), []);
deepStrictEqual(Object.getOwnPropertySymbols(object), [ Symbol.toStringTag ]);

strictEqual(object[toStringTagSymbol], tag);
strictEqual(object.toString(), `[object ${tag}]`);
}

{
const tag = 5;
const object = { [toStringTagSymbol]: tag };

deepStrictEqual(Object.keys(object), []);
deepStrictEqual(Object.getOwnPropertyNames(object), []);
deepStrictEqual(Object.getOwnPropertySymbols(object), [ Symbol.toStringTag ]);

strictEqual(object[toStringTagSymbol], tag);
strictEqual(object.toString(), '[object Object]');
}
8 changes: 8 additions & 0 deletions test/js-native-api/test_symbol/test_symbol.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define NAPI_EXPERIMENTAL
#include <js_native_api.h>
#include "../common.h"

Expand All @@ -23,10 +24,17 @@ static napi_value New(napi_env env, napi_callback_info info) {
return symbol;
}

static napi_value ToStringTag(napi_env env, napi_callback_info info) {
napi_value symbol;
NODE_API_CALL(env, napi_get_symbol_to_string_tag(env, &symbol));
return symbol;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NODE_API_PROPERTY("New", New),
DECLARE_NODE_API_PROPERTY("ToStringTag", ToStringTag),
};

NODE_API_CALL(env, napi_define_properties(
Expand Down