electron/shell/common/api/electron_api_environment.cc
trop[bot] d426b92326
refactor: avoid deprecated v8::Context::GetIsolate() calls (pt 1) (#47843)
* refactor: avoid redundant GetIsolate() calls in NodeBindings::CreateEnvironment()

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

refactor: prefer JavascriptEnvironment::GetIsolate() in the browser layer

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2025-07-21 13:12:24 -04: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)