Save the prototype of EventEmitter

This commit is contained in:
Cheng Zhao 2016-08-02 18:07:58 +09:00
Родитель 892026458d
Коммит 895b8b47ee
3 изменённых файлов: 40 добавлений и 0 удалений

Просмотреть файл

@ -10,10 +10,15 @@
#include "native_mate/object_template_builder.h"
#include "ui/events/event_constants.h"
#include "atom/common/node_includes.h"
namespace mate {
namespace {
// The prototype of Node's EventEmitter.
v8::Persistent<v8::Object> g_event_emitter_prototype;
v8::Persistent<v8::ObjectTemplate> event_template;
void PreventDefault(mate::Arguments* args) {
@ -75,6 +80,29 @@ v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags) {
return obj.GetHandle();
}
void InheritFromEventEmitter(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> constructor) {
}
void SetEventEmitterPrototype(v8::Isolate* isolate,
v8::Local<v8::Object> prototype) {
g_event_emitter_prototype.Reset(isolate, prototype);
}
} // namespace internal
} // namespace mate
namespace {
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
mate::Dictionary(context->GetIsolate(), exports)
.SetMethod("setEventEmitterPrototype",
&mate::internal::SetEventEmitterPrototype);
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_event_emitter, Initialize)

Просмотреть файл

@ -32,6 +32,9 @@ v8::Local<v8::Object> CreateCustomEvent(
v8::Local<v8::Object> event);
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
void InheritFromEventEmitter(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> constructor);
} // namespace internal
// Provide helperers to emit event in JavaScript.
@ -87,6 +90,11 @@ class EventEmitter : public Wrappable<T> {
protected:
EventEmitter() {}
static void InheritFromEventEmitter(
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> constructor) {
internal::InheritFromEventEmitter(isolate, constructor);
}
private:
// this.emit(name, event, args...);
template<typename... Args>

Просмотреть файл

@ -6,6 +6,10 @@ const util = require('util')
const Module = require('module')
const v8 = require('v8')
// Save the prototype of EventEmitter, must be called before using native API.
const {setEventEmitterPrototype} = process.binding('atom_browser_event_emitter')
setEventEmitterPrototype(require('events').EventEmitter)
// We modified the original process.argv to let node.js load the atom.js,
// we need to restore it here.
process.argv.splice(1, 1)