Use native_mate to simplify id_weak_map api.
This commit is contained in:
parent
338d11ef01
commit
8162689014
3 changed files with 70 additions and 114 deletions
|
@ -1,5 +1,4 @@
|
||||||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||||
// Copyright (c) 2012 Intel Corp. All rights reserved.
|
|
||||||
// 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.
|
||||||
|
|
||||||
|
@ -10,6 +9,9 @@
|
||||||
#include "atom/common/v8/native_type_conversions.h"
|
#include "atom/common/v8/native_type_conversions.h"
|
||||||
#include "atom/common/v8/node_common.h"
|
#include "atom/common/v8/node_common.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
|
#include "native_mate/constructor.h"
|
||||||
|
#include "native_mate/function_template.h"
|
||||||
|
#include "native_mate/object_template_builder.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
@ -22,11 +24,48 @@ IDWeakMap::IDWeakMap()
|
||||||
IDWeakMap::~IDWeakMap() {
|
IDWeakMap::~IDWeakMap() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IDWeakMap::Has(int key) const {
|
// 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"),
|
||||||
|
mate::Converter<int32_t>::ToV8(isolate, key));
|
||||||
|
|
||||||
|
map_[key] = new RefCountedPersistent<v8::Object>(object);
|
||||||
|
map_[key]->MakeWeak(this, WeakCallback);
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
v8::Handle<v8::Value> IDWeakMap::Get(int32_t key) {
|
||||||
|
if (!Has(key)) {
|
||||||
|
node::ThrowError("Invalid key");
|
||||||
|
return v8::Undefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
return map_[key]->NewHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IDWeakMap::Has(int32_t key) const {
|
||||||
return map_.find(key) != map_.end();
|
return map_.find(key) != map_.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDWeakMap::Erase(int key) {
|
std::vector<int32_t> IDWeakMap::Keys() const {
|
||||||
|
std::vector<int32_t> keys;
|
||||||
|
keys.reserve(map_.size());
|
||||||
|
for (auto it = map_.begin(); it != map_.end(); ++it)
|
||||||
|
keys.push_back(it->first);
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IDWeakMap::Remove(int32_t key) {
|
||||||
if (Has(key))
|
if (Has(key))
|
||||||
map_.erase(key);
|
map_.erase(key);
|
||||||
else
|
else
|
||||||
|
@ -37,102 +76,21 @@ int IDWeakMap::GetNextID() {
|
||||||
return ++next_id_;
|
return ++next_id_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
void IDWeakMap::Initialize(v8::Handle<v8::Object> exports) {
|
||||||
void IDWeakMap::WeakCallback(v8::Isolate* isolate,
|
v8::Local<v8::FunctionTemplate> constructor = mate::CreateConstructor(
|
||||||
v8::Persistent<v8::Object>* value,
|
v8::Isolate::GetCurrent(),
|
||||||
IDWeakMap* self) {
|
"IDWeakMap",
|
||||||
v8::HandleScope handle_scope(isolate);
|
base::Bind(&mate::NewOperatorFactory<IDWeakMap>));
|
||||||
v8::Local<v8::Object> local = v8::Local<v8::Object>::New(isolate, *value);
|
|
||||||
self->Erase(
|
|
||||||
FromV8Value(local->GetHiddenValue(v8::String::New("IDWeakMapKey"))));
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
mate::ObjectTemplateBuilder builder(
|
||||||
void IDWeakMap::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
v8::Isolate::GetCurrent(), constructor->PrototypeTemplate());
|
||||||
(new IDWeakMap)->Wrap(args.This());
|
builder.SetMethod("add", &IDWeakMap::Add)
|
||||||
}
|
.SetMethod("get", &IDWeakMap::Get)
|
||||||
|
.SetMethod("has", &IDWeakMap::Has)
|
||||||
|
.SetMethod("keys", &IDWeakMap::Keys)
|
||||||
|
.SetMethod("remove", &IDWeakMap::Remove);
|
||||||
|
|
||||||
// static
|
exports->Set(v8::String::NewSymbol("IDWeakMap"), constructor->GetFunction());
|
||||||
void IDWeakMap::Add(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
if (!args[0]->IsObject())
|
|
||||||
return node::ThrowTypeError("Bad argument");
|
|
||||||
|
|
||||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
|
||||||
|
|
||||||
int key = self->GetNextID();
|
|
||||||
v8::Local<v8::Object> v8_value = args[0]->ToObject();
|
|
||||||
v8_value->SetHiddenValue(v8::String::New("IDWeakMapKey"), ToV8Value(key));
|
|
||||||
|
|
||||||
self->map_[key] = new RefCountedPersistent<v8::Object>(v8_value);
|
|
||||||
self->map_[key]->MakeWeak(self, WeakCallback);
|
|
||||||
|
|
||||||
args.GetReturnValue().Set(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void IDWeakMap::Get(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
int key;
|
|
||||||
if (!FromV8Arguments(args, &key))
|
|
||||||
return node::ThrowTypeError("Bad argument");
|
|
||||||
|
|
||||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
|
||||||
if (!self->Has(key))
|
|
||||||
return node::ThrowError("Invalid key");
|
|
||||||
|
|
||||||
args.GetReturnValue().Set(self->map_[key]->NewHandle());
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void IDWeakMap::Has(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
int key;
|
|
||||||
if (!FromV8Arguments(args, &key))
|
|
||||||
return node::ThrowTypeError("Bad argument");
|
|
||||||
|
|
||||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
|
||||||
args.GetReturnValue().Set(self->Has(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void IDWeakMap::Keys(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
|
||||||
|
|
||||||
v8::Local<v8::Array> keys = v8::Array::New(self->map_.size());
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for (auto el = self->map_.begin(); el != self->map_.end(); ++el) {
|
|
||||||
keys->Set(i, ToV8Value(el->first));
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
args.GetReturnValue().Set(keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void IDWeakMap::Remove(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
int key;
|
|
||||||
if (!FromV8Arguments(args, &key))
|
|
||||||
return node::ThrowTypeError("Bad argument");
|
|
||||||
|
|
||||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
|
||||||
if (!self->Has(key))
|
|
||||||
return node::ThrowError("Invalid key");
|
|
||||||
|
|
||||||
self->Erase(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void IDWeakMap::Initialize(v8::Handle<v8::Object> target) {
|
|
||||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
|
|
||||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
|
||||||
t->SetClassName(v8::String::NewSymbol("IDWeakMap"));
|
|
||||||
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "add", Add);
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "get", Get);
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "has", Has);
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "keys", Keys);
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "remove", Remove);
|
|
||||||
|
|
||||||
target->Set(v8::String::NewSymbol("IDWeakMap"), t->GetFunction());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
|
@ -7,41 +7,39 @@
|
||||||
#define ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
|
#define ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "atom/common/v8/scoped_persistent.h"
|
#include "atom/common/v8/scoped_persistent.h"
|
||||||
#include "base/basictypes.h"
|
#include "base/basictypes.h"
|
||||||
#include "vendor/node/src/node_object_wrap.h"
|
#include "native_mate/wrappable.h"
|
||||||
|
|
||||||
namespace atom {
|
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 node::ObjectWrap {
|
class IDWeakMap : public mate::Wrappable<IDWeakMap> {
|
||||||
public:
|
public:
|
||||||
static void Initialize(v8::Handle<v8::Object> target);
|
IDWeakMap();
|
||||||
|
|
||||||
|
static void Initialize(v8::Handle<v8::Object> exports);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IDWeakMap();
|
|
||||||
virtual ~IDWeakMap();
|
virtual ~IDWeakMap();
|
||||||
|
|
||||||
bool Has(int key) const;
|
|
||||||
void Erase(int key);
|
|
||||||
int GetNextID();
|
|
||||||
|
|
||||||
static void WeakCallback(v8::Isolate* isolate,
|
static void WeakCallback(v8::Isolate* isolate,
|
||||||
v8::Persistent<v8::Object>* value,
|
v8::Persistent<v8::Object>* value,
|
||||||
IDWeakMap* self);
|
IDWeakMap* self);
|
||||||
|
|
||||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
int32_t Add(v8::Isolate* isolate, v8::Handle<v8::Object> object);
|
||||||
static void Add(const v8::FunctionCallbackInfo<v8::Value>& args);
|
v8::Handle<v8::Value> Get(int32_t key);
|
||||||
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
|
bool Has(int32_t key) const;
|
||||||
static void Has(const v8::FunctionCallbackInfo<v8::Value>& args);
|
std::vector<int32_t> Keys() const;
|
||||||
static void Keys(const v8::FunctionCallbackInfo<v8::Value>& args);
|
void Remove(int32_t key);
|
||||||
static void Remove(const v8::FunctionCallbackInfo<v8::Value>& args);
|
int GetNextID();
|
||||||
|
|
||||||
int next_id_;
|
int32_t next_id_;
|
||||||
std::map<int, RefCountedV8Object> map_;
|
std::map<int32_t, RefCountedV8Object> map_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
|
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
|
||||||
};
|
};
|
||||||
|
|
2
vendor/native_mate
vendored
2
vendor/native_mate
vendored
|
@ -1 +1 @@
|
||||||
Subproject commit 78ab726ec7b14ecea6240a684002c6910561ef5c
|
Subproject commit e2e1faa2250ef5a41666cca29ebeac681bc83fa1
|
Loading…
Add table
Add a link
Reference in a new issue