blob: e95ff88c3afcff8ddc4a621c31a10fa67ec23401 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BINDINGS_V8_DOM_WRAPPER_MAP
#define BINDINGS_V8_DOM_WRAPPER_MAP
#include <v8.h>
#include <wtf/HashMap.h>
// A table of wrappers with weak pointers.
// This table allows us to avoid track wrapped objects for debugging
// and for ensuring that we don't double wrap the same object.
template<class KeyType, class ValueType>
class WeakReferenceMap {
public:
WeakReferenceMap(v8::WeakReferenceCallback callback) :
weak_reference_callback_(callback) { }
#ifndef NDEBUG
virtual ~WeakReferenceMap() {
if (map_.size() > 0) {
fprintf(stderr, "Leak %d JS wrappers.\n", map_.size());
// Print out details.
}
}
#endif
// Get the JS wrapper object of an object.
virtual v8::Persistent<ValueType> get(KeyType* obj) {
ValueType* wrapper = map_.get(obj);
return wrapper ? v8::Persistent<ValueType>(wrapper)
: v8::Persistent<ValueType>();
}
virtual void set(KeyType* obj, v8::Persistent<ValueType> wrapper) {
ASSERT(!map_.contains(obj));
wrapper.MakeWeak(obj, weak_reference_callback_);
map_.set(obj, *wrapper);
}
virtual void forget(KeyType* obj) {
ASSERT(obj);
ValueType* wrapper = map_.take(obj);
if (wrapper) {
v8::Persistent<ValueType> handle(wrapper);
handle.Dispose();
handle.Clear();
}
}
bool contains(KeyType* obj) {
return map_.contains(obj);
}
HashMap<KeyType*, ValueType*>& impl() {
return map_;
}
protected:
HashMap<KeyType*, ValueType*> map_;
v8::WeakReferenceCallback weak_reference_callback_;
};
template <class KeyType>
class DOMWrapperMap : public WeakReferenceMap<KeyType, v8::Object> {
public:
DOMWrapperMap(v8::WeakReferenceCallback callback) :
WeakReferenceMap<KeyType, v8::Object>(callback) { }
};
#endif // BINDINGS_V8_DOM_WRAPPER_MAP
|