Skip to content

Commit 11be423

Browse files
targosnodejs-github-bot
authored andcommitted
src: use new versions of V8 String APIs
Refs: v8/v8@a0c8b80 Refs: v8/v8@47a2457
1 parent 47c2c03 commit 11be423

File tree

9 files changed

+66
-76
lines changed

9 files changed

+66
-76
lines changed

benchmark/napi/function_args/binding.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ void CallWithString(const FunctionCallbackInfo<Value>& args) {
2121
assert(args.Length() == 1 && args[0]->IsString());
2222
if (args.Length() == 1 && args[0]->IsString()) {
2323
Local<String> str = args[0].As<String>();
24-
const int32_t length = str->Utf8Length(args.GetIsolate()) + 1;
24+
const size_t length = str->Utf8LengthV2(args.GetIsolate()) + 1;
2525
char* buf = new char[length];
26-
str->WriteUtf8(args.GetIsolate(), buf, length);
26+
str->WriteUtf8V2(
27+
args.GetIsolate(), buf, length, String::WriteFlags::kNullTerminate);
2728
delete[] buf;
2829
}
2930
}

src/crypto/crypto_util.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,12 @@ ByteSource ByteSource::FromStringOrBuffer(Environment* env,
393393
ByteSource ByteSource::FromString(Environment* env, Local<String> str,
394394
bool ntc) {
395395
CHECK(str->IsString());
396-
size_t size = str->Utf8Length(env->isolate());
396+
size_t size = str->Utf8LengthV2(env->isolate());
397397
size_t alloc_size = ntc ? size + 1 : size;
398398
ByteSource::Builder out(alloc_size);
399-
int opts = String::NO_OPTIONS;
400-
if (!ntc) opts |= String::NO_NULL_TERMINATION;
401-
str->WriteUtf8(env->isolate(), out.data<char>(), alloc_size, nullptr, opts);
399+
int opts = String::WriteFlags::kNone;
400+
if (ntc) opts |= String::WriteFlags::kNullTerminate;
401+
str->WriteUtf8V2(env->isolate(), out.data<char>(), alloc_size, opts);
402402
return std::move(out).release();
403403
}
404404

src/encoding_binding.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ void BindingData::EncodeInto(const FunctionCallbackInfo<Value>& args) {
100100
size_t dest_length = dest->ByteLength();
101101

102102
int nchars;
103+
// TODO: Use `WriteUtf8V2`, which has no `nchars_ref` parameter.
103104
int written = source->WriteUtf8(
104105
isolate,
105106
write_result,
@@ -120,7 +121,7 @@ void BindingData::EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
120121
CHECK(args[0]->IsString());
121122

122123
Local<String> str = args[0].As<String>();
123-
size_t length = str->Utf8Length(isolate);
124+
size_t length = str->Utf8LengthV2(isolate);
124125

125126
Local<ArrayBuffer> ab;
126127
{
@@ -130,11 +131,11 @@ void BindingData::EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
130131

131132
CHECK(bs);
132133

133-
str->WriteUtf8(isolate,
134-
static_cast<char*>(bs->Data()),
135-
-1, // We are certain that `data` is sufficiently large
136-
nullptr,
137-
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
134+
str->WriteUtf8V2(
135+
isolate,
136+
static_cast<char*>(bs->Data()),
137+
length, // We are certain that `data` is sufficiently large
138+
String::WriteFlags::kReplaceInvalidUtf8);
138139

139140
ab = ArrayBuffer::New(isolate, std::move(bs));
140141
}

src/js_native_api_v8.cc

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,16 +2446,13 @@ napi_status NAPI_CDECL napi_get_value_string_latin1(
24462446
CHECK_ARG(env, result);
24472447
*result = val.As<v8::String>()->Length();
24482448
} else if (bufsize != 0) {
2449-
int copied =
2450-
val.As<v8::String>()->WriteOneByte(env->isolate,
2451-
reinterpret_cast<uint8_t*>(buf),
2452-
0,
2453-
bufsize - 1,
2454-
v8::String::NO_NULL_TERMINATION);
2449+
size_t length = bufsize - 1;
2450+
val.As<v8::String>()->WriteOneByteV2(
2451+
env->isolate, 0, length, reinterpret_cast<uint8_t*>(buf));
24552452

2456-
buf[copied] = '\0';
2453+
buf[length] = '\0';
24572454
if (result != nullptr) {
2458-
*result = copied;
2455+
*result = length;
24592456
}
24602457
} else if (result != nullptr) {
24612458
*result = 0;
@@ -2482,14 +2479,13 @@ napi_status NAPI_CDECL napi_get_value_string_utf8(
24822479

24832480
if (!buf) {
24842481
CHECK_ARG(env, result);
2485-
*result = val.As<v8::String>()->Utf8Length(env->isolate);
2482+
*result = val.As<v8::String>()->Utf8LengthV2(env->isolate);
24862483
} else if (bufsize != 0) {
2487-
int copied = val.As<v8::String>()->WriteUtf8(
2484+
size_t copied = val.As<v8::String>()->WriteUtf8V2(
24882485
env->isolate,
24892486
buf,
24902487
bufsize - 1,
2491-
nullptr,
2492-
v8::String::REPLACE_INVALID_UTF8 | v8::String::NO_NULL_TERMINATION);
2488+
v8::String::WriteFlags::kReplaceInvalidUtf8);
24932489

24942490
buf[copied] = '\0';
24952491
if (result != nullptr) {
@@ -2526,15 +2522,13 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env,
25262522
// V8 assumes UTF-16 length is the same as the number of characters.
25272523
*result = val.As<v8::String>()->Length();
25282524
} else if (bufsize != 0) {
2529-
int copied = val.As<v8::String>()->Write(env->isolate,
2530-
reinterpret_cast<uint16_t*>(buf),
2531-
0,
2532-
bufsize - 1,
2533-
v8::String::NO_NULL_TERMINATION);
2525+
size_t length = bufsize - 1;
2526+
val.As<v8::String>()->WriteV2(
2527+
env->isolate, 0, length, reinterpret_cast<uint16_t*>(buf));
25342528

2535-
buf[copied] = '\0';
2529+
buf[length] = '\0';
25362530
if (result != nullptr) {
2537-
*result = copied;
2531+
*result = length;
25382532
}
25392533
} else if (result != nullptr) {
25402534
*result = 0;

src/node_buffer.cc

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
650650
// Can't use StringBytes::Write() in all cases. For example if attempting
651651
// to write a two byte character into a one byte Buffer.
652652
if (enc == UTF8) {
653-
str_length = str_obj->Utf8Length(env->isolate());
653+
str_length = str_obj->Utf8LengthV2(env->isolate());
654654
node::Utf8Value str(env->isolate(), args[1]);
655655
memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length));
656656

@@ -736,7 +736,8 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
736736
CHECK(args[0]->IsString());
737737

738738
// Fast case: avoid StringBytes on UTF8 string. Jump to v8.
739-
args.GetReturnValue().Set(args[0].As<String>()->Utf8Length(env->isolate()));
739+
size_t length = args[0].As<String>()->Utf8LengthV2(env->isolate());
740+
args.GetReturnValue().Set(static_cast<uint64_t>(length));
740741
}
741742

742743
uint32_t FastByteLengthUtf8(Local<Value> receiver,
@@ -1003,8 +1004,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10031004
if (needle_data == nullptr) {
10041005
return args.GetReturnValue().Set(-1);
10051006
}
1006-
needle->WriteOneByte(
1007-
isolate, needle_data, 0, needle_length, String::NO_NULL_TERMINATION);
1007+
needle->WriteOneByteV2(isolate, 0, needle_length, needle_data);
10081008

10091009
result = nbytes::SearchString(reinterpret_cast<const uint8_t*>(haystack),
10101010
haystack_length,
@@ -1254,11 +1254,7 @@ static void Btoa(const FunctionCallbackInfo<Value>& args) {
12541254
simdutf::binary_to_base64(ext->data(), ext->length(), buffer.out());
12551255
} else if (input->IsOneByte()) {
12561256
MaybeStackBuffer<uint8_t> stack_buf(input->Length());
1257-
input->WriteOneByte(env->isolate(),
1258-
stack_buf.out(),
1259-
0,
1260-
input->Length(),
1261-
String::NO_NULL_TERMINATION);
1257+
input->WriteOneByteV2(env->isolate(), 0, input->Length(), stack_buf.out());
12621258

12631259
size_t expected_length =
12641260
simdutf::base64_length_from_binary(input->Length());
@@ -1317,11 +1313,8 @@ static void Atob(const FunctionCallbackInfo<Value>& args) {
13171313
ext->data(), ext->length(), buffer.out(), simdutf::base64_default);
13181314
} else if (input->IsOneByte()) {
13191315
MaybeStackBuffer<uint8_t> stack_buf(input->Length());
1320-
input->WriteOneByte(args.GetIsolate(),
1321-
stack_buf.out(),
1322-
0,
1323-
input->Length(),
1324-
String::NO_NULL_TERMINATION);
1316+
input->WriteOneByteV2(
1317+
args.GetIsolate(), 0, input->Length(), stack_buf.out());
13251318
const char* data = reinterpret_cast<const char*>(*stack_buf);
13261319
size_t expected_length =
13271320
simdutf::maximal_binary_length_from_base64(data, input->Length());

src/node_http2.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,10 @@ Origins::Origins(
474474

475475
CHECK_LE(origin_contents + origin_string_len,
476476
static_cast<char*>(bs_->Data()) + bs_->ByteLength());
477-
CHECK_EQ(origin_string->WriteOneByte(
478-
env->isolate(),
479-
reinterpret_cast<uint8_t*>(origin_contents),
480-
0,
481-
origin_string_len,
482-
String::NO_NULL_TERMINATION),
483-
origin_string_len);
477+
origin_string->WriteOneByteV2(env->isolate(),
478+
0,
479+
origin_string_len,
480+
reinterpret_cast<uint8_t*>(origin_contents));
484481

485482
size_t n = 0;
486483
char* p;
@@ -3129,8 +3126,13 @@ void Http2Session::AltSvc(const FunctionCallbackInfo<Value>& args) {
31293126

31303127
MaybeStackBuffer<uint8_t> origin(origin_len);
31313128
MaybeStackBuffer<uint8_t> value(value_len);
3132-
origin_str->WriteOneByte(env->isolate(), *origin);
3133-
value_str->WriteOneByte(env->isolate(), *value);
3129+
origin_str->WriteOneByteV2(env->isolate(),
3130+
0,
3131+
origin_len,
3132+
*origin,
3133+
String::WriteFlags::kNullTerminate);
3134+
value_str->WriteOneByteV2(
3135+
env->isolate(), 0, value_len, *value, String::WriteFlags::kNullTerminate);
31343136

31353137
session->AltSvc(id, *origin, origin_len, *value, value_len);
31363138
}

src/node_http_common-inl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ NgHeaders<T>::NgHeaders(Environment* env, v8::Local<v8::Array> headers) {
3737
nv_t* const nva = reinterpret_cast<nv_t*>(start);
3838

3939
CHECK_LE(header_contents + header_string_len, *buf_ + buf_.length());
40-
CHECK_EQ(header_string.As<v8::String>()->WriteOneByte(
40+
header_string.As<v8::String>()->WriteOneByteV2(
4141
env->isolate(),
42-
reinterpret_cast<uint8_t*>(header_contents),
4342
0,
4443
header_string_len,
45-
v8::String::NO_NULL_TERMINATION),
46-
header_string_len);
44+
reinterpret_cast<uint8_t*>(header_contents));
4745

4846
size_t n = 0;
4947
char* p;

src/string_bytes.cc

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,11 @@ MaybeLocal<Value> ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate,
200200

201201
} // anonymous namespace
202202

203-
size_t StringBytes::WriteUCS2(
204-
Isolate* isolate, char* buf, size_t buflen, Local<String> str, int flags) {
203+
size_t StringBytes::WriteUCS2(Isolate* isolate,
204+
char* buf,
205+
size_t buflen,
206+
Local<String> str,
207+
int flags = String::WriteFlags::kNone) {
205208
uint16_t* const dst = reinterpret_cast<uint16_t*>(buf);
206209

207210
size_t max_chars = buflen / sizeof(*dst);
@@ -212,7 +215,8 @@ size_t StringBytes::WriteUCS2(
212215
uint16_t* const aligned_dst = nbytes::AlignUp(dst, sizeof(*dst));
213216
size_t nchars;
214217
if (aligned_dst == dst) {
215-
nchars = str->Write(isolate, dst, 0, max_chars, flags);
218+
nchars = max_chars;
219+
str->WriteV2(isolate, 0, nchars, dst, flags);
216220
return nchars * sizeof(*dst);
217221
}
218222

@@ -223,15 +227,15 @@ size_t StringBytes::WriteUCS2(
223227
if (max_chars == 0) {
224228
return 0;
225229
}
226-
nchars = str->Write(isolate, aligned_dst, 0, max_chars - 1, flags);
227-
CHECK_EQ(nchars, max_chars - 1);
230+
nchars = max_chars - 1;
231+
str->WriteV2(isolate, 0, nchars, aligned_dst, flags);
228232

229233
// Shift everything to unaligned-left
230234
memmove(dst, aligned_dst, nchars * sizeof(*dst));
231235

232236
// One more char to be written
233237
uint16_t last;
234-
CHECK_EQ(str->Write(isolate, &last, nchars, 1, flags), 1);
238+
str->WriteV2(isolate, nchars, 1, &last, flags);
235239
memcpy(buf + nchars * sizeof(*dst), &last, sizeof(last));
236240
nchars++;
237241

@@ -250,9 +254,7 @@ size_t StringBytes::Write(Isolate* isolate,
250254
Local<String> str = val.As<String>();
251255
String::ValueView input_view(isolate, str);
252256

253-
int flags = String::HINT_MANY_WRITES_EXPECTED |
254-
String::NO_NULL_TERMINATION |
255-
String::REPLACE_INVALID_UTF8;
257+
int flags = String::WriteFlags::kReplaceInvalidUtf8;
256258

257259
switch (encoding) {
258260
case ASCII:
@@ -262,13 +264,14 @@ size_t StringBytes::Write(Isolate* isolate,
262264
memcpy(buf, input_view.data8(), nbytes);
263265
} else {
264266
uint8_t* const dst = reinterpret_cast<uint8_t*>(buf);
265-
nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags);
267+
str->WriteOneByteV2(isolate, 0, buflen, dst, flags);
268+
nbytes = buflen;
266269
}
267270
break;
268271

269272
case BUFFER:
270273
case UTF8:
271-
nbytes = str->WriteUtf8(isolate, buf, buflen, nullptr, flags);
274+
nbytes = str->WriteUtf8V2(isolate, buf, buflen, flags);
272275
break;
273276

274277
case UCS2: {

src/util.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ static void MakeUtf8String(Isolate* isolate,
127127

128128
// TODO(@anonrig): Use simdutf to speed up non-one-byte strings once it's
129129
// implemented
130-
const int flags =
131-
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
132-
const int length =
133-
string->WriteUtf8(isolate, target->out(), storage, nullptr, flags);
130+
const int flags = String::WriteFlags::kReplaceInvalidUtf8;
131+
const size_t length =
132+
string->WriteUtf8V2(isolate, target->out(), storage, flags);
134133
target->SetLengthAndZeroTerminate(length);
135134
}
136135

@@ -154,9 +153,8 @@ TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
154153
const size_t storage = string->Length() + 1;
155154
AllocateSufficientStorage(storage);
156155

157-
const int flags = String::NO_NULL_TERMINATION;
158-
const int length = string->Write(isolate, out(), 0, storage, flags);
159-
SetLengthAndZeroTerminate(length);
156+
string->WriteV2(isolate, 0, storage - 1, out());
157+
SetLengthAndZeroTerminate(storage);
160158
}
161159

162160
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {

0 commit comments

Comments
 (0)