| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 42b4f0f commit 242139f
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3081,6 +3081,50 @@ The native string is copied. | |||
| 3081 | 3081 | The JavaScript `string` type is described in | |
| 3082 | 3082 | [Section 6.1.4][] of the ECMAScript Language Specification. | |
| 3083 | 3083 | ||
| 3084 | + #### `node_api_create_property_key_utf16` | ||
| 3085 | + | ||
| 3086 | + <!-- YAML | ||
| 3087 | + added: REPLACEME | ||
| 3088 | + --> | ||
| 3089 | + | ||
| 3090 | + > Stability: 1 - Experimental | ||
| 3091 | + | ||
| 3092 | + ```c | ||
| 3093 | + napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, | ||
| 3094 | + const char16_t* str, | ||
| 3095 | + size_t length, | ||
| 3096 | + napi_value* result); | ||
| 3097 | + ``` | ||
| 3098 | + | ||
| 3099 | + * `[in] env`: The environment that the API is invoked under. | ||
| 3100 | + * `[in] str`: Character buffer representing a UTF16-LE-encoded string. | ||
| 3101 | + * `[in] length`: The length of the string in two-byte code units, or | ||
| 3102 | + `NAPI_AUTO_LENGTH` if it is null-terminated. | ||
| 3103 | + * `[out] result`: A `napi_value` representing an optimized JavaScript `string` | ||
| 3104 | + to be used as a property key for objects. | ||
| 3105 | + | ||
| 3106 | + Returns `napi_ok` if the API succeeded. | ||
| 3107 | + | ||
| 3108 | + This API creates an optimized JavaScript `string` value from | ||
| 3109 | + a UTF16-LE-encoded C string to be used as a property key for objects. | ||
| 3110 | + The native string is copied. | ||
| 3111 | + | ||
| 3112 | + Many JavaScript engines including V8 use internalized strings as keys | ||
| 3113 | + to set and get property values. They typically use a hash table to create | ||
| 3114 | + and lookup such strings. While it adds some cost per key creation, it improves | ||
| 3115 | + the performance after that by enabling comparison of string pointers instead | ||
| 3116 | + of the whole strings. | ||
| 3117 | + | ||
| 3118 | + If a new JavaScript string is intended to be used as a property key, then for | ||
| 3119 | + some JavaScript engines it will be more efficient to use | ||
| 3120 | + the `node_api_create_property_key_utf16` function. | ||
| 3121 | + Otherwise, use the `napi_create_string_utf16` or | ||
| 3122 | + `node_api_create_external_string_utf16` functions as there may be additional | ||
| 3123 | + overhead in creating/storing strings with this method. | ||
| 3124 | + | ||
| 3125 | + The JavaScript `string` type is described in | ||
| 3126 | + [Section 6.1.4][] of the ECMAScript Language Specification. | ||
| 3127 | + | ||
| 3084 | 3128 | ### Functions to convert from Node-API to C types | |
| 3085 | 3129 | ||
| 3086 | 3130 | #### `napi_get_array_length` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,6 +111,13 @@ node_api_create_external_string_utf16(napi_env env, | |||
| 111 | 111 | napi_value* result, | |
| 112 | 112 | bool* copied); | |
| 113 | 113 | #endif // NAPI_EXPERIMENTAL | |
| 114 | + | ||
| 115 | + #ifdef NAPI_EXPERIMENTAL | ||
| 116 | + #define NODE_API_EXPERIMENTAL_HAS_PROPERTY_KEYS | ||
| 117 | + NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_utf16( | ||
| 118 | + napi_env env, const char16_t* str, size_t length, napi_value* result); | ||
| 119 | + #endif // NAPI_EXPERIMENTAL | ||
| 120 | + | ||
| 114 | 121 | NAPI_EXTERN napi_status NAPI_CDECL napi_create_symbol(napi_env env, | |
| 115 | 122 | napi_value description, | |
| 116 | 123 | napi_value* result); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1708,6 +1708,18 @@ napi_status NAPI_CDECL node_api_create_external_string_utf16( | |||
| 1708 | 1708 | }); | |
| 1709 | 1709 | } | |
| 1710 | 1710 | ||
| 1711 | + napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env, | ||
| 1712 | + const char16_t* str, | ||
| 1713 | + size_t length, | ||
| 1714 | + napi_value* result) { | ||
| 1715 | + return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) { | ||
| 1716 | + return v8::String::NewFromTwoByte(isolate, | ||
| 1717 | + reinterpret_cast<const uint16_t*>(str), | ||
| 1718 | + v8::NewStringType::kInternalized, | ||
| 1719 | + static_cast<int>(length)); | ||
| 1720 | + }); | ||
| 1721 | + } | ||
| 1722 | + | ||
| 1711 | 1723 | napi_status NAPI_CDECL napi_create_double(napi_env env, | |
| 1712 | 1724 | double value, | |
| 1713 | 1725 | napi_value* result) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,8 @@ assert.strictEqual(test_string.TestLatin1External(empty), empty); | |||
| 16 | 16 | assert.strictEqual(test_string.TestUtf16External(empty), empty); | |
| 17 | 17 | assert.strictEqual(test_string.TestLatin1ExternalAutoLength(empty), empty); | |
| 18 | 18 | assert.strictEqual(test_string.TestUtf16ExternalAutoLength(empty), empty); | |
| 19 | + assert.strictEqual(test_string.TestPropertyKeyUtf16(empty), empty); | ||
| 20 | + assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(empty), empty); | ||
| 19 | 21 | assert.strictEqual(test_string.Utf16Length(empty), 0); | |
| 20 | 22 | assert.strictEqual(test_string.Utf8Length(empty), 0); | |
| 21 | 23 | ||
@@ -33,6 +35,8 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str1), str1); | |||
| 33 | 35 | assert.strictEqual(test_string.TestLatin1Insufficient(str1), str1.slice(0, 3)); | |
| 34 | 36 | assert.strictEqual(test_string.TestUtf8Insufficient(str1), str1.slice(0, 3)); | |
| 35 | 37 | assert.strictEqual(test_string.TestUtf16Insufficient(str1), str1.slice(0, 3)); | |
| 38 | + assert.strictEqual(test_string.TestPropertyKeyUtf16(str1), str1); | ||
| 39 | + assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str1), str1); | ||
| 36 | 40 | assert.strictEqual(test_string.Utf16Length(str1), 11); | |
| 37 | 41 | assert.strictEqual(test_string.Utf8Length(str1), 11); | |
| 38 | 42 | ||
@@ -50,6 +54,8 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str2), str2); | |||
| 50 | 54 | assert.strictEqual(test_string.TestLatin1Insufficient(str2), str2.slice(0, 3)); | |
| 51 | 55 | assert.strictEqual(test_string.TestUtf8Insufficient(str2), str2.slice(0, 3)); | |
| 52 | 56 | assert.strictEqual(test_string.TestUtf16Insufficient(str2), str2.slice(0, 3)); | |
| 57 | + assert.strictEqual(test_string.TestPropertyKeyUtf16(str2), str2); | ||
| 58 | + assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str2), str2); | ||
| 53 | 59 | assert.strictEqual(test_string.Utf16Length(str2), 62); | |
| 54 | 60 | assert.strictEqual(test_string.Utf8Length(str2), 62); | |
| 55 | 61 | ||
@@ -67,6 +73,8 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str3), str3); | |||
| 67 | 73 | assert.strictEqual(test_string.TestLatin1Insufficient(str3), str3.slice(0, 3)); | |
| 68 | 74 | assert.strictEqual(test_string.TestUtf8Insufficient(str3), str3.slice(0, 3)); | |
| 69 | 75 | assert.strictEqual(test_string.TestUtf16Insufficient(str3), str3.slice(0, 3)); | |
| 76 | + assert.strictEqual(test_string.TestPropertyKeyUtf16(str3), str3); | ||
| 77 | + assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str3), str3); | ||
| 70 | 78 | assert.strictEqual(test_string.Utf16Length(str3), 27); | |
| 71 | 79 | assert.strictEqual(test_string.Utf8Length(str3), 27); | |
| 72 | 80 | ||
@@ -84,6 +92,8 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str4), str4); | |||
| 84 | 92 | assert.strictEqual(test_string.TestLatin1Insufficient(str4), str4.slice(0, 3)); | |
| 85 | 93 | assert.strictEqual(test_string.TestUtf8Insufficient(str4), str4.slice(0, 1)); | |
| 86 | 94 | assert.strictEqual(test_string.TestUtf16Insufficient(str4), str4.slice(0, 3)); | |
| 95 | + assert.strictEqual(test_string.TestPropertyKeyUtf16(str4), str4); | ||
| 96 | + assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str4), str4); | ||
| 87 | 97 | assert.strictEqual(test_string.Utf16Length(str4), 31); | |
| 88 | 98 | assert.strictEqual(test_string.Utf8Length(str4), 62); | |
| 89 | 99 | ||
@@ -101,6 +111,8 @@ assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str5), str5); | |||
| 101 | 111 | assert.strictEqual(test_string.TestLatin1Insufficient(str5), str5.slice(0, 3)); | |
| 102 | 112 | assert.strictEqual(test_string.TestUtf8Insufficient(str5), str5.slice(0, 1)); | |
| 103 | 113 | assert.strictEqual(test_string.TestUtf16Insufficient(str5), str5.slice(0, 3)); | |
| 114 | + assert.strictEqual(test_string.TestPropertyKeyUtf16(str5), str5); | ||
| 115 | + assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str5), str5); | ||
| 104 | 116 | assert.strictEqual(test_string.Utf16Length(str5), 63); | |
| 105 | 117 | assert.strictEqual(test_string.Utf8Length(str5), 126); | |
| 106 | 118 | ||
@@ -113,6 +125,8 @@ assert.strictEqual(test_string.TestUtf16External(str6), str6); | |||
| 113 | 125 | assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str6), str6); | |
| 114 | 126 | assert.strictEqual(test_string.TestUtf8Insufficient(str6), str6.slice(0, 1)); | |
| 115 | 127 | assert.strictEqual(test_string.TestUtf16Insufficient(str6), str6.slice(0, 3)); | |
| 128 | + assert.strictEqual(test_string.TestPropertyKeyUtf16(str6), str6); | ||
| 129 | + assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str6), str6); | ||
| 116 | 130 | assert.strictEqual(test_string.Utf16Length(str6), 5); | |
| 117 | 131 | assert.strictEqual(test_string.Utf8Length(str6), 14); | |
| 118 | 132 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -294,6 +294,23 @@ static napi_value TestUtf16Insufficient(napi_env env, napi_callback_info info) { | |||
| 294 | 294 | return output; | |
| 295 | 295 | } | |
| 296 | 296 | ||
| 297 | + static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) { | ||
| 298 | + return TestTwoByteImpl(env, | ||
| 299 | + info, | ||
| 300 | + napi_get_value_string_utf16, | ||
| 301 | + node_api_create_property_key_utf16, | ||
| 302 | + actual_length); | ||
| 303 | + } | ||
| 304 | + | ||
| 305 | + static napi_value TestPropertyKeyUtf16AutoLength(napi_env env, | ||
| 306 | + napi_callback_info info) { | ||
| 307 | + return TestTwoByteImpl(env, | ||
| 308 | + info, | ||
| 309 | + napi_get_value_string_utf16, | ||
| 310 | + node_api_create_property_key_utf16, | ||
| 311 | + auto_length); | ||
| 312 | + } | ||
| 313 | + | ||
| 297 | 314 | static napi_value Utf16Length(napi_env env, napi_callback_info info) { | |
| 298 | 315 | napi_value args[1]; | |
| 299 | 316 | NODE_API_CALL(env, validate_and_retrieve_single_string_arg(env, info, args)); | |
@@ -410,6 +427,9 @@ napi_value Init(napi_env env, napi_value exports) { | |||
| 410 | 427 | DECLARE_NODE_API_PROPERTY("TestLargeLatin1", TestLargeLatin1), | |
| 411 | 428 | DECLARE_NODE_API_PROPERTY("TestLargeUtf16", TestLargeUtf16), | |
| 412 | 429 | DECLARE_NODE_API_PROPERTY("TestMemoryCorruption", TestMemoryCorruption), | |
| 430 | + DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16", TestPropertyKeyUtf16), | ||
| 431 | + DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16AutoLength", | ||
| 432 | + TestPropertyKeyUtf16AutoLength), | ||
| 413 | 433 | }; | |
| 414 | 434 | ||
| 415 | 435 | init_test_null(env, exports); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments