|
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 #ifndef builtin_MapObject_h |
|
8 #define builtin_MapObject_h |
|
9 |
|
10 #include "jsobj.h" |
|
11 |
|
12 #include "vm/Runtime.h" |
|
13 |
|
14 namespace js { |
|
15 |
|
16 /* |
|
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. |
|
21 * |
|
22 * All values except ropes are hashable as-is. |
|
23 */ |
|
24 class HashableValue { |
|
25 EncapsulatedValue value; |
|
26 |
|
27 public: |
|
28 struct Hasher { |
|
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); } |
|
34 }; |
|
35 |
|
36 HashableValue() : value(UndefinedValue()) {} |
|
37 |
|
38 bool setValue(JSContext *cx, HandleValue 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(); } |
|
43 }; |
|
44 |
|
45 class AutoHashableValueRooter : private AutoGCRooter |
|
46 { |
|
47 public: |
|
48 explicit AutoHashableValueRooter(JSContext *cx |
|
49 MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
|
50 : AutoGCRooter(cx, HASHABLEVALUE) |
|
51 { |
|
52 MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
|
53 } |
|
54 |
|
55 bool setValue(JSContext *cx, HandleValue v) { |
|
56 return value.setValue(cx, v); |
|
57 } |
|
58 |
|
59 operator const HashableValue & () { |
|
60 return value; |
|
61 } |
|
62 |
|
63 friend void AutoGCRooter::trace(JSTracer *trc); |
|
64 void trace(JSTracer *trc); |
|
65 |
|
66 private: |
|
67 HashableValue value; |
|
68 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
|
69 }; |
|
70 |
|
71 template <class Key, class Value, class OrderedHashPolicy, class AllocPolicy> |
|
72 class OrderedHashMap; |
|
73 |
|
74 template <class T, class OrderedHashPolicy, class AllocPolicy> |
|
75 class OrderedHashSet; |
|
76 |
|
77 typedef OrderedHashMap<HashableValue, |
|
78 RelocatableValue, |
|
79 HashableValue::Hasher, |
|
80 RuntimeAllocPolicy> ValueMap; |
|
81 |
|
82 typedef OrderedHashSet<HashableValue, |
|
83 HashableValue::Hasher, |
|
84 RuntimeAllocPolicy> ValueSet; |
|
85 |
|
86 class MapObject : public JSObject { |
|
87 public: |
|
88 enum IteratorKind { Keys, Values, Entries }; |
|
89 |
|
90 static JSObject *initClass(JSContext *cx, JSObject *obj); |
|
91 static const Class class_; |
|
92 private: |
|
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 bool construct(JSContext *cx, unsigned argc, Value *vp); |
|
100 |
|
101 static bool is(HandleValue v); |
|
102 |
|
103 static bool iterator_impl(JSContext *cx, CallArgs args, IteratorKind kind); |
|
104 |
|
105 static bool size_impl(JSContext *cx, CallArgs args); |
|
106 static bool size(JSContext *cx, unsigned argc, Value *vp); |
|
107 static bool get_impl(JSContext *cx, CallArgs args); |
|
108 static bool get(JSContext *cx, unsigned argc, Value *vp); |
|
109 static bool has_impl(JSContext *cx, CallArgs args); |
|
110 static bool has(JSContext *cx, unsigned argc, Value *vp); |
|
111 static bool set_impl(JSContext *cx, CallArgs args); |
|
112 static bool set(JSContext *cx, unsigned argc, Value *vp); |
|
113 static bool delete_impl(JSContext *cx, CallArgs args); |
|
114 static bool delete_(JSContext *cx, unsigned argc, Value *vp); |
|
115 static bool keys_impl(JSContext *cx, CallArgs args); |
|
116 static bool keys(JSContext *cx, unsigned argc, Value *vp); |
|
117 static bool values_impl(JSContext *cx, CallArgs args); |
|
118 static bool values(JSContext *cx, unsigned argc, Value *vp); |
|
119 static bool entries_impl(JSContext *cx, CallArgs args); |
|
120 static bool entries(JSContext *cx, unsigned argc, Value *vp); |
|
121 static bool clear_impl(JSContext *cx, CallArgs args); |
|
122 static bool clear(JSContext *cx, unsigned argc, Value *vp); |
|
123 }; |
|
124 |
|
125 class SetObject : public JSObject { |
|
126 public: |
|
127 enum IteratorKind { Values, Entries }; |
|
128 static JSObject *initClass(JSContext *cx, JSObject *obj); |
|
129 static const Class class_; |
|
130 private: |
|
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 bool construct(JSContext *cx, unsigned argc, Value *vp); |
|
138 |
|
139 static bool is(HandleValue v); |
|
140 |
|
141 static bool iterator_impl(JSContext *cx, CallArgs args, IteratorKind kind); |
|
142 |
|
143 static bool size_impl(JSContext *cx, CallArgs args); |
|
144 static bool size(JSContext *cx, unsigned argc, Value *vp); |
|
145 static bool has_impl(JSContext *cx, CallArgs args); |
|
146 static bool has(JSContext *cx, unsigned argc, Value *vp); |
|
147 static bool add_impl(JSContext *cx, CallArgs args); |
|
148 static bool add(JSContext *cx, unsigned argc, Value *vp); |
|
149 static bool delete_impl(JSContext *cx, CallArgs args); |
|
150 static bool delete_(JSContext *cx, unsigned argc, Value *vp); |
|
151 static bool values_impl(JSContext *cx, CallArgs args); |
|
152 static bool values(JSContext *cx, unsigned argc, Value *vp); |
|
153 static bool entries_impl(JSContext *cx, CallArgs args); |
|
154 static bool entries(JSContext *cx, unsigned argc, Value *vp); |
|
155 static bool clear_impl(JSContext *cx, CallArgs args); |
|
156 static bool clear(JSContext *cx, unsigned argc, Value *vp); |
|
157 }; |
|
158 |
|
159 } /* namespace js */ |
|
160 |
|
161 extern JSObject * |
|
162 js_InitMapClass(JSContext *cx, js::HandleObject obj); |
|
163 |
|
164 extern JSObject * |
|
165 js_InitSetClass(JSContext *cx, js::HandleObject obj); |
|
166 |
|
167 #endif /* builtin_MapObject_h */ |