diff --git a/src/node_i18n.cc b/src/node_i18n.cc index 0bcf10a0b35acc..267c24f85ed15d 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -104,14 +104,15 @@ namespace { template MaybeLocal ToBufferEndian(Environment* env, MaybeStackBuffer* buf) { - MaybeLocal ret = Buffer::New(env, buf); - if (ret.IsEmpty()) - return ret; + Local ret; + if (!Buffer::New(env, buf).ToLocal(&ret)) { + return {}; + } static_assert(sizeof(T) == 1 || sizeof(T) == 2, "Currently only one- or two-byte buffers are supported"); if constexpr (sizeof(T) > 1 && IsBigEndian()) { - SPREAD_BUFFER_ARG(ret.ToLocalChecked(), retbuf); + SPREAD_BUFFER_ARG(ret, retbuf); CHECK(nbytes::SwapBytes16(retbuf_data, retbuf_length)); } @@ -317,19 +318,22 @@ void Transcode(const FunctionCallbackInfo&args) { status = U_ILLEGAL_ARGUMENT_ERROR; } - if (result.IsEmpty()) - return args.GetReturnValue().Set(status); + Local res; + if (result.ToLocal(&res)) { + return args.GetReturnValue().Set(res); + } - return args.GetReturnValue().Set(result.ToLocalChecked()); + return args.GetReturnValue().Set(status); } void ICUErrorName(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); CHECK(args[0]->IsInt32()); UErrorCode status = static_cast(args[0].As()->Value()); - args.GetReturnValue().Set( - String::NewFromUtf8(env->isolate(), - u_errorName(status)).ToLocalChecked()); + Local res; + if (String::NewFromUtf8(env->isolate(), u_errorName(status)).ToLocal(&res)) { + args.GetReturnValue().Set(res); + } } } // anonymous namespace @@ -390,7 +394,10 @@ void ConverterObject::Create(const FunctionCallbackInfo& args) { CHECK_GE(args.Length(), 2); Utf8Value label(env->isolate(), args[0]); - int flags = args[1]->Uint32Value(env->context()).ToChecked(); + uint32_t flags; + if (!args[1]->Uint32Value(env->context()).To(&flags)) { + return; + } bool fatal = (flags & CONVERTER_FLAGS_FATAL) == CONVERTER_FLAGS_FATAL; @@ -430,7 +437,10 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { } ArrayBufferViewContents input(args[1]); - int flags = args[2]->Uint32Value(env->context()).ToChecked(); + uint32_t flags; + if (!args[2]->Uint32Value(env->context()).To(&flags)) { + return; + } CHECK(args[3]->IsString()); Local from_encoding = args[3].As(); diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 73c0c38dc7bf45..bae1387da5d897 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -1150,16 +1150,16 @@ void MessagePort::ReceiveMessage(const FunctionCallbackInfo& args) { MessagePort* port = Unwrap(args[0].As()); if (port == nullptr) { // Return 'no messages' for a closed port. - args.GetReturnValue().Set( - Environment::GetCurrent(args)->no_message_symbol()); + args.GetReturnValue().Set(env->no_message_symbol()); return; } - MaybeLocal payload = - port->ReceiveMessage(port->object()->GetCreationContextChecked(), - MessageProcessingMode::kForceReadMessages); - if (!payload.IsEmpty()) - args.GetReturnValue().Set(payload.ToLocalChecked()); + Local payload; + if (port->ReceiveMessage(port->object()->GetCreationContextChecked(), + MessageProcessingMode::kForceReadMessages) + .ToLocal(&payload)) { + args.GetReturnValue().Set(payload); + } } void MessagePort::MoveToContext(const FunctionCallbackInfo& args) { diff --git a/src/node_modules.cc b/src/node_modules.cc index 38d2c65c7f3282..e362663053526f 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -398,9 +398,10 @@ void BindingData::GetNearestParentPackageJSONType( return; } - Local value = - ToV8Value(realm->context(), package_json->type).ToLocalChecked(); - args.GetReturnValue().Set(value); + Local value; + if (ToV8Value(realm->context(), package_json->type).ToLocal(&value)) { + args.GetReturnValue().Set(value); + } } void BindingData::GetPackageScopeConfig( @@ -462,12 +463,11 @@ void BindingData::GetPackageScopeConfig( auto package_json_url_as_path = url::FileURLToPath(realm->env(), *package_json_url); CHECK(package_json_url_as_path); - return args.GetReturnValue().Set( - String::NewFromUtf8(realm->isolate(), - package_json_url_as_path->c_str(), - NewStringType::kNormal, - package_json_url_as_path->size()) - .ToLocalChecked()); + Local ret; + if (ToV8Value(realm->context(), *package_json_url_as_path, realm->isolate()) + .ToLocal(&ret)) { + args.GetReturnValue().Set(ret); + } } void FlushCompileCache(const FunctionCallbackInfo& args) { @@ -499,11 +499,13 @@ void EnableCompileCache(const FunctionCallbackInfo& args) { } Utf8Value value(isolate, args[0]); CompileCacheEnableResult result = env->EnableCompileCache(*value); - Local values[] = { - v8::Integer::New(isolate, static_cast(result.status)), - ToV8Value(context, result.message).ToLocalChecked(), - ToV8Value(context, result.cache_directory).ToLocalChecked()}; - args.GetReturnValue().Set(Array::New(isolate, &values[0], arraysize(values))); + Local values[3]; + values[0] = v8::Integer::New(isolate, static_cast(result.status)); + if (ToV8Value(context, result.message).ToLocal(&values[1]) && + ToV8Value(context, result.cache_directory).ToLocal(&values[2])) { + args.GetReturnValue().Set( + Array::New(isolate, &values[0], arraysize(values))); + } } void GetCompileCacheDir(const FunctionCallbackInfo& args) { @@ -514,9 +516,12 @@ void GetCompileCacheDir(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(v8::String::Empty(isolate)); return; } - args.GetReturnValue().Set( - ToV8Value(context, env->compile_cache_handler()->cache_dir()) - .ToLocalChecked()); + + Local ret; + if (ToV8Value(context, env->compile_cache_handler()->cache_dir()) + .ToLocal(&ret)) { + args.GetReturnValue().Set(ret); + } } void GetCompileCacheEntry(const FunctionCallbackInfo& args) { diff --git a/src/node_options.cc b/src/node_options.cc index a6dc00d7d7213f..7bd16b7d77783a 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -1364,9 +1364,11 @@ void GetCLIOptionsValues(const FunctionCallbackInfo& args) { std::string negated_name = "--no" + item.first.substr(1, item.first.size()); Local negated_value = Boolean::New(isolate, !original_value); - Local negated_name_v8 = - ToV8Value(context, negated_name).ToLocalChecked().As(); - option_names.push_back(negated_name_v8); + Local negated_name_v8; + if (!ToV8Value(context, negated_name).ToLocal(&negated_name_v8)) { + return; + } + option_names.push_back(negated_name_v8.As()); option_values.push_back(negated_value); break; } @@ -1414,9 +1416,11 @@ void GetCLIOptionsValues(const FunctionCallbackInfo& args) { UNREACHABLE(); } CHECK(!value.IsEmpty()); - Local name = - ToV8Value(context, item.first).ToLocalChecked().As(); - option_names.push_back(name); + Local name; + if (!ToV8Value(context, item.first).ToLocal(&name)) { + return; + } + option_names.push_back(name.As()); option_values.push_back(value); } @@ -1455,10 +1459,10 @@ void GetCLIOptionsInfo(const FunctionCallbackInfo& args) { const auto& option_info = item.second; auto field = option_info.field; - Local name = - ToV8Value(context, item.first).ToLocalChecked().As(); + Local name; Local help_text; - if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text)) { + if (!ToV8Value(context, item.first).ToLocal(&name) || + !ToV8Value(context, option_info.help_text).ToLocal(&help_text)) { return; } constexpr size_t kInfoSize = 4; diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index c67d1ac00a972b..bb55f4fcef5ea2 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -136,14 +136,15 @@ static void Cwd(const FunctionCallbackInfo& args) { char buf[PATH_MAX_BYTES]; size_t cwd_len = sizeof(buf); int err = uv_cwd(buf, &cwd_len); - if (err) + if (err) { return env->ThrowUVException(err, "uv_cwd"); + } - Local cwd = String::NewFromUtf8(env->isolate(), - buf, - NewStringType::kNormal, - cwd_len).ToLocalChecked(); - args.GetReturnValue().Set(cwd); + Local cwd; + if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, cwd_len) + .ToLocal(&cwd)) { + args.GetReturnValue().Set(cwd); + } } static void Kill(const FunctionCallbackInfo& args) { diff --git a/src/node_process_object.cc b/src/node_process_object.cc index bbbc2403b6fa6d..3f1b276d10bdd0 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -22,21 +22,20 @@ using v8::Isolate; using v8::Local; using v8::MaybeLocal; using v8::Name; -using v8::NewStringType; using v8::None; using v8::Object; using v8::PropertyCallbackInfo; using v8::SideEffectType; -using v8::String; using v8::Value; static void ProcessTitleGetter(Local property, const PropertyCallbackInfo& info) { std::string title = GetProcessTitle("node"); - info.GetReturnValue().Set( - String::NewFromUtf8(info.GetIsolate(), title.data(), - NewStringType::kNormal, title.size()) - .ToLocalChecked()); + Local ret; + auto isolate = info.GetIsolate(); + if (ToV8Value(isolate->GetCurrentContext(), title, isolate).ToLocal(&ret)) { + info.GetReturnValue().Set(ret); + } } static void ProcessTitleSetter(Local property, @@ -196,28 +195,34 @@ void PatchProcessObject(const FunctionCallbackInfo& args) { .FromJust()); // process.argv - process->Set(context, - FIXED_ONE_BYTE_STRING(isolate, "argv"), - ToV8Value(context, env->argv()).ToLocalChecked()).Check(); + Local val; + if (!ToV8Value(context, env->argv()).ToLocal(&val) || + !process->Set(context, FIXED_ONE_BYTE_STRING(isolate, "argv"), val) + .IsJust()) { + return; + } // process.execArgv - process->Set(context, - FIXED_ONE_BYTE_STRING(isolate, "execArgv"), - ToV8Value(context, env->exec_argv()) - .ToLocalChecked()).Check(); + if (!ToV8Value(context, env->exec_argv()).ToLocal(&val) || + !process->Set(context, FIXED_ONE_BYTE_STRING(isolate, "execArgv"), val) + .IsJust()) { + return; + } READONLY_PROPERTY(process, "pid", Integer::New(isolate, uv_os_getpid())); - CHECK(process - ->SetNativeDataProperty(context, - FIXED_ONE_BYTE_STRING(isolate, "ppid"), - GetParentProcessId, - nullptr, - Local(), - None, - SideEffectType::kHasNoSideEffect) - .FromJust()); + if (!process + ->SetNativeDataProperty(context, + FIXED_ONE_BYTE_STRING(isolate, "ppid"), + GetParentProcessId, + nullptr, + Local(), + None, + SideEffectType::kHasNoSideEffect) + .IsJust()) { + return; + } // --security-revert flags #define V(code, _, __) \ @@ -230,27 +235,25 @@ void PatchProcessObject(const FunctionCallbackInfo& args) { #undef V // process.execPath - process - ->Set(context, - FIXED_ONE_BYTE_STRING(isolate, "execPath"), - String::NewFromUtf8(isolate, - env->exec_path().c_str(), - NewStringType::kInternalized, - env->exec_path().size()) - .ToLocalChecked()) - .Check(); + if (!ToV8Value(context, env->exec_path(), isolate).ToLocal(&val) || + !process->Set(context, FIXED_ONE_BYTE_STRING(isolate, "execPath"), val) + .IsJust()) { + return; + } // process.debugPort - CHECK(process - ->SetNativeDataProperty( - context, - FIXED_ONE_BYTE_STRING(isolate, "debugPort"), - DebugPortGetter, - env->owns_process_state() ? DebugPortSetter : nullptr, - Local(), - None, - SideEffectType::kHasNoSideEffect) - .FromJust()); + if (!process + ->SetNativeDataProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "debugPort"), + DebugPortGetter, + env->owns_process_state() ? DebugPortSetter : nullptr, + Local(), + None, + SideEffectType::kHasNoSideEffect) + .IsJust()) { + return; + } // process.versions Local versions = Object::New(isolate); diff --git a/src/node_report.cc b/src/node_report.cc index 9ab66162ec32a6..c64fc1a2d5f08f 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -439,10 +439,14 @@ static Maybe ErrorToString(Isolate* isolate, } else if (!error->IsObject()) { maybe_str = error->ToString(context); } else if (error->IsObject()) { - MaybeLocal stack = error.As()->Get( - context, FIXED_ONE_BYTE_STRING(isolate, "stack")); - if (!stack.IsEmpty() && stack.ToLocalChecked()->IsString()) { - maybe_str = stack.ToLocalChecked().As(); + Local stack; + if (!error.As() + ->Get(context, FIXED_ONE_BYTE_STRING(isolate, "stack")) + .ToLocal(&stack)) { + return Nothing(); + } + if (stack->IsString()) { + maybe_str = stack.As(); } } diff --git a/src/node_report_module.cc b/src/node_report_module.cc index 7a87a53203dcc7..105e53f81385e3 100644 --- a/src/node_report_module.cc +++ b/src/node_report_module.cc @@ -44,10 +44,12 @@ void WriteReport(const FunctionCallbackInfo& info) { else error = Local(); - filename = TriggerNodeReport(env, *message, *trigger, filename, error); // Return value is the report filename - info.GetReturnValue().Set( - String::NewFromUtf8(isolate, filename.c_str()).ToLocalChecked()); + filename = TriggerNodeReport(env, *message, *trigger, filename, error); + Local ret; + if (ToV8Value(env->context(), filename, env->isolate()).ToLocal(&ret)) { + info.GetReturnValue().Set(ret); + } } // External JavaScript API for returning a report @@ -67,8 +69,10 @@ void GetReport(const FunctionCallbackInfo& info) { GetNodeReport(env, "JavaScript API", __func__, error, out); // Return value is the contents of a report as a string. - info.GetReturnValue().Set( - String::NewFromUtf8(isolate, out.str().c_str()).ToLocalChecked()); + Local ret; + if (ToV8Value(env->context(), out.str(), env->isolate()).ToLocal(&ret)) { + info.GetReturnValue().Set(ret); + } } static void GetCompact(const FunctionCallbackInfo& info) { @@ -110,8 +114,10 @@ static void GetDirectory(const FunctionCallbackInfo& info) { Mutex::ScopedLock lock(per_process::cli_options_mutex); Environment* env = Environment::GetCurrent(info); std::string directory = per_process::cli_options->report_directory; - auto result = String::NewFromUtf8(env->isolate(), directory.c_str()); - info.GetReturnValue().Set(result.ToLocalChecked()); + Local ret; + if (ToV8Value(env->context(), directory, env->isolate()).ToLocal(&ret)) { + info.GetReturnValue().Set(ret); + } } static void SetDirectory(const FunctionCallbackInfo& info) { @@ -126,8 +132,10 @@ static void GetFilename(const FunctionCallbackInfo& info) { Mutex::ScopedLock lock(per_process::cli_options_mutex); Environment* env = Environment::GetCurrent(info); std::string filename = per_process::cli_options->report_filename; - auto result = String::NewFromUtf8(env->isolate(), filename.c_str()); - info.GetReturnValue().Set(result.ToLocalChecked()); + Local ret; + if (ToV8Value(env->context(), filename, env->isolate()).ToLocal(&ret)) { + info.GetReturnValue().Set(ret); + } } static void SetFilename(const FunctionCallbackInfo& info) { @@ -141,8 +149,10 @@ static void SetFilename(const FunctionCallbackInfo& info) { static void GetSignal(const FunctionCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); std::string signal = env->isolate_data()->options()->report_signal; - auto result = String::NewFromUtf8(env->isolate(), signal.c_str()); - info.GetReturnValue().Set(result.ToLocalChecked()); + Local ret; + if (ToV8Value(env->context(), signal, env->isolate()).ToLocal(&ret)) { + info.GetReturnValue().Set(ret); + } } static void SetSignal(const FunctionCallbackInfo& info) { diff --git a/src/node_serdes.cc b/src/node_serdes.cc index 7a70997bc024ef..245a524907da1a 100644 --- a/src/node_serdes.cc +++ b/src/node_serdes.cc @@ -103,67 +103,69 @@ SerializerContext::SerializerContext(Environment* env, Local wrap) void SerializerContext::ThrowDataCloneError(Local message) { Local args[1] = { message }; - Local get_data_clone_error = - object()->Get(env()->context(), - env()->get_data_clone_error_string()) - .ToLocalChecked(); + Local get_data_clone_error; + if (!object() + ->Get(env()->context(), env()->get_data_clone_error_string()) + .ToLocal(&get_data_clone_error)) { + // A superseding error will have been thrown by v8. + return; + } CHECK(get_data_clone_error->IsFunction()); - MaybeLocal error = - get_data_clone_error.As()->Call(env()->context(), - object(), - arraysize(args), - args); - - if (error.IsEmpty()) return; - - env()->isolate()->ThrowException(error.ToLocalChecked()); + Local error; + if (get_data_clone_error.As() + ->Call(env()->context(), object(), arraysize(args), args) + .ToLocal(&error)) { + env()->isolate()->ThrowException(error); + } } Maybe SerializerContext::GetSharedArrayBufferId( Isolate* isolate, Local shared_array_buffer) { Local args[1] = { shared_array_buffer }; - Local get_shared_array_buffer_id = - object()->Get(env()->context(), - env()->get_shared_array_buffer_id_string()) - .ToLocalChecked(); + Local get_shared_array_buffer_id; + if (!object() + ->Get(env()->context(), env()->get_shared_array_buffer_id_string()) + .ToLocal(&get_shared_array_buffer_id)) { + return Nothing(); + } if (!get_shared_array_buffer_id->IsFunction()) { return ValueSerializer::Delegate::GetSharedArrayBufferId( isolate, shared_array_buffer); } - MaybeLocal id = - get_shared_array_buffer_id.As()->Call(env()->context(), - object(), - arraysize(args), - args); - - if (id.IsEmpty()) return Nothing(); + Local id; + if (!get_shared_array_buffer_id.As() + ->Call(env()->context(), object(), arraysize(args), args) + .ToLocal(&id)) { + return Nothing(); + } - return id.ToLocalChecked()->Uint32Value(env()->context()); + return id->Uint32Value(env()->context()); } Maybe SerializerContext::WriteHostObject(Isolate* isolate, Local input) { - MaybeLocal ret; Local args[1] = { input }; - Local write_host_object = - object()->Get(env()->context(), - env()->write_host_object_string()).ToLocalChecked(); + Local write_host_object; + if (!object() + ->Get(env()->context(), env()->write_host_object_string()) + .ToLocal(&write_host_object)) { + return Nothing(); + } if (!write_host_object->IsFunction()) { return ValueSerializer::Delegate::WriteHostObject(isolate, input); } - ret = write_host_object.As()->Call(env()->context(), - object(), - arraysize(args), - args); - - if (ret.IsEmpty()) + Local ret; + if (!write_host_object.As() + ->Call(env()->context(), object(), arraysize(args), args) + .ToLocal(&ret)) { return Nothing(); + } return Just(true); } @@ -209,12 +211,10 @@ void SerializerContext::ReleaseBuffer(const FunctionCallbackInfo& args) { // Note: Both ValueSerializer and this Buffer::New() variant use malloc() // as the underlying allocator. std::pair ret = ctx->serializer_.Release(); - auto buf = Buffer::New(ctx->env(), - reinterpret_cast(ret.first), - ret.second); - - if (!buf.IsEmpty()) { - args.GetReturnValue().Set(buf.ToLocalChecked()); + Local buf; + if (Buffer::New(ctx->env(), reinterpret_cast(ret.first), ret.second) + .ToLocal(&buf)) { + args.GetReturnValue().Set(buf); } } @@ -295,31 +295,31 @@ DeserializerContext::DeserializerContext(Environment* env, } MaybeLocal DeserializerContext::ReadHostObject(Isolate* isolate) { - Local read_host_object = - object()->Get(env()->context(), - env()->read_host_object_string()).ToLocalChecked(); + Local read_host_object; + if (!object() + ->Get(env()->context(), env()->read_host_object_string()) + .ToLocal(&read_host_object)) { + return {}; + } if (!read_host_object->IsFunction()) { return ValueDeserializer::Delegate::ReadHostObject(isolate); } Isolate::AllowJavascriptExecutionScope allow_js(isolate); - MaybeLocal ret = - read_host_object.As()->Call(env()->context(), - object(), - 0, - nullptr); - - if (ret.IsEmpty()) - return MaybeLocal(); + Local ret; + if (!read_host_object.As() + ->Call(env()->context(), object(), 0, nullptr) + .ToLocal(&ret)) { + return {}; + } - Local return_value = ret.ToLocalChecked(); - if (!return_value->IsObject()) { + if (!ret->IsObject()) { env()->ThrowTypeError("readHostObject must return an object"); - return MaybeLocal(); + return {}; } - return return_value.As(); + return ret.As(); } void DeserializerContext::New(const FunctionCallbackInfo& args) { @@ -350,9 +350,10 @@ void DeserializerContext::ReadValue(const FunctionCallbackInfo& args) { DeserializerContext* ctx; ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This()); - MaybeLocal ret = ctx->deserializer_.ReadValue(ctx->env()->context()); - - if (!ret.IsEmpty()) args.GetReturnValue().Set(ret.ToLocalChecked()); + Local ret; + if (ctx->deserializer_.ReadValue(ctx->env()->context()).ToLocal(&ret)) { + args.GetReturnValue().Set(ret); + } } void DeserializerContext::TransferArrayBuffer( diff --git a/src/node_trace_events.cc b/src/node_trace_events.cc index 9787b14352753c..ef659f1c39f7ee 100644 --- a/src/node_trace_events.cc +++ b/src/node_trace_events.cc @@ -24,7 +24,6 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Isolate; using v8::Local; -using v8::NewStringType; using v8::Object; using v8::String; using v8::Uint8Array; @@ -107,12 +106,10 @@ void GetEnabledCategories(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); std::string categories = GetTracingAgentWriter()->agent()->GetEnabledCategories(); - if (!categories.empty()) { - args.GetReturnValue().Set( - String::NewFromUtf8(env->isolate(), - categories.c_str(), - NewStringType::kNormal, - categories.size()).ToLocalChecked()); + Local ret; + if (!categories.empty() && + ToV8Value(env->context(), categories, env->isolate()).ToLocal(&ret)) { + args.GetReturnValue().Set(ret); } } diff --git a/src/node_v8.cc b/src/node_v8.cc index a7f0ba7973498e..3458c16ef41e7d 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -409,11 +409,10 @@ void GCProfiler::Stop(const FunctionCallbackInfo& args) { profiler->writer()->json_end(); profiler->state = GCProfiler::GCProfilerState::kStopped; auto string = profiler->out_stream()->str(); - args.GetReturnValue().Set(String::NewFromUtf8(env->isolate(), - string.data(), - v8::NewStringType::kNormal, - string.size()) - .ToLocalChecked()); + Local ret; + if (ToV8Value(env->context(), string, env->isolate()).ToLocal(&ret)) { + args.GetReturnValue().Set(ret); + } } void Initialize(Local target,