create binding to idweakmap
This commit is contained in:
parent
eae7c840b7
commit
ac4df34ecd
8 changed files with 135 additions and 40 deletions
|
@ -2,9 +2,10 @@ ipc = require 'ipc'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
objectsRegistry = require './objects-registry.js'
|
objectsRegistry = require './objects-registry.js'
|
||||||
v8Util = process.atomBinding 'v8_util'
|
v8Util = process.atomBinding 'v8_util'
|
||||||
|
WeakMap = process.atomBinding('weak_map').WeakMap
|
||||||
|
|
||||||
# weak refereence to callback with their registry ID.
|
# Weak reference to callback with their registry ID.
|
||||||
rendererCallbacks = v8Util.createWeakMap()
|
rendererCallbacks = new WeakMap()
|
||||||
|
|
||||||
# Convert a real value into meta data.
|
# Convert a real value into meta data.
|
||||||
valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
||||||
|
@ -73,13 +74,13 @@ unwrapArgs = (sender, args) ->
|
||||||
returnValue = metaToValue meta.value
|
returnValue = metaToValue meta.value
|
||||||
-> returnValue
|
-> returnValue
|
||||||
when 'function'
|
when 'function'
|
||||||
|
if rendererCallbacks.has(meta.id)
|
||||||
|
return rendererCallbacks.get(meta.id)
|
||||||
|
|
||||||
rendererReleased = false
|
rendererReleased = false
|
||||||
objectsRegistry.once "clear-#{sender.getId()}", ->
|
objectsRegistry.once "clear-#{sender.getId()}", ->
|
||||||
rendererReleased = true
|
rendererReleased = true
|
||||||
|
|
||||||
if rendererCallbacks.has(meta.id)
|
|
||||||
return rendererCallbacks.get(meta.id)
|
|
||||||
|
|
||||||
ret = ->
|
ret = ->
|
||||||
if rendererReleased
|
if rendererReleased
|
||||||
throw new Error("Attempting to call a function in a renderer window
|
throw new Error("Attempting to call a function in a renderer window
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
#include "atom/common/api/object_life_monitor.h"
|
#include "atom/common/api/object_life_monitor.h"
|
||||||
#include "atom/common/id_weak_map.h"
|
#include "atom/common/id_weak_map.h"
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
#include "native_mate/handle.h"
|
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "v8/include/v8-profiler.h"
|
#include "v8/include/v8-profiler.h"
|
||||||
|
|
||||||
|
@ -48,11 +47,6 @@ void TakeHeapSnapshot(v8::Isolate* isolate) {
|
||||||
isolate->GetHeapProfiler()->TakeHeapSnapshot();
|
isolate->GetHeapProfiler()->TakeHeapSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
mate::Handle<atom::IDWeakMap> CreateWeakMap(v8::Isolate* isolate) {
|
|
||||||
auto handle = mate::CreateHandle(isolate, new atom::IDWeakMap);
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||||
v8::Local<v8::Context> context, void* priv) {
|
v8::Local<v8::Context> context, void* priv) {
|
||||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
||||||
|
@ -63,7 +57,6 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||||
dict.SetMethod("getObjectHash", &GetObjectHash);
|
dict.SetMethod("getObjectHash", &GetObjectHash);
|
||||||
dict.SetMethod("setDestructor", &SetDestructor);
|
dict.SetMethod("setDestructor", &SetDestructor);
|
||||||
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
||||||
dict.SetMethod("createWeakMap", &CreateWeakMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
79
atom/common/api/atom_api_weak_map.cc
Normal file
79
atom/common/api/atom_api_weak_map.cc
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
// Copyright (c) 2015 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "atom/common/api/atom_api_weak_map.h"
|
||||||
|
|
||||||
|
#include "atom/common/node_includes.h"
|
||||||
|
#include "native_mate/constructor.h"
|
||||||
|
#include "native_mate/dictionary.h"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
namespace api {
|
||||||
|
|
||||||
|
WeakMap::WeakMap() {
|
||||||
|
id_weak_map_.reset(new atom::IDWeakMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
WeakMap::~WeakMap() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void WeakMap::Set(v8::Isolate* isolate,
|
||||||
|
int32_t id,
|
||||||
|
v8::Local<v8::Object> object) {
|
||||||
|
id_weak_map_->Set(isolate, id, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Object> WeakMap::Get(v8::Isolate* isolate, int32_t id) {
|
||||||
|
return id_weak_map_->Get(isolate, id).ToLocalChecked();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WeakMap::Has(int32_t id) {
|
||||||
|
return id_weak_map_->Has(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WeakMap::Remove(int32_t id) {
|
||||||
|
id_weak_map_->Remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WeakMap::IsDestroyed() const {
|
||||||
|
return !id_weak_map_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void WeakMap::BuildPrototype(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::ObjectTemplate> prototype) {
|
||||||
|
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||||
|
.SetMethod("set", &WeakMap::Set)
|
||||||
|
.SetMethod("get", &WeakMap::Get)
|
||||||
|
.SetMethod("has", &WeakMap::Has)
|
||||||
|
.SetMethod("remove", &WeakMap::Remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
mate::Wrappable* WeakMap::Create(v8::Isolate* isolate) {
|
||||||
|
return new WeakMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace api
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using atom::api::WeakMap;
|
||||||
|
|
||||||
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||||
|
v8::Local<v8::Context> context, void* priv) {
|
||||||
|
v8::Isolate* isolate = context->GetIsolate();
|
||||||
|
v8::Local<v8::Function> constructor = mate::CreateConstructor<WeakMap>(
|
||||||
|
isolate, "WeakMap", base::Bind(&WeakMap::Create));
|
||||||
|
mate::Dictionary weak_map(isolate, constructor);
|
||||||
|
mate::Dictionary dict(isolate, exports);
|
||||||
|
dict.Set("WeakMap", weak_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_weak_map, Initialize)
|
46
atom/common/api/atom_api_weak_map.h
Normal file
46
atom/common/api/atom_api_weak_map.h
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
// Copyright (c) 2015 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef ATOM_COMMON_API_ATOM_API_WEAK_MAP_H_
|
||||||
|
#define ATOM_COMMON_API_ATOM_API_WEAK_MAP_H_
|
||||||
|
|
||||||
|
#include "atom/common/id_weak_map.h"
|
||||||
|
#include "native_mate/object_template_builder.h"
|
||||||
|
#include "native_mate/handle.h"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
namespace api {
|
||||||
|
|
||||||
|
class WeakMap : public mate::Wrappable {
|
||||||
|
public:
|
||||||
|
static mate::Wrappable* Create(v8::Isolate* isolate);
|
||||||
|
|
||||||
|
static void BuildPrototype(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::ObjectTemplate> prototype);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
WeakMap();
|
||||||
|
virtual ~WeakMap();
|
||||||
|
|
||||||
|
// mate::Wrappable:
|
||||||
|
bool IsDestroyed() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Api for IDWeakMap.
|
||||||
|
void Set(v8::Isolate* isolate, int32_t id, v8::Local<v8::Object> object);
|
||||||
|
v8::Local<v8::Object> Get(v8::Isolate* isolate, int32_t id);
|
||||||
|
bool Has(int32_t id);
|
||||||
|
void Remove(int32_t id);
|
||||||
|
|
||||||
|
scoped_ptr<IDWeakMap> id_weak_map_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(WeakMap);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace api
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_COMMON_API_ATOM_API_WEAK_MAP_H_
|
|
@ -8,18 +8,6 @@
|
||||||
|
|
||||||
#include "native_mate/converter.h"
|
#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 atom {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -106,13 +94,4 @@ int32_t IDWeakMap::GetNextID() {
|
||||||
return ++next_id_;
|
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
|
} // namespace atom
|
||||||
|
|
|
@ -9,14 +9,12 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "base/memory/linked_ptr.h"
|
#include "base/memory/linked_ptr.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
|
||||||
#include "native_mate/wrappable.h"
|
|
||||||
#include "v8/include/v8.h"
|
#include "v8/include/v8.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
// 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 {
|
class IDWeakMap {
|
||||||
public:
|
public:
|
||||||
IDWeakMap();
|
IDWeakMap();
|
||||||
~IDWeakMap();
|
~IDWeakMap();
|
||||||
|
@ -45,10 +43,6 @@ class IDWeakMap : public mate::Wrappable {
|
||||||
// Clears the weak map.
|
// Clears the weak map.
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
protected:
|
|
||||||
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
||||||
v8::Isolate* isolate) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Returns next available ID.
|
// Returns next available ID.
|
||||||
int32_t GetNextID();
|
int32_t GetNextID();
|
||||||
|
|
|
@ -52,6 +52,7 @@ REFERENCE_MODULE(atom_common_native_image);
|
||||||
REFERENCE_MODULE(atom_common_screen);
|
REFERENCE_MODULE(atom_common_screen);
|
||||||
REFERENCE_MODULE(atom_common_shell);
|
REFERENCE_MODULE(atom_common_shell);
|
||||||
REFERENCE_MODULE(atom_common_v8_util);
|
REFERENCE_MODULE(atom_common_v8_util);
|
||||||
|
REFERENCE_MODULE(atom_common_weak_map);
|
||||||
REFERENCE_MODULE(atom_renderer_ipc);
|
REFERENCE_MODULE(atom_renderer_ipc);
|
||||||
REFERENCE_MODULE(atom_renderer_web_frame);
|
REFERENCE_MODULE(atom_renderer_web_frame);
|
||||||
#undef REFERENCE_MODULE
|
#undef REFERENCE_MODULE
|
||||||
|
|
|
@ -260,6 +260,8 @@
|
||||||
'atom/common/api/atom_api_native_image_mac.mm',
|
'atom/common/api/atom_api_native_image_mac.mm',
|
||||||
'atom/common/api/atom_api_shell.cc',
|
'atom/common/api/atom_api_shell.cc',
|
||||||
'atom/common/api/atom_api_v8_util.cc',
|
'atom/common/api/atom_api_v8_util.cc',
|
||||||
|
'atom/common/api/atom_api_weak_map.cc',
|
||||||
|
'atom/common/api/atom_api_weak_map.h',
|
||||||
'atom/common/api/atom_bindings.cc',
|
'atom/common/api/atom_bindings.cc',
|
||||||
'atom/common/api/atom_bindings.h',
|
'atom/common/api/atom_bindings.h',
|
||||||
'atom/common/api/event_emitter_caller.cc',
|
'atom/common/api/event_emitter_caller.cc',
|
||||||
|
|
Loading…
Reference in a new issue