Skip to content

Commit

Permalink
Loosen restriction on the type of value that we can create a referenc…
Browse files Browse the repository at this point in the history
…e from
  • Loading branch information
JckXia committed Aug 29, 2021
1 parent 52ebe0f commit 25dcc0e
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 @@ -2460,9 +2460,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];

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;
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 25dcc0e

Please sign in to comment.