js/src/builtin/MapObject.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef builtin_MapObject_h
michael@0 8 #define builtin_MapObject_h
michael@0 9
michael@0 10 #include "jsobj.h"
michael@0 11
michael@0 12 #include "vm/Runtime.h"
michael@0 13
michael@0 14 namespace js {
michael@0 15
michael@0 16 /*
michael@0 17 * Comparing two ropes for equality can fail. The js::HashTable template
michael@0 18 * requires infallible hash() and match() operations. Therefore we require
michael@0 19 * all values to be converted to hashable form before being used as a key
michael@0 20 * in a Map or Set object.
michael@0 21 *
michael@0 22 * All values except ropes are hashable as-is.
michael@0 23 */
michael@0 24 class HashableValue {
michael@0 25 EncapsulatedValue value;
michael@0 26
michael@0 27 public:
michael@0 28 struct Hasher {
michael@0 29 typedef HashableValue Lookup;
michael@0 30 static HashNumber hash(const Lookup &v) { return v.hash(); }
michael@0 31 static bool match(const HashableValue &k, const Lookup &l) { return k == l; }
michael@0 32 static bool isEmpty(const HashableValue &v) { return v.value.isMagic(JS_HASH_KEY_EMPTY); }
michael@0 33 static void makeEmpty(HashableValue *vp) { vp->value = MagicValue(JS_HASH_KEY_EMPTY); }
michael@0 34 };
michael@0 35
michael@0 36 HashableValue() : value(UndefinedValue()) {}
michael@0 37
michael@0 38 bool setValue(JSContext *cx, HandleValue v);
michael@0 39 HashNumber hash() const;
michael@0 40 bool operator==(const HashableValue &other) const;
michael@0 41 HashableValue mark(JSTracer *trc) const;
michael@0 42 Value get() const { return value.get(); }
michael@0 43 };
michael@0 44
michael@0 45 class AutoHashableValueRooter : private AutoGCRooter
michael@0 46 {
michael@0 47 public:
michael@0 48 explicit AutoHashableValueRooter(JSContext *cx
michael@0 49 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
michael@0 50 : AutoGCRooter(cx, HASHABLEVALUE)
michael@0 51 {
michael@0 52 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 53 }
michael@0 54
michael@0 55 bool setValue(JSContext *cx, HandleValue v) {
michael@0 56 return value.setValue(cx, v);
michael@0 57 }
michael@0 58
michael@0 59 operator const HashableValue & () {
michael@0 60 return value;
michael@0 61 }
michael@0 62
michael@0 63 friend void AutoGCRooter::trace(JSTracer *trc);
michael@0 64 void trace(JSTracer *trc);
michael@0 65
michael@0 66 private:
michael@0 67 HashableValue value;
michael@0 68 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
michael@0 69 };
michael@0 70
michael@0 71 template <class Key, class Value, class OrderedHashPolicy, class AllocPolicy>
michael@0 72 class OrderedHashMap;
michael@0 73
michael@0 74 template <class T, class OrderedHashPolicy, class AllocPolicy>
michael@0 75 class OrderedHashSet;
michael@0 76
michael@0 77 typedef OrderedHashMap<HashableValue,
michael@0 78 RelocatableValue,
michael@0 79 HashableValue::Hasher,
michael@0 80 RuntimeAllocPolicy> ValueMap;
michael@0 81
michael@0 82 typedef OrderedHashSet<HashableValue,
michael@0 83 HashableValue::Hasher,
michael@0 84 RuntimeAllocPolicy> ValueSet;
michael@0 85
michael@0 86 class MapObject : public JSObject {
michael@0 87 public:
michael@0 88 enum IteratorKind { Keys, Values, Entries };
michael@0 89
michael@0 90 static JSObject *initClass(JSContext *cx, JSObject *obj);
michael@0 91 static const Class class_;
michael@0 92 private:
michael@0 93 static const JSPropertySpec properties[];
michael@0 94 static const JSFunctionSpec methods[];
michael@0 95 ValueMap *getData() { return static_cast<ValueMap *>(getPrivate()); }
michael@0 96 static ValueMap & extract(CallReceiver call);
michael@0 97 static void mark(JSTracer *trc, JSObject *obj);
michael@0 98 static void finalize(FreeOp *fop, JSObject *obj);
michael@0 99 static bool construct(JSContext *cx, unsigned argc, Value *vp);
michael@0 100
michael@0 101 static bool is(HandleValue v);
michael@0 102
michael@0 103 static bool iterator_impl(JSContext *cx, CallArgs args, IteratorKind kind);
michael@0 104
michael@0 105 static bool size_impl(JSContext *cx, CallArgs args);
michael@0 106 static bool size(JSContext *cx, unsigned argc, Value *vp);
michael@0 107 static bool get_impl(JSContext *cx, CallArgs args);
michael@0 108 static bool get(JSContext *cx, unsigned argc, Value *vp);
michael@0 109 static bool has_impl(JSContext *cx, CallArgs args);
michael@0 110 static bool has(JSContext *cx, unsigned argc, Value *vp);
michael@0 111 static bool set_impl(JSContext *cx, CallArgs args);
michael@0 112 static bool set(JSContext *cx, unsigned argc, Value *vp);
michael@0 113 static bool delete_impl(JSContext *cx, CallArgs args);
michael@0 114 static bool delete_(JSContext *cx, unsigned argc, Value *vp);
michael@0 115 static bool keys_impl(JSContext *cx, CallArgs args);
michael@0 116 static bool keys(JSContext *cx, unsigned argc, Value *vp);
michael@0 117 static bool values_impl(JSContext *cx, CallArgs args);
michael@0 118 static bool values(JSContext *cx, unsigned argc, Value *vp);
michael@0 119 static bool entries_impl(JSContext *cx, CallArgs args);
michael@0 120 static bool entries(JSContext *cx, unsigned argc, Value *vp);
michael@0 121 static bool clear_impl(JSContext *cx, CallArgs args);
michael@0 122 static bool clear(JSContext *cx, unsigned argc, Value *vp);
michael@0 123 };
michael@0 124
michael@0 125 class SetObject : public JSObject {
michael@0 126 public:
michael@0 127 enum IteratorKind { Values, Entries };
michael@0 128 static JSObject *initClass(JSContext *cx, JSObject *obj);
michael@0 129 static const Class class_;
michael@0 130 private:
michael@0 131 static const JSPropertySpec properties[];
michael@0 132 static const JSFunctionSpec methods[];
michael@0 133 ValueSet *getData() { return static_cast<ValueSet *>(getPrivate()); }
michael@0 134 static ValueSet & extract(CallReceiver call);
michael@0 135 static void mark(JSTracer *trc, JSObject *obj);
michael@0 136 static void finalize(FreeOp *fop, JSObject *obj);
michael@0 137 static bool construct(JSContext *cx, unsigned argc, Value *vp);
michael@0 138
michael@0 139 static bool is(HandleValue v);
michael@0 140
michael@0 141 static bool iterator_impl(JSContext *cx, CallArgs args, IteratorKind kind);
michael@0 142
michael@0 143 static bool size_impl(JSContext *cx, CallArgs args);
michael@0 144 static bool size(JSContext *cx, unsigned argc, Value *vp);
michael@0 145 static bool has_impl(JSContext *cx, CallArgs args);
michael@0 146 static bool has(JSContext *cx, unsigned argc, Value *vp);
michael@0 147 static bool add_impl(JSContext *cx, CallArgs args);
michael@0 148 static bool add(JSContext *cx, unsigned argc, Value *vp);
michael@0 149 static bool delete_impl(JSContext *cx, CallArgs args);
michael@0 150 static bool delete_(JSContext *cx, unsigned argc, Value *vp);
michael@0 151 static bool values_impl(JSContext *cx, CallArgs args);
michael@0 152 static bool values(JSContext *cx, unsigned argc, Value *vp);
michael@0 153 static bool entries_impl(JSContext *cx, CallArgs args);
michael@0 154 static bool entries(JSContext *cx, unsigned argc, Value *vp);
michael@0 155 static bool clear_impl(JSContext *cx, CallArgs args);
michael@0 156 static bool clear(JSContext *cx, unsigned argc, Value *vp);
michael@0 157 };
michael@0 158
michael@0 159 } /* namespace js */
michael@0 160
michael@0 161 extern JSObject *
michael@0 162 js_InitMapClass(JSContext *cx, js::HandleObject obj);
michael@0 163
michael@0 164 extern JSObject *
michael@0 165 js_InitSetClass(JSContext *cx, js::HandleObject obj);
michael@0 166
michael@0 167 #endif /* builtin_MapObject_h */

mercurial