use idweakmap for holding callbacks in browser

This commit is contained in:
Robo 2015-10-28 21:29:46 +05:30
parent 62d15953ff
commit eae7c840b7
6 changed files with 63 additions and 13 deletions

View file

@ -8,6 +8,18 @@
#include "native_mate/converter.h"
namespace mate {
template<typename T>
struct Converter<v8::MaybeLocal<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::MaybeLocal<T> val) {
return ConvertToV8(isolate, val.ToLocalChecked());
}
};
} // namespace mate
namespace atom {
namespace {
@ -41,6 +53,15 @@ int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
return id;
}
void IDWeakMap::Set(v8::Isolate* isolate,
int32_t id,
v8::Local<v8::Object> object) {
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
ObjectKey* key = new ObjectKey(id, this);
global->SetWeak(key, OnObjectGC, v8::WeakCallbackType::kParameter);
map_[id] = global;
}
v8::MaybeLocal<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
auto iter = map_.find(id);
if (iter == map_.end())
@ -85,4 +106,13 @@ int32_t IDWeakMap::GetNextID() {
return ++next_id_;
}
mate::ObjectTemplateBuilder IDWeakMap::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
.SetMethod("set", &IDWeakMap::Set)
.SetMethod("get", &IDWeakMap::Get)
.SetMethod("has", &IDWeakMap::Has)
.SetMethod("remove", &IDWeakMap::Remove);
}
} // namespace atom