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 <algorithm>
#include "atom/common/v8/native_type_conversions.h"
#include "atom/common/v8/node_common.h"
#include "base/logging.h" #include "base/logging.h"
#include "native_mate/constructor.h" #include "native_mate/constructor.h"
#include "native_mate/function_template.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
#include "atom/common/v8/node_common.h"
namespace atom { namespace atom {
namespace api { namespace api {
@ -24,16 +23,6 @@ IDWeakMap::IDWeakMap()
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 IDWeakMap::Add(v8::Isolate* isolate, v8::Handle<v8::Object> object) {
int32_t key = GetNextID(); int32_t key = GetNextID();
object->SetHiddenValue(mate::StringToV8(isolate, "IDWeakMapKey"), object->SetHiddenValue(mate::StringToV8(isolate, "IDWeakMapKey"),
@ -76,25 +65,46 @@ int IDWeakMap::GetNextID() {
return ++next_id_; return ++next_id_;
} }
void IDWeakMap::Initialize(v8::Handle<v8::Object> exports) { // static
v8::Local<v8::FunctionTemplate> constructor = mate::CreateConstructor( void IDWeakMap::BuildPrototype(v8::Isolate* isolate,
v8::Isolate::GetCurrent(), v8::Handle<v8::ObjectTemplate> prototype) {
"IDWeakMap", mate::ObjectTemplateBuilder(isolate, prototype)
base::Bind(&mate::NewOperatorFactory<IDWeakMap>)); .SetMethod("add", &IDWeakMap::Add)
.SetMethod("get", &IDWeakMap::Get)
.SetMethod("has", &IDWeakMap::Has)
.SetMethod("keys", &IDWeakMap::Keys)
.SetMethod("remove", &IDWeakMap::Remove);
}
mate::ObjectTemplateBuilder builder( // static
v8::Isolate::GetCurrent(), constructor->PrototypeTemplate()); void IDWeakMap::WeakCallback(v8::Isolate* isolate,
builder.SetMethod("add", &IDWeakMap::Add) v8::Persistent<v8::Object>* value,
.SetMethod("get", &IDWeakMap::Get) IDWeakMap* self) {
.SetMethod("has", &IDWeakMap::Has) v8::HandleScope handle_scope(isolate);
.SetMethod("keys", &IDWeakMap::Keys) v8::Local<v8::Object> object = v8::Local<v8::Object>::New(isolate, *value);
.SetMethod("remove", &IDWeakMap::Remove); int32_t key = object->GetHiddenValue(
mate::StringToV8(isolate, "IDWeakMapKey"))->Int32Value();
exports->Set(v8::String::NewSymbol("IDWeakMap"), constructor->GetFunction()); self->Remove(key);
} }
} // namespace api } // namespace api
} // namespace atom } // 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 { namespace api {
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer. // 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: public:
IDWeakMap(); IDWeakMap();
static void Initialize(v8::Handle<v8::Object> exports); static void BuildPrototype(v8::Isolate* isolate,
v8::Handle<v8::ObjectTemplate> prototype);
private: private:
virtual ~IDWeakMap(); 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); int32_t Add(v8::Isolate* isolate, v8::Handle<v8::Object> object);
v8::Handle<v8::Value> Get(int32_t key); v8::Handle<v8::Value> Get(int32_t key);
bool Has(int32_t key) const; bool Has(int32_t key) const;
@ -38,6 +35,10 @@ class IDWeakMap : public mate::Wrappable<IDWeakMap> {
void Remove(int32_t key); void Remove(int32_t key);
int GetNextID(); int GetNextID();
static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self);
int32_t next_id_; int32_t next_id_;
std::map<int32_t, RefCountedV8Object> map_; std::map<int32_t, RefCountedV8Object> map_;

2
vendor/native_mate vendored

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