From fdc94c9bcdceadbabee42528deae1a960d57633a Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Sat, 19 Oct 2024 12:27:10 -0400 Subject: [PATCH 1/5] src: migrate `String::Value` to `String::ValueView` Fixes #54417 Ref: #55452 --- src/inspector_js_api.cc | 7 +++++-- src/node_buffer.cc | 29 ++++++++++++++--------------- src/string_bytes.cc | 18 ++++++++++-------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index 282575601545d1..55644e9b8fe8ab 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -246,8 +246,11 @@ static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo& args) { CHECK(args[0]->IsString()); Local task_name = args[0].As(); - String::Value task_name_value(args.GetIsolate(), task_name); - StringView task_name_view(*task_name_value, task_name_value.length()); + + std::vector task_name_buffer(task_name->Length()); + task_name->Write( + env->isolate(), task_name_buffer.data(), 0, task_name->Length()); + StringView task_name_view(task_name_buffer.data(), task_name_buffer.size()); CHECK(args[1]->IsNumber()); int64_t task_id = args[1]->IntegerValue(env->context()).FromJust(); diff --git a/src/node_buffer.cc b/src/node_buffer.cc index cd51d9acf9540d..c1caff5c1d7a0a 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -965,11 +965,11 @@ void IndexOfString(const FunctionCallbackInfo& args) { size_t result = haystack_length; if (enc == UCS2) { - String::Value needle_value(isolate, needle); - if (*needle_value == nullptr) - return args.GetReturnValue().Set(-1); + std::unique_ptr needle_buffer(new uint16_t[needle->Length()]); + int needle_length = needle->Write( + isolate, reinterpret_cast(needle_buffer.get())); - if (haystack_length < 2 || needle_value.length() < 1) { + if (haystack_length < 2 || needle_length < 1) { return args.GetReturnValue().Set(-1); } @@ -989,13 +989,12 @@ void IndexOfString(const FunctionCallbackInfo& args) { offset / 2, is_forward); } else { - result = - nbytes::SearchString(reinterpret_cast(haystack), - haystack_length / 2, - reinterpret_cast(*needle_value), - needle_value.length(), - offset / 2, - is_forward); + result = nbytes::SearchString(reinterpret_cast(haystack), + haystack_length / 2, + needle_buffer.get(), + needle_length, + offset / 2, + is_forward); } result *= 2; } else if (enc == UTF8) { @@ -1295,10 +1294,10 @@ static void Btoa(const FunctionCallbackInfo& args) { input->Length(), buffer.out()); } else { - String::Value value(env->isolate(), input); + String::ValueView value(env->isolate(), input); MaybeStackBuffer stack_buf(value.length()); size_t out_len = simdutf::convert_utf16_to_latin1( - reinterpret_cast(*value), + reinterpret_cast(value.data16()), value.length(), stack_buf.out()); if (out_len == 0) { // error @@ -1355,8 +1354,8 @@ static void Atob(const FunctionCallbackInfo& args) { buffer.SetLength(expected_length); result = simdutf::base64_to_binary(data, input->Length(), buffer.out()); } else { // 16-bit case - String::Value value(env->isolate(), input); - auto data = reinterpret_cast(*value); + String::ValueView value(env->isolate(), input); + auto data = reinterpret_cast(value.data16()); size_t expected_length = simdutf::maximal_binary_length_from_base64(data, value.length()); buffer.AllocateSufficientStorage(expected_length); diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 8a94d0eb63245c..1b80bdb2644e60 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -305,10 +305,10 @@ size_t StringBytes::Write(Isolate* isolate, input_view.length()); } } else { - String::Value value(isolate, str); + String::ValueView value(isolate, str); size_t written_len = buflen; auto result = simdutf::base64_to_binary_safe( - reinterpret_cast(*value), + reinterpret_cast(value.data16()), value.length(), buf, written_len, @@ -319,7 +319,8 @@ size_t StringBytes::Write(Isolate* isolate, // The input does not follow the WHATWG forgiving-base64 specification // (adapted for base64url with + and / replaced by - and _). // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = nbytes::Base64Decode(buf, buflen, *value, value.length()); + nbytes = + nbytes::Base64Decode(buf, buflen, value.data16(), value.length()); } } break; @@ -344,10 +345,10 @@ size_t StringBytes::Write(Isolate* isolate, input_view.length()); } } else { - String::Value value(isolate, str); + String::ValueView value(isolate, str); size_t written_len = buflen; auto result = simdutf::base64_to_binary_safe( - reinterpret_cast(*value), + reinterpret_cast(value.data16()), value.length(), buf, written_len); @@ -356,7 +357,8 @@ size_t StringBytes::Write(Isolate* isolate, } else { // The input does not follow the WHATWG base64 specification // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = nbytes::Base64Decode(buf, buflen, *value, value.length()); + nbytes = + nbytes::Base64Decode(buf, buflen, value.data16(), value.length()); } } break; @@ -369,8 +371,8 @@ size_t StringBytes::Write(Isolate* isolate, reinterpret_cast(input_view.data8()), input_view.length()); } else { - String::Value value(isolate, str); - nbytes = nbytes::HexDecode(buf, buflen, *value, value.length()); + String::ValueView value(isolate, str); + nbytes = nbytes::HexDecode(buf, buflen, value.data8(), value.length()); } break; From b0501f5a8c31e30bf29c2950eeaa155385cf95f0 Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Sat, 19 Oct 2024 13:53:21 -0400 Subject: [PATCH 2/5] fixup! src: migrate `String::Value` to `String::ValueView` --- src/node_buffer.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index c1caff5c1d7a0a..c1c66c532f114c 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -965,9 +965,8 @@ void IndexOfString(const FunctionCallbackInfo& args) { size_t result = haystack_length; if (enc == UCS2) { - std::unique_ptr needle_buffer(new uint16_t[needle->Length()]); - int needle_length = needle->Write( - isolate, reinterpret_cast(needle_buffer.get())); + uint16_t* needle_buffer = new uint16_t[needle->Length()]; + int needle_length = needle->Write(isolate, needle_buffer); if (haystack_length < 2 || needle_length < 1) { return args.GetReturnValue().Set(-1); @@ -991,7 +990,7 @@ void IndexOfString(const FunctionCallbackInfo& args) { } else { result = nbytes::SearchString(reinterpret_cast(haystack), haystack_length / 2, - needle_buffer.get(), + needle_buffer, needle_length, offset / 2, is_forward); From f2ed23b666a34b9f1256bc1ffe3dc70820c50102 Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Sat, 19 Oct 2024 14:46:50 -0400 Subject: [PATCH 3/5] fixup! fixup! src: migrate `String::Value` to `String::ValueView` --- src/inspector_js_api.cc | 7 ++----- src/node_buffer.cc | 9 ++++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index 55644e9b8fe8ab..a43f1a361ef702 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -245,12 +245,9 @@ static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); CHECK(args[0]->IsString()); - Local task_name = args[0].As(); - std::vector task_name_buffer(task_name->Length()); - task_name->Write( - env->isolate(), task_name_buffer.data(), 0, task_name->Length()); - StringView task_name_view(task_name_buffer.data(), task_name_buffer.size()); + TwoByteValue task_name_buffer(args.GetIsolate(), args[0]); + StringView task_name_view(*task_name_buffer, task_name_buffer.length()); CHECK(args[1]->IsNumber()); int64_t task_id = args[1]->IntegerValue(env->context()).FromJust(); diff --git a/src/node_buffer.cc b/src/node_buffer.cc index c1c66c532f114c..a29f858e689b97 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -965,10 +965,9 @@ void IndexOfString(const FunctionCallbackInfo& args) { size_t result = haystack_length; if (enc == UCS2) { - uint16_t* needle_buffer = new uint16_t[needle->Length()]; - int needle_length = needle->Write(isolate, needle_buffer); + TwoByteValue needle_buffer(isolate, needle); - if (haystack_length < 2 || needle_length < 1) { + if (haystack_length < 2 || needle_buffer.length()) { return args.GetReturnValue().Set(-1); } @@ -990,8 +989,8 @@ void IndexOfString(const FunctionCallbackInfo& args) { } else { result = nbytes::SearchString(reinterpret_cast(haystack), haystack_length / 2, - needle_buffer, - needle_length, + needle_buffer.out(), + needle_buffer.length(), offset / 2, is_forward); } From 15937ab4b433855d5cfb6753e020b58f96bc908c Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Sat, 19 Oct 2024 15:09:11 -0400 Subject: [PATCH 4/5] fixup! fixup! fixup! src: migrate `String::Value` to `String::ValueView` --- src/node_buffer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index a29f858e689b97..44c54c7a178385 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -967,7 +967,7 @@ void IndexOfString(const FunctionCallbackInfo& args) { if (enc == UCS2) { TwoByteValue needle_buffer(isolate, needle); - if (haystack_length < 2 || needle_buffer.length()) { + if (haystack_length < 2 || needle_buffer.length() < 1) { return args.GetReturnValue().Set(-1); } From 862d75cdbbfb72e4fd619a9a37402d67156cb723 Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:31:21 -0400 Subject: [PATCH 5/5] fixup! fixup! fixup! fixup! src: migrate `String::Value` to `String::ValueView` --- src/string_bytes.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 1b80bdb2644e60..9e12fd1585639b 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -305,11 +305,10 @@ size_t StringBytes::Write(Isolate* isolate, input_view.length()); } } else { - String::ValueView value(isolate, str); size_t written_len = buflen; auto result = simdutf::base64_to_binary_safe( - reinterpret_cast(value.data16()), - value.length(), + reinterpret_cast(input_view.data16()), + input_view.length(), buf, written_len, simdutf::base64_url); @@ -319,8 +318,8 @@ size_t StringBytes::Write(Isolate* isolate, // The input does not follow the WHATWG forgiving-base64 specification // (adapted for base64url with + and / replaced by - and _). // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = - nbytes::Base64Decode(buf, buflen, value.data16(), value.length()); + nbytes = nbytes::Base64Decode( + buf, buflen, input_view.data16(), input_view.length()); } } break; @@ -345,11 +344,10 @@ size_t StringBytes::Write(Isolate* isolate, input_view.length()); } } else { - String::ValueView value(isolate, str); size_t written_len = buflen; auto result = simdutf::base64_to_binary_safe( - reinterpret_cast(value.data16()), - value.length(), + reinterpret_cast(input_view.data16()), + input_view.length(), buf, written_len); if (result.error == simdutf::error_code::SUCCESS) { @@ -357,8 +355,8 @@ size_t StringBytes::Write(Isolate* isolate, } else { // The input does not follow the WHATWG base64 specification // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = - nbytes::Base64Decode(buf, buflen, value.data16(), value.length()); + nbytes = nbytes::Base64Decode( + buf, buflen, input_view.data16(), input_view.length()); } } break;