Use the new Constructor API.

This commit is contained in:
Cheng Zhao 2014-04-16 11:58:48 +08:00
parent 8162689014
commit 1ae30328d6
3 changed files with 46 additions and 35 deletions

View file

@ -6,13 +6,12 @@
#include <algorithm>
#include "atom/common/v8/native_type_conversions.h"
#include "atom/common/v8/node_common.h"
#include "base/logging.h"
#include "native_mate/constructor.h"
#include "native_mate/function_template.h"
#include "native_mate/object_template_builder.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
@ -24,16 +23,6 @@ IDWeakMap::IDWeakMap()
IDWeakMap::~IDWeakMap() {
}
// static
void IDWeakMap::WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self) {
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> local = v8::Local<v8::Object>::New(isolate, *value);
self->Remove(
FromV8Value(local->GetHiddenValue(v8::String::New("IDWeakMapKey"))));
}
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Handle<v8::Object> object) {
int32_t key = GetNextID();
object->SetHiddenValue(mate::StringToV8(isolate, "IDWeakMapKey"),
@ -76,25 +65,46 @@ int IDWeakMap::GetNextID() {
return ++next_id_;
}
void IDWeakMap::Initialize(v8::Handle<v8::Object> exports) {
v8::Local<v8::FunctionTemplate> constructor = mate::CreateConstructor(
v8::Isolate::GetCurrent(),
"IDWeakMap",
base::Bind(&mate::NewOperatorFactory<IDWeakMap>));
mate::ObjectTemplateBuilder builder(
v8::Isolate::GetCurrent(), constructor->PrototypeTemplate());
builder.SetMethod("add", &IDWeakMap::Add)
// static
void IDWeakMap::BuildPrototype(v8::Isolate* isolate,
v8::Handle<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("add", &IDWeakMap::Add)
.SetMethod("get", &IDWeakMap::Get)
.SetMethod("has", &IDWeakMap::Has)
.SetMethod("keys", &IDWeakMap::Keys)
.SetMethod("remove", &IDWeakMap::Remove);
}
exports->Set(v8::String::NewSymbol("IDWeakMap"), constructor->GetFunction());
// static
void IDWeakMap::WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self) {
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> object = v8::Local<v8::Object>::New(isolate, *value);
int32_t key = object->GetHiddenValue(
mate::StringToV8(isolate, "IDWeakMapKey"))->Int32Value();
self->Remove(key);
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_id_weak_map, atom::api::IDWeakMap::Initialize)
namespace {
void Initialize(v8::Handle<v8::Object> exports) {
using atom::api::IDWeakMap;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Function> constructor = mate::CreateConstructor<IDWeakMap>(
isolate,
"IDWeakMap",
base::Bind(&mate::NewOperatorFactory<IDWeakMap>));
exports->Set(mate::StringToV8(isolate, "IDWeakMap"), constructor);
}
} // namespace
NODE_MODULE(atom_common_id_weak_map, Initialize)

View file

@ -18,19 +18,16 @@ namespace atom {
namespace api {
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer.
class IDWeakMap : public mate::Wrappable<IDWeakMap> {
class IDWeakMap : public mate::Wrappable {
public:
IDWeakMap();
static void Initialize(v8::Handle<v8::Object> exports);
static void BuildPrototype(v8::Isolate* isolate,
v8::Handle<v8::ObjectTemplate> prototype);
private:
virtual ~IDWeakMap();
static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self);
int32_t Add(v8::Isolate* isolate, v8::Handle<v8::Object> object);
v8::Handle<v8::Value> Get(int32_t key);
bool Has(int32_t key) const;
@ -38,6 +35,10 @@ class IDWeakMap : public mate::Wrappable<IDWeakMap> {
void Remove(int32_t key);
int GetNextID();
static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self);
int32_t next_id_;
std::map<int32_t, RefCountedV8Object> map_;

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit e2e1faa2250ef5a41666cca29ebeac681bc83fa1
Subproject commit ace550d6b20a62ca1101153a06bccb4ba1484a14