Skip to content

Commit

Permalink
src: make napi_create_reference accept symbol
Browse files Browse the repository at this point in the history
PR-URL: #39926
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
JckXia authored and richardlau committed Jan 25, 2022
1 parent fc328f1 commit f74fe2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2454,9 +2454,9 @@ napi_status napi_create_reference(napi_env env,
CHECK_ARG(env, result);

v8::Local<v8::Value> 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 =
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 @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions test/js-native-api/test_reference/test_reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];

NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL,NULL));
NAPI_ASSERT(env, argc == 1, "Expect one argument only (symbol description)");

napi_value result_symbol;

NAPI_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;
Expand Down Expand Up @@ -178,6 +192,7 @@ napi_value Init(napi_env env, napi_value exports) {
CreateExternalWithFinalize),
DECLARE_NAPI_PROPERTY("checkExternal", CheckExternal),
DECLARE_NAPI_PROPERTY("createReference", CreateReference),
DECLARE_NAPI_PROPERTY("createSymbol", CreateSymbol),
DECLARE_NAPI_PROPERTY("deleteReference", DeleteReference),
DECLARE_NAPI_PROPERTY("incrementRefcount", IncrementRefcount),
DECLARE_NAPI_PROPERTY("decrementRefcount", DecrementRefcount),
Expand Down

0 comments on commit f74fe2a

Please sign in to comment.