Use Dictionary to set module.exports.

This commit is contained in:
Cheng Zhao 2014-04-16 15:31:59 +08:00
parent aa1efe70e2
commit a2407c6b02
7 changed files with 59 additions and 68 deletions

View file

@ -5,7 +5,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "native_mate/object_template_builder.h" #include "native_mate/dictionary.h"
#include "ui/base/clipboard/clipboard.h" #include "ui/base/clipboard/clipboard.h"
#include "atom/common/v8/node_common.h" #include "atom/common/v8/node_common.h"
@ -49,13 +49,12 @@ void Clear() {
} }
void Initialize(v8::Handle<v8::Object> exports) { void Initialize(v8::Handle<v8::Object> exports) {
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
builder.SetMethod("has", &Has) dict.SetMethod("has", &Has);
.SetMethod("read", &Read) dict.SetMethod("read", &Read);
.SetMethod("readText", &ReadText) dict.SetMethod("readText", &ReadText);
.SetMethod("writeText", &WriteText) dict.SetMethod("writeText", &WriteText);
.SetMethod("clear", &Clear); dict.SetMethod("clear", &Clear);
exports->SetPrototype(builder.Build()->NewInstance());
} }
} // namespace } // namespace

View file

@ -7,7 +7,7 @@
#include "atom/common/crash_reporter/crash_reporter.h" #include "atom/common/crash_reporter/crash_reporter.h"
#include "base/bind.h" #include "base/bind.h"
#include "native_mate/object_template_builder.h" #include "native_mate/dictionary.h"
#include "atom/common/v8/node_common.h" #include "atom/common/v8/node_common.h"
@ -37,11 +37,10 @@ namespace {
void Initialize(v8::Handle<v8::Object> exports) { void Initialize(v8::Handle<v8::Object> exports) {
using crash_reporter::CrashReporter; using crash_reporter::CrashReporter;
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
builder.SetMethod("start", dict.SetMethod("start",
base::Bind(&CrashReporter::Start, base::Bind(&CrashReporter::Start,
base::Unretained(CrashReporter::GetInstance()))); base::Unretained(CrashReporter::GetInstance())));
exports->SetPrototype(builder.Build()->NewInstance());
} }
} // namespace } // namespace

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "native_mate/object_template_builder.h" #include "native_mate/dictionary.h"
#include "ui/gfx/screen.h" #include "ui/gfx/screen.h"
#include "atom/common/v8/node_common.h" #include "atom/common/v8/node_common.h"
@ -92,15 +92,13 @@ void Initialize(v8::Handle<v8::Object> exports) {
#endif #endif
gfx::Screen* screen = gfx::Screen::GetNativeScreen(); gfx::Screen* screen = gfx::Screen::GetNativeScreen();
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); dict.SetMethod("getCursorScreenPoint",
builder.SetMethod("getCursorScreenPoint", base::Bind(&gfx::Screen::GetCursorScreenPoint,
base::Bind(&gfx::Screen::GetCursorScreenPoint, base::Unretained(screen)));
base::Unretained(screen))) dict.SetMethod("getPrimaryDisplay",
.SetMethod("getPrimaryDisplay", base::Bind(&gfx::Screen::GetPrimaryDisplay,
base::Bind(&gfx::Screen::GetPrimaryDisplay, base::Unretained(screen)));
base::Unretained(screen)));
exports->SetPrototype(builder.Build()->NewInstance());
} }
} // namespace } // namespace

View file

@ -6,7 +6,7 @@
#include "atom/common/platform_util.h" #include "atom/common/platform_util.h"
#include "atom/common/v8_converters/file_path_converter.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 "url/gurl.h"
#include "atom/common/v8/node_common.h" #include "atom/common/v8/node_common.h"
@ -33,13 +33,12 @@ struct Converter<GURL> {
namespace { namespace {
void Initialize(v8::Handle<v8::Object> exports) { void Initialize(v8::Handle<v8::Object> exports) {
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
builder.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder) dict.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder);
.SetMethod("openItem", &platform_util::OpenItem) dict.SetMethod("openItem", &platform_util::OpenItem);
.SetMethod("openExternal", &platform_util::OpenExternal) dict.SetMethod("openExternal", &platform_util::OpenExternal);
.SetMethod("moveItemToTrash", &platform_util::MoveItemToTrash) dict.SetMethod("moveItemToTrash", &platform_util::MoveItemToTrash);
.SetMethod("beep", &platform_util::Beep); dict.SetMethod("beep", &platform_util::Beep);
exports->SetPrototype(builder.Build()->NewInstance());
} }
} // namespace } // namespace

View file

@ -3,57 +3,54 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "atom/common/api/object_life_monitor.h" #include "atom/common/api/object_life_monitor.h"
#include "native_mate/dictionary.h"
#include "v8/include/v8-profiler.h" #include "v8/include/v8-profiler.h"
#include "atom/common/v8/node_common.h" #include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
namespace { namespace {
void CreateObjectWithName(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::Handle<v8::Object> CreateObjectWithName(v8::Handle<v8::String> name) {
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
t->SetClassName(args[0]->ToString()); t->SetClassName(name);
args.GetReturnValue().Set(t->GetFunction()->NewInstance()); return t->GetFunction()->NewInstance();
} }
void GetHiddenValue(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::Handle<v8::Value> GetHiddenValue(v8::Handle<v8::Object> object,
args.GetReturnValue().Set( v8::Handle<v8::String> key) {
args[0]->ToObject()->GetHiddenValue(args[1]->ToString())); return object->GetHiddenValue(key);
} }
void SetHiddenValue(const v8::FunctionCallbackInfo<v8::Value>& args) { void SetHiddenValue(v8::Handle<v8::Object> object,
args[0]->ToObject()->SetHiddenValue(args[1]->ToString(), args[2]); v8::Handle<v8::String> key,
v8::Handle<v8::Value> value) {
object->SetHiddenValue(key, value);
} }
void GetObjectHash(const v8::FunctionCallbackInfo<v8::Value>& args) { int32_t GetObjectHash(v8::Handle<v8::Object> object) {
args.GetReturnValue().Set(args[0]->ToObject()->GetIdentityHash()); return object->GetIdentityHash();
} }
void SetDestructor(const v8::FunctionCallbackInfo<v8::Value>& args) { void SetDestructor(v8::Handle<v8::Object> object,
ObjectLifeMonitor::BindTo(args[0]->ToObject(), args[1]); v8::Handle<v8::Function> callback) {
atom::ObjectLifeMonitor::BindTo(object, callback);
} }
void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args) { void TakeHeapSnapshot() {
node::node_isolate->GetHeapProfiler()->TakeHeapSnapshot( node::node_isolate->GetHeapProfiler()->TakeHeapSnapshot(
v8::String::New("test")); v8::String::New("test"));
} }
} // namespace void Initialize(v8::Handle<v8::Object> exports) {
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
void InitializeV8Util(v8::Handle<v8::Object> target) { dict.SetMethod("createObjectWithName", &CreateObjectWithName);
NODE_SET_METHOD(target, "createObjectWithName", CreateObjectWithName); dict.SetMethod("getHiddenValue", &GetHiddenValue);
NODE_SET_METHOD(target, "getHiddenValue", GetHiddenValue); dict.SetMethod("setHiddenValue", &SetHiddenValue);
NODE_SET_METHOD(target, "setHiddenValue", SetHiddenValue); dict.SetMethod("getObjectHash", &GetObjectHash);
NODE_SET_METHOD(target, "getObjectHash", GetObjectHash); dict.SetMethod("setDestructor", &SetDestructor);
NODE_SET_METHOD(target, "setDestructor", SetDestructor); dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
NODE_SET_METHOD(target, "takeHeapSnapshot", TakeHeapSnapshot);
} }
} // namespace api } // namespace
} // namespace atom NODE_MODULE(atom_common_v8_util, Initialize)
NODE_MODULE(atom_common_v8_util, atom::api::InitializeV8Util)

View file

@ -6,7 +6,7 @@
#include "atom/common/v8/v8_value_converter.h" #include "atom/common/v8/v8_value_converter.h"
#include "atom/common/v8_converters/string16_converter.h" #include "atom/common/v8_converters/string16_converter.h"
#include "content/public/renderer/render_view.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/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.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<v8::Object> exports) { void Initialize(v8::Handle<v8::Object> exports) {
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
builder.SetMethod("send", &Send) dict.SetMethod("send", &Send);
.SetMethod("sendSync", &SendSync); dict.SetMethod("sendSync", &SendSync);
exports->SetPrototype(builder.Build()->NewInstance());
} }
} // namespace } // namespace

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit c9fa29ef640e8d3d2edf6b48c2311b16ce314464 Subproject commit 94dec0ff85c9337d33cae01638897bc1059b7dca