Skip to content

Commit

Permalink
src: use String::Write{OneByte,Utf8} with isolate
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
targos committed Sep 3, 2018
1 parent d50e1ff commit 8375f75
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion benchmark/napi/function_args/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void CallWithString(const FunctionCallbackInfo<Value>& args) {
Local<String> str = args[0].As<String>();
const int32_t length = str->Utf8Length() + 1;
char* buf = new char[length];
str->WriteUtf8(buf, length);
str->WriteUtf8(args.GetIsolate(), buf, length);
delete [] buf;
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2362,9 +2362,12 @@ napi_status napi_get_value_string_latin1(napi_env env,
CHECK_ARG(env, result);
*result = val.As<v8::String>()->Length();
} else {
int copied = val.As<v8::String>()->WriteOneByte(
reinterpret_cast<uint8_t*>(buf), 0, bufsize - 1,
v8::String::NO_NULL_TERMINATION);
int copied =
val.As<v8::String>()->WriteOneByte(env->isolate,
reinterpret_cast<uint8_t*>(buf),
0,
bufsize - 1,
v8::String::NO_NULL_TERMINATION);

buf[copied] = '\0';
if (result != nullptr) {
Expand Down Expand Up @@ -2399,8 +2402,11 @@ napi_status napi_get_value_string_utf8(napi_env env,
*result = val.As<v8::String>()->Utf8Length();
} else {
int copied = val.As<v8::String>()->WriteUtf8(
buf, bufsize - 1, nullptr, v8::String::REPLACE_INVALID_UTF8 |
v8::String::NO_NULL_TERMINATION);
env->isolate,
buf,
bufsize - 1,
nullptr,
v8::String::REPLACE_INVALID_UTF8 | v8::String::NO_NULL_TERMINATION);

buf[copied] = '\0';
if (result != nullptr) {
Expand Down Expand Up @@ -2435,9 +2441,11 @@ napi_status napi_get_value_string_utf16(napi_env env,
// V8 assumes UTF-16 length is the same as the number of characters.
*result = val.As<v8::String>()->Length();
} else {
int copied = val.As<v8::String>()->Write(
reinterpret_cast<uint16_t*>(buf), 0, bufsize - 1,
v8::String::NO_NULL_TERMINATION);
int copied = val.As<v8::String>()->Write(env->isolate,
reinterpret_cast<uint16_t*>(buf),
0,
bufsize - 1,
v8::String::NO_NULL_TERMINATION);

buf[copied] = '\0';
if (result != nullptr) {
Expand Down
30 changes: 16 additions & 14 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -806,15 +806,16 @@ int64_t IndexOfOffset(size_t length,
}

void IndexOfString(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();

CHECK(args[1]->IsString());
CHECK(args[2]->IsNumber());
CHECK(args[4]->IsBoolean());

enum encoding enc = ParseEncoding(args.GetIsolate(),
args[3],
UTF8);
enum encoding enc = ParseEncoding(isolate, 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);

Local<String> needle = args[1].As<String>();
Expand All @@ -826,8 +827,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
const size_t haystack_length = (enc == UCS2) ?
ts_obj_length &~ 1 : ts_obj_length; // NOLINT(whitespace/operators)

const size_t needle_length =
StringBytes::Size(args.GetIsolate(), needle, enc);
const size_t needle_length = StringBytes::Size(isolate, needle, enc);

int64_t opt_offset = IndexOfOffset(haystack_length,
offset_i64,
Expand Down Expand Up @@ -857,7 +857,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
size_t result = haystack_length;

if (enc == UCS2) {
String::Value needle_value(args.GetIsolate(), needle);
String::Value needle_value(isolate, needle);
if (*needle_value == nullptr)
return args.GetReturnValue().Set(-1);

Expand All @@ -867,7 +867,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {

if (IsBigEndian()) {
StringBytes::InlineDecoder decoder;
decoder.Decode(Environment::GetCurrent(args), needle, args[3], UCS2);
decoder.Decode(env, needle, args[3], UCS2);
const uint16_t* decoded_string =
reinterpret_cast<const uint16_t*>(decoder.out());

Expand All @@ -890,7 +890,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
}
result *= 2;
} else if (enc == UTF8) {
String::Utf8Value needle_value(args.GetIsolate(), needle);
String::Utf8Value needle_value(isolate, needle);
if (*needle_value == nullptr)
return args.GetReturnValue().Set(-1);

Expand All @@ -906,7 +906,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().Set(-1);
}
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),
haystack_length,
Expand Down Expand Up @@ -1057,18 +1057,20 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
// Used in TextEncoder.prototype.encode.
static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());

Local<String> str = args[0].As<String>();
size_t length = str->Utf8Length();
char* data = node::UncheckedMalloc(length);
str->WriteUtf8(data,
-1, // We are certain that `data` is sufficiently large
str->WriteUtf8(isolate,
data,
-1, // We are certain that `data` is sufficiently large
nullptr,
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
auto array_buf = ArrayBuffer::New(env->isolate(), data, length,
ArrayBufferCreationMode::kInternalized);
auto array_buf = ArrayBuffer::New(
isolate, data, length, ArrayBufferCreationMode::kInternalized);
auto array = Uint8Array::New(array_buf, 0, length);
args.GetReturnValue().Set(array);
}
Expand Down
14 changes: 8 additions & 6 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,12 @@ Headers::Headers(Isolate* isolate,
nghttp2_nv* const nva = reinterpret_cast<nghttp2_nv*>(start);

CHECK_LE(header_contents + header_string_len, *buf_ + buf_.length());
CHECK_EQ(header_string.As<String>()
->WriteOneByte(reinterpret_cast<uint8_t*>(header_contents),
0, header_string_len,
String::NO_NULL_TERMINATION),
CHECK_EQ(header_string.As<String>()->WriteOneByte(
isolate,
reinterpret_cast<uint8_t*>(header_contents),
0,
header_string_len,
String::NO_NULL_TERMINATION),
header_string_len);

size_t n = 0;
Expand Down Expand Up @@ -2633,8 +2635,8 @@ void Http2Session::AltSvc(const FunctionCallbackInfo<Value>& args) {

MaybeStackBuffer<uint8_t> origin(origin_len);
MaybeStackBuffer<uint8_t> value(value_len);
origin_str->WriteOneByte(*origin);
value_str->WriteOneByte(*value);
origin_str->WriteOneByte(env->isolate(), *origin);
value_str->WriteOneByte(env->isolate(), *value);

session->AltSvc(id, *origin, origin_len, *value, value_len);
}
Expand Down
17 changes: 9 additions & 8 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ static size_t hex_decode(char* buf,
return i;
}


size_t StringBytes::WriteUCS2(char* buf,
size_t StringBytes::WriteUCS2(Isolate* isolate,
char* buf,
size_t buflen,
Local<String> str,
int flags,
Expand All @@ -273,7 +273,7 @@ size_t StringBytes::WriteUCS2(char* buf,
size_t nchars;
size_t alignment = reinterpret_cast<uintptr_t>(dst) % sizeof(*dst);
if (alignment == 0) {
nchars = str->Write(dst, 0, max_chars, flags);
nchars = str->Write(isolate, dst, 0, max_chars, flags);
*chars_written = nchars;
return nchars * sizeof(*dst);
}
Expand All @@ -283,14 +283,15 @@ size_t StringBytes::WriteUCS2(char* buf,
CHECK_EQ(reinterpret_cast<uintptr_t>(aligned_dst) % sizeof(*dst), 0);

// 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
memmove(dst, aligned_dst, nchars * sizeof(*dst));

// One more char to be written
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));
nchars++;
}
Expand Down Expand Up @@ -329,20 +330,20 @@ size_t StringBytes::Write(Isolate* isolate,
memcpy(buf, ext->data(), nbytes);
} else {
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;
break;

case BUFFER:
case UTF8:
nbytes = str->WriteUtf8(buf, buflen, chars_written, flags);
nbytes = str->WriteUtf8(isolate, buf, buflen, chars_written, flags);
break;

case UCS2: {
size_t nchars;

nbytes = WriteUCS2(buf, buflen, str, flags, &nchars);
nbytes = WriteUCS2(isolate, buf, buflen, str, flags, &nchars);
*chars_written = static_cast<int>(nchars);

// Node's "ucs2" encoding wants LE character data stored in
Expand Down
3 changes: 2 additions & 1 deletion src/string_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class StringBytes {
v8::Local<v8::Value>* error);

private:
static size_t WriteUCS2(char* buf,
static size_t WriteUCS2(v8::Isolate* isolate,
char* buf,
size_t buflen,
v8::Local<v8::String> str,
int flags,
Expand Down
5 changes: 3 additions & 2 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ static void MakeUtf8String(Isolate* isolate,
target->AllocateSufficientStorage(storage);
const int flags =
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);
}

Expand All @@ -71,7 +72,7 @@ TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
AllocateSufficientStorage(storage);

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);
}

Expand Down

0 comments on commit 8375f75

Please sign in to comment.