Bug 1874684 - Part 10: Replace BigInt with Int128 in RoundNumberToIncrement. r=mgaudet
[gecko.git] / js / src / gc / WeakMapPtr.cpp
blob0910e7d3be61a2bd87b58485661c90290cf8670d
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.
16 using namespace js;
18 namespace WeakMapDetails {
20 template <typename T>
21 struct DataType {};
23 template <>
24 struct DataType<JSObject*> {
25 using BarrieredType = HeapPtr<JSObject*>;
26 using HasherType = StableCellHasher<BarrieredType>;
27 static JSObject* NullValue() { return nullptr; }
30 template <>
31 struct DataType<JS::Value> {
32 using BarrieredType = HeapPtr<Value>;
33 static JS::Value NullValue() { return JS::UndefinedValue(); }
36 template <typename K, typename V>
37 struct Utils {
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));
51 ptr = nullptr;
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);
59 if (!map) {
60 return false;
62 ptr = map;
63 return true;
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);
77 if (!result) {
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();
99 map->remove(result);
100 return value;
102 return WeakMapDetails::DataType<V>::NullValue();
106 // Supported specializations of JS::WeakMap:
109 template class JS_PUBLIC_API JS::WeakMapPtr<JSObject*, JSObject*>;
111 #ifdef DEBUG
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>;
114 #endif