diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index 5210d8a581c627..8cd39e091242bf 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -472,7 +472,7 @@ void StartProfilers(Environment* env) { }, env); std::string coverage_str = - env->env_vars()->Get("NODE_V8_COVERAGE").FromMaybe(std::string()); + env->env_vars()->Get("NODE_V8_COVERAGE").value_or(std::string()); if (!coverage_str.empty() || env->options()->test_runner_coverage) { CHECK_NULL(env->coverage_connection()); env->set_coverage_connection(std::make_unique(env)); diff --git a/src/node_credentials.cc b/src/node_credentials.cc index 80605c92012c31..6333c64231d30f 100644 --- a/src/node_credentials.cc +++ b/src/node_credentials.cc @@ -91,7 +91,10 @@ bool SafeGetenv(const char* key, env_vars = per_process::system_environment; } - return env_vars->Get(key).To(text); + std::optional value = env_vars->Get(key); + if (!value.has_value()) return false; + *text = value.value(); + return true; } static void SafeGetenv(const FunctionCallbackInfo& args) { diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 8bc027b24c718f..1cb57fcaea2628 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -52,7 +52,7 @@ void Dotenv::SetEnvironment(node::Environment* env) { auto existing = env->env_vars()->Get(key.data()); - if (existing.IsNothing()) { + if (!existing.has_value()) { env->env_vars()->Set( isolate, v8::String::NewFromUtf8( diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 85f82180d48d6c..fdf86f17d460f5 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -6,6 +6,7 @@ #include "node_process-inl.h" #include // tzset(), _tzset() +#include namespace node { using v8::Array; @@ -19,6 +20,7 @@ using v8::Integer; using v8::Intercepted; using v8::Isolate; using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; using v8::MaybeLocal; @@ -38,7 +40,7 @@ using v8::Value; class RealEnvStore final : public KVStore { public: MaybeLocal Get(Isolate* isolate, Local key) const override; - Maybe Get(const char* key) const override; + std::optional Get(const char* key) const override; void Set(Isolate* isolate, Local key, Local value) override; int32_t Query(Isolate* isolate, Local key) const override; int32_t Query(const char* key) const override; @@ -49,7 +51,7 @@ class RealEnvStore final : public KVStore { class MapKVStore final : public KVStore { public: MaybeLocal Get(Isolate* isolate, Local key) const override; - Maybe Get(const char* key) const override; + std::optional Get(const char* key) const override; void Set(Isolate* isolate, Local key, Local value) override; int32_t Query(Isolate* isolate, Local key) const override; int32_t Query(const char* key) const override; @@ -101,7 +103,7 @@ void DateTimeConfigurationChangeNotification( } } -Maybe RealEnvStore::Get(const char* key) const { +std::optional RealEnvStore::Get(const char* key) const { Mutex::ScopedLock lock(per_process::env_var_mutex); size_t init_sz = 256; @@ -116,19 +118,19 @@ Maybe RealEnvStore::Get(const char* key) const { } if (ret >= 0) { // Env key value fetch success. - return Just(std::string(*val, init_sz)); + return std::string(*val, init_sz); } - return Nothing(); + return std::nullopt; } MaybeLocal RealEnvStore::Get(Isolate* isolate, Local property) const { node::Utf8Value key(isolate, property); - Maybe value = Get(*key); + std::optional value = Get(*key); - if (value.IsJust()) { - std::string val = value.FromJust(); + if (value.has_value()) { + std::string val = value.value(); return String::NewFromUtf8( isolate, val.data(), NewStringType::kNormal, val.size()); } @@ -229,17 +231,17 @@ std::shared_ptr KVStore::Clone(Isolate* isolate) const { return copy; } -Maybe MapKVStore::Get(const char* key) const { +std::optional MapKVStore::Get(const char* key) const { Mutex::ScopedLock lock(mutex_); auto it = map_.find(key); - return it == map_.end() ? Nothing() : Just(it->second); + return it == map_.end() ? std::nullopt : std::make_optional(it->second); } MaybeLocal MapKVStore::Get(Isolate* isolate, Local key) const { Utf8Value str(isolate, key); - Maybe value = Get(*str); - if (value.IsNothing()) return Local(); - std::string val = value.FromJust(); + std::optional value = Get(*str); + if (!value.has_value()) return MaybeLocal(); + std::string val = value.value(); return String::NewFromUtf8( isolate, val.data(), NewStringType::kNormal, val.size()); } @@ -291,30 +293,29 @@ std::shared_ptr KVStore::CreateMapKVStore() { return std::make_shared(); } -Maybe KVStore::AssignFromObject(Local context, +Maybe KVStore::AssignFromObject(Local context, Local entries) { Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); Local keys; if (!entries->GetOwnPropertyNames(context).ToLocal(&keys)) - return Nothing(); + return Nothing(); uint32_t keys_length = keys->Length(); for (uint32_t i = 0; i < keys_length; i++) { Local key; - if (!keys->Get(context, i).ToLocal(&key)) - return Nothing(); + if (!keys->Get(context, i).ToLocal(&key)) return Nothing(); if (!key->IsString()) continue; Local value; Local value_string; if (!entries->Get(context, key).ToLocal(&value) || !value->ToString(context).ToLocal(&value_string)) { - return Nothing(); + return Nothing(); } Set(isolate, key.As(), value_string); } - return Just(true); + return JustVoid(); } // TODO(bnoordhuis) Not super efficient but called infrequently. Not worth diff --git a/src/node_worker.cc b/src/node_worker.cc index a946939a88fc58..e8026fe24c7021 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -531,8 +531,12 @@ void Worker::New(const FunctionCallbackInfo& args) { } else if (args[1]->IsObject()) { // User provided env. env_vars = KVStore::CreateMapKVStore(); - env_vars->AssignFromObject(isolate->GetCurrentContext(), - args[1].As()); + if (env_vars + ->AssignFromObject(isolate->GetCurrentContext(), + args[1].As()) + .IsNothing()) { + return; + } } else { // Env is shared. env_vars = env->env_vars(); @@ -542,21 +546,21 @@ void Worker::New(const FunctionCallbackInfo& args) { per_isolate_opts.reset(new PerIsolateOptions()); HandleEnvOptions(per_isolate_opts->per_env, [&env_vars](const char* name) { - return env_vars->Get(name).FromMaybe(""); + return env_vars->Get(name).value_or(""); }); #ifndef NODE_WITHOUT_NODE_OPTIONS - std::string node_options; - if (env_vars->Get("NODE_OPTIONS").To(&node_options)) { + std::optional node_options = env_vars->Get("NODE_OPTIONS"); + if (node_options.has_value()) { std::vector errors{}; std::vector env_argv = - ParseNodeOptionsEnvVar(node_options, &errors); + ParseNodeOptionsEnvVar(node_options.value(), &errors); // [0] is expected to be the program name, add dummy string. env_argv.insert(env_argv.begin(), ""); std::vector invalid_args{}; - std::string parent_node_options; - USE(env->env_vars()->Get("NODE_OPTIONS").To(&parent_node_options)); + std::optional parent_node_options = + env->env_vars()->Get("NODE_OPTIONS"); // If the worker code passes { env: { ...process.env, ... } } or // the NODE_OPTIONS is otherwise character-for-character equal to the diff --git a/src/util.h b/src/util.h index a3fa79f749d94e..a6da8720c499df 100644 --- a/src/util.h +++ b/src/util.h @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -306,7 +307,7 @@ class KVStore { virtual v8::MaybeLocal Get(v8::Isolate* isolate, v8::Local key) const = 0; - virtual v8::Maybe Get(const char* key) const = 0; + virtual std::optional Get(const char* key) const = 0; virtual void Set(v8::Isolate* isolate, v8::Local key, v8::Local value) = 0; @@ -317,7 +318,7 @@ class KVStore { virtual v8::Local Enumerate(v8::Isolate* isolate) const = 0; virtual std::shared_ptr Clone(v8::Isolate* isolate) const; - virtual v8::Maybe AssignFromObject(v8::Local context, + virtual v8::Maybe AssignFromObject(v8::Local context, v8::Local entries); v8::Maybe AssignToObject(v8::Isolate* isolate, v8::Local context,