diff --git a/test/js-native-api/test_object/test.js b/test/js-native-api/test_object/test.js index 8e44a1b5ed647f..3047b1f58dc602 100644 --- a/test/js-native-api/test_object/test.js +++ b/test/js-native-api/test_object/test.js @@ -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')); @@ -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); diff --git a/test/js-native-api/test_object/test_object.c b/test/js-native-api/test_object/test_object.c index c7f3d55f901340..9e2c6de0e0557b 100644 --- a/test/js-native-api/test_object/test_object.c +++ b/test/js-native-api/test_object/test_object.c @@ -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; + 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]; @@ -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]; @@ -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]; @@ -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),