Skip to content

Commit 0d3ea23

Browse files
authored
add node_api_create_property_key_utf16 as a alias of napi_create_string_utf16 (#95)
1 parent c3e2d6c commit 0d3ea23

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

packages/emnapi/include/js_native_api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ node_api_create_external_string_utf16(napi_env env,
117117
napi_value* result,
118118
bool* copied);
119119
#endif // NAPI_EXPERIMENTAL
120+
121+
#ifdef NAPI_EXPERIMENTAL
122+
#define NODE_API_EXPERIMENTAL_HAS_PROPERTY_KEYS
123+
NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_utf16(
124+
napi_env env, const char16_t* str, size_t length, napi_value* result);
125+
#endif // NAPI_EXPERIMENTAL
126+
120127
NAPI_EXTERN napi_status NAPI_CDECL napi_create_symbol(napi_env env,
121128
napi_value description,
122129
napi_value* result);

packages/emnapi/src/value/convert2napi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ export function node_api_create_external_string_utf16 (
165165
)
166166
}
167167

168+
/**
169+
* @__sig ipppp
170+
*/
171+
export function node_api_create_property_key_utf16 (env: napi_env, str: const_char16_t_p, length: size_t, result: Pointer<napi_value>): napi_status {
172+
return napi_create_string_utf16(env, str, length, result)
173+
}
174+
168175
/**
169176
* @__sig ipjp
170177
*/

packages/test/string/binding.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ static napi_status create_external_latin1(napi_env env,
159159
return napi_ok;
160160
}
161161

162+
static napi_status create_property_key_utf16(napi_env env,
163+
const char16_t* string,
164+
size_t length,
165+
napi_value* result) {
166+
// Convert UTF-16 string to napi_value
167+
napi_status status =
168+
node_api_create_property_key_utf16(env, string, length, result);
169+
170+
if (status != napi_ok) {
171+
// Handle necessary operations in case of an error
172+
return status;
173+
}
174+
175+
return napi_ok;
176+
}
177+
162178
// strlen for char16_t. Needed in case we're copying a string of length
163179
// NAPI_AUTO_LENGTH.
164180
static size_t strlen16(const char16_t* string) {
@@ -333,6 +349,23 @@ static napi_value TestUtf16Insufficient(napi_env env, napi_callback_info info) {
333349
return output;
334350
}
335351

352+
static napi_value TestPropertyKeyUtf16(napi_env env, napi_callback_info info) {
353+
return TestTwoByteImpl(env,
354+
info,
355+
napi_get_value_string_utf16,
356+
node_api_create_property_key_utf16,
357+
actual_length);
358+
}
359+
360+
static napi_value TestPropertyKeyUtf16AutoLength(napi_env env,
361+
napi_callback_info info) {
362+
return TestTwoByteImpl(env,
363+
info,
364+
napi_get_value_string_utf16,
365+
node_api_create_property_key_utf16,
366+
auto_length);
367+
}
368+
336369
static napi_value Utf16Length(napi_env env, napi_callback_info info) {
337370
napi_value args[1];
338371
NODE_API_CALL(env, validate_and_retrieve_single_string_arg(env, info, args));
@@ -473,6 +506,9 @@ napi_value Init(napi_env env, napi_value exports) {
473506
DECLARE_NODE_API_PROPERTY("TestLargeLatin1", TestLargeLatin1),
474507
DECLARE_NODE_API_PROPERTY("TestLargeUtf16", TestLargeUtf16),
475508
DECLARE_NODE_API_PROPERTY("TestMemoryCorruption", TestMemoryCorruption),
509+
DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16", TestPropertyKeyUtf16),
510+
DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16AutoLength",
511+
TestPropertyKeyUtf16AutoLength),
476512
DECLARE_NODE_API_PROPERTY("TestUtf8Large", TestUtf8Large),
477513
DECLARE_NODE_API_PROPERTY("TestUtf16Large", TestUtf16Large),
478514
};

packages/test/string/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module.exports = function test (test_string) {
1414
assert.strictEqual(test_string.TestUtf16External(empty), empty)
1515
assert.strictEqual(test_string.TestLatin1ExternalAutoLength(empty), empty)
1616
assert.strictEqual(test_string.TestUtf16ExternalAutoLength(empty), empty)
17+
assert.strictEqual(test_string.TestPropertyKeyUtf16(empty), empty)
18+
assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(empty), empty)
1719
assert.strictEqual(test_string.Utf16Length(empty), 0)
1820
assert.strictEqual(test_string.Utf8Length(empty), 0)
1921

@@ -31,6 +33,8 @@ module.exports = function test (test_string) {
3133
assert.strictEqual(test_string.TestLatin1Insufficient(str1), str1.slice(0, 3))
3234
assert.strictEqual(test_string.TestUtf8Insufficient(str1), str1.slice(0, 3))
3335
assert.strictEqual(test_string.TestUtf16Insufficient(str1), str1.slice(0, 3))
36+
assert.strictEqual(test_string.TestPropertyKeyUtf16(str1), str1)
37+
assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str1), str1)
3438
assert.strictEqual(test_string.Utf16Length(str1), 11)
3539
assert.strictEqual(test_string.Utf8Length(str1), 11)
3640

@@ -48,6 +52,8 @@ module.exports = function test (test_string) {
4852
assert.strictEqual(test_string.TestLatin1Insufficient(str2), str2.slice(0, 3))
4953
assert.strictEqual(test_string.TestUtf8Insufficient(str2), str2.slice(0, 3))
5054
assert.strictEqual(test_string.TestUtf16Insufficient(str2), str2.slice(0, 3))
55+
assert.strictEqual(test_string.TestPropertyKeyUtf16(str2), str2)
56+
assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str2), str2)
5157
assert.strictEqual(test_string.Utf16Length(str2), 62)
5258
assert.strictEqual(test_string.Utf8Length(str2), 62)
5359

@@ -65,6 +71,8 @@ module.exports = function test (test_string) {
6571
assert.strictEqual(test_string.TestLatin1Insufficient(str3), str3.slice(0, 3))
6672
assert.strictEqual(test_string.TestUtf8Insufficient(str3), str3.slice(0, 3))
6773
assert.strictEqual(test_string.TestUtf16Insufficient(str3), str3.slice(0, 3))
74+
assert.strictEqual(test_string.TestPropertyKeyUtf16(str3), str3)
75+
assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str3), str3)
6876
assert.strictEqual(test_string.Utf16Length(str3), 27)
6977
assert.strictEqual(test_string.Utf8Length(str3), 27)
7078

@@ -82,6 +90,8 @@ module.exports = function test (test_string) {
8290
assert.strictEqual(test_string.TestLatin1Insufficient(str4), str4.slice(0, 3))
8391
assert.strictEqual(test_string.TestUtf8Insufficient(str4), str4.slice(0, 1))
8492
assert.strictEqual(test_string.TestUtf16Insufficient(str4), str4.slice(0, 3))
93+
assert.strictEqual(test_string.TestPropertyKeyUtf16(str4), str4)
94+
assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str4), str4)
8595
assert.strictEqual(test_string.Utf16Length(str4), 31)
8696
assert.strictEqual(test_string.Utf8Length(str4), 62)
8797

@@ -99,6 +109,8 @@ module.exports = function test (test_string) {
99109
assert.strictEqual(test_string.TestLatin1Insufficient(str5), str5.slice(0, 3))
100110
assert.strictEqual(test_string.TestUtf8Insufficient(str5), str5.slice(0, 1))
101111
assert.strictEqual(test_string.TestUtf16Insufficient(str5), str5.slice(0, 3))
112+
assert.strictEqual(test_string.TestPropertyKeyUtf16(str5), str5)
113+
assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str5), str5)
102114
assert.strictEqual(test_string.Utf16Length(str5), 63)
103115
assert.strictEqual(test_string.Utf8Length(str5), 126)
104116

@@ -111,6 +123,8 @@ module.exports = function test (test_string) {
111123
assert.strictEqual(test_string.TestUtf16ExternalAutoLength(str6), str6)
112124
assert.strictEqual(test_string.TestUtf8Insufficient(str6), str6.slice(0, 1))
113125
assert.strictEqual(test_string.TestUtf16Insufficient(str6), str6.slice(0, 3))
126+
assert.strictEqual(test_string.TestPropertyKeyUtf16(str6), str6)
127+
assert.strictEqual(test_string.TestPropertyKeyUtf16AutoLength(str6), str6)
114128
assert.strictEqual(test_string.Utf16Length(str6), 5)
115129
assert.strictEqual(test_string.Utf8Length(str6), 14)
116130

0 commit comments

Comments
 (0)