Set prototype of constructor directly

This commit is contained in:
Cheng Zhao 2016-08-02 20:38:35 +09:00
parent 8c3232dc56
commit 844f32aa36
19 changed files with 84 additions and 158 deletions

View file

@ -639,6 +639,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
auto command_line = base::CommandLine::ForCurrentProcess();
mate::Dictionary dict(isolate, exports);
dict.Set("App", atom::api::App::GetConstructor(isolate)->GetFunction());
dict.Set("app", atom::api::App::Create(isolate));
dict.SetMethod("appendSwitch", &AppendSwitch);
dict.SetMethod("appendArgument",

View file

@ -122,11 +122,14 @@ void AutoUpdater::BuildPrototype(
namespace {
using atom::api::AutoUpdater;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("autoUpdater", atom::api::AutoUpdater::Create(isolate));
dict.Set("autoUpdater", AutoUpdater::Create(isolate));
dict.Set("AutoUpdater", AutoUpdater::GetConstructor(isolate)->GetFunction());
}
} // namespace

View file

@ -23,14 +23,6 @@ namespace atom {
namespace api {
namespace {
// The wrapDebugger funtion which is implemented in JavaScript.
using WrapDebuggerCallback = base::Callback<void(v8::Local<v8::Value>)>;
WrapDebuggerCallback g_wrap_debugger;
} // namespace
Debugger::Debugger(v8::Isolate* isolate, content::WebContents* web_contents)
: web_contents_(web_contents),
previous_request_id_(0) {
@ -151,10 +143,7 @@ void Debugger::SendCommand(mate::Arguments* args) {
mate::Handle<Debugger> Debugger::Create(
v8::Isolate* isolate,
content::WebContents* web_contents) {
auto handle = mate::CreateHandle(
isolate, new Debugger(isolate, web_contents));
g_wrap_debugger.Run(handle.ToV8());
return handle;
return mate::CreateHandle(isolate, new Debugger(isolate, web_contents));
}
// static
@ -168,21 +157,19 @@ void Debugger::BuildPrototype(v8::Isolate* isolate,
.SetMethod("sendCommand", &Debugger::SendCommand);
}
void SetWrapDebugger(const WrapDebuggerCallback& callback) {
g_wrap_debugger = callback;
}
} // namespace api
} // namespace atom
namespace {
using atom::api::Debugger;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.SetMethod("_setWrapDebugger", &atom::api::SetWrapDebugger);
mate::Dictionary(isolate, exports)
.Set("Debugger", Debugger::GetConstructor(isolate)->GetFunction());
}
} // namespace

View file

@ -51,10 +51,6 @@ namespace api {
namespace {
// The wrapDownloadItem funtion which is implemented in JavaScript
using WrapDownloadItemCallback = base::Callback<void(v8::Local<v8::Value>)>;
WrapDownloadItemCallback g_wrap_download_item;
std::map<uint32_t, v8::Global<v8::Object>> g_download_item_objects;
} // namespace
@ -197,7 +193,6 @@ mate::Handle<DownloadItem> DownloadItem::Create(
return mate::CreateHandle(isolate, static_cast<DownloadItem*>(existing));
auto handle = mate::CreateHandle(isolate, new DownloadItem(isolate, item));
g_wrap_download_item.Run(handle.ToV8());
// Reference this object in case it got garbage collected.
g_download_item_objects[handle->weak_map_id()] =
@ -205,10 +200,6 @@ mate::Handle<DownloadItem> DownloadItem::Create(
return handle;
}
void SetWrapDownloadItem(const WrapDownloadItemCallback& callback) {
g_wrap_download_item = callback;
}
} // namespace api
} // namespace atom
@ -218,8 +209,9 @@ namespace {
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.SetMethod("_setWrapDownloadItem", &atom::api::SetWrapDownloadItem);
mate::Dictionary(isolate, exports)
.Set("DownloadItem",
atom::api::DownloadItem::GetConstructor(isolate)->GetFunction());
}
} // namespace

View file

@ -63,16 +63,19 @@ void PowerMonitor::BuildPrototype(
namespace {
using atom::api::PowerMonitor;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
#if defined(OS_MACOSX)
base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
#endif
using atom::api::PowerMonitor;
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("powerMonitor", PowerMonitor::Create(isolate));
dict.Set("PowerMonitor",
PowerMonitor::GetConstructor(isolate)->GetFunction());
}
} // namespace

View file

@ -129,10 +129,14 @@ void Screen::BuildPrototype(
namespace {
using atom::api::Screen;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
dict.Set("screen", atom::api::Screen::Create(context->GetIsolate()));
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("screen", Screen::Create(isolate));
dict.Set("Screen", Screen::GetConstructor(isolate)->GetFunction());
}
} // namespace

View file

@ -170,10 +170,6 @@ namespace {
const char kPersistPrefix[] = "persist:";
// The wrapSession funtion which is implemented in JavaScript
using WrapSessionCallback = base::Callback<void(v8::Local<v8::Value>)>;
WrapSessionCallback g_wrap_session;
// Referenced session objects.
std::map<uint32_t, v8::Global<v8::Object>> g_sessions;
@ -541,7 +537,6 @@ mate::Handle<Session> Session::CreateFrom(
auto handle = mate::CreateHandle(
isolate, new Session(isolate, browser_context));
g_wrap_session.Run(handle.ToV8());
// The Sessions should never be garbage collected, since the common pattern is
// to use partition strings, instead of using the Session object directly.
@ -596,16 +591,14 @@ void Session::BuildPrototype(v8::Isolate* isolate,
.SetProperty("webRequest", &Session::WebRequest);
}
void SetWrapSession(const WrapSessionCallback& callback) {
g_wrap_session = callback;
}
} // namespace api
} // namespace atom
namespace {
using atom::api::Session;
v8::Local<v8::Value> FromPartition(
const std::string& partition, mate::Arguments* args) {
if (!atom::Browser::Get()->is_ready()) {
@ -614,16 +607,15 @@ v8::Local<v8::Value> FromPartition(
}
base::DictionaryValue options;
args->GetNext(&options);
return atom::api::Session::FromPartition(
args->isolate(), partition, options).ToV8();
return Session::FromPartition(args->isolate(), partition, options).ToV8();
}
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("Session", Session::GetConstructor(isolate)->GetFunction());
dict.SetMethod("fromPartition", &FromPartition);
dict.SetMethod("_setWrapSession", &atom::api::SetWrapSession);
}
} // namespace

View file

@ -69,11 +69,15 @@ void SystemPreferences::BuildPrototype(
namespace {
using atom::api::SystemPreferences;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("systemPreferences", atom::api::SystemPreferences::Create(isolate));
dict.Set("systemPreferences", SystemPreferences::Create(isolate));
dict.Set("SystemPreferences",
SystemPreferences::GetConstructor(isolate)->GetFunction());
}
} // namespace

View file

@ -221,10 +221,6 @@ namespace api {
namespace {
// The wrapWebContents function which is implemented in JavaScript
using WrapWebContentsCallback = base::Callback<void(v8::Local<v8::Value>)>;
WrapWebContentsCallback g_wrap_web_contents;
content::ServiceWorkerContext* GetServiceWorkerContext(
const content::WebContents* web_contents) {
auto context = web_contents->GetBrowserContext();
@ -1470,41 +1466,32 @@ mate::Handle<WebContents> WebContents::CreateFrom(
return mate::CreateHandle(isolate, static_cast<WebContents*>(existing));
// Otherwise create a new WebContents wrapper object.
auto handle = mate::CreateHandle(
isolate, new WebContents(isolate, web_contents));
g_wrap_web_contents.Run(handle.ToV8());
return handle;
return mate::CreateHandle(isolate, new WebContents(isolate, web_contents));
}
// static
mate::Handle<WebContents> WebContents::Create(
v8::Isolate* isolate, const mate::Dictionary& options) {
auto handle = mate::CreateHandle(isolate, new WebContents(isolate, options));
g_wrap_web_contents.Run(handle.ToV8());
return handle;
}
void SetWrapWebContents(const WrapWebContentsCallback& callback) {
g_wrap_web_contents = callback;
return mate::CreateHandle(isolate, new WebContents(isolate, options));
}
} // namespace api
} // namespace atom
namespace {
using atom::api::WebContents;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.SetMethod("create", &atom::api::WebContents::Create);
dict.SetMethod("_setWrapWebContents", &atom::api::SetWrapWebContents);
dict.SetMethod("fromId",
&mate::TrackableObject<atom::api::WebContents>::FromWeakMapID);
dict.Set("WebContents", WebContents::GetConstructor(isolate)->GetFunction());
dict.SetMethod("create", &WebContents::Create);
dict.SetMethod("fromId", &mate::TrackableObject<WebContents>::FromWeakMapID);
dict.SetMethod("getAllWebContents",
&mate::TrackableObject<atom::api::WebContents>::GetAll);
&mate::TrackableObject<WebContents>::GetAll);
}
} // namespace

View file

@ -16,9 +16,6 @@ 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) {
@ -80,29 +77,6 @@ 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)

View file

@ -32,9 +32,6 @@ 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.
@ -90,11 +87,6 @@ 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>