mirror of
https://github.com/electron/electron.git
synced 2026-05-02 03:00:22 -04:00
chore: stop leaking v8 environment (#22761)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "shell/common/gin_helper/destroyable.h"
|
||||
|
||||
#include "base/no_destructor.h"
|
||||
#include "gin/converter.h"
|
||||
#include "shell/common/gin_helper/wrappable_base.h"
|
||||
|
||||
@@ -11,9 +12,15 @@ namespace gin_helper {
|
||||
|
||||
namespace {
|
||||
|
||||
// Cached function templates, leaked on exit. (They are leaked in V8 anyway.)
|
||||
v8::Global<v8::FunctionTemplate> g_destroy_func;
|
||||
v8::Global<v8::FunctionTemplate> g_is_destroyed_func;
|
||||
v8::Global<v8::FunctionTemplate>* GetDestroyFunc() {
|
||||
static base::NoDestructor<v8::Global<v8::FunctionTemplate>> destroy_func;
|
||||
return destroy_func.get();
|
||||
}
|
||||
|
||||
v8::Global<v8::FunctionTemplate>* GetIsDestroyedFunc() {
|
||||
static base::NoDestructor<v8::Global<v8::FunctionTemplate>> is_destroyed_func;
|
||||
return is_destroyed_func.get();
|
||||
}
|
||||
|
||||
void DestroyFunc(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
||||
v8::Local<v8::Object> holder = info.Holder();
|
||||
@@ -45,22 +52,22 @@ bool Destroyable::IsDestroyed(v8::Local<v8::Object> object) {
|
||||
void Destroyable::MakeDestroyable(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype) {
|
||||
// Cache the FunctionTemplate of "destroy" and "isDestroyed".
|
||||
if (g_destroy_func.IsEmpty()) {
|
||||
if (GetDestroyFunc()->IsEmpty()) {
|
||||
auto templ = v8::FunctionTemplate::New(isolate, DestroyFunc);
|
||||
templ->RemovePrototype();
|
||||
g_destroy_func.Reset(isolate, templ);
|
||||
GetDestroyFunc()->Reset(isolate, templ);
|
||||
templ = v8::FunctionTemplate::New(isolate, IsDestroyedFunc);
|
||||
templ->RemovePrototype();
|
||||
g_is_destroyed_func.Reset(isolate, templ);
|
||||
GetIsDestroyedFunc()->Reset(isolate, templ);
|
||||
}
|
||||
|
||||
auto proto_templ = prototype->PrototypeTemplate();
|
||||
proto_templ->Set(
|
||||
gin::StringToSymbol(isolate, "destroy"),
|
||||
v8::Local<v8::FunctionTemplate>::New(isolate, g_destroy_func));
|
||||
v8::Local<v8::FunctionTemplate>::New(isolate, *GetDestroyFunc()));
|
||||
proto_templ->Set(
|
||||
gin::StringToSymbol(isolate, "isDestroyed"),
|
||||
v8::Local<v8::FunctionTemplate>::New(isolate, g_is_destroyed_func));
|
||||
v8::Local<v8::FunctionTemplate>::New(isolate, *GetIsDestroyedFunc()));
|
||||
}
|
||||
|
||||
} // namespace gin_helper
|
||||
|
||||
Reference in New Issue
Block a user