src: use String::Write{OneByte,Utf8} with isolate
PR-URL: https://github.com/nodejs/node/pull/22531 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
7c84489b88
commit
a64fd7f320
@ -21,7 +21,7 @@ void CallWithString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
Local<String> str = args[0].As<String>();
|
Local<String> str = args[0].As<String>();
|
||||||
const int32_t length = str->Utf8Length() + 1;
|
const int32_t length = str->Utf8Length() + 1;
|
||||||
char* buf = new char[length];
|
char* buf = new char[length];
|
||||||
str->WriteUtf8(buf, length);
|
str->WriteUtf8(args.GetIsolate(), buf, length);
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2362,8 +2362,11 @@ napi_status napi_get_value_string_latin1(napi_env env,
|
|||||||
CHECK_ARG(env, result);
|
CHECK_ARG(env, result);
|
||||||
*result = val.As<v8::String>()->Length();
|
*result = val.As<v8::String>()->Length();
|
||||||
} else {
|
} else {
|
||||||
int copied = val.As<v8::String>()->WriteOneByte(
|
int copied =
|
||||||
reinterpret_cast<uint8_t*>(buf), 0, bufsize - 1,
|
val.As<v8::String>()->WriteOneByte(env->isolate,
|
||||||
|
reinterpret_cast<uint8_t*>(buf),
|
||||||
|
0,
|
||||||
|
bufsize - 1,
|
||||||
v8::String::NO_NULL_TERMINATION);
|
v8::String::NO_NULL_TERMINATION);
|
||||||
|
|
||||||
buf[copied] = '\0';
|
buf[copied] = '\0';
|
||||||
@ -2399,8 +2402,11 @@ napi_status napi_get_value_string_utf8(napi_env env,
|
|||||||
*result = val.As<v8::String>()->Utf8Length();
|
*result = val.As<v8::String>()->Utf8Length();
|
||||||
} else {
|
} else {
|
||||||
int copied = val.As<v8::String>()->WriteUtf8(
|
int copied = val.As<v8::String>()->WriteUtf8(
|
||||||
buf, bufsize - 1, nullptr, v8::String::REPLACE_INVALID_UTF8 |
|
env->isolate,
|
||||||
v8::String::NO_NULL_TERMINATION);
|
buf,
|
||||||
|
bufsize - 1,
|
||||||
|
nullptr,
|
||||||
|
v8::String::REPLACE_INVALID_UTF8 | v8::String::NO_NULL_TERMINATION);
|
||||||
|
|
||||||
buf[copied] = '\0';
|
buf[copied] = '\0';
|
||||||
if (result != nullptr) {
|
if (result != nullptr) {
|
||||||
@ -2435,8 +2441,10 @@ napi_status napi_get_value_string_utf16(napi_env env,
|
|||||||
// V8 assumes UTF-16 length is the same as the number of characters.
|
// V8 assumes UTF-16 length is the same as the number of characters.
|
||||||
*result = val.As<v8::String>()->Length();
|
*result = val.As<v8::String>()->Length();
|
||||||
} else {
|
} else {
|
||||||
int copied = val.As<v8::String>()->Write(
|
int copied = val.As<v8::String>()->Write(env->isolate,
|
||||||
reinterpret_cast<uint16_t*>(buf), 0, bufsize - 1,
|
reinterpret_cast<uint16_t*>(buf),
|
||||||
|
0,
|
||||||
|
bufsize - 1,
|
||||||
v8::String::NO_NULL_TERMINATION);
|
v8::String::NO_NULL_TERMINATION);
|
||||||
|
|
||||||
buf[copied] = '\0';
|
buf[copied] = '\0';
|
||||||
|
@ -806,15 +806,16 @@ int64_t IndexOfOffset(size_t length,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
||||||
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
Isolate* isolate = env->isolate();
|
||||||
|
|
||||||
CHECK(args[1]->IsString());
|
CHECK(args[1]->IsString());
|
||||||
CHECK(args[2]->IsNumber());
|
CHECK(args[2]->IsNumber());
|
||||||
CHECK(args[4]->IsBoolean());
|
CHECK(args[4]->IsBoolean());
|
||||||
|
|
||||||
enum encoding enc = ParseEncoding(args.GetIsolate(),
|
enum encoding enc = ParseEncoding(isolate, args[3], UTF8);
|
||||||
args[3],
|
|
||||||
UTF8);
|
|
||||||
|
|
||||||
THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]);
|
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
|
||||||
SPREAD_BUFFER_ARG(args[0], ts_obj);
|
SPREAD_BUFFER_ARG(args[0], ts_obj);
|
||||||
|
|
||||||
Local<String> needle = args[1].As<String>();
|
Local<String> needle = args[1].As<String>();
|
||||||
@ -826,8 +827,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
const size_t haystack_length = (enc == UCS2) ?
|
const size_t haystack_length = (enc == UCS2) ?
|
||||||
ts_obj_length &~ 1 : ts_obj_length; // NOLINT(whitespace/operators)
|
ts_obj_length &~ 1 : ts_obj_length; // NOLINT(whitespace/operators)
|
||||||
|
|
||||||
const size_t needle_length =
|
const size_t needle_length = StringBytes::Size(isolate, needle, enc);
|
||||||
StringBytes::Size(args.GetIsolate(), needle, enc);
|
|
||||||
|
|
||||||
int64_t opt_offset = IndexOfOffset(haystack_length,
|
int64_t opt_offset = IndexOfOffset(haystack_length,
|
||||||
offset_i64,
|
offset_i64,
|
||||||
@ -857,7 +857,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
size_t result = haystack_length;
|
size_t result = haystack_length;
|
||||||
|
|
||||||
if (enc == UCS2) {
|
if (enc == UCS2) {
|
||||||
String::Value needle_value(args.GetIsolate(), needle);
|
String::Value needle_value(isolate, needle);
|
||||||
if (*needle_value == nullptr)
|
if (*needle_value == nullptr)
|
||||||
return args.GetReturnValue().Set(-1);
|
return args.GetReturnValue().Set(-1);
|
||||||
|
|
||||||
@ -867,7 +867,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
|
|
||||||
if (IsBigEndian()) {
|
if (IsBigEndian()) {
|
||||||
StringBytes::InlineDecoder decoder;
|
StringBytes::InlineDecoder decoder;
|
||||||
decoder.Decode(Environment::GetCurrent(args), needle, args[3], UCS2);
|
decoder.Decode(env, needle, args[3], UCS2);
|
||||||
const uint16_t* decoded_string =
|
const uint16_t* decoded_string =
|
||||||
reinterpret_cast<const uint16_t*>(decoder.out());
|
reinterpret_cast<const uint16_t*>(decoder.out());
|
||||||
|
|
||||||
@ -890,7 +890,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
}
|
}
|
||||||
result *= 2;
|
result *= 2;
|
||||||
} else if (enc == UTF8) {
|
} else if (enc == UTF8) {
|
||||||
String::Utf8Value needle_value(args.GetIsolate(), needle);
|
String::Utf8Value needle_value(isolate, needle);
|
||||||
if (*needle_value == nullptr)
|
if (*needle_value == nullptr)
|
||||||
return args.GetReturnValue().Set(-1);
|
return args.GetReturnValue().Set(-1);
|
||||||
|
|
||||||
@ -906,7 +906,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
|||||||
return args.GetReturnValue().Set(-1);
|
return args.GetReturnValue().Set(-1);
|
||||||
}
|
}
|
||||||
needle->WriteOneByte(
|
needle->WriteOneByte(
|
||||||
needle_data, 0, needle_length, String::NO_NULL_TERMINATION);
|
isolate, needle_data, 0, needle_length, String::NO_NULL_TERMINATION);
|
||||||
|
|
||||||
result = SearchString(reinterpret_cast<const uint8_t*>(haystack),
|
result = SearchString(reinterpret_cast<const uint8_t*>(haystack),
|
||||||
haystack_length,
|
haystack_length,
|
||||||
@ -1057,18 +1057,20 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
|
|||||||
// Used in TextEncoder.prototype.encode.
|
// Used in TextEncoder.prototype.encode.
|
||||||
static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
|
static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
|
||||||
Environment* env = Environment::GetCurrent(args);
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
Isolate* isolate = env->isolate();
|
||||||
CHECK_GE(args.Length(), 1);
|
CHECK_GE(args.Length(), 1);
|
||||||
CHECK(args[0]->IsString());
|
CHECK(args[0]->IsString());
|
||||||
|
|
||||||
Local<String> str = args[0].As<String>();
|
Local<String> str = args[0].As<String>();
|
||||||
size_t length = str->Utf8Length();
|
size_t length = str->Utf8Length();
|
||||||
char* data = node::UncheckedMalloc(length);
|
char* data = node::UncheckedMalloc(length);
|
||||||
str->WriteUtf8(data,
|
str->WriteUtf8(isolate,
|
||||||
|
data,
|
||||||
-1, // We are certain that `data` is sufficiently large
|
-1, // We are certain that `data` is sufficiently large
|
||||||
nullptr,
|
nullptr,
|
||||||
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
|
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
|
||||||
auto array_buf = ArrayBuffer::New(env->isolate(), data, length,
|
auto array_buf = ArrayBuffer::New(
|
||||||
ArrayBufferCreationMode::kInternalized);
|
isolate, data, length, ArrayBufferCreationMode::kInternalized);
|
||||||
auto array = Uint8Array::New(array_buf, 0, length);
|
auto array = Uint8Array::New(array_buf, 0, length);
|
||||||
args.GetReturnValue().Set(array);
|
args.GetReturnValue().Set(array);
|
||||||
}
|
}
|
||||||
|
@ -381,9 +381,11 @@ Headers::Headers(Isolate* isolate,
|
|||||||
nghttp2_nv* const nva = reinterpret_cast<nghttp2_nv*>(start);
|
nghttp2_nv* const nva = reinterpret_cast<nghttp2_nv*>(start);
|
||||||
|
|
||||||
CHECK_LE(header_contents + header_string_len, *buf_ + buf_.length());
|
CHECK_LE(header_contents + header_string_len, *buf_ + buf_.length());
|
||||||
CHECK_EQ(header_string.As<String>()
|
CHECK_EQ(header_string.As<String>()->WriteOneByte(
|
||||||
->WriteOneByte(reinterpret_cast<uint8_t*>(header_contents),
|
isolate,
|
||||||
0, header_string_len,
|
reinterpret_cast<uint8_t*>(header_contents),
|
||||||
|
0,
|
||||||
|
header_string_len,
|
||||||
String::NO_NULL_TERMINATION),
|
String::NO_NULL_TERMINATION),
|
||||||
header_string_len);
|
header_string_len);
|
||||||
|
|
||||||
@ -2633,8 +2635,8 @@ void Http2Session::AltSvc(const FunctionCallbackInfo<Value>& args) {
|
|||||||
|
|
||||||
MaybeStackBuffer<uint8_t> origin(origin_len);
|
MaybeStackBuffer<uint8_t> origin(origin_len);
|
||||||
MaybeStackBuffer<uint8_t> value(value_len);
|
MaybeStackBuffer<uint8_t> value(value_len);
|
||||||
origin_str->WriteOneByte(*origin);
|
origin_str->WriteOneByte(env->isolate(), *origin);
|
||||||
value_str->WriteOneByte(*value);
|
value_str->WriteOneByte(env->isolate(), *value);
|
||||||
|
|
||||||
session->AltSvc(id, *origin, origin_len, *value, value_len);
|
session->AltSvc(id, *origin, origin_len, *value, value_len);
|
||||||
}
|
}
|
||||||
|
@ -257,8 +257,8 @@ static size_t hex_decode(char* buf,
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t StringBytes::WriteUCS2(Isolate* isolate,
|
||||||
size_t StringBytes::WriteUCS2(char* buf,
|
char* buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
Local<String> str,
|
Local<String> str,
|
||||||
int flags,
|
int flags,
|
||||||
@ -273,7 +273,7 @@ size_t StringBytes::WriteUCS2(char* buf,
|
|||||||
size_t nchars;
|
size_t nchars;
|
||||||
size_t alignment = reinterpret_cast<uintptr_t>(dst) % sizeof(*dst);
|
size_t alignment = reinterpret_cast<uintptr_t>(dst) % sizeof(*dst);
|
||||||
if (alignment == 0) {
|
if (alignment == 0) {
|
||||||
nchars = str->Write(dst, 0, max_chars, flags);
|
nchars = str->Write(isolate, dst, 0, max_chars, flags);
|
||||||
*chars_written = nchars;
|
*chars_written = nchars;
|
||||||
return nchars * sizeof(*dst);
|
return nchars * sizeof(*dst);
|
||||||
}
|
}
|
||||||
@ -283,14 +283,15 @@ size_t StringBytes::WriteUCS2(char* buf,
|
|||||||
CHECK_EQ(reinterpret_cast<uintptr_t>(aligned_dst) % sizeof(*dst), 0);
|
CHECK_EQ(reinterpret_cast<uintptr_t>(aligned_dst) % sizeof(*dst), 0);
|
||||||
|
|
||||||
// Write all but the last char
|
// Write all but the last char
|
||||||
nchars = str->Write(aligned_dst, 0, max_chars - 1, flags);
|
nchars = str->Write(isolate, aligned_dst, 0, max_chars - 1, flags);
|
||||||
|
|
||||||
// Shift everything to unaligned-left
|
// Shift everything to unaligned-left
|
||||||
memmove(dst, aligned_dst, nchars * sizeof(*dst));
|
memmove(dst, aligned_dst, nchars * sizeof(*dst));
|
||||||
|
|
||||||
// One more char to be written
|
// One more char to be written
|
||||||
uint16_t last;
|
uint16_t last;
|
||||||
if (nchars == max_chars - 1 && str->Write(&last, nchars, 1, flags) != 0) {
|
if (nchars == max_chars - 1 &&
|
||||||
|
str->Write(isolate, &last, nchars, 1, flags) != 0) {
|
||||||
memcpy(buf + nchars * sizeof(*dst), &last, sizeof(last));
|
memcpy(buf + nchars * sizeof(*dst), &last, sizeof(last));
|
||||||
nchars++;
|
nchars++;
|
||||||
}
|
}
|
||||||
@ -329,20 +330,20 @@ size_t StringBytes::Write(Isolate* isolate,
|
|||||||
memcpy(buf, ext->data(), nbytes);
|
memcpy(buf, ext->data(), nbytes);
|
||||||
} else {
|
} else {
|
||||||
uint8_t* const dst = reinterpret_cast<uint8_t*>(buf);
|
uint8_t* const dst = reinterpret_cast<uint8_t*>(buf);
|
||||||
nbytes = str->WriteOneByte(dst, 0, buflen, flags);
|
nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags);
|
||||||
}
|
}
|
||||||
*chars_written = nbytes;
|
*chars_written = nbytes;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BUFFER:
|
case BUFFER:
|
||||||
case UTF8:
|
case UTF8:
|
||||||
nbytes = str->WriteUtf8(buf, buflen, chars_written, flags);
|
nbytes = str->WriteUtf8(isolate, buf, buflen, chars_written, flags);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case UCS2: {
|
case UCS2: {
|
||||||
size_t nchars;
|
size_t nchars;
|
||||||
|
|
||||||
nbytes = WriteUCS2(buf, buflen, str, flags, &nchars);
|
nbytes = WriteUCS2(isolate, buf, buflen, str, flags, &nchars);
|
||||||
*chars_written = static_cast<int>(nchars);
|
*chars_written = static_cast<int>(nchars);
|
||||||
|
|
||||||
// Node's "ucs2" encoding wants LE character data stored in
|
// Node's "ucs2" encoding wants LE character data stored in
|
||||||
|
@ -112,7 +112,8 @@ class StringBytes {
|
|||||||
v8::Local<v8::Value>* error);
|
v8::Local<v8::Value>* error);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static size_t WriteUCS2(char* buf,
|
static size_t WriteUCS2(v8::Isolate* isolate,
|
||||||
|
char* buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
v8::Local<v8::String> str,
|
v8::Local<v8::String> str,
|
||||||
int flags,
|
int flags,
|
||||||
|
@ -45,7 +45,8 @@ static void MakeUtf8String(Isolate* isolate,
|
|||||||
target->AllocateSufficientStorage(storage);
|
target->AllocateSufficientStorage(storage);
|
||||||
const int flags =
|
const int flags =
|
||||||
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
|
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
|
||||||
const int length = string->WriteUtf8(target->out(), storage, 0, flags);
|
const int length =
|
||||||
|
string->WriteUtf8(isolate, target->out(), storage, 0, flags);
|
||||||
target->SetLengthAndZeroTerminate(length);
|
target->SetLengthAndZeroTerminate(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
|
|||||||
AllocateSufficientStorage(storage);
|
AllocateSufficientStorage(storage);
|
||||||
|
|
||||||
const int flags = String::NO_NULL_TERMINATION;
|
const int flags = String::NO_NULL_TERMINATION;
|
||||||
const int length = string->Write(out(), 0, storage, flags);
|
const int length = string->Write(isolate, out(), 0, storage, flags);
|
||||||
SetLengthAndZeroTerminate(length);
|
SetLengthAndZeroTerminate(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user