From a2407c6b0231ded40e5db475cb280706dbdf27e3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 16 Apr 2014 15:31:59 +0800 Subject: [PATCH] Use Dictionary to set module.exports. --- atom/common/api/atom_api_clipboard.cc | 15 +++--- atom/common/api/atom_api_crash_reporter.cc | 11 ++--- atom/common/api/atom_api_screen.cc | 18 +++---- atom/common/api/atom_api_shell.cc | 15 +++--- atom/common/api/atom_api_v8_util.cc | 57 ++++++++++------------ atom/renderer/api/atom_api_renderer_ipc.cc | 9 ++-- vendor/native_mate | 2 +- 7 files changed, 59 insertions(+), 68 deletions(-) diff --git a/atom/common/api/atom_api_clipboard.cc b/atom/common/api/atom_api_clipboard.cc index d41e0ff885..3bb3758608 100644 --- a/atom/common/api/atom_api_clipboard.cc +++ b/atom/common/api/atom_api_clipboard.cc @@ -5,7 +5,7 @@ #include #include -#include "native_mate/object_template_builder.h" +#include "native_mate/dictionary.h" #include "ui/base/clipboard/clipboard.h" #include "atom/common/v8/node_common.h" @@ -49,13 +49,12 @@ void Clear() { } void Initialize(v8::Handle exports) { - mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); - builder.SetMethod("has", &Has) - .SetMethod("read", &Read) - .SetMethod("readText", &ReadText) - .SetMethod("writeText", &WriteText) - .SetMethod("clear", &Clear); - exports->SetPrototype(builder.Build()->NewInstance()); + mate::Dictionary dict(v8::Isolate::GetCurrent(), exports); + dict.SetMethod("has", &Has); + dict.SetMethod("read", &Read); + dict.SetMethod("readText", &ReadText); + dict.SetMethod("writeText", &WriteText); + dict.SetMethod("clear", &Clear); } } // namespace diff --git a/atom/common/api/atom_api_crash_reporter.cc b/atom/common/api/atom_api_crash_reporter.cc index ba840e36d2..a3338e7ed3 100644 --- a/atom/common/api/atom_api_crash_reporter.cc +++ b/atom/common/api/atom_api_crash_reporter.cc @@ -7,7 +7,7 @@ #include "atom/common/crash_reporter/crash_reporter.h" #include "base/bind.h" -#include "native_mate/object_template_builder.h" +#include "native_mate/dictionary.h" #include "atom/common/v8/node_common.h" @@ -37,11 +37,10 @@ namespace { void Initialize(v8::Handle exports) { using crash_reporter::CrashReporter; - mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); - builder.SetMethod("start", - base::Bind(&CrashReporter::Start, - base::Unretained(CrashReporter::GetInstance()))); - exports->SetPrototype(builder.Build()->NewInstance()); + mate::Dictionary dict(v8::Isolate::GetCurrent(), exports); + dict.SetMethod("start", + base::Bind(&CrashReporter::Start, + base::Unretained(CrashReporter::GetInstance()))); } } // namespace diff --git a/atom/common/api/atom_api_screen.cc b/atom/common/api/atom_api_screen.cc index 2814cb68dd..1b244cde28 100644 --- a/atom/common/api/atom_api_screen.cc +++ b/atom/common/api/atom_api_screen.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "native_mate/object_template_builder.h" +#include "native_mate/dictionary.h" #include "ui/gfx/screen.h" #include "atom/common/v8/node_common.h" @@ -92,15 +92,13 @@ void Initialize(v8::Handle exports) { #endif gfx::Screen* screen = gfx::Screen::GetNativeScreen(); - - mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); - builder.SetMethod("getCursorScreenPoint", - base::Bind(&gfx::Screen::GetCursorScreenPoint, - base::Unretained(screen))) - .SetMethod("getPrimaryDisplay", - base::Bind(&gfx::Screen::GetPrimaryDisplay, - base::Unretained(screen))); - exports->SetPrototype(builder.Build()->NewInstance()); + mate::Dictionary dict(v8::Isolate::GetCurrent(), exports); + dict.SetMethod("getCursorScreenPoint", + base::Bind(&gfx::Screen::GetCursorScreenPoint, + base::Unretained(screen))); + dict.SetMethod("getPrimaryDisplay", + base::Bind(&gfx::Screen::GetPrimaryDisplay, + base::Unretained(screen))); } } // namespace diff --git a/atom/common/api/atom_api_shell.cc b/atom/common/api/atom_api_shell.cc index be6ab614d3..edb90997a3 100644 --- a/atom/common/api/atom_api_shell.cc +++ b/atom/common/api/atom_api_shell.cc @@ -6,7 +6,7 @@ #include "atom/common/platform_util.h" #include "atom/common/v8_converters/file_path_converter.h" -#include "native_mate/object_template_builder.h" +#include "native_mate/dictionary.h" #include "url/gurl.h" #include "atom/common/v8/node_common.h" @@ -33,13 +33,12 @@ struct Converter { namespace { void Initialize(v8::Handle exports) { - mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); - builder.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder) - .SetMethod("openItem", &platform_util::OpenItem) - .SetMethod("openExternal", &platform_util::OpenExternal) - .SetMethod("moveItemToTrash", &platform_util::MoveItemToTrash) - .SetMethod("beep", &platform_util::Beep); - exports->SetPrototype(builder.Build()->NewInstance()); + mate::Dictionary dict(v8::Isolate::GetCurrent(), exports); + dict.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder); + dict.SetMethod("openItem", &platform_util::OpenItem); + dict.SetMethod("openExternal", &platform_util::OpenExternal); + dict.SetMethod("moveItemToTrash", &platform_util::MoveItemToTrash); + dict.SetMethod("beep", &platform_util::Beep); } } // namespace diff --git a/atom/common/api/atom_api_v8_util.cc b/atom/common/api/atom_api_v8_util.cc index 2bebf53df4..8889591f0b 100644 --- a/atom/common/api/atom_api_v8_util.cc +++ b/atom/common/api/atom_api_v8_util.cc @@ -3,57 +3,54 @@ // found in the LICENSE file. #include "atom/common/api/object_life_monitor.h" +#include "native_mate/dictionary.h" #include "v8/include/v8-profiler.h" #include "atom/common/v8/node_common.h" -namespace atom { - -namespace api { - namespace { -void CreateObjectWithName(const v8::FunctionCallbackInfo& args) { +v8::Handle CreateObjectWithName(v8::Handle name) { v8::Local t = v8::FunctionTemplate::New(); - t->SetClassName(args[0]->ToString()); - args.GetReturnValue().Set(t->GetFunction()->NewInstance()); + t->SetClassName(name); + return t->GetFunction()->NewInstance(); } -void GetHiddenValue(const v8::FunctionCallbackInfo& args) { - args.GetReturnValue().Set( - args[0]->ToObject()->GetHiddenValue(args[1]->ToString())); +v8::Handle GetHiddenValue(v8::Handle object, + v8::Handle key) { + return object->GetHiddenValue(key); } -void SetHiddenValue(const v8::FunctionCallbackInfo& args) { - args[0]->ToObject()->SetHiddenValue(args[1]->ToString(), args[2]); +void SetHiddenValue(v8::Handle object, + v8::Handle key, + v8::Handle value) { + object->SetHiddenValue(key, value); } -void GetObjectHash(const v8::FunctionCallbackInfo& args) { - args.GetReturnValue().Set(args[0]->ToObject()->GetIdentityHash()); +int32_t GetObjectHash(v8::Handle object) { + return object->GetIdentityHash(); } -void SetDestructor(const v8::FunctionCallbackInfo& args) { - ObjectLifeMonitor::BindTo(args[0]->ToObject(), args[1]); +void SetDestructor(v8::Handle object, + v8::Handle callback) { + atom::ObjectLifeMonitor::BindTo(object, callback); } -void TakeHeapSnapshot(const v8::FunctionCallbackInfo& args) { +void TakeHeapSnapshot() { node::node_isolate->GetHeapProfiler()->TakeHeapSnapshot( v8::String::New("test")); } -} // namespace - -void InitializeV8Util(v8::Handle target) { - NODE_SET_METHOD(target, "createObjectWithName", CreateObjectWithName); - NODE_SET_METHOD(target, "getHiddenValue", GetHiddenValue); - NODE_SET_METHOD(target, "setHiddenValue", SetHiddenValue); - NODE_SET_METHOD(target, "getObjectHash", GetObjectHash); - NODE_SET_METHOD(target, "setDestructor", SetDestructor); - NODE_SET_METHOD(target, "takeHeapSnapshot", TakeHeapSnapshot); +void Initialize(v8::Handle exports) { + mate::Dictionary dict(v8::Isolate::GetCurrent(), exports); + dict.SetMethod("createObjectWithName", &CreateObjectWithName); + dict.SetMethod("getHiddenValue", &GetHiddenValue); + dict.SetMethod("setHiddenValue", &SetHiddenValue); + dict.SetMethod("getObjectHash", &GetObjectHash); + dict.SetMethod("setDestructor", &SetDestructor); + dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot); } -} // namespace api +} // namespace -} // namespace atom - -NODE_MODULE(atom_common_v8_util, atom::api::InitializeV8Util) +NODE_MODULE(atom_common_v8_util, Initialize) diff --git a/atom/renderer/api/atom_api_renderer_ipc.cc b/atom/renderer/api/atom_api_renderer_ipc.cc index bd305f1977..c93d8a0a3b 100644 --- a/atom/renderer/api/atom_api_renderer_ipc.cc +++ b/atom/renderer/api/atom_api_renderer_ipc.cc @@ -6,7 +6,7 @@ #include "atom/common/v8/v8_value_converter.h" #include "atom/common/v8_converters/string16_converter.h" #include "content/public/renderer/render_view.h" -#include "native_mate/object_template_builder.h" +#include "native_mate/dictionary.h" #include "third_party/WebKit/public/web/WebFrame.h" #include "third_party/WebKit/public/web/WebView.h" @@ -83,10 +83,9 @@ string16 SendSync(const string16& channel, const base::ListValue& arguments) { } void Initialize(v8::Handle exports) { - mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); - builder.SetMethod("send", &Send) - .SetMethod("sendSync", &SendSync); - exports->SetPrototype(builder.Build()->NewInstance()); + mate::Dictionary dict(v8::Isolate::GetCurrent(), exports); + dict.SetMethod("send", &Send); + dict.SetMethod("sendSync", &SendSync); } } // namespace diff --git a/vendor/native_mate b/vendor/native_mate index c9fa29ef64..94dec0ff85 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit c9fa29ef640e8d3d2edf6b48c2311b16ce314464 +Subproject commit 94dec0ff85c9337d33cae01638897bc1059b7dca