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/. */
7
#ifndef builtin_MapObject_h
8
#define builtin_MapObject_h
17
* Comparing two ropes for equality can fail. The js::HashTable template
18
* requires infallible hash() and match() operations. Therefore we require
19
* all values to be converted to hashable form before being used as a key
20
* in a Map or Set object.
22
* All values except ropes are hashable as-is.
25
EncapsulatedValue value;
29
typedef HashableValue Lookup;
30
static HashNumber hash(const Lookup &v) { return v.hash(); }
31
static bool match(const HashableValue &k, const Lookup &l) { return k == l; }
32
static bool isEmpty(const HashableValue &v) { return v.value.isMagic(JS_HASH_KEY_EMPTY); }
33
static void makeEmpty(HashableValue *vp) { vp->value = MagicValue(JS_HASH_KEY_EMPTY); }
36
HashableValue() : value(UndefinedValue()) {}
38
bool setValue(JSContext *cx, const Value &v);
39
HashNumber hash() const;
40
bool operator==(const HashableValue &other) const;
41
HashableValue mark(JSTracer *trc) const;
42
Value get() const { return value.get(); }
45
class AutoHashableValueRooter : private AutoGCRooter
48
explicit AutoHashableValueRooter(JSContext *cx
49
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
50
: AutoGCRooter(cx, HASHABLEVALUE)
52
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
55
bool setValue(JSContext *cx, const Value &v) {
56
return value.setValue(cx, v);
59
operator const HashableValue & () {
63
friend void AutoGCRooter::trace(JSTracer *trc);
64
void trace(JSTracer *trc);
68
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
71
template <class Key, class Value, class OrderedHashPolicy, class AllocPolicy>
74
template <class T, class OrderedHashPolicy, class AllocPolicy>
77
typedef OrderedHashMap<HashableValue,
79
HashableValue::Hasher,
80
RuntimeAllocPolicy> ValueMap;
82
typedef OrderedHashSet<HashableValue,
83
HashableValue::Hasher,
84
RuntimeAllocPolicy> ValueSet;
86
class MapObject : public JSObject {
88
enum IteratorKind { Keys, Values, Entries };
90
static JSObject *initClass(JSContext *cx, JSObject *obj);
93
static const JSPropertySpec properties[];
94
static const JSFunctionSpec methods[];
95
ValueMap *getData() { return static_cast<ValueMap *>(getPrivate()); }
96
static ValueMap & extract(CallReceiver call);
97
static void mark(JSTracer *trc, JSObject *obj);
98
static void finalize(FreeOp *fop, JSObject *obj);
99
static JSBool construct(JSContext *cx, unsigned argc, Value *vp);
101
static bool is(const Value &v);
103
static bool iterator_impl(JSContext *cx, CallArgs args, IteratorKind kind);
105
static bool size_impl(JSContext *cx, CallArgs args);
106
static JSBool size(JSContext *cx, unsigned argc, Value *vp);
107
static bool get_impl(JSContext *cx, CallArgs args);
108
static JSBool get(JSContext *cx, unsigned argc, Value *vp);
109
static bool has_impl(JSContext *cx, CallArgs args);
110
static JSBool has(JSContext *cx, unsigned argc, Value *vp);
111
static bool set_impl(JSContext *cx, CallArgs args);
112
static JSBool set(JSContext *cx, unsigned argc, Value *vp);
113
static bool delete_impl(JSContext *cx, CallArgs args);
114
static JSBool delete_(JSContext *cx, unsigned argc, Value *vp);
115
static bool keys_impl(JSContext *cx, CallArgs args);
116
static JSBool keys(JSContext *cx, unsigned argc, Value *vp);
117
static bool values_impl(JSContext *cx, CallArgs args);
118
static JSBool values(JSContext *cx, unsigned argc, Value *vp);
119
static bool entries_impl(JSContext *cx, CallArgs args);
120
static JSBool entries(JSContext *cx, unsigned argc, Value *vp);
121
static bool clear_impl(JSContext *cx, CallArgs args);
122
static JSBool clear(JSContext *cx, unsigned argc, Value *vp);
125
class SetObject : public JSObject {
127
enum IteratorKind { Values, Entries };
128
static JSObject *initClass(JSContext *cx, JSObject *obj);
131
static const JSPropertySpec properties[];
132
static const JSFunctionSpec methods[];
133
ValueSet *getData() { return static_cast<ValueSet *>(getPrivate()); }
134
static ValueSet & extract(CallReceiver call);
135
static void mark(JSTracer *trc, JSObject *obj);
136
static void finalize(FreeOp *fop, JSObject *obj);
137
static JSBool construct(JSContext *cx, unsigned argc, Value *vp);
139
static bool is(const Value &v);
141
static bool iterator_impl(JSContext *cx, CallArgs args, IteratorKind kind);
143
static bool size_impl(JSContext *cx, CallArgs args);
144
static JSBool size(JSContext *cx, unsigned argc, Value *vp);
145
static bool has_impl(JSContext *cx, CallArgs args);
146
static JSBool has(JSContext *cx, unsigned argc, Value *vp);
147
static bool add_impl(JSContext *cx, CallArgs args);
148
static JSBool add(JSContext *cx, unsigned argc, Value *vp);
149
static bool delete_impl(JSContext *cx, CallArgs args);
150
static JSBool delete_(JSContext *cx, unsigned argc, Value *vp);
151
static bool values_impl(JSContext *cx, CallArgs args);
152
static JSBool values(JSContext *cx, unsigned argc, Value *vp);
153
static bool entries_impl(JSContext *cx, CallArgs args);
154
static JSBool entries(JSContext *cx, unsigned argc, Value *vp);
155
static bool clear_impl(JSContext *cx, CallArgs args);
156
static JSBool clear(JSContext *cx, unsigned argc, Value *vp);
162
js_InitMapClass(JSContext *cx, js::HandleObject obj);
165
js_InitSetClass(JSContext *cx, js::HandleObject obj);
167
#endif /* builtin_MapObject_h */