Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: EventEmitters without gin_helper (#22726)
- Loading branch information
Showing
12 changed files
with
277 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
patches/chromium/gin_allow_passing_an_objecttemplate_to_objecttemplatebuilder.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Jeremy Apthorp <jeremya@chromium.org> | ||
Date: Tue, 17 Mar 2020 12:51:02 -0700 | ||
Subject: gin: allow passing an ObjectTemplate to ObjectTemplateBuilder | ||
|
||
This allows us to pass an InstanceTemplate from a FunctionTemplate we | ||
construct ourselves. In particular, this means we can customize the | ||
prototype chain of the object. | ||
|
||
diff --git a/gin/object_template_builder.cc b/gin/object_template_builder.cc | ||
index 543cd694e7becec72c2e48b9f235ad02da777df8..dbc753ce357b4c18a3180528dcb3aafb86041428 100644 | ||
--- a/gin/object_template_builder.cc | ||
+++ b/gin/object_template_builder.cc | ||
@@ -143,11 +143,15 @@ void IndexedPropertyEnumerator( | ||
ObjectTemplateBuilder::ObjectTemplateBuilder(v8::Isolate* isolate) | ||
: ObjectTemplateBuilder(isolate, nullptr) {} | ||
|
||
+ObjectTemplateBuilder::ObjectTemplateBuilder(v8::Isolate* isolate, const char *type_name) | ||
+ : ObjectTemplateBuilder(isolate, nullptr, v8::ObjectTemplate::New(isolate)) {} | ||
+ | ||
ObjectTemplateBuilder::ObjectTemplateBuilder(v8::Isolate* isolate, | ||
- const char* type_name) | ||
+ const char* type_name, | ||
+ v8::Local<v8::ObjectTemplate> tmpl) | ||
: isolate_(isolate), | ||
type_name_(type_name), | ||
- template_(v8::ObjectTemplate::New(isolate)) { | ||
+ template_(tmpl) { | ||
template_->SetInternalFieldCount(kNumberOfInternalFields); | ||
} | ||
|
||
diff --git a/gin/object_template_builder.h b/gin/object_template_builder.h | ||
index 6fb331cbbfe242cb871edc5eec8ab8e620e0a17d..5ac479026eb040414961fb45913c91c74f2488dc 100644 | ||
--- a/gin/object_template_builder.h | ||
+++ b/gin/object_template_builder.h | ||
@@ -46,6 +46,7 @@ class GIN_EXPORT ObjectTemplateBuilder { | ||
public: | ||
explicit ObjectTemplateBuilder(v8::Isolate* isolate); | ||
ObjectTemplateBuilder(v8::Isolate* isolate, const char* type_name); | ||
+ ObjectTemplateBuilder(v8::Isolate* isolate, const char* type_name, v8::Local<v8::ObjectTemplate> tmpl); | ||
ObjectTemplateBuilder(const ObjectTemplateBuilder& other); | ||
~ObjectTemplateBuilder(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2019 Slack Technologies, Inc. | ||
// Use of this source code is governed by the MIT license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "shell/browser/api/electron_api_event_emitter.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/callback.h" | ||
#include "gin/dictionary.h" | ||
#include "shell/common/gin_converters/callback_converter.h" | ||
#include "shell/common/node_includes.h" | ||
#include "v8/include/v8.h" | ||
|
||
namespace { | ||
|
||
v8::Global<v8::Object> event_emitter_prototype; | ||
|
||
void SetEventEmitterPrototype(v8::Isolate* isolate, | ||
v8::Local<v8::Object> proto) { | ||
event_emitter_prototype.Reset(isolate, proto); | ||
} | ||
|
||
void Initialize(v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value> unused, | ||
v8::Local<v8::Context> context, | ||
void* priv) { | ||
v8::Isolate* isolate = context->GetIsolate(); | ||
|
||
gin::Dictionary dict(isolate, exports); | ||
dict.Set("setEventEmitterPrototype", | ||
base::BindRepeating(&SetEventEmitterPrototype)); | ||
} | ||
|
||
} // namespace | ||
|
||
namespace electron { | ||
|
||
v8::Local<v8::Object> GetEventEmitterPrototype(v8::Isolate* isolate) { | ||
CHECK(!event_emitter_prototype.IsEmpty()); | ||
return event_emitter_prototype.Get(isolate); | ||
} | ||
|
||
} // namespace electron | ||
|
||
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_event_emitter, Initialize) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2019 Slack Technologies, Inc. | ||
// Use of this source code is governed by the MIT license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef SHELL_BROWSER_API_ELECTRON_API_EVENT_EMITTER_H_ | ||
#define SHELL_BROWSER_API_ELECTRON_API_EVENT_EMITTER_H_ | ||
|
||
namespace v8 { | ||
template <typename T> | ||
class Local; | ||
class Object; | ||
class Isolate; | ||
} // namespace v8 | ||
|
||
namespace electron { | ||
|
||
v8::Local<v8::Object> GetEventEmitterPrototype(v8::Isolate* isolate); | ||
|
||
} // namespace electron | ||
|
||
#endif // SHELL_BROWSER_API_ELECTRON_API_EVENT_EMITTER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2019 Slack Technologies, Inc. | ||
// Use of this source code is governed by the MIT license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "shell/browser/event_emitter_mixin.h" | ||
|
||
#include "gin/public/wrapper_info.h" | ||
#include "shell/browser/api/electron_api_event_emitter.h" | ||
|
||
namespace gin_helper { | ||
|
||
namespace internal { | ||
|
||
gin::WrapperInfo kWrapperInfo = {gin::kEmbedderNativeGin}; | ||
|
||
v8::Local<v8::FunctionTemplate> GetEventEmitterTemplate(v8::Isolate* isolate) { | ||
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | ||
v8::Local<v8::FunctionTemplate> tmpl = | ||
data->GetFunctionTemplate(&kWrapperInfo); | ||
|
||
if (tmpl.IsEmpty()) { | ||
tmpl = v8::FunctionTemplate::New(isolate); | ||
v8::Local<v8::Context> context = isolate->GetCurrentContext(); | ||
v8::Local<v8::Function> func = tmpl->GetFunction(context).ToLocalChecked(); | ||
|
||
v8::Local<v8::Object> eventemitter_prototype = | ||
electron::GetEventEmitterPrototype(isolate); | ||
|
||
v8::Local<v8::Value> func_prototype; | ||
CHECK(func->Get(context, gin::StringToSymbol(isolate, "prototype")) | ||
.ToLocal(&func_prototype)); | ||
|
||
CHECK(func_prototype.As<v8::Object>() | ||
->SetPrototype(context, eventemitter_prototype) | ||
.ToChecked()); | ||
|
||
data->SetFunctionTemplate(&kWrapperInfo, tmpl); | ||
} | ||
|
||
return tmpl; | ||
} | ||
|
||
} // namespace internal | ||
|
||
} // namespace gin_helper |
Oops, something went wrong.