diff --git a/benchmark/napi/function_args/binding.cc b/benchmark/napi/function_args/binding.cc index 078fe0ee3ea..5cdca8bd357 100644 --- a/benchmark/napi/function_args/binding.cc +++ b/benchmark/napi/function_args/binding.cc @@ -21,9 +21,10 @@ void CallWithString(const FunctionCallbackInfo& args) { assert(args.Length() == 1 && args[0]->IsString()); if (args.Length() == 1 && args[0]->IsString()) { Local str = args[0].As(); - const int32_t length = str->Utf8Length(args.GetIsolate()) + 1; + const size_t length = str->Utf8LengthV2(args.GetIsolate()) + 1; char* buf = new char[length]; - str->WriteUtf8(args.GetIsolate(), buf, length); + str->WriteUtf8V2( + args.GetIsolate(), buf, length, String::WriteFlags::kNullTerminate); delete[] buf; } } diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index 60610b1b795..0745304d379 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -393,12 +393,12 @@ ByteSource ByteSource::FromStringOrBuffer(Environment* env, ByteSource ByteSource::FromString(Environment* env, Local str, bool ntc) { CHECK(str->IsString()); - size_t size = str->Utf8Length(env->isolate()); + size_t size = str->Utf8LengthV2(env->isolate()); size_t alloc_size = ntc ? size + 1 : size; ByteSource::Builder out(alloc_size); - int opts = String::NO_OPTIONS; - if (!ntc) opts |= String::NO_NULL_TERMINATION; - str->WriteUtf8(env->isolate(), out.data(), alloc_size, nullptr, opts); + int opts = String::WriteFlags::kNone; + if (ntc) opts |= String::WriteFlags::kNullTerminate; + str->WriteUtf8V2(env->isolate(), out.data(), alloc_size, opts); return std::move(out).release(); } diff --git a/src/encoding_binding.cc b/src/encoding_binding.cc index 885a0d07231..0d2941cef16 100644 --- a/src/encoding_binding.cc +++ b/src/encoding_binding.cc @@ -100,6 +100,7 @@ void BindingData::EncodeInto(const FunctionCallbackInfo& args) { size_t dest_length = dest->ByteLength(); int nchars; + // TODO: Use `WriteUtf8V2`, which has no `nchars_ref` parameter. int written = source->WriteUtf8( isolate, write_result, @@ -120,7 +121,7 @@ void BindingData::EncodeUtf8String(const FunctionCallbackInfo& args) { CHECK(args[0]->IsString()); Local str = args[0].As(); - size_t length = str->Utf8Length(isolate); + size_t length = str->Utf8LengthV2(isolate); Local ab; { @@ -130,11 +131,11 @@ void BindingData::EncodeUtf8String(const FunctionCallbackInfo& args) { CHECK(bs); - str->WriteUtf8(isolate, - static_cast(bs->Data()), - -1, // We are certain that `data` is sufficiently large - nullptr, - String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8); + str->WriteUtf8V2( + isolate, + static_cast(bs->Data()), + length, // We are certain that `data` is sufficiently large + String::WriteFlags::kReplaceInvalidUtf8); ab = ArrayBuffer::New(isolate, std::move(bs)); } diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 6e1680a74e2..abb4f8d6ff4 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2446,16 +2446,13 @@ napi_status NAPI_CDECL napi_get_value_string_latin1( CHECK_ARG(env, result); *result = val.As()->Length(); } else if (bufsize != 0) { - int copied = - val.As()->WriteOneByte(env->isolate, - reinterpret_cast(buf), - 0, - bufsize - 1, - v8::String::NO_NULL_TERMINATION); + size_t length = bufsize - 1; + val.As()->WriteOneByteV2( + env->isolate, 0, length, reinterpret_cast(buf)); - buf[copied] = '\0'; + buf[length] = '\0'; if (result != nullptr) { - *result = copied; + *result = length; } } else if (result != nullptr) { *result = 0; @@ -2482,14 +2479,13 @@ napi_status NAPI_CDECL napi_get_value_string_utf8( if (!buf) { CHECK_ARG(env, result); - *result = val.As()->Utf8Length(env->isolate); + *result = val.As()->Utf8LengthV2(env->isolate); } else if (bufsize != 0) { - int copied = val.As()->WriteUtf8( + size_t copied = val.As()->WriteUtf8V2( env->isolate, buf, bufsize - 1, - nullptr, - v8::String::REPLACE_INVALID_UTF8 | v8::String::NO_NULL_TERMINATION); + v8::String::WriteFlags::kReplaceInvalidUtf8); buf[copied] = '\0'; if (result != nullptr) { @@ -2526,15 +2522,13 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env, // V8 assumes UTF-16 length is the same as the number of characters. *result = val.As()->Length(); } else if (bufsize != 0) { - int copied = val.As()->Write(env->isolate, - reinterpret_cast(buf), - 0, - bufsize - 1, - v8::String::NO_NULL_TERMINATION); + size_t length = bufsize - 1; + val.As()->WriteV2( + env->isolate, 0, length, reinterpret_cast(buf)); - buf[copied] = '\0'; + buf[length] = '\0'; if (result != nullptr) { - *result = copied; + *result = length; } } else if (result != nullptr) { *result = 0; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 8e659ac06ab..40e82031741 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -650,7 +650,7 @@ void Fill(const FunctionCallbackInfo& args) { // Can't use StringBytes::Write() in all cases. For example if attempting // to write a two byte character into a one byte Buffer. if (enc == UTF8) { - str_length = str_obj->Utf8Length(env->isolate()); + str_length = str_obj->Utf8LengthV2(env->isolate()); node::Utf8Value str(env->isolate(), args[1]); memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length)); @@ -736,7 +736,8 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo& args) { CHECK(args[0]->IsString()); // Fast case: avoid StringBytes on UTF8 string. Jump to v8. - args.GetReturnValue().Set(args[0].As()->Utf8Length(env->isolate())); + size_t length = args[0].As()->Utf8LengthV2(env->isolate()); + args.GetReturnValue().Set(static_cast(length)); } uint32_t FastByteLengthUtf8(Local receiver, @@ -1003,8 +1004,7 @@ void IndexOfString(const FunctionCallbackInfo& args) { if (needle_data == nullptr) { return args.GetReturnValue().Set(-1); } - needle->WriteOneByte( - isolate, needle_data, 0, needle_length, String::NO_NULL_TERMINATION); + needle->WriteOneByteV2(isolate, 0, needle_length, needle_data); result = nbytes::SearchString(reinterpret_cast(haystack), haystack_length, @@ -1254,11 +1254,7 @@ static void Btoa(const FunctionCallbackInfo& args) { simdutf::binary_to_base64(ext->data(), ext->length(), buffer.out()); } else if (input->IsOneByte()) { MaybeStackBuffer stack_buf(input->Length()); - input->WriteOneByte(env->isolate(), - stack_buf.out(), - 0, - input->Length(), - String::NO_NULL_TERMINATION); + input->WriteOneByteV2(env->isolate(), 0, input->Length(), stack_buf.out()); size_t expected_length = simdutf::base64_length_from_binary(input->Length()); @@ -1317,11 +1313,8 @@ static void Atob(const FunctionCallbackInfo& args) { ext->data(), ext->length(), buffer.out(), simdutf::base64_default); } else if (input->IsOneByte()) { MaybeStackBuffer stack_buf(input->Length()); - input->WriteOneByte(args.GetIsolate(), - stack_buf.out(), - 0, - input->Length(), - String::NO_NULL_TERMINATION); + input->WriteOneByteV2( + args.GetIsolate(), 0, input->Length(), stack_buf.out()); const char* data = reinterpret_cast(*stack_buf); size_t expected_length = simdutf::maximal_binary_length_from_base64(data, input->Length()); diff --git a/src/node_http2.cc b/src/node_http2.cc index b23f4080a6d..3f0b25b3435 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -474,13 +474,10 @@ Origins::Origins( CHECK_LE(origin_contents + origin_string_len, static_cast(bs_->Data()) + bs_->ByteLength()); - CHECK_EQ(origin_string->WriteOneByte( - env->isolate(), - reinterpret_cast(origin_contents), - 0, - origin_string_len, - String::NO_NULL_TERMINATION), - origin_string_len); + origin_string->WriteOneByteV2(env->isolate(), + 0, + origin_string_len, + reinterpret_cast(origin_contents)); size_t n = 0; char* p; @@ -3129,8 +3126,13 @@ void Http2Session::AltSvc(const FunctionCallbackInfo& args) { MaybeStackBuffer origin(origin_len); MaybeStackBuffer value(value_len); - origin_str->WriteOneByte(env->isolate(), *origin); - value_str->WriteOneByte(env->isolate(), *value); + origin_str->WriteOneByteV2(env->isolate(), + 0, + origin_len, + *origin, + String::WriteFlags::kNullTerminate); + value_str->WriteOneByteV2( + env->isolate(), 0, value_len, *value, String::WriteFlags::kNullTerminate); session->AltSvc(id, *origin, origin_len, *value, value_len); } diff --git a/src/node_http_common-inl.h b/src/node_http_common-inl.h index f7f4408ecb6..8eb4fe8aa6f 100644 --- a/src/node_http_common-inl.h +++ b/src/node_http_common-inl.h @@ -37,13 +37,11 @@ NgHeaders::NgHeaders(Environment* env, v8::Local headers) { nv_t* const nva = reinterpret_cast(start); CHECK_LE(header_contents + header_string_len, *buf_ + buf_.length()); - CHECK_EQ(header_string.As()->WriteOneByte( + header_string.As()->WriteOneByteV2( env->isolate(), - reinterpret_cast(header_contents), 0, header_string_len, - v8::String::NO_NULL_TERMINATION), - header_string_len); + reinterpret_cast(header_contents)); size_t n = 0; char* p; diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 8a94d0eb632..f2b900a638f 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -200,8 +200,11 @@ MaybeLocal ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate, } // anonymous namespace -size_t StringBytes::WriteUCS2( - Isolate* isolate, char* buf, size_t buflen, Local str, int flags) { +size_t StringBytes::WriteUCS2(Isolate* isolate, + char* buf, + size_t buflen, + Local str, + int flags = String::WriteFlags::kNone) { uint16_t* const dst = reinterpret_cast(buf); size_t max_chars = buflen / sizeof(*dst); @@ -212,7 +215,8 @@ size_t StringBytes::WriteUCS2( uint16_t* const aligned_dst = nbytes::AlignUp(dst, sizeof(*dst)); size_t nchars; if (aligned_dst == dst) { - nchars = str->Write(isolate, dst, 0, max_chars, flags); + nchars = max_chars; + str->WriteV2(isolate, 0, nchars, dst, flags); return nchars * sizeof(*dst); } @@ -223,15 +227,15 @@ size_t StringBytes::WriteUCS2( if (max_chars == 0) { return 0; } - nchars = str->Write(isolate, aligned_dst, 0, max_chars - 1, flags); - CHECK_EQ(nchars, max_chars - 1); + nchars = max_chars - 1; + str->WriteV2(isolate, 0, nchars, aligned_dst, flags); // Shift everything to unaligned-left memmove(dst, aligned_dst, nchars * sizeof(*dst)); // One more char to be written uint16_t last; - CHECK_EQ(str->Write(isolate, &last, nchars, 1, flags), 1); + str->WriteV2(isolate, nchars, 1, &last, flags); memcpy(buf + nchars * sizeof(*dst), &last, sizeof(last)); nchars++; @@ -250,9 +254,7 @@ size_t StringBytes::Write(Isolate* isolate, Local str = val.As(); String::ValueView input_view(isolate, str); - int flags = String::HINT_MANY_WRITES_EXPECTED | - String::NO_NULL_TERMINATION | - String::REPLACE_INVALID_UTF8; + int flags = String::WriteFlags::kReplaceInvalidUtf8; switch (encoding) { case ASCII: @@ -262,13 +264,14 @@ size_t StringBytes::Write(Isolate* isolate, memcpy(buf, input_view.data8(), nbytes); } else { uint8_t* const dst = reinterpret_cast(buf); - nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags); + str->WriteOneByteV2(isolate, 0, buflen, dst, flags); + nbytes = buflen; } break; case BUFFER: case UTF8: - nbytes = str->WriteUtf8(isolate, buf, buflen, nullptr, flags); + nbytes = str->WriteUtf8V2(isolate, buf, buflen, flags); break; case UCS2: { diff --git a/src/util.cc b/src/util.cc index 8bf239db6e4..ee912792a03 100644 --- a/src/util.cc +++ b/src/util.cc @@ -127,10 +127,9 @@ static void MakeUtf8String(Isolate* isolate, // TODO(@anonrig): Use simdutf to speed up non-one-byte strings once it's // implemented - const int flags = - String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8; - const int length = - string->WriteUtf8(isolate, target->out(), storage, nullptr, flags); + const int flags = String::WriteFlags::kReplaceInvalidUtf8; + const size_t length = + string->WriteUtf8V2(isolate, target->out(), storage, flags); target->SetLengthAndZeroTerminate(length); } @@ -154,9 +153,8 @@ TwoByteValue::TwoByteValue(Isolate* isolate, Local value) { const size_t storage = string->Length() + 1; AllocateSufficientStorage(storage); - const int flags = String::NO_NULL_TERMINATION; - const int length = string->Write(isolate, out(), 0, storage, flags); - SetLengthAndZeroTerminate(length); + string->WriteV2(isolate, 0, storage - 1, out()); + SetLengthAndZeroTerminate(storage); } BufferValue::BufferValue(Isolate* isolate, Local value) {