Skip to content

Commit

Permalink
add node_api_create_property_key_utf16 as a alias of `napi_create_s…
Browse files Browse the repository at this point in the history
…tring_utf16` (#95)
  • Loading branch information
toyobayashi authored Dec 29, 2023
1 parent c3e2d6c commit 0d3ea23
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/emnapi/include/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ node_api_create_external_string_utf16(napi_env env,
napi_value* result,
bool* copied);
#endif // 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);
Expand Down
7 changes: 7 additions & 0 deletions packages/emnapi/src/value/convert2napi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export function node_api_create_external_string_utf16 (
)
}

/**
* @__sig ipppp
*/
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 {
return napi_create_string_utf16(env, str, length, result)
}

/**
* @__sig ipjp
*/
Expand Down
36 changes: 36 additions & 0 deletions packages/test/string/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ 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 =
node_api_create_property_key_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) {
Expand Down Expand Up @@ -333,6 +349,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));
Expand Down Expand Up @@ -473,6 +506,9 @@ 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),
DECLARE_NODE_API_PROPERTY("TestPropertyKeyUtf16AutoLength",
TestPropertyKeyUtf16AutoLength),
DECLARE_NODE_API_PROPERTY("TestUtf8Large", TestUtf8Large),
DECLARE_NODE_API_PROPERTY("TestUtf16Large", TestUtf16Large),
};
Expand Down
14 changes: 14 additions & 0 deletions packages/test/string/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = function test (test_string) {
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)

Expand All @@ -31,6 +33,8 @@ module.exports = function test (test_string) {
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)

Expand All @@ -48,6 +52,8 @@ module.exports = function test (test_string) {
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)

Expand All @@ -65,6 +71,8 @@ module.exports = function test (test_string) {
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)

Expand All @@ -82,6 +90,8 @@ module.exports = function test (test_string) {
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)

Expand All @@ -99,6 +109,8 @@ module.exports = function test (test_string) {
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)

Expand All @@ -111,6 +123,8 @@ module.exports = function test (test_string) {
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)

Expand Down

0 comments on commit 0d3ea23

Please sign in to comment.