summaryrefslogtreecommitdiff
path: root/test/js-native-api/test_string
diff options
context:
space:
mode:
authorAnthony Tuininga <anthony.tuininga@oracle.com>2019-03-04 15:02:27 -0700
committerRuben Bridgewater <ruben@bridgewater.de>2019-03-09 00:21:50 +0100
commit914d90835986722fe8e07e7894032ad0ecde13e6 (patch)
treecdfcda236694b72f3a97712a2271f138e55af6e5 /test/js-native-api/test_string
parent4fb88cd8881441bb9af3bacc326fe3d9339de0d0 (diff)
downloadandroid-node-v8-914d90835986722fe8e07e7894032ad0ecde13e6.tar.gz
android-node-v8-914d90835986722fe8e07e7894032ad0ecde13e6.tar.bz2
android-node-v8-914d90835986722fe8e07e7894032ad0ecde13e6.zip
n-api: improve performance creating strings
Improve performance creating strings using N-API by ensuring that the strings are not internalized. Added test cases for latin-1 and utf-16 strings. PR-URL: https://github.com/nodejs/node/pull/26439 Fixes: https://github.com/nodejs/node/issues/26437 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/js-native-api/test_string')
-rw-r--r--test/js-native-api/test_string/test.js8
-rw-r--r--test/js-native-api/test_string/test_string.c28
2 files changed, 36 insertions, 0 deletions
diff --git a/test/js-native-api/test_string/test.js b/test/js-native-api/test_string/test.js
index 5ce3d739c7..86fdc16406 100644
--- a/test/js-native-api/test_string/test.js
+++ b/test/js-native-api/test_string/test.js
@@ -73,3 +73,11 @@ assert.strictEqual(test_string.Utf8Length(str6), 14);
assert.throws(() => {
test_string.TestLargeUtf8();
}, /^Error: Invalid argument$/);
+
+assert.throws(() => {
+ test_string.TestLargeLatin1();
+}, /^Error: Invalid argument$/);
+
+assert.throws(() => {
+ test_string.TestLargeUtf16();
+}, /^Error: Invalid argument$/);
diff --git a/test/js-native-api/test_string/test_string.c b/test/js-native-api/test_string/test_string.c
index 5b62d9de6c..1f08559140 100644
--- a/test/js-native-api/test_string/test_string.c
+++ b/test/js-native-api/test_string/test_string.c
@@ -216,6 +216,32 @@ static napi_value TestLargeUtf8(napi_env env, napi_callback_info info) {
return output;
}
+static napi_value TestLargeLatin1(napi_env env, napi_callback_info info) {
+ napi_value output;
+ if (SIZE_MAX > INT_MAX) {
+ NAPI_CALL(env, napi_create_string_latin1(env, "", ((size_t)INT_MAX) + 1, &output));
+ } else {
+ // just throw the expected error as there is nothing to test
+ // in this case since we can't overflow
+ NAPI_CALL(env, napi_throw_error(env, NULL, "Invalid argument"));
+ }
+
+ return output;
+}
+
+static napi_value TestLargeUtf16(napi_env env, napi_callback_info info) {
+ napi_value output;
+ if (SIZE_MAX > INT_MAX) {
+ NAPI_CALL(env, napi_create_string_utf16(env, "", ((size_t)INT_MAX) + 1, &output));
+ } else {
+ // just throw the expected error as there is nothing to test
+ // in this case since we can't overflow
+ NAPI_CALL(env, napi_throw_error(env, NULL, "Invalid argument"));
+ }
+
+ return output;
+}
+
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
@@ -228,6 +254,8 @@ napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_PROPERTY("Utf16Length", Utf16Length),
DECLARE_NAPI_PROPERTY("Utf8Length", Utf8Length),
DECLARE_NAPI_PROPERTY("TestLargeUtf8", TestLargeUtf8),
+ DECLARE_NAPI_PROPERTY("TestLargeLatin1", TestLargeLatin1),
+ DECLARE_NAPI_PROPERTY("TestLargeUtf16", TestLargeUtf16),
};
NAPI_CALL(env, napi_define_properties(