From 8375f753c0861b8a96c368e6347010a343d0535e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 26 Aug 2018 10:28:17 +0200 Subject: [PATCH] src: use String::Write{OneByte,Utf8} with isolate PR-URL: https://github.com/nodejs/node/pull/22531 Reviewed-By: Colin Ihrig Reviewed-By: Daniel Bevenius Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- benchmark/napi/function_args/binding.cc | 2 +- src/node_api.cc | 24 +++++++++++++------- src/node_buffer.cc | 30 +++++++++++++------------ src/node_http2.cc | 14 +++++++----- src/string_bytes.cc | 17 +++++++------- src/string_bytes.h | 3 ++- src/util.cc | 5 +++-- 7 files changed, 55 insertions(+), 40 deletions(-) diff --git a/benchmark/napi/function_args/binding.cc b/benchmark/napi/function_args/binding.cc index 11eb394a6a98f5..2c8fc7231f8daa 100644 --- a/benchmark/napi/function_args/binding.cc +++ b/benchmark/napi/function_args/binding.cc @@ -21,7 +21,7 @@ void CallWithString(const FunctionCallbackInfo& args) { Local str = args[0].As(); const int32_t length = str->Utf8Length() + 1; char* buf = new char[length]; - str->WriteUtf8(buf, length); + str->WriteUtf8(args.GetIsolate(), buf, length); delete [] buf; } } diff --git a/src/node_api.cc b/src/node_api.cc index d966e596918291..8ccadab3061cd7 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -2362,9 +2362,12 @@ napi_status napi_get_value_string_latin1(napi_env env, CHECK_ARG(env, result); *result = val.As()->Length(); } else { - int copied = val.As()->WriteOneByte( - reinterpret_cast(buf), 0, bufsize - 1, - v8::String::NO_NULL_TERMINATION); + int copied = + val.As()->WriteOneByte(env->isolate, + reinterpret_cast(buf), + 0, + bufsize - 1, + v8::String::NO_NULL_TERMINATION); buf[copied] = '\0'; if (result != nullptr) { @@ -2399,8 +2402,11 @@ napi_status napi_get_value_string_utf8(napi_env env, *result = val.As()->Utf8Length(); } else { int copied = val.As()->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) { @@ -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()->Length(); } else { - int copied = val.As()->Write( - reinterpret_cast(buf), 0, bufsize - 1, - v8::String::NO_NULL_TERMINATION); + int copied = val.As()->Write(env->isolate, + reinterpret_cast(buf), + 0, + bufsize - 1, + v8::String::NO_NULL_TERMINATION); buf[copied] = '\0'; if (result != nullptr) { diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 858852e748fe03..95ffaa1993fb95 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -806,15 +806,16 @@ int64_t IndexOfOffset(size_t length, } void IndexOfString(const FunctionCallbackInfo& 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 needle = args[1].As(); @@ -826,8 +827,7 @@ void IndexOfString(const FunctionCallbackInfo& 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, @@ -857,7 +857,7 @@ void IndexOfString(const FunctionCallbackInfo& 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); @@ -867,7 +867,7 @@ void IndexOfString(const FunctionCallbackInfo& 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(decoder.out()); @@ -890,7 +890,7 @@ void IndexOfString(const FunctionCallbackInfo& 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); @@ -906,7 +906,7 @@ void IndexOfString(const FunctionCallbackInfo& 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(haystack), haystack_length, @@ -1057,18 +1057,20 @@ void Swap64(const FunctionCallbackInfo& args) { // Used in TextEncoder.prototype.encode. static void EncodeUtf8String(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + Isolate* isolate = env->isolate(); CHECK_GE(args.Length(), 1); CHECK(args[0]->IsString()); Local str = args[0].As(); 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); } diff --git a/src/node_http2.cc b/src/node_http2.cc index e741120f5034d5..8841601040cad8 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -381,10 +381,12 @@ Headers::Headers(Isolate* isolate, nghttp2_nv* const nva = reinterpret_cast(start); CHECK_LE(header_contents + header_string_len, *buf_ + buf_.length()); - CHECK_EQ(header_string.As() - ->WriteOneByte(reinterpret_cast(header_contents), - 0, header_string_len, - String::NO_NULL_TERMINATION), + CHECK_EQ(header_string.As()->WriteOneByte( + isolate, + reinterpret_cast(header_contents), + 0, + header_string_len, + String::NO_NULL_TERMINATION), header_string_len); size_t n = 0; @@ -2633,8 +2635,8 @@ void Http2Session::AltSvc(const FunctionCallbackInfo& args) { MaybeStackBuffer origin(origin_len); MaybeStackBuffer 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); } diff --git a/src/string_bytes.cc b/src/string_bytes.cc index da54ca6023721f..c106571a468301 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -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 str, int flags, @@ -273,7 +273,7 @@ size_t StringBytes::WriteUCS2(char* buf, size_t nchars; size_t alignment = reinterpret_cast(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); } @@ -283,14 +283,15 @@ size_t StringBytes::WriteUCS2(char* buf, CHECK_EQ(reinterpret_cast(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++; } @@ -329,20 +330,20 @@ size_t StringBytes::Write(Isolate* isolate, memcpy(buf, ext->data(), nbytes); } else { uint8_t* const dst = reinterpret_cast(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(nchars); // Node's "ucs2" encoding wants LE character data stored in diff --git a/src/string_bytes.h b/src/string_bytes.h index 7a70f06f63dec4..5f5fcd9fa0f05e 100644 --- a/src/string_bytes.h +++ b/src/string_bytes.h @@ -112,7 +112,8 @@ class StringBytes { v8::Local* error); private: - static size_t WriteUCS2(char* buf, + static size_t WriteUCS2(v8::Isolate* isolate, + char* buf, size_t buflen, v8::Local str, int flags, diff --git a/src/util.cc b/src/util.cc index 66be18eae2d150..c36ea5169d3313 100644 --- a/src/util.cc +++ b/src/util.cc @@ -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); } @@ -71,7 +72,7 @@ TwoByteValue::TwoByteValue(Isolate* isolate, Local 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); }