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 7, 2024
1 parent 8882a21 commit 4fc399d
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 49 deletions.
42 changes: 21 additions & 21 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using v8::Function;
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 +635,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 +653,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 +669,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 +689,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 +704,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 +728,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 +750,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 +769,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,17 +793,17 @@ 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).
Maybe<bool> InitializeContext(Local<Context> context) {
Maybe<void> InitializeContext(Local<Context> context) {
if (InitializeMainContextForSnapshot(context).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

return InitializeContextRuntime(context);
Expand Down
9 changes: 5 additions & 4 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using v8::HandleScope;
using v8::Integer;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::NewStringType;
Expand All @@ -30,7 +31,7 @@ void EmitBeforeExit(Environment* env) {
USE(EmitProcessBeforeExit(env));
}

Maybe<bool> EmitProcessBeforeExit(Environment* env) {
Maybe<void> EmitProcessBeforeExit(Environment* env) {
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "BeforeExit");
if (!env->destroy_async_id_list()->empty())
AsyncWrap::DestroyAsyncIdsCallback(env);
Expand All @@ -40,14 +41,14 @@ Maybe<bool> EmitProcessBeforeExit(Environment* env) {
Context::Scope context_scope(env->context());

if (!env->can_call_into_js()) {
return Nothing<bool>();
return Nothing<void>();
}

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<void>()
: JustVoid();
}

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
4 changes: 2 additions & 2 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ NODE_EXTERN v8::Local<v8::Context> NewContext(

// Runs Node.js-specific tweaks on an already constructed context
// Return value indicates success of operation
NODE_EXTERN v8::Maybe<bool> InitializeContext(v8::Local<v8::Context> context);
NODE_EXTERN v8::Maybe<void> InitializeContext(v8::Local<v8::Context> context);

// If `platform` is passed, it will be used to register new Worker instances.
// It can be `nullptr`, in which case creating new Workers inside of
Expand Down Expand Up @@ -850,7 +850,7 @@ NODE_EXTERN void SetTracingController(v8::TracingController* controller);

// Run `process.emit('beforeExit')` as it would usually happen when Node.js is
// run in standalone mode.
NODE_EXTERN v8::Maybe<bool> EmitProcessBeforeExit(Environment* env);
NODE_EXTERN v8::Maybe<void> EmitProcessBeforeExit(Environment* env);
NODE_DEPRECATED("Use Maybe version (EmitProcessBeforeExit) instead",
NODE_EXTERN void EmitBeforeExit(Environment* env));
// Run `process.emit('exit')` as it would usually happen when Node.js is run
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 @@ -1388,25 +1393,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
6 changes: 3 additions & 3 deletions src/string_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ class StringBytes {
public:
class InlineDecoder : public MaybeStackBuffer<char> {
public:
inline v8::Maybe<bool> Decode(Environment* env,
inline v8::Maybe<void> Decode(Environment* env,
v8::Local<v8::String> string,
enum encoding enc) {
size_t storage;
if (!StringBytes::StorageSize(env->isolate(), string, enc).To(&storage))
return v8::Nothing<bool>();
return v8::Nothing<void>();
AllocateSufficientStorage(storage);
const size_t length =
StringBytes::Write(env->isolate(), out(), storage, string, enc);

// No zero terminator is included when using this method.
SetLength(length);
return v8::Just(true);
return v8::JustVoid();
}

inline size_t size() const { return length(); }
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class KVStore {
virtual std::shared_ptr<KVStore> Clone(v8::Isolate* isolate) const;
virtual v8::Maybe<void> AssignFromObject(v8::Local<v8::Context> context,
v8::Local<v8::Object> entries);
v8::Maybe<bool> AssignToObject(v8::Isolate* isolate,
v8::Maybe<void> AssignToObject(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> object);

Expand Down

0 comments on commit 4fc399d

Please sign in to comment.