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

test: cover napi_get/set/has_named_property() #26947

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
4 changes: 4 additions & 0 deletions test/js-native-api/test_object/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const object = {
};

assert.strictEqual(test_object.Get(object, 'hello'), 'world');
assert.strictEqual(test_object.GetNamed(object, 'hello'), 'world');
assert.deepStrictEqual(test_object.Get(object, 'array'),
[ 1, 94, 'str', 12.321, { test: 'obj in arr' } ]);
assert.deepStrictEqual(test_object.Get(object, 'newObject'),
{ test: 'obj in obj' });

assert(test_object.Has(object, 'hello'));
assert(test_object.HasNamed(object, 'hello'));
assert(test_object.Has(object, 'array'));
assert(test_object.Has(object, 'newObject'));

Expand Down Expand Up @@ -113,8 +115,10 @@ assert.strictEqual(newObject.test_string, 'test string');
assert.strictEqual(test_object.Get(object2, sym1), '@@iterator');
assert.strictEqual(test_object.Get(object2, sym2), sym3);
assert(test_object.Set(object2, 'string', 'value'));
assert(test_object.SetNamed(object2, 'named_string', 'value'));
assert(test_object.Set(object2, sym4, 123));
assert(test_object.Has(object2, 'string'));
assert(test_object.HasNamed(object2, 'named_string'));
assert(test_object.Has(object2, sym4));
assert.strictEqual(test_object.Get(object2, 'string'), 'value');
assert.strictEqual(test_object.Get(object2, sym4), 123);
Expand Down
107 changes: 107 additions & 0 deletions test/js-native-api/test_object/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ static napi_value Get(napi_env env, napi_callback_info info) {
return output;
}

static napi_value GetNamed(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
char key[256] = "";
size_t key_length;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");

napi_valuetype valuetype0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Perhaps it would be more consistent to name the NAPI variables like value_type0 instead of valuetype0, like is done for other variables like key_length above. Or is this intentionally done for the NAPI types?

NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an object as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype1 == napi_string,
"Wrong type of arguments. Expects a string as second.");

napi_value object = args[0];
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], key, 255, &key_length));
key[255] = 0;
NAPI_ASSERT(env, key_length <= 255,
"Cannot accommodate keys longer than 255 bytes");
napi_value output;
NAPI_CALL(env, napi_get_named_property(env, object, key, &output));

return output;
}

static napi_value Set(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value args[3];
Expand Down Expand Up @@ -57,6 +90,41 @@ static napi_value Set(napi_env env, napi_callback_info info) {
return valuetrue;
}

static napi_value SetNamed(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value args[3];
char key[256] = "";
size_t key_length;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 3, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an object as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype1 == napi_string,
"Wrong type of arguments. Expects a string as second.");

NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], key, 255, &key_length));
key[255] = 0;
NAPI_ASSERT(env, key_length <= 255,
"Cannot accommodate keys longer than 255 bytes");

NAPI_CALL(env, napi_set_named_property(env, args[0], key, args[2]));

napi_value valuetrue;
NAPI_CALL(env, napi_get_boolean(env, true, &valuetrue));

return valuetrue;
}

static napi_value Has(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
Expand Down Expand Up @@ -85,6 +153,42 @@ static napi_value Has(napi_env env, napi_callback_info info) {
return ret;
}

static napi_value HasNamed(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
char key[256] = "";
size_t key_length;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an object as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype1 == napi_string || valuetype1 == napi_symbol,
"Wrong type of arguments. Expects a string as second.");

NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], key, 255, &key_length));
key[255] = 0;
NAPI_ASSERT(env, key_length <= 255,
"Cannot accommodate keys longer than 255 bytes");

bool has_property;
NAPI_CALL(env, napi_has_named_property(env, args[0], key, &has_property));

napi_value ret;
NAPI_CALL(env, napi_get_boolean(env, has_property, &ret));

return ret;
}

static napi_value HasOwn(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
Expand Down Expand Up @@ -220,8 +324,11 @@ EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("Get", Get),
DECLARE_NAPI_PROPERTY("GetNamed", GetNamed),
DECLARE_NAPI_PROPERTY("Set", Set),
DECLARE_NAPI_PROPERTY("SetNamed", SetNamed),
DECLARE_NAPI_PROPERTY("Has", Has),
DECLARE_NAPI_PROPERTY("HasNamed", HasNamed),
DECLARE_NAPI_PROPERTY("HasOwn", HasOwn),
DECLARE_NAPI_PROPERTY("Delete", Delete),
DECLARE_NAPI_PROPERTY("New", New),
Expand Down