Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/api/async_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ AsyncResource::AsyncResource(Isolate* isolate,
Local<Object> resource,
const char* name,
async_id trigger_async_id)
: AsyncResource(
isolate, resource, std::string_view(name), trigger_async_id) {}

AsyncResource::AsyncResource(Isolate* isolate,
Local<Object> resource,
std::string_view name,
async_id trigger_async_id)
: env_(Environment::GetCurrent(isolate)),
resource_(isolate, resource),
context_frame_(isolate, async_context_frame::current(isolate)) {
Expand Down
11 changes: 10 additions & 1 deletion src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,18 @@ async_context EmitAsyncInit(Isolate* isolate,
Local<Object> resource,
const char* name,
async_id trigger_async_id) {
return EmitAsyncInit(
isolate, resource, std::string_view(name), trigger_async_id);
}

async_context EmitAsyncInit(Isolate* isolate,
Local<Object> resource,
std::string_view name,
async_id trigger_async_id) {
HandleScope handle_scope(isolate);
Local<String> type =
String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
String::NewFromUtf8(
isolate, name.data(), NewStringType::kInternalized, name.size())
.ToLocalChecked();
return EmitAsyncInit(isolate, resource, type, trigger_async_id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/inspector_js_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {

CHECK(args[0]->IsString());
Local<String> task_name = args[0].As<String>();
String::Value task_name_value(args.GetIsolate(), task_name);
StringView task_name_view(*task_name_value, task_name_value.length());
TwoByteValue task_name_value(args.GetIsolate(), task_name);
StringView task_name_view(task_name_value.out(), task_name_value.length());

CHECK(args[1]->IsNumber());
int64_t task_id;
Expand Down
8 changes: 8 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,10 @@ NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
const char* name,
async_id trigger_async_id = -1);
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
std::string_view name,
async_id trigger_async_id = -1);

NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
Expand Down Expand Up @@ -1508,6 +1512,10 @@ class NODE_EXTERN AsyncResource {
v8::Local<v8::Object> resource,
const char* name,
async_id trigger_async_id = -1);
AsyncResource(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
std::string_view name,
async_id trigger_async_id = -1);

virtual ~AsyncResource();

Expand Down
4 changes: 2 additions & 2 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class ThreadSafeFunction : public node::AsyncResource {
napi_threadsafe_function_call_js call_js_cb_)
: AsyncResource(env_->isolate,
resource,
*v8::String::Utf8Value(env_->isolate, name)),
node::Utf8Value(env_->isolate, name).ToStringView()),
thread_count(thread_count_),
is_closing(false),
dispatch_state(kDispatchIdle),
Expand Down Expand Up @@ -1150,7 +1150,7 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork {
: AsyncResource(
env->isolate,
async_resource,
*v8::String::Utf8Value(env->isolate, async_resource_name)),
node::Utf8Value(env->isolate, async_resource_name).ToStringView()),
ThreadPoolWork(env->node_env(), "node_api"),
_env(env),
_data(data),
Expand Down
46 changes: 21 additions & 25 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
size_t result = haystack_length;

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

TwoByteValue needle_value(isolate, needle);
if (haystack_length < 2 || needle_value.length() < 1) {
return args.GetReturnValue().Set(-1);
}
Expand All @@ -1011,27 +1007,27 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
offset / 2,
is_forward);
} else {
result =
nbytes::SearchString(reinterpret_cast<const uint16_t*>(haystack),
haystack_length / 2,
reinterpret_cast<const uint16_t*>(*needle_value),
needle_value.length(),
offset / 2,
is_forward);
result = nbytes::SearchString(reinterpret_cast<const uint16_t*>(haystack),
haystack_length / 2,
needle_value.out(),
needle_value.length(),
offset / 2,
is_forward);
}
result *= 2;
} else if (enc == UTF8) {
String::Utf8Value needle_value(isolate, needle);
Utf8Value needle_value(isolate, needle);
if (*needle_value == nullptr)
return args.GetReturnValue().Set(-1);

result =
nbytes::SearchString(reinterpret_cast<const uint8_t*>(haystack),
haystack_length,
reinterpret_cast<const uint8_t*>(*needle_value),
needle_length,
offset,
is_forward);
CHECK_GE(needle_length, needle_value.length());

result = nbytes::SearchString(
reinterpret_cast<const uint8_t*>(haystack),
haystack_length,
reinterpret_cast<const uint8_t*>(needle_value.out()),
needle_length,
offset,
is_forward);
} else if (enc == LATIN1) {
uint8_t* needle_data = node::UncheckedMalloc<uint8_t>(needle_length);
if (needle_data == nullptr) {
Expand Down Expand Up @@ -1316,10 +1312,10 @@ static void Btoa(const FunctionCallbackInfo<Value>& args) {
input->Length(),
buffer.out());
} else {
String::Value value(env->isolate(), input);
TwoByteValue value(env->isolate(), input);
MaybeStackBuffer<char> stack_buf(value.length());
size_t out_len = simdutf::convert_utf16_to_latin1(
reinterpret_cast<const char16_t*>(*value),
reinterpret_cast<const char16_t*>(value.out()),
value.length(),
stack_buf.out());
if (out_len == 0) { // error
Expand Down Expand Up @@ -1370,8 +1366,8 @@ static void Atob(const FunctionCallbackInfo<Value>& args) {
buffer.SetLength(expected_length);
result = simdutf::base64_to_binary(data, input->Length(), buffer.out());
} else { // 16-bit case
String::Value value(env->isolate(), input);
auto data = reinterpret_cast<const char16_t*>(*value);
TwoByteValue value(env->isolate(), input);
auto data = reinterpret_cast<const char16_t*>(value.out());
size_t expected_length =
simdutf::maximal_binary_length_from_base64(data, value.length());
buffer.AllocateSufficientStorage(expected_length);
Expand Down
15 changes: 7 additions & 8 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1024,15 +1024,14 @@ void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
break;
}
Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
Utf8Value msg(isolate, message->Get());
// (filename):(line) (message)
std::stringstream warning;
warning << *filename;
warning << ":";
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
warning << " ";
v8::String::Utf8Value msg(isolate, message->Get());
warning << *msg;
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
std::string warning =
SPrintF("%s:%s %s",
filename,
message->GetLineNumber(env->context()).FromMaybe(-1),
msg);
USE(ProcessEmitWarningGeneric(env, warning, "V8"));
break;
}
case Isolate::MessageErrorLevel::kMessageError:
Expand Down
3 changes: 1 addition & 2 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,7 @@ static Maybe<std::string> ErrorToString(Isolate* isolate,
if (!maybe_str.ToLocal(&js_str)) {
return Nothing<std::string>();
}
String::Utf8Value sv(isolate, js_str);
return Just<>(std::string(*sv, sv.length()));
return Just(Utf8Value(isolate, js_str).ToString());
}

static void PrintEmptyJavaScriptStack(JSONWriter* writer) {
Expand Down
6 changes: 2 additions & 4 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1508,8 +1508,7 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
}

if (table_value->IsString()) {
String::Utf8Value str(env->isolate(), table_value);
table = *str;
table = Utf8Value(env->isolate(), table_value).ToString();
} else {
THROW_ERR_INVALID_ARG_TYPE(
env->isolate(), "The \"options.table\" argument must be a string.");
Expand All @@ -1529,8 +1528,7 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
return;
}
if (db_value->IsString()) {
String::Utf8Value str(env->isolate(), db_value);
db_name = std::string(*str);
db_name = Utf8Value(env->isolate(), db_value).ToString();
} else {
THROW_ERR_INVALID_ARG_TYPE(
env->isolate(), "The \"options.db\" argument must be a string.");
Expand Down
4 changes: 2 additions & 2 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {

void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());
String::Utf8Value flags(args.GetIsolate(), args[0]);
V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
Utf8Value flags(args.GetIsolate(), args[0]);
V8::SetFlagsFromString(flags.out(), flags.length());
}

void StartCpuProfile(const FunctionCallbackInfo<Value>& args) {
Expand Down
15 changes: 5 additions & 10 deletions src/permission/permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ using v8::IntegrityLevel;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;

namespace permission {
Expand All @@ -34,24 +33,20 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsString());

String::Utf8Value utf8_deny_scope(env->isolate(), args[0]);
if (*utf8_deny_scope == nullptr) {
return;
}

const std::string deny_scope = *utf8_deny_scope;
const std::string deny_scope = Utf8Value(env->isolate(), args[0]).ToString();
PermissionScope scope = Permission::StringToPermission(deny_scope);
if (scope == PermissionScope::kPermissionsRoot) {
return args.GetReturnValue().Set(false);
}

if (args.Length() > 1 && !args[1]->IsUndefined()) {
String::Utf8Value utf8_arg(env->isolate(), args[1]);
if (*utf8_arg == nullptr) {
Utf8Value utf8_arg(env->isolate(), args[1]);
if (utf8_arg.length() == 0) {
args.GetReturnValue().Set(false);
return;
}
return args.GetReturnValue().Set(
env->permission()->is_granted(env, scope, *utf8_arg));
env->permission()->is_granted(env, scope, utf8_arg.ToStringView()));
}

return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
Expand Down
12 changes: 6 additions & 6 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ size_t StringBytes::Write(Isolate* isolate,
input_view.length());
}
} else {
String::Value value(isolate, str);
TwoByteValue value(isolate, str);
size_t written_len = buflen;
auto result = simdutf::base64_to_binary_safe(
reinterpret_cast<const char16_t*>(*value),
reinterpret_cast<const char16_t*>(value.out()),
value.length(),
buf,
written_len,
Expand Down Expand Up @@ -343,10 +343,10 @@ size_t StringBytes::Write(Isolate* isolate,
input_view.length());
}
} else {
String::Value value(isolate, str);
TwoByteValue value(isolate, str);
size_t written_len = buflen;
auto result = simdutf::base64_to_binary_safe(
reinterpret_cast<const char16_t*>(*value),
reinterpret_cast<const char16_t*>(value.out()),
value.length(),
buf,
written_len);
Expand All @@ -368,8 +368,8 @@ size_t StringBytes::Write(Isolate* isolate,
reinterpret_cast<const char*>(input_view.data8()),
input_view.length());
} else {
String::Value value(isolate, str);
nbytes = nbytes::HexDecode(buf, buflen, *value, value.length());
TwoByteValue value(isolate, str);
nbytes = nbytes::HexDecode(buf, buflen, value.out(), value.length());
}
break;

Expand Down
22 changes: 22 additions & 0 deletions tools/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6489,6 +6489,26 @@ def CheckLocalVectorUsage(filename, lines, error):
'Do not use std::vector<v8::Local<T>>. '
'Use v8::LocalVector<T> instead.')

def CheckStringValueUsage(filename, lines, error):
"""Logs an error if v8's String::Value/Utf8Value are used.
Args:
filename: The name of the current file.
lines: An array of strings, each representing a line of the file.
error: The function to call with any errors found.
"""
if filename.startswith('test/') or filename.startswith('test\\'):
return # Skip test files, where Node.js headers may not be available

for linenum, line in enumerate(lines):
if Search(r'\bString::Utf8Value\b', line):
error(filename, linenum, 'runtime/v8_string_value', 5,
'Do not use v8::String::Utf8Value. '
'Use node::Utf8Value instead.')
if Search(r'\bString::Value\b', line):
error(filename, linenum, 'runtime/v8_string_value', 5,
'Do not use v8::String::Value. '
'Use node::TwoByteValue instead.')

def ProcessLine(filename, file_extension, clean_lines, line,
include_state, function_state, nesting_state, error,
extra_check_functions=None):
Expand Down Expand Up @@ -6660,6 +6680,8 @@ def ProcessFileData(filename, file_extension, lines, error,

CheckLocalVectorUsage(filename, lines, error)

CheckStringValueUsage(filename, lines, error)

def ProcessConfigOverrides(filename):
""" Loads the configuration files and processes the config overrides.
Expand Down
Loading