1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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/. */
7 #include "js/WeakMapPtr.h"
9 #include "gc/WeakMap-inl.h"
12 // Machinery for the externally-linkable JS::WeakMapPtr, which wraps js::WeakMap
13 // for a few public data types.
18 namespace WeakMapDetails
{
24 struct DataType
<JSObject
*> {
25 using BarrieredType
= HeapPtr
<JSObject
*>;
26 using HasherType
= StableCellHasher
<BarrieredType
>;
27 static JSObject
* NullValue() { return nullptr; }
31 struct DataType
<JS::Value
> {
32 using BarrieredType
= HeapPtr
<Value
>;
33 static JS::Value
NullValue() { return JS::UndefinedValue(); }
36 template <typename K
, typename V
>
38 using KeyType
= typename DataType
<K
>::BarrieredType
;
39 using ValueType
= typename DataType
<V
>::BarrieredType
;
40 using Type
= WeakMap
<KeyType
, ValueType
>;
41 using PtrType
= Type
*;
42 static PtrType
cast(void* ptr
) { return static_cast<PtrType
>(ptr
); }
45 } // namespace WeakMapDetails
47 template <typename K
, typename V
>
48 void JS::WeakMapPtr
<K
, V
>::destroy() {
49 MOZ_ASSERT(initialized());
50 js_delete(WeakMapDetails::Utils
<K
, V
>::cast(ptr
));
54 template <typename K
, typename V
>
55 bool JS::WeakMapPtr
<K
, V
>::init(JSContext
* cx
) {
56 MOZ_ASSERT(!initialized());
57 typename
WeakMapDetails::Utils
<K
, V
>::PtrType map
=
58 cx
->new_
<typename
WeakMapDetails::Utils
<K
, V
>::Type
>(cx
);
66 template <typename K
, typename V
>
67 void JS::WeakMapPtr
<K
, V
>::trace(JSTracer
* trc
) {
68 MOZ_ASSERT(initialized());
69 return WeakMapDetails::Utils
<K
, V
>::cast(ptr
)->trace(trc
);
72 template <typename K
, typename V
>
73 V
JS::WeakMapPtr
<K
, V
>::lookup(const K
& key
) {
74 MOZ_ASSERT(initialized());
75 typename
WeakMapDetails::Utils
<K
, V
>::Type::Ptr result
=
76 WeakMapDetails::Utils
<K
, V
>::cast(ptr
)->lookup(key
);
78 return WeakMapDetails::DataType
<V
>::NullValue();
80 return result
->value();
83 template <typename K
, typename V
>
84 bool JS::WeakMapPtr
<K
, V
>::put(JSContext
* cx
, const K
& key
, const V
& value
) {
85 MOZ_ASSERT(initialized());
86 return WeakMapDetails::Utils
<K
, V
>::cast(ptr
)->put(key
, value
);
89 template <typename K
, typename V
>
90 V
JS::WeakMapPtr
<K
, V
>::removeValue(const K
& key
) {
91 using Map
= typename
WeakMapDetails::Utils
<K
, V
>::Type
;
92 using Ptr
= typename
Map::Ptr
;
94 MOZ_ASSERT(initialized());
96 Map
* map
= WeakMapDetails::Utils
<K
, V
>::cast(ptr
);
97 if (Ptr result
= map
->lookup(key
)) {
98 V value
= result
->value();
102 return WeakMapDetails::DataType
<V
>::NullValue();
106 // Supported specializations of JS::WeakMap:
109 template class JS_PUBLIC_API
JS::WeakMapPtr
<JSObject
*, JSObject
*>;
112 // Nobody's using this at the moment, but we want to make sure it compiles.
113 template class JS_PUBLIC_API
JS::WeakMapPtr
<JSObject
*, JS::Value
>;