Skip to content

Commit

Permalink
src: move more stuff over to use Maybe<void>
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Sep 24, 2024
1 parent b8493a5 commit d7f1948
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 57 deletions.
42 changes: 23 additions & 19 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::MaybeLocal;
Expand Down Expand Up @@ -635,7 +636,7 @@ void ProtoThrower(const FunctionCallbackInfo<Value>& info) {

// This runs at runtime, regardless of whether the context
// is created from a snapshot.
Maybe<bool> InitializeContextRuntime(Local<Context> context) {
Maybe<void> InitializeContextRuntime(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
HandleScope handle_scope(isolate);

Expand All @@ -653,7 +654,7 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
Boolean::New(isolate, is_code_generation_from_strings_allowed));

if (per_process::cli_options->disable_proto == "") {
return Just(true);
return JustVoid();
}

// Remove __proto__
Expand All @@ -669,14 +670,14 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
if (!context->Global()
->Get(context, object_string)
.ToLocal(&object_v)) {
return Nothing<bool>();
return Nothing<void>();
}

Local<Value> prototype_v;
if (!object_v.As<Object>()
->Get(context, prototype_string)
.ToLocal(&prototype_v)) {
return Nothing<bool>();
return Nothing<void>();
}

prototype = prototype_v.As<Object>();
Expand All @@ -689,13 +690,13 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
if (prototype
->Delete(context, proto_string)
.IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}
} else if (per_process::cli_options->disable_proto == "throw") {
Local<Value> thrower;
if (!Function::New(context, ProtoThrower)
.ToLocal(&thrower)) {
return Nothing<bool>();
return Nothing<void>();
}

PropertyDescriptor descriptor(thrower, thrower);
Expand All @@ -704,17 +705,17 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
if (prototype
->DefineProperty(context, proto_string, descriptor)
.IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}
} else if (per_process::cli_options->disable_proto != "") {
// Validated in ProcessGlobalArgs
UNREACHABLE("invalid --disable-proto mode");
}

return Just(true);
return JustVoid();
}

Maybe<bool> InitializeBaseContextForSnapshot(Local<Context> context) {
Maybe<void> InitializeBaseContextForSnapshot(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
HandleScope handle_scope(isolate);

Expand All @@ -728,18 +729,18 @@ Maybe<bool> InitializeBaseContextForSnapshot(Local<Context> context) {

Local<Value> intl_v;
if (!context->Global()->Get(context, intl_string).ToLocal(&intl_v)) {
return Nothing<bool>();
return Nothing<void>();
}

if (intl_v->IsObject() &&
intl_v.As<Object>()->Delete(context, break_iter_string).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}
}
return Just(true);
return JustVoid();
}

Maybe<bool> InitializeMainContextForSnapshot(Local<Context> context) {
Maybe<void> InitializeMainContextForSnapshot(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
HandleScope handle_scope(isolate);

Expand All @@ -750,12 +751,12 @@ Maybe<bool> InitializeMainContextForSnapshot(Local<Context> context) {
ContextEmbedderIndex::kAllowCodeGenerationFromStrings, True(isolate));

if (InitializeBaseContextForSnapshot(context).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}
return InitializePrimordials(context);
}

Maybe<bool> InitializePrimordials(Local<Context> context) {
Maybe<void> InitializePrimordials(Local<Context> context) {
// Run per-context JS files.
Isolate* isolate = context->GetIsolate();
Context::Scope context_scope(context);
Expand All @@ -769,7 +770,7 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
if (primordials->SetPrototype(context, Null(isolate)).IsNothing() ||
!GetPerContextExports(context).ToLocal(&exports) ||
exports->Set(context, primordials_string, primordials).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

static const char* context_files[] = {"internal/per_context/primordials",
Expand All @@ -793,11 +794,11 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
context, *module, arraysize(arguments), arguments, nullptr)
.IsEmpty()) {
// Execution failed during context creation.
return Nothing<bool>();
return Nothing<void>();
}
}

return Just(true);
return JustVoid();
}

// This initializes the main context (i.e. vm contexts are not included).
Expand All @@ -806,7 +807,10 @@ Maybe<bool> InitializeContext(Local<Context> context) {
return Nothing<bool>();
}

return InitializeContextRuntime(context);
if (InitializeContextRuntime(context).IsNothing()) {
return Nothing<bool>();
}
return Just(true);
}

uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {
Expand Down
4 changes: 2 additions & 2 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ Maybe<bool> EmitProcessBeforeExit(Environment* env) {
Local<Integer> exit_code = Integer::New(
isolate, static_cast<int32_t>(env->exit_code(ExitCode::kNoFailure)));

return ProcessEmit(env, "beforeExit", exit_code).IsEmpty() ?
Nothing<bool>() : Just(true);
return ProcessEmit(env, "beforeExit", exit_code).IsEmpty() ? Nothing<bool>()
: Just(true);
}

static ExitCode EmitExitInternal(Environment* env) {
Expand Down
5 changes: 3 additions & 2 deletions src/base_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::Object;
Expand Down Expand Up @@ -110,9 +111,9 @@ Maybe<std::vector<BaseObjectPtr<BaseObject>>> BaseObject::NestedTransferables()
return Just(std::vector<BaseObjectPtr<BaseObject>>{});
}

Maybe<bool> BaseObject::FinalizeTransferRead(Local<Context> context,
Maybe<void> BaseObject::FinalizeTransferRead(Local<Context> context,
ValueDeserializer* deserializer) {
return Just(true);
return JustVoid();
}

BaseObject::PointerData* BaseObject::pointer_data() {
Expand Down
2 changes: 1 addition & 1 deletion src/base_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class BaseObject : public MemoryRetainer {
virtual std::unique_ptr<worker::TransferData> CloneForMessaging() const;
virtual v8::Maybe<std::vector<BaseObjectPtrImpl<BaseObject, false>>>
NestedTransferables() const;
virtual v8::Maybe<bool> FinalizeTransferRead(
virtual v8::Maybe<void> FinalizeTransferRead(
v8::Local<v8::Context> context, v8::ValueDeserializer* deserializer);

// Indicates whether this object is expected to use a strong reference during
Expand Down
8 changes: 4 additions & 4 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1480,13 +1480,13 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {

switch (order) {
case DNS_ORDER_IPV4_FIRST:
if (add(true, false).IsNothing()) return;
if (add(false, true).IsNothing()) return;
if (add(true, false).IsNothing() || add(false, true).IsNothing())
return;

break;
case DNS_ORDER_IPV6_FIRST:
if (add(false, true).IsNothing()) return;
if (add(true, false).IsNothing()) return;
if (add(false, true).IsNothing() || add(true, false).IsNothing())
return;

break;
default:
Expand Down
7 changes: 3 additions & 4 deletions src/node_env_var.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ using v8::HandleScope;
using v8::Integer;
using v8::Intercepted;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
Expand Down Expand Up @@ -320,7 +319,7 @@ Maybe<void> KVStore::AssignFromObject(Local<Context> context,

// TODO(bnoordhuis) Not super efficient but called infrequently. Not worth
// the trouble yet of specializing for RealEnvStore and MapKVStore.
Maybe<bool> KVStore::AssignToObject(v8::Isolate* isolate,
Maybe<void> KVStore::AssignToObject(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> object) {
HandleScope scope(isolate);
Expand All @@ -333,9 +332,9 @@ Maybe<bool> KVStore::AssignToObject(v8::Isolate* isolate,
ok = ok && key->IsString();
ok = ok && Get(isolate, key.As<String>()).ToLocal(&value);
ok = ok && object->Set(context, key, value).To(&ok);
if (!ok) return Nothing<bool>();
if (!ok) return Nothing<void>();
}
return Just(true);
return JustVoid();
}

static Intercepted EnvGetter(Local<Name> property,
Expand Down
6 changes: 3 additions & 3 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ void SignalExit(int signal, siginfo_t* info, void* ucontext);
std::string GetProcessTitle(const char* default_title);
std::string GetHumanReadableProcessName();

v8::Maybe<bool> InitializeBaseContextForSnapshot(
v8::Maybe<void> InitializeBaseContextForSnapshot(
v8::Local<v8::Context> context);
v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
v8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);
v8::Maybe<void> InitializeContextRuntime(v8::Local<v8::Context> context);
v8::Maybe<void> InitializePrimordials(v8::Local<v8::Context> context);

class NodeArrayBufferAllocator : public ArrayBufferAllocator {
public:
Expand Down
19 changes: 12 additions & 7 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ using v8::Global;
using v8::HandleScope;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::MaybeLocal;
Expand Down Expand Up @@ -337,7 +338,11 @@ class SerializerDelegate : public ValueSerializer::Delegate {
// methods like toString(). It's probably confusing if that gets lost
// in transmission.
Local<Object> normal_object = Object::New(isolate);
env_->env_vars()->AssignToObject(isolate, env_->context(), normal_object);
if (env_->env_vars()
->AssignToObject(isolate, env_->context(), normal_object)
.IsNothing()) {
return Nothing<bool>();
}
serializer->WriteUint32(kNormalObject); // Instead of a BaseObject.
return serializer->WriteValue(env_->context(), normal_object);
}
Expand Down Expand Up @@ -1390,25 +1395,25 @@ JSTransferable::NestedTransferables() const {
return Just(ret);
}

Maybe<bool> JSTransferable::FinalizeTransferRead(
Maybe<void> JSTransferable::FinalizeTransferRead(
Local<Context> context, ValueDeserializer* deserializer) {
// Call `this[kDeserialize](data)` where `data` comes from the return value
// of `this[kTransfer]()` or `this[kClone]()`.
HandleScope handle_scope(env()->isolate());
Local<Value> data;
if (!deserializer->ReadValue(context).ToLocal(&data)) return Nothing<bool>();
if (!deserializer->ReadValue(context).ToLocal(&data)) return Nothing<void>();

Local<Symbol> method_name = env()->messaging_deserialize_symbol();
Local<Value> method;
if (!target()->Get(context, method_name).ToLocal(&method)) {
return Nothing<bool>();
return Nothing<void>();
}
if (!method->IsFunction()) return Just(true);
if (!method->IsFunction()) return JustVoid();

if (method.As<Function>()->Call(context, target(), 1, &data).IsEmpty()) {
return Nothing<bool>();
return Nothing<void>();
}
return Just(true);
return JustVoid();
}

JSTransferable::Data::Data(std::string&& deserialize_info,
Expand Down
2 changes: 1 addition & 1 deletion src/node_messaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class JSTransferable : public BaseObject {
std::unique_ptr<TransferData> CloneForMessaging() const override;
v8::Maybe<std::vector<BaseObjectPtr<BaseObject>>>
NestedTransferables() const override;
v8::Maybe<bool> FinalizeTransferRead(
v8::Maybe<void> FinalizeTransferRead(
v8::Local<v8::Context> context,
v8::ValueDeserializer* deserializer) override;

Expand Down
Loading

0 comments on commit d7f1948

Please sign in to comment.