electron/shell/common/api/electron_api_environment.cc
Charles Kerr 39cca586f6
refactor: avoid deprecated v8::Context::GetIsolate() calls (pt 1) (#47760)
* refactor: avoid redundant GetIsolate() calls in NodeBindings::CreateEnvironment()

Xref: https://chromium-review.googlesource.com/c/v8/v8/+/6563615

* refactor: use v8::Isolate::GetCurrent() in Initialize() methods

* refactor: add v8::Isolate* arg to NodeBindings::CreateEnvironment()

* fixup! refactor: use v8::Isolate::GetCurrent() in Initialize() methods

* refactor: add v8::Isolate* arg to RendererClientBase::DidCreateScriptContext()

* fixup! refactor: add v8::Isolate* arg to NodeBindings::CreateEnvironment()

* fixup! fixup! refactor: use v8::Isolate::GetCurrent() in Initialize() methods

refactor: prefer JavascriptEnvironment::GetIsolate() in the browser layer
2025-07-21 16:34:38 +02:00

41 lines
1.2 KiB
C++

// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "base/environment.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace {
v8::Local<v8::Value> GetVar(v8::Isolate* isolate, const std::string& name) {
if (std::optional<std::string> value =
base::Environment::Create()->GetVar(name)) {
return gin::StringToV8(isolate, *value);
} else {
return v8::Null(isolate);
}
}
bool HasVar(const std::string& name) {
return base::Environment::Create()->HasVar(name);
}
bool SetVar(const std::string& name, const std::string& value) {
return base::Environment::Create()->SetVar(name, value);
}
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* const isolate = v8::Isolate::GetCurrent();
gin_helper::Dictionary dict{isolate, exports};
dict.SetMethod("getVar", &GetVar);
dict.SetMethod("hasVar", &HasVar);
dict.SetMethod("setVar", &SetVar);
}
} // namespace
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_environment, Initialize)