From f686b2ef1d91815c7391a1199294971a4c2a1fd0 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Thu, 19 Oct 2023 22:21:05 +0300 Subject: [PATCH 01/30] node-api: refactor napi set property function for improved performance --- src/js_native_api_v8.cc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 044244eccfe9ea..ae26d60fdcbce2 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1109,6 +1109,31 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, return GET_RETURN_STATUS(env); } +napi_status NAPI_CDECL napi_set_property_utf16(napi_env env, + napi_value object, + const uint16_t* utf16name, + napi_value value) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, utf16name); + CHECK_ARG(env, value); + + v8::Local context = env->context(); + v8::Local obj; + + CHECK_TO_OBJECT(env, context, obj, object); + + v8::Local k = v8::String::NewFromTwoByte(context->GetIsolate(), + utf16name, + v8::NewStringType::kNormal) + .ToLocalChecked(); + v8::Local val = v8impl::V8LocalValueFromJsValue(value); + + v8::Maybe set_maybe = obj->Set(context, k, val); + + RETURN_STATUS_IF_FALSE(env, set_maybe.FromMaybe(false), napi_generic_failure); + return GET_RETURN_STATUS(env); +} + napi_status NAPI_CDECL napi_has_property(napi_env env, napi_value object, napi_value key, From 6d66cbdc0ec1e39b63bdb3d66a51d8563ce5318f Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Wed, 25 Oct 2023 14:47:13 +0300 Subject: [PATCH 02/30] fix: lint --- src/js_native_api_v8.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index ae26d60fdcbce2..731f579fe0ab21 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1110,9 +1110,9 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, } napi_status NAPI_CDECL napi_set_property_utf16(napi_env env, - napi_value object, - const uint16_t* utf16name, - napi_value value) { + napi_value object, + const uint16_t* utf16name, + napi_value value) { NAPI_PREAMBLE(env); CHECK_ARG(env, utf16name); CHECK_ARG(env, value); From 4ecd6316d4171f7669026d13627412c8f192e960 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Wed, 25 Oct 2023 14:52:32 +0300 Subject: [PATCH 03/30] fix: lint --- src/js_native_api_v8.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 731f579fe0ab21..ee2e510631a42e 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1122,10 +1122,10 @@ napi_status NAPI_CDECL napi_set_property_utf16(napi_env env, CHECK_TO_OBJECT(env, context, obj, object); - v8::Local k = v8::String::NewFromTwoByte(context->GetIsolate(), - utf16name, - v8::NewStringType::kNormal) - .ToLocalChecked(); + v8::Local k = + v8::String::NewFromTwoByte( + context->GetIsolate(), utf16name, v8::NewStringType::kNormal) + .ToLocalChecked(); v8::Local val = v8impl::V8LocalValueFromJsValue(value); v8::Maybe set_maybe = obj->Set(context, k, val); From b36b9f7ced860292b3e3616d2eb7ddb535905554 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 6 Nov 2023 18:31:42 +0300 Subject: [PATCH 04/30] refactor: Improve performance by using internalized property keys --- src/js_native_api_v8.cc | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index ee2e510631a42e..a0984d96bc90df 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1109,26 +1109,42 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, return GET_RETURN_STATUS(env); } -napi_status NAPI_CDECL napi_set_property_utf16(napi_env env, - napi_value object, - const uint16_t* utf16name, - napi_value value) { +napi_status node_api_create_property_key_utf16(napi_env env, + const uint16_t* utf16name, napi_value* result) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, utf16name); + v8::Local context = env->context(); + + v8::Local k = v8::String::NewFromTwoByte( + context->GetIsolate(), utf16name, v8::NewStringType::kInternalized) + .ToLocalChecked(); + + v8impl::JsValueFromV8LocalValue(*result, k); + + return napi_ok; +} + +napi_status NAPI_CDECL napi_set_property_utf16(napi_env env, napi_value object, + const uint16_t* utf16name, napi_value value) { NAPI_PREAMBLE(env); CHECK_ARG(env, utf16name); CHECK_ARG(env, value); v8::Local context = env->context(); v8::Local obj; - CHECK_TO_OBJECT(env, context, obj, object); - v8::Local k = - v8::String::NewFromTwoByte( - context->GetIsolate(), utf16name, v8::NewStringType::kNormal) - .ToLocalChecked(); - v8::Local val = v8impl::V8LocalValueFromJsValue(value); + napi_value property_key; + napi_status status = node_api_create_property_key_utf16(env, + utf16name, &property_key); - v8::Maybe set_maybe = obj->Set(context, k, val); + if (status != napi_ok) { + return status; + } + + v8::Local val = v8impl::V8LocalValueFromJsValue(value); + v8::Maybe set_maybe = obj-> + Set(context, v8::Local::Cast(property_key), val); RETURN_STATUS_IF_FALSE(env, set_maybe.FromMaybe(false), napi_generic_failure); return GET_RETURN_STATUS(env); From 44641af392f3627dd6a042a015a099d7d41338b1 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 6 Nov 2023 19:47:26 +0300 Subject: [PATCH 05/30] refactor: Update node_api_create_property_key_utf16 signature and remove napi_set_property_utf16 --- src/js_native_api_v8.cc | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index a0984d96bc90df..dc2636f35155b5 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1110,13 +1110,13 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, } napi_status node_api_create_property_key_utf16(napi_env env, - const uint16_t* utf16name, napi_value* result) { + const uint16_t* utf16name, size_t length, napi_value* result) { NAPI_PREAMBLE(env); CHECK_ARG(env, utf16name); v8::Local context = env->context(); - v8::Local k = v8::String::NewFromTwoByte( - context->GetIsolate(), utf16name, v8::NewStringType::kInternalized) + context->GetIsolate(), + utf16name, v8::NewStringType::kInternalized, static_cast(length)) .ToLocalChecked(); v8impl::JsValueFromV8LocalValue(*result, k); @@ -1124,32 +1124,6 @@ napi_status node_api_create_property_key_utf16(napi_env env, return napi_ok; } -napi_status NAPI_CDECL napi_set_property_utf16(napi_env env, napi_value object, - const uint16_t* utf16name, napi_value value) { - NAPI_PREAMBLE(env); - CHECK_ARG(env, utf16name); - CHECK_ARG(env, value); - - v8::Local context = env->context(); - v8::Local obj; - CHECK_TO_OBJECT(env, context, obj, object); - - napi_value property_key; - napi_status status = node_api_create_property_key_utf16(env, - utf16name, &property_key); - - if (status != napi_ok) { - return status; - } - - v8::Local val = v8impl::V8LocalValueFromJsValue(value); - v8::Maybe set_maybe = obj-> - Set(context, v8::Local::Cast(property_key), val); - - RETURN_STATUS_IF_FALSE(env, set_maybe.FromMaybe(false), napi_generic_failure); - return GET_RETURN_STATUS(env); -} - napi_status NAPI_CDECL napi_has_property(napi_env env, napi_value object, napi_value key, From dab954480d5fbde84e317322ccd048a32f55a24b Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 6 Nov 2023 19:52:27 +0300 Subject: [PATCH 06/30] lint --- src/js_native_api_v8.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index dc2636f35155b5..57e447c5a65dff 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1114,10 +1114,12 @@ napi_status node_api_create_property_key_utf16(napi_env env, NAPI_PREAMBLE(env); CHECK_ARG(env, utf16name); v8::Local context = env->context(); - v8::Local k = v8::String::NewFromTwoByte( - context->GetIsolate(), - utf16name, v8::NewStringType::kInternalized, static_cast(length)) - .ToLocalChecked(); + v8::Local k = + v8::String::NewFromTwoByte(context->GetIsolate(), + utf16name, + v8::NewStringType::kInternalized, + static_cast(length)) + .ToLocalChecked(); v8impl::JsValueFromV8LocalValue(*result, k); From 8cf75bc62d660fbed31e03d6f288973769e97a07 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 6 Nov 2023 19:57:55 +0300 Subject: [PATCH 07/30] lint --- src/js_native_api_v8.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 57e447c5a65dff..2072eefcd6547a 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1110,7 +1110,9 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, } napi_status node_api_create_property_key_utf16(napi_env env, - const uint16_t* utf16name, size_t length, napi_value* result) { + const uint16_t* utf16name, + size_t length, + napi_value* result) { NAPI_PREAMBLE(env); CHECK_ARG(env, utf16name); v8::Local context = env->context(); From 202cdaae0fb9a74c68066cf811c000bdca050012 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 6 Nov 2023 20:09:16 +0300 Subject: [PATCH 08/30] fix: Resolve compilation error in node_api_create_property_key_utf16 --- src/js_native_api_v8.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 2072eefcd6547a..63ded925be0291 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1123,7 +1123,7 @@ napi_status node_api_create_property_key_utf16(napi_env env, static_cast(length)) .ToLocalChecked(); - v8impl::JsValueFromV8LocalValue(*result, k); + *result = k; return napi_ok; } From da9371c23103a4233772c8c8f3583a9fcfcfbb23 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 6 Nov 2023 20:15:59 +0300 Subject: [PATCH 09/30] fix: Resolve type conversion error in node_api_create_property_key_utf16 --- src/js_native_api_v8.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 63ded925be0291..b94128de588e14 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1123,7 +1123,7 @@ napi_status node_api_create_property_key_utf16(napi_env env, static_cast(length)) .ToLocalChecked(); - *result = k; + *result = reinterpret_cast(&k); return napi_ok; } From 9e9653cccc797cf13ddbfaf19876b364632d6aef Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Tue, 7 Nov 2023 12:41:10 +0300 Subject: [PATCH 10/30] refactor: Simplify node_api_create_property_key_utf16 implementation --- src/js_native_api_v8.cc | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index b94128de588e14..4f9030b0060982 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1109,23 +1109,16 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, return GET_RETURN_STATUS(env); } -napi_status node_api_create_property_key_utf16(napi_env env, - const uint16_t* utf16name, - size_t length, - napi_value* result) { - NAPI_PREAMBLE(env); - CHECK_ARG(env, utf16name); - v8::Local context = env->context(); - v8::Local k = - v8::String::NewFromTwoByte(context->GetIsolate(), - utf16name, - v8::NewStringType::kInternalized, - static_cast(length)) - .ToLocalChecked(); - - *result = reinterpret_cast(&k); - - return napi_ok; +napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result) { + return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) { + return v8::String::NewFromTwoByte(isolate, + reinterpret_cast(str), + v8::NewStringType::kInternalized, + static_cast(length)); + }); } napi_status NAPI_CDECL napi_has_property(napi_env env, From f7ab085acfcb1d00cc7782648219ec5df59afd90 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Tue, 7 Nov 2023 12:44:41 +0300 Subject: [PATCH 11/30] lint --- src/js_native_api_v8.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 4f9030b0060982..2dcd2584a97025 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1110,9 +1110,9 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, } napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result) { + const char16_t* str, + size_t length, + napi_value* result) { return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) { return v8::String::NewFromTwoByte(isolate, reinterpret_cast(str), From d072933a54b0cac59f07f970a83bd30d41a6243c Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Sun, 12 Nov 2023 16:15:40 +0300 Subject: [PATCH 12/30] add node_api_create_property_key_utf16 property name --- src/js_native_api.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/js_native_api.h b/src/js_native_api.h index d665052b947552..962631b56f95e6 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -208,6 +208,10 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_has_property(napi_env env, napi_value object, napi_value key, bool* result); +NAPI_EXTERN napi_status NAPİ_CDECL node_api_create_property_key_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result); NAPI_EXTERN napi_status NAPI_CDECL napi_get_property(napi_env env, napi_value object, napi_value key, From 382f3e02b41680b942191b853ab307a143f4ca48 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Sun, 12 Nov 2023 16:25:00 +0300 Subject: [PATCH 13/30] added doc for node_api_create_property_key_utf16 --- doc/api/n-api.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index dd1bc884e506a8..c2172c6874e148 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2892,6 +2892,33 @@ life cycle of the JavaScript value. The JavaScript `string` type is described in [Section 6.1.4][] of the ECMAScript Language Specification. + +#### `node_api_create_property_key_utf16` + + + +> Stability: 1 - Experimental + +```c +napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result); +``` + +* `[in] env`: The environment that the API is invoked under. +* `[in] str`: A pointer to a buffer containing `UTF-16` encoded characters. +* `[in] length`: The length of the string in `UTF-16` code units. +* `[out] result`: A `napi_value` representing a JavaScript `string`. +Returns napi_ok if the API succeeded. + +This API creates a JavaScript `string` value from a `UTF-16` encoded C string. The native `string` may not be copied and must thus exist for the entire life cycle of the JavaScript value. + +The JavaScript `string` type is described in Section 6.1.4 of the ECMAScript Language Specification. + #### `napi_create_string_utf16` > Stability: 1 - Experimental @@ -2912,7 +2911,7 @@ napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, * `[in] str`: A pointer to a buffer containing `UTF-16` encoded characters. * `[in] length`: The length of the string in `UTF-16` code units. * `[out] result`: A `napi_value` representing a JavaScript `string`. - Returns napi\_ok if the API succeeded. +Returns napi_ok if the API succeeded. This API creates a JavaScript `string` value from a `UTF-16` encoded C string. The native `string` may not be copied and must thus exist for the entire life cycle of the JavaScript value. From 6a5bdc77073f29e8affad0d893f975a801fdff38 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 13 Nov 2023 14:07:51 +0300 Subject: [PATCH 17/30] update: code snipet --- src/js_native_api.h | 14 +++++++++----- src/js_native_api_v8.cc | 24 ++++++++++++------------ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/js_native_api.h b/src/js_native_api.h index 7deb981aa347d9..726b02ef934d77 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -110,6 +110,15 @@ node_api_create_external_string_utf16(napi_env env, napi_value* result, bool* copied); #endif // NAPI_EXPERIMENTAL +#ifdef NAPI_EXPERIMENTAL + +NAPI_EXTERN napi_status NAPI_CDECL +node_api_create_property_key_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result); + +#endif // NAPI_EXPERIMENTAL NAPI_EXTERN napi_status NAPI_CDECL napi_create_symbol(napi_env env, napi_value description, napi_value* result); @@ -208,11 +217,6 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_has_property(napi_env env, napi_value object, napi_value key, bool* result); -NAPI_EXTERN napi_status NAPİ_CDECL -node_api_create_property_key_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result); NAPI_EXTERN napi_status NAPI_CDECL napi_get_property(napi_env env, napi_value object, napi_value key, diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 2dcd2584a97025..94b03e0e23195f 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1109,18 +1109,6 @@ napi_status NAPI_CDECL napi_set_property(napi_env env, return GET_RETURN_STATUS(env); } -napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result) { - return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) { - return v8::String::NewFromTwoByte(isolate, - reinterpret_cast(str), - v8::NewStringType::kInternalized, - static_cast(length)); - }); -} - napi_status NAPI_CDECL napi_has_property(napi_env env, napi_value object, napi_value key, @@ -1636,6 +1624,18 @@ node_api_create_external_string_latin1(napi_env env, }); } +napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result) { + return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) { + return v8::String::NewFromTwoByte(isolate, + reinterpret_cast(str), + v8::NewStringType::kInternalized, + static_cast(length)); + }); +} + napi_status NAPI_CDECL node_api_create_external_string_utf16(napi_env env, char16_t* str, From 3ad361bf99185fcf80b143e29c25bb4f5195a04a Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 13 Nov 2023 14:08:11 +0300 Subject: [PATCH 18/30] test: added test for node_api_create_property_key_utf16 --- test/js-native-api/test_string/test.js | 7 ++++++ test/js-native-api/test_string/test_string.c | 24 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/test/js-native-api/test_string/test.js b/test/js-native-api/test_string/test.js index 8926b9451175bd..1938d11e5f16b2 100644 --- a/test/js-native-api/test_string/test.js +++ b/test/js-native-api/test_string/test.js @@ -18,6 +18,7 @@ assert.strictEqual(test_string.TestLatin1ExternalAutoLength(empty), empty); assert.strictEqual(test_string.TestUtf16ExternalAutoLength(empty), empty); assert.strictEqual(test_string.Utf16Length(empty), 0); assert.strictEqual(test_string.Utf8Length(empty), 0); +assert.strictEqual(test_string.TestPropertyKeyUtf16(empty), empty); const str1 = 'hello world'; assert.strictEqual(test_string.TestLatin1(str1), str1); @@ -35,6 +36,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str1), str1.slice(0, 3)); assert.strictEqual(test_string.TestUtf16Insufficient(str1), str1.slice(0, 3)); assert.strictEqual(test_string.Utf16Length(str1), 11); assert.strictEqual(test_string.Utf8Length(str1), 11); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str1), str1); const str2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; assert.strictEqual(test_string.TestLatin1(str2), str2); @@ -52,6 +54,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str2), str2.slice(0, 3)); assert.strictEqual(test_string.TestUtf16Insufficient(str2), str2.slice(0, 3)); assert.strictEqual(test_string.Utf16Length(str2), 62); assert.strictEqual(test_string.Utf8Length(str2), 62); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str2), str2); const str3 = '?!@#$%^&*()_+-=[]{}/.,<>\'"\\'; assert.strictEqual(test_string.TestLatin1(str3), str3); @@ -69,6 +72,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str3), str3.slice(0, 3)); assert.strictEqual(test_string.TestUtf16Insufficient(str3), str3.slice(0, 3)); assert.strictEqual(test_string.Utf16Length(str3), 27); assert.strictEqual(test_string.Utf8Length(str3), 27); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str3), str3); const str4 = '¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿'; assert.strictEqual(test_string.TestLatin1(str4), str4); @@ -86,6 +90,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str4), str4.slice(0, 1)); assert.strictEqual(test_string.TestUtf16Insufficient(str4), str4.slice(0, 3)); assert.strictEqual(test_string.Utf16Length(str4), 31); assert.strictEqual(test_string.Utf8Length(str4), 62); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str4), str4); const str5 = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ'; assert.strictEqual(test_string.TestLatin1(str5), str5); @@ -103,6 +108,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str5), str5.slice(0, 1)); assert.strictEqual(test_string.TestUtf16Insufficient(str5), str5.slice(0, 3)); assert.strictEqual(test_string.Utf16Length(str5), 63); assert.strictEqual(test_string.Utf8Length(str5), 126); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str5), str5); const str6 = '\u{2003}\u{2101}\u{2001}\u{202}\u{2011}'; assert.strictEqual(test_string.TestUtf8(str6), str6); @@ -115,6 +121,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str6), str6.slice(0, 1)); assert.strictEqual(test_string.TestUtf16Insufficient(str6), str6.slice(0, 3)); assert.strictEqual(test_string.Utf16Length(str6), 5); assert.strictEqual(test_string.Utf8Length(str6), 14); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str6), str6); assert.throws(() => { test_string.TestLargeUtf8(); diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index b2046e3b873392..7cf181cd2f3d83 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -120,6 +120,21 @@ static napi_status create_external_latin1(napi_env env, return napi_ok; } +static napi_status create_property_key_utf16(napi_env env, + const char16_t* string, + size_t length, + napi_value* result) { + // Convert UTF-16 string to napi_value + napi_status status = napi_create_string_utf16(env, string, length, result); + + if (status != napi_ok) { + // Handle necessary operations in case of an error + return status; + } + + return napi_ok; +} + // strlen for char16_t. Needed in case we're copying a string of length // NAPI_AUTO_LENGTH. static size_t strlen16(const char16_t* string) { @@ -221,6 +236,15 @@ static napi_value TestUtf16External(napi_env env, napi_callback_info info) { actual_length); } + +static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) { + return TestOneByteImpl(env, + info, + napi_get_value_string_utf16, + create_property_key_utf16, + actual_length); +} + static napi_value TestLatin1ExternalAutoLength(napi_env env, napi_callback_info info) { return TestOneByteImpl(env, From 99e3ae50f0bec7dad27921cf7521ecf39f55688a Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 13 Nov 2023 14:09:46 +0300 Subject: [PATCH 19/30] lint --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 8bec18a720dd93..9a490458945698 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2911,7 +2911,7 @@ napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, * `[in] str`: A pointer to a buffer containing `UTF-16` encoded characters. * `[in] length`: The length of the string in `UTF-16` code units. * `[out] result`: A `napi_value` representing a JavaScript `string`. -Returns napi_ok if the API succeeded. + Returns napi\_ok if the API succeeded. This API creates a JavaScript `string` value from a `UTF-16` encoded C string. The native `string` may not be copied and must thus exist for the entire life cycle of the JavaScript value. From 05f7ab0109f6fa0d8105283fca050c8f93351947 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 13 Nov 2023 14:22:09 +0300 Subject: [PATCH 20/30] lint --- src/js_native_api.h | 7 ++----- test/js-native-api/test_string/test_string.c | 15 +++++++-------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/js_native_api.h b/src/js_native_api.h index 726b02ef934d77..e8e9462cece82c 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -112,11 +112,8 @@ node_api_create_external_string_utf16(napi_env env, #endif // NAPI_EXPERIMENTAL #ifdef NAPI_EXPERIMENTAL -NAPI_EXTERN napi_status NAPI_CDECL -node_api_create_property_key_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_utf16( + napi_env env, const char16_t* str, size_t length, napi_value* result); #endif // NAPI_EXPERIMENTAL NAPI_EXTERN napi_status NAPI_CDECL napi_create_symbol(napi_env env, diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index 7cf181cd2f3d83..4d16d95d901077 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -124,15 +124,15 @@ static napi_status create_property_key_utf16(napi_env env, const char16_t* string, size_t length, napi_value* result) { - // Convert UTF-16 string to napi_value - napi_status status = napi_create_string_utf16(env, string, length, result); + // Convert UTF-16 string to napi_value + napi_status status = napi_create_string_utf16(env, string, length, result); - if (status != napi_ok) { - // Handle necessary operations in case of an error - return status; - } + if (status != napi_ok) { + // Handle necessary operations in case of an error + return status; + } - return napi_ok; + return napi_ok; } // strlen for char16_t. Needed in case we're copying a string of length @@ -236,7 +236,6 @@ static napi_value TestUtf16External(napi_env env, napi_callback_info info) { actual_length); } - static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) { return TestOneByteImpl(env, info, From fe733dbd69a9f975a7230679847737feeb4a721c Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Mon, 13 Nov 2023 14:25:02 +0300 Subject: [PATCH 21/30] lint --- test/js-native-api/test_string/test_string.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index 4d16d95d901077..e571e72748497b 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -128,8 +128,8 @@ static napi_status create_property_key_utf16(napi_env env, napi_status status = napi_create_string_utf16(env, string, length, result); if (status != napi_ok) { - // Handle necessary operations in case of an error - return status; + // Handle necessary operations in case of an error + return status; } return napi_ok; From 89cb720758272d67b86b95e5b03163fa52906e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20Can=20Alt=C4=B1n?= Date: Sun, 19 Nov 2023 12:50:19 +0300 Subject: [PATCH 22/30] Update doc/api/n-api.md Co-authored-by: Chengzhong Wu --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 9a490458945698..e8a64b966b88ec 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2913,7 +2913,7 @@ napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, * `[out] result`: A `napi_value` representing a JavaScript `string`. Returns napi\_ok if the API succeeded. -This API creates a JavaScript `string` value from a `UTF-16` encoded C string. The native `string` may not be copied and must thus exist for the entire life cycle of the JavaScript value. +This API creates a JavaScript `string` value from a `UTF-16` encoded C string. The native string is copied. The JavaScript `string` type is described in Section 6.1.4 of the ECMAScript Language Specification. From 8179670dac7d76dc66e7ccb9e143c0da978fc477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20Can=20Alt=C4=B1n?= Date: Sun, 19 Nov 2023 12:50:27 +0300 Subject: [PATCH 23/30] Update test/js-native-api/test_string/test_string.c Co-authored-by: Chengzhong Wu --- test/js-native-api/test_string/test_string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index e571e72748497b..d64c1372360319 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -240,7 +240,7 @@ static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) { return TestOneByteImpl(env, info, napi_get_value_string_utf16, - create_property_key_utf16, + node_api_create_property_key_utf16, actual_length); } From e7bbd09698023c8c991dee85fcafe39a323e896a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20Can=20Alt=C4=B1n?= Date: Sun, 19 Nov 2023 12:50:52 +0300 Subject: [PATCH 24/30] Update test/js-native-api/test_string/test_string.c Co-authored-by: Chengzhong Wu --- test/js-native-api/test_string/test_string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index d64c1372360319..6a916636b893fc 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -237,7 +237,7 @@ static napi_value TestUtf16External(napi_env env, napi_callback_info info) { } static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) { - return TestOneByteImpl(env, + return TestTwoByteImpl(env, info, napi_get_value_string_utf16, node_api_create_property_key_utf16, From f5d252e0cbc68fa48caf8a71cea929e9e1217b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20Can=20Alt=C4=B1n?= Date: Sun, 19 Nov 2023 12:51:09 +0300 Subject: [PATCH 25/30] Update doc/api/n-api.md Co-authored-by: Chengzhong Wu --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index e8a64b966b88ec..a459f2f1eb2b58 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2915,7 +2915,7 @@ napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, This API creates a JavaScript `string` value from a `UTF-16` encoded C string. The native string is copied. -The JavaScript `string` type is described in Section 6.1.4 of the ECMAScript Language Specification. +The JavaScript `string` type is described in Section 6.1.4 of the ECMAScript Language Specification. The string created with this API hints that it will be used as a property key. Aside from performance implications, there are no differences from `napi_create_string_utf16`. #### `napi_create_string_utf16` From 555ba66f9433c2b1f56af036e0f06f9a34a0d086 Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Sun, 19 Nov 2023 14:19:08 +0300 Subject: [PATCH 26/30] call node_api_create_property_key_utf16 --- test/js-native-api/test_string/test_string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index e571e72748497b..9a149f3eb20624 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -125,7 +125,7 @@ static napi_status create_property_key_utf16(napi_env env, size_t length, napi_value* result) { // Convert UTF-16 string to napi_value - napi_status status = napi_create_string_utf16(env, string, length, result); + napi_status status = node_api_create_property_key_utf16(env, string, length, result); if (status != napi_ok) { // Handle necessary operations in case of an error From fcfa24c82fb955aeed40592aeaeeec2bc9db00cc Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Sun, 19 Nov 2023 14:24:48 +0300 Subject: [PATCH 27/30] added TestPropertyKeyUtf16 napi_property_descriptor --- test/js-native-api/test_string/test_string.c | 1 + 1 file changed, 1 insertion(+) diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index 6a9f1c17a0188a..8cca8260da8c59 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -433,6 +433,7 @@ napi_value Init(napi_env env, napi_value exports) { DECLARE_NODE_API_PROPERTY("TestLargeLatin1", TestLargeLatin1), DECLARE_NODE_API_PROPERTY("TestLargeUtf16", TestLargeUtf16), DECLARE_NODE_API_PROPERTY("TestMemoryCorruption", TestMemoryCorruption), + DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16", TestPropertyKeyUtf16), }; init_test_null(env, exports); From f448405b5339420ca19a39db747fe28a71a4c66f Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Sun, 19 Nov 2023 14:57:09 +0300 Subject: [PATCH 28/30] lint doc --- doc/api/n-api.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index a459f2f1eb2b58..46612abe2b4266 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2913,9 +2913,17 @@ napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, * `[out] result`: A `napi_value` representing a JavaScript `string`. Returns napi\_ok if the API succeeded. -This API creates a JavaScript `string` value from a `UTF-16` encoded C string. The native string is copied. +This API creates a JavaScript `string` value +from a `UTF-16` encoded C string. The native string is copied. -The JavaScript `string` type is described in Section 6.1.4 of the ECMAScript Language Specification. The string created with this API hints that it will be used as a property key. Aside from performance implications, there are no differences from `napi_create_string_utf16`. +The JavaScript `string` type is +described in Section 6.1.4 of the +ECMAScript Language Specification. +The string created with this API +hints that it will be used as a property key. +Aside from performance implications, +there are no differences +from `napi_create_string_utf16`. #### `napi_create_string_utf16` From cc8ba66556b522d4bc9fa9b542dd1653fe70411b Mon Sep 17 00:00:00 2001 From: "mert.altin" Date: Sun, 19 Nov 2023 15:01:54 +0300 Subject: [PATCH 29/30] lint cpp --- test/js-native-api/test_string/test_string.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index 8cca8260da8c59..81fab4f34972c8 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -125,7 +125,8 @@ static napi_status create_property_key_utf16(napi_env env, size_t length, napi_value* result) { // Convert UTF-16 string to napi_value - napi_status status = node_api_create_property_key_utf16(env, string, length, result); + napi_status status = + node_api_create_property_key_utf16(env, string, length, result); if (status != napi_ok) { // Handle necessary operations in case of an error From 70e02d8bdf6ecf008c03e776b3438b02230496b9 Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Sun, 3 Dec 2023 15:01:24 -0800 Subject: [PATCH 30/30] minor updates to PR #50282 --- doc/api/n-api.md | 75 +++++++++++--------- src/js_native_api.h | 5 +- src/js_native_api_v8.cc | 24 +++---- test/js-native-api/test_string/test.js | 21 ++++-- test/js-native-api/test_string/test_string.c | 27 ++++--- 5 files changed, 90 insertions(+), 62 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 46612abe2b4266..e523c6199edae2 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2892,39 +2892,6 @@ life cycle of the JavaScript value. The JavaScript `string` type is described in [Section 6.1.4][] of the ECMAScript Language Specification. -#### `node_api_create_property_key_utf16` - - - -> Stability: 1 - Experimental - -```c -napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result); -``` - -* `[in] env`: The environment that the API is invoked under. -* `[in] str`: A pointer to a buffer containing `UTF-16` encoded characters. -* `[in] length`: The length of the string in `UTF-16` code units. -* `[out] result`: A `napi_value` representing a JavaScript `string`. - Returns napi\_ok if the API succeeded. - -This API creates a JavaScript `string` value -from a `UTF-16` encoded C string. The native string is copied. - -The JavaScript `string` type is -described in Section 6.1.4 of the -ECMAScript Language Specification. -The string created with this API -hints that it will be used as a property key. -Aside from performance implications, -there are no differences -from `napi_create_string_utf16`. - #### `napi_create_string_utf16` + +> Stability: 1 - Experimental + +```c +napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result); +``` + +* `[in] env`: The environment that the API is invoked under. +* `[in] str`: Character buffer representing a UTF16-LE-encoded string. +* `[in] length`: The length of the string in two-byte code units, or + `NAPI_AUTO_LENGTH` if it is null-terminated. +* `[out] result`: A `napi_value` representing an optimized JavaScript `string` + to be used as a property key for objects. + +Returns `napi_ok` if the API succeeded. + +This API creates an optimized JavaScript `string` value from +a UTF16-LE-encoded C string to be used as a property key for objects. +The native string is copied. + +Many JavaScript engines including V8 use internalized strings as keys +to set and get property values. They typically use a hash table to create +and lookup such strings. While it adds some cost per key creation, it improves +the performance after that by enabling comparison of string pointers instead +of the whole strings. + +If a new JavaScript string is intended to be used as a property key, then +it is more efficient to use the `node_api_create_property_key_utf16` function. +Otherwise, for the string values use the `napi_create_string_utf16` or +`node_api_create_external_string_utf16` functions. + +The JavaScript `string` type is described in +[Section 6.1.4][] of the ECMAScript Language Specification. + ### Functions to convert from Node-API to C types #### `napi_get_array_length` diff --git a/src/js_native_api.h b/src/js_native_api.h index e8e9462cece82c..5b8a26ed2331cf 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -110,12 +110,13 @@ node_api_create_external_string_utf16(napi_env env, napi_value* result, bool* copied); #endif // NAPI_EXPERIMENTAL -#ifdef NAPI_EXPERIMENTAL +#ifdef NAPI_EXPERIMENTAL +#define NODE_API_EXPERIMENTAL_HAS_PROPERTY_KEYS NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_utf16( napi_env env, const char16_t* str, size_t length, napi_value* result); - #endif // NAPI_EXPERIMENTAL + NAPI_EXTERN napi_status NAPI_CDECL napi_create_symbol(napi_env env, napi_value description, napi_value* result); diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 94b03e0e23195f..78d6f24e8796ee 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1624,18 +1624,6 @@ node_api_create_external_string_latin1(napi_env env, }); } -napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result) { - return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) { - return v8::String::NewFromTwoByte(isolate, - reinterpret_cast(str), - v8::NewStringType::kInternalized, - static_cast(length)); - }); -} - napi_status NAPI_CDECL node_api_create_external_string_utf16(napi_env env, char16_t* str, @@ -1663,6 +1651,18 @@ node_api_create_external_string_utf16(napi_env env, }); } +napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result) { + return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) { + return v8::String::NewFromTwoByte(isolate, + reinterpret_cast(str), + v8::NewStringType::kInternalized, + static_cast(length)); + }); +} + napi_status NAPI_CDECL napi_create_double(napi_env env, double value, napi_value* result) { diff --git a/test/js-native-api/test_string/test.js b/test/js-native-api/test_string/test.js index 1938d11e5f16b2..ffcf0fab754e7c 100644 --- a/test/js-native-api/test_string/test.js +++ b/test/js-native-api/test_string/test.js @@ -16,9 +16,10 @@ assert.strictEqual(test_string.TestLatin1External(empty), empty); assert.strictEqual(test_string.TestUtf16External(empty), empty); assert.strictEqual(test_string.TestLatin1ExternalAutoLength(empty), empty); assert.strictEqual(test_string.TestUtf16ExternalAutoLength(empty), empty); +assert.strictEqual(test_string.TestPropertyKeyUtf16(empty), empty); +assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(empty), empty); assert.strictEqual(test_string.Utf16Length(empty), 0); assert.strictEqual(test_string.Utf8Length(empty), 0); -assert.strictEqual(test_string.TestPropertyKeyUtf16(empty), empty); const str1 = 'hello world'; assert.strictEqual(test_string.TestLatin1(str1), str1); @@ -34,9 +35,10 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str1), str1); assert.strictEqual(test_string.TestLatin1Insufficient(str1), str1.slice(0, 3)); assert.strictEqual(test_string.TestUtf8Insufficient(str1), str1.slice(0, 3)); assert.strictEqual(test_string.TestUtf16Insufficient(str1), str1.slice(0, 3)); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str1), str1); +assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str1), str1); assert.strictEqual(test_string.Utf16Length(str1), 11); assert.strictEqual(test_string.Utf8Length(str1), 11); -assert.strictEqual(test_string.TestPropertyKeyUtf16(str1), str1); const str2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; assert.strictEqual(test_string.TestLatin1(str2), str2); @@ -52,9 +54,10 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str2), str2); assert.strictEqual(test_string.TestLatin1Insufficient(str2), str2.slice(0, 3)); assert.strictEqual(test_string.TestUtf8Insufficient(str2), str2.slice(0, 3)); assert.strictEqual(test_string.TestUtf16Insufficient(str2), str2.slice(0, 3)); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str2), str2); +assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str2), str2); assert.strictEqual(test_string.Utf16Length(str2), 62); assert.strictEqual(test_string.Utf8Length(str2), 62); -assert.strictEqual(test_string.TestPropertyKeyUtf16(str2), str2); const str3 = '?!@#$%^&*()_+-=[]{}/.,<>\'"\\'; assert.strictEqual(test_string.TestLatin1(str3), str3); @@ -70,9 +73,10 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str3), str3); assert.strictEqual(test_string.TestLatin1Insufficient(str3), str3.slice(0, 3)); assert.strictEqual(test_string.TestUtf8Insufficient(str3), str3.slice(0, 3)); assert.strictEqual(test_string.TestUtf16Insufficient(str3), str3.slice(0, 3)); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str3), str3); +assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str3), str3); assert.strictEqual(test_string.Utf16Length(str3), 27); assert.strictEqual(test_string.Utf8Length(str3), 27); -assert.strictEqual(test_string.TestPropertyKeyUtf16(str3), str3); const str4 = '¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿'; assert.strictEqual(test_string.TestLatin1(str4), str4); @@ -88,9 +92,10 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str4), str4); assert.strictEqual(test_string.TestLatin1Insufficient(str4), str4.slice(0, 3)); assert.strictEqual(test_string.TestUtf8Insufficient(str4), str4.slice(0, 1)); assert.strictEqual(test_string.TestUtf16Insufficient(str4), str4.slice(0, 3)); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str4), str4); +assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str4), str4); assert.strictEqual(test_string.Utf16Length(str4), 31); assert.strictEqual(test_string.Utf8Length(str4), 62); -assert.strictEqual(test_string.TestPropertyKeyUtf16(str4), str4); const str5 = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ'; assert.strictEqual(test_string.TestLatin1(str5), str5); @@ -106,9 +111,10 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str5), str5); assert.strictEqual(test_string.TestLatin1Insufficient(str5), str5.slice(0, 3)); assert.strictEqual(test_string.TestUtf8Insufficient(str5), str5.slice(0, 1)); assert.strictEqual(test_string.TestUtf16Insufficient(str5), str5.slice(0, 3)); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str5), str5); +assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str5), str5); assert.strictEqual(test_string.Utf16Length(str5), 63); assert.strictEqual(test_string.Utf8Length(str5), 126); -assert.strictEqual(test_string.TestPropertyKeyUtf16(str5), str5); const str6 = '\u{2003}\u{2101}\u{2001}\u{202}\u{2011}'; assert.strictEqual(test_string.TestUtf8(str6), str6); @@ -119,9 +125,10 @@ assert.strictEqual(test_string.TestUtf16External(str6), str6); assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str6), str6); assert.strictEqual(test_string.TestUtf8Insufficient(str6), str6.slice(0, 1)); assert.strictEqual(test_string.TestUtf16Insufficient(str6), str6.slice(0, 3)); +assert.strictEqual(test_string.TestPropertyKeyUtf16(str6), str6); +assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str6), str6); assert.strictEqual(test_string.Utf16Length(str6), 5); assert.strictEqual(test_string.Utf8Length(str6), 14); -assert.strictEqual(test_string.TestPropertyKeyUtf16(str6), str6); assert.throws(() => { test_string.TestLargeUtf8(); diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c index 81fab4f34972c8..fb7102eabac2be 100644 --- a/test/js-native-api/test_string/test_string.c +++ b/test/js-native-api/test_string/test_string.c @@ -237,14 +237,6 @@ static napi_value TestUtf16External(napi_env env, napi_callback_info info) { actual_length); } -static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) { - return TestTwoByteImpl(env, - info, - napi_get_value_string_utf16, - node_api_create_property_key_utf16, - actual_length); -} - static napi_value TestLatin1ExternalAutoLength(napi_env env, napi_callback_info info) { return TestOneByteImpl(env, @@ -318,6 +310,23 @@ static napi_value TestUtf16Insufficient(napi_env env, napi_callback_info info) { return output; } +static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) { + return TestTwoByteImpl(env, + info, + napi_get_value_string_utf16, + node_api_create_property_key_utf16, + actual_length); +} + +static napi_value TestPropertyKeyUtf16AutoLength(napi_env env, + napi_callback_info info) { + return TestTwoByteImpl(env, + info, + napi_get_value_string_utf16, + node_api_create_property_key_utf16, + auto_length); +} + static napi_value Utf16Length(napi_env env, napi_callback_info info) { napi_value args[1]; NODE_API_CALL(env, validate_and_retrieve_single_string_arg(env, info, args)); @@ -435,6 +444,8 @@ napi_value Init(napi_env env, napi_value exports) { DECLARE_NODE_API_PROPERTY("TestLargeUtf16", TestLargeUtf16), DECLARE_NODE_API_PROPERTY("TestMemoryCorruption", TestMemoryCorruption), DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16", TestPropertyKeyUtf16), + DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16AutoLength", + TestPropertyKeyUtf16AutoLength), }; init_test_null(env, exports);