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

@ -3,7 +3,9 @@
// found in the LICENSE file.
#include "atom/common/api/object_life_monitor.h"
#include "atom/common/id_weak_map.h"
#include "atom/common/node_includes.h"
#include "native_mate/handle.h"
#include "native_mate/dictionary.h"
#include "v8/include/v8-profiler.h"
@ -46,6 +48,11 @@ void TakeHeapSnapshot(v8::Isolate* isolate) {
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,
v8::Local<v8::Context> context, void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
@ -56,6 +63,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
dict.SetMethod("getObjectHash", &GetObjectHash);
dict.SetMethod("setDestructor", &SetDestructor);
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
dict.SetMethod("createWeakMap", &CreateWeakMap);
}
} // namespace

View file

@ -1,3 +1,5 @@
v8Util = process.atomBinding 'v8_util'
module.exports =
class CallbacksRegistry
constructor: ->
@ -7,10 +9,6 @@ class CallbacksRegistry
add: (callback) ->
id = ++@nextId
for id,value of @callbacks
if value == callback
return id
# Capture the location of the function and put it in the ID string,
# so that release errors can be tracked down easily.
regexp = /at (.*)/gi
@ -21,10 +19,14 @@ class CallbacksRegistry
continue if location.indexOf('(native)') isnt -1
continue if location.indexOf('atom.asar') isnt -1
[x, filenameAndLine] = /([^/^\)]*)\)?$/gi.exec(location)
id = "#{filenameAndLine} (#{id})"
break
if v8Util.getHiddenValue(callback, 'metaId')?
return v8Util.getHiddenValue(callback, 'metaId')
@callbacks[id] = callback
v8Util.setHiddenValue callback, 'metaId', id
v8Util.setHiddenValue callback, 'location', filenameAndLine
id
get: (id) ->