Skip to content

Commit 2e67d06

Browse files
src: factor out new string arg check
Create a macro that can be used to check the parameters passed into `NewString` and reused to check the same parameters as passed to `NewExternalString`
1 parent 3c5eb43 commit 2e67d06

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/js_native_api_v8.cc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@
3838
#define CHECK_NEW_FROM_UTF8(env, result, str) \
3939
CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), NAPI_AUTO_LENGTH)
4040

41+
#define CHECK_NEW_STRING_ARGS(env, str, length, result) \
42+
do { \
43+
CHECK_ENV_NOT_IN_GC((env)); \
44+
if ((length) > 0) CHECK_ARG((env), (str)); \
45+
CHECK_ARG((env), (result)); \
46+
RETURN_STATUS_IF_FALSE( \
47+
(env), \
48+
((length) == NAPI_AUTO_LENGTH) || (length) <= INT_MAX, \
49+
napi_invalid_arg); \
50+
} while (0)
51+
4152
#define CREATE_TYPED_ARRAY( \
4253
env, type, size_of_element, buffer, byte_offset, length, out) \
4354
do { \
@@ -81,12 +92,7 @@ napi_status NewString(napi_env env,
8192
size_t length,
8293
napi_value* result,
8394
StringMaker string_maker) {
84-
CHECK_ENV(env);
85-
env->CheckGCAccess();
86-
if (length > 0) CHECK_ARG(env, str);
87-
CHECK_ARG(env, result);
88-
RETURN_STATUS_IF_FALSE(
89-
env, (length == NAPI_AUTO_LENGTH) || length <= INT_MAX, napi_invalid_arg);
95+
CHECK_NEW_STRING_ARGS(env, str, length, result);
9096

9197
auto isolate = env->isolate;
9298
auto str_maybe = string_maker(isolate);
@@ -105,9 +111,10 @@ napi_status NewExternalString(napi_env env,
105111
bool* copied,
106112
CreateAPI create_api,
107113
StringMaker string_maker) {
114+
CHECK_NEW_STRING_ARGS(env, str, length, result);
115+
108116
napi_status status;
109117
#if defined(V8_ENABLE_SANDBOX)
110-
env->CheckGCAccess();
111118
status = create_api(env, str, length, result);
112119
if (status == napi_ok) {
113120
if (copied != nullptr) {

0 commit comments

Comments
 (0)