Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "js/WeakMapPtr.h"
8 :
9 : #include "jsweakmap.h"
10 :
11 : //
12 : // Machinery for the externally-linkable JS::WeakMapPtr, which wraps js::WeakMap
13 : // for a few public data types.
14 : //
15 :
16 : using namespace js;
17 :
18 : namespace details {
19 :
20 : template<typename T>
21 : struct DataType
22 : {
23 : };
24 :
25 : template<>
26 : struct DataType<JSObject*>
27 : {
28 : using BarrieredType = HeapPtr<JSObject*>;
29 : using HasherType = MovableCellHasher<BarrieredType>;
30 0 : static JSObject* NullValue() { return nullptr; }
31 : };
32 :
33 : template<>
34 : struct DataType<JS::Value>
35 : {
36 : using BarrieredType = HeapPtr<Value>;
37 0 : static JS::Value NullValue() { return JS::UndefinedValue(); }
38 : };
39 :
40 : template <typename K, typename V>
41 : struct Utils
42 : {
43 : typedef typename DataType<K>::BarrieredType KeyType;
44 : typedef typename DataType<K>::HasherType HasherType;
45 : typedef typename DataType<V>::BarrieredType ValueType;
46 : typedef WeakMap<KeyType, ValueType, HasherType> Type;
47 : typedef Type* PtrType;
48 0 : static PtrType cast(void* ptr) { return static_cast<PtrType>(ptr); }
49 : };
50 :
51 : } /* namespace */
52 :
53 : template <typename K, typename V>
54 : void
55 0 : JS::WeakMapPtr<K, V>::destroy()
56 : {
57 0 : MOZ_ASSERT(initialized());
58 0 : js_delete(details::Utils<K, V>::cast(ptr));
59 0 : ptr = nullptr;
60 0 : }
61 :
62 : template <typename K, typename V>
63 : bool
64 0 : JS::WeakMapPtr<K, V>::init(JSContext* cx)
65 : {
66 0 : MOZ_ASSERT(!initialized());
67 0 : typename details::Utils<K, V>::PtrType map = cx->runtime()->new_<typename details::Utils<K,V>::Type>(cx);
68 0 : if (!map || !map->init())
69 0 : return false;
70 0 : ptr = map;
71 0 : return true;
72 : }
73 :
74 : template <typename K, typename V>
75 : void
76 0 : JS::WeakMapPtr<K, V>::trace(JSTracer* trc)
77 : {
78 0 : MOZ_ASSERT(initialized());
79 0 : return details::Utils<K, V>::cast(ptr)->trace(trc);
80 : }
81 :
82 : template <typename K, typename V>
83 : V
84 0 : JS::WeakMapPtr<K, V>::lookup(const K& key)
85 : {
86 0 : MOZ_ASSERT(initialized());
87 0 : typename details::Utils<K, V>::Type::Ptr result = details::Utils<K, V>::cast(ptr)->lookup(key);
88 0 : if (!result)
89 0 : return details::DataType<V>::NullValue();
90 0 : return result->value();
91 : }
92 :
93 : template <typename K, typename V>
94 : bool
95 0 : JS::WeakMapPtr<K, V>::put(JSContext* cx, const K& key, const V& value)
96 : {
97 0 : MOZ_ASSERT(initialized());
98 0 : return details::Utils<K, V>::cast(ptr)->put(key, value);
99 : }
100 :
101 : //
102 : // Supported specializations of JS::WeakMap:
103 : //
104 :
105 : template class JS_PUBLIC_API(JS::WeakMapPtr)<JSObject*, JSObject*>;
106 :
107 : #ifdef DEBUG
108 : // Nobody's using this at the moment, but we want to make sure it compiles.
109 : template class JS_PUBLIC_API(JS::WeakMapPtr)<JSObject*, JS::Value>;
110 : #endif
|