Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ["Dict"]; |
michael@0 | 8 | |
michael@0 | 9 | /** |
michael@0 | 10 | * Transforms a given key into a property name guaranteed not to collide with |
michael@0 | 11 | * any built-ins. |
michael@0 | 12 | */ |
michael@0 | 13 | function convert(aKey) { |
michael@0 | 14 | return ":" + aKey; |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * Transforms a property into a key suitable for providing to the outside world. |
michael@0 | 19 | */ |
michael@0 | 20 | function unconvert(aProp) { |
michael@0 | 21 | return aProp.substr(1); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * A dictionary of strings to arbitrary JS objects. This should be used whenever |
michael@0 | 26 | * the keys are potentially arbitrary, to avoid collisions with built-in |
michael@0 | 27 | * properties. |
michael@0 | 28 | * |
michael@0 | 29 | * @param aInitial An object containing the initial keys and values of this |
michael@0 | 30 | * dictionary. Only the "own" enumerable properties of the |
michael@0 | 31 | * object are considered. |
michael@0 | 32 | * If |aInitial| is a string, it is assumed to be JSON and parsed into an object. |
michael@0 | 33 | */ |
michael@0 | 34 | this.Dict = function Dict(aInitial) { |
michael@0 | 35 | if (aInitial === undefined) |
michael@0 | 36 | aInitial = {}; |
michael@0 | 37 | if (typeof aInitial == "string") |
michael@0 | 38 | aInitial = JSON.parse(aInitial); |
michael@0 | 39 | var items = {}, count = 0; |
michael@0 | 40 | // That we don't look up the prototype chain is guaranteed by Iterator. |
michael@0 | 41 | for (var [key, val] in Iterator(aInitial)) { |
michael@0 | 42 | items[convert(key)] = val; |
michael@0 | 43 | count++; |
michael@0 | 44 | } |
michael@0 | 45 | this._state = {count: count, items: items}; |
michael@0 | 46 | return Object.freeze(this); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | Dict.prototype = Object.freeze({ |
michael@0 | 50 | /** |
michael@0 | 51 | * The number of items in the dictionary. |
michael@0 | 52 | */ |
michael@0 | 53 | get count() { |
michael@0 | 54 | return this._state.count; |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * Gets the value for a key from the dictionary. If the key is not a string, |
michael@0 | 59 | * it will be converted to a string before the lookup happens. |
michael@0 | 60 | * |
michael@0 | 61 | * @param aKey The key to look up |
michael@0 | 62 | * @param [aDefault] An optional default value to return if the key is not |
michael@0 | 63 | * present. Defaults to |undefined|. |
michael@0 | 64 | * @returns The item, or aDefault if it isn't found. |
michael@0 | 65 | */ |
michael@0 | 66 | get: function Dict_get(aKey, aDefault) { |
michael@0 | 67 | var prop = convert(aKey); |
michael@0 | 68 | var items = this._state.items; |
michael@0 | 69 | return items.hasOwnProperty(prop) ? items[prop] : aDefault; |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * Sets the value for a key in the dictionary. If the key is a not a string, |
michael@0 | 74 | * it will be converted to a string before the set happens. |
michael@0 | 75 | */ |
michael@0 | 76 | set: function Dict_set(aKey, aValue) { |
michael@0 | 77 | var prop = convert(aKey); |
michael@0 | 78 | var items = this._state.items; |
michael@0 | 79 | if (!items.hasOwnProperty(prop)) |
michael@0 | 80 | this._state.count++; |
michael@0 | 81 | items[prop] = aValue; |
michael@0 | 82 | }, |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Sets a lazy getter function for a key's value. If the key is a not a string, |
michael@0 | 86 | * it will be converted to a string before the set happens. |
michael@0 | 87 | * @param aKey |
michael@0 | 88 | * The key to set |
michael@0 | 89 | * @param aThunk |
michael@0 | 90 | * A getter function to be called the first time the value for aKey is |
michael@0 | 91 | * retrieved. It is guaranteed that aThunk wouldn't be called more |
michael@0 | 92 | * than once. Note that the key value may be retrieved either |
michael@0 | 93 | * directly, by |get|, or indirectly, by |listvalues| or by iterating |
michael@0 | 94 | * |values|. For the later, the value is only retrieved if and when |
michael@0 | 95 | * the iterator gets to the value in question. Also note that calling |
michael@0 | 96 | * |has| for a lazy-key does not invoke aThunk. |
michael@0 | 97 | * |
michael@0 | 98 | * @note No context is provided for aThunk when it's invoked. |
michael@0 | 99 | * Use Function.bind if you wish to run it in a certain context. |
michael@0 | 100 | */ |
michael@0 | 101 | setAsLazyGetter: function Dict_setAsLazyGetter(aKey, aThunk) { |
michael@0 | 102 | let prop = convert(aKey); |
michael@0 | 103 | let items = this._state.items; |
michael@0 | 104 | if (!items.hasOwnProperty(prop)) |
michael@0 | 105 | this._state.count++; |
michael@0 | 106 | |
michael@0 | 107 | Object.defineProperty(items, prop, { |
michael@0 | 108 | get: function() { |
michael@0 | 109 | delete items[prop]; |
michael@0 | 110 | return items[prop] = aThunk(); |
michael@0 | 111 | }, |
michael@0 | 112 | configurable: true, |
michael@0 | 113 | enumerable: true |
michael@0 | 114 | }); |
michael@0 | 115 | }, |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * Returns whether a key is set as a lazy getter. This returns |
michael@0 | 119 | * true only if the getter function was not called already. |
michael@0 | 120 | * @param aKey |
michael@0 | 121 | * The key to look up. |
michael@0 | 122 | * @returns whether aKey is set as a lazy getter. |
michael@0 | 123 | */ |
michael@0 | 124 | isLazyGetter: function Dict_isLazyGetter(aKey) { |
michael@0 | 125 | let descriptor = Object.getOwnPropertyDescriptor(this._state.items, |
michael@0 | 126 | convert(aKey)); |
michael@0 | 127 | return (descriptor && descriptor.get != null); |
michael@0 | 128 | }, |
michael@0 | 129 | |
michael@0 | 130 | /** |
michael@0 | 131 | * Returns whether a key is in the dictionary. If the key is a not a string, |
michael@0 | 132 | * it will be converted to a string before the lookup happens. |
michael@0 | 133 | */ |
michael@0 | 134 | has: function Dict_has(aKey) { |
michael@0 | 135 | return (this._state.items.hasOwnProperty(convert(aKey))); |
michael@0 | 136 | }, |
michael@0 | 137 | |
michael@0 | 138 | /** |
michael@0 | 139 | * Deletes a key from the dictionary. If the key is a not a string, it will be |
michael@0 | 140 | * converted to a string before the delete happens. |
michael@0 | 141 | * |
michael@0 | 142 | * @returns true if the key was found, false if it wasn't. |
michael@0 | 143 | */ |
michael@0 | 144 | del: function Dict_del(aKey) { |
michael@0 | 145 | var prop = convert(aKey); |
michael@0 | 146 | if (this._state.items.hasOwnProperty(prop)) { |
michael@0 | 147 | delete this._state.items[prop]; |
michael@0 | 148 | this._state.count--; |
michael@0 | 149 | return true; |
michael@0 | 150 | } |
michael@0 | 151 | return false; |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | /** |
michael@0 | 155 | * Returns a shallow copy of this dictionary. |
michael@0 | 156 | */ |
michael@0 | 157 | copy: function Dict_copy() { |
michael@0 | 158 | var newItems = {}; |
michael@0 | 159 | for (var [key, val] in this.items) |
michael@0 | 160 | newItems[key] = val; |
michael@0 | 161 | return new Dict(newItems); |
michael@0 | 162 | }, |
michael@0 | 163 | |
michael@0 | 164 | /* |
michael@0 | 165 | * List and iterator functions |
michael@0 | 166 | * |
michael@0 | 167 | * No guarantees whatsoever are made about the order of elements. |
michael@0 | 168 | */ |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * Returns a list of all the keys in the dictionary in an arbitrary order. |
michael@0 | 172 | */ |
michael@0 | 173 | listkeys: function Dict_listkeys() { |
michael@0 | 174 | return [unconvert(k) for (k in this._state.items)]; |
michael@0 | 175 | }, |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * Returns a list of all the values in the dictionary in an arbitrary order. |
michael@0 | 179 | */ |
michael@0 | 180 | listvalues: function Dict_listvalues() { |
michael@0 | 181 | var items = this._state.items; |
michael@0 | 182 | return [items[k] for (k in items)]; |
michael@0 | 183 | }, |
michael@0 | 184 | |
michael@0 | 185 | /** |
michael@0 | 186 | * Returns a list of all the items in the dictionary as key-value pairs |
michael@0 | 187 | * in an arbitrary order. |
michael@0 | 188 | */ |
michael@0 | 189 | listitems: function Dict_listitems() { |
michael@0 | 190 | var items = this._state.items; |
michael@0 | 191 | return [[unconvert(k), items[k]] for (k in items)]; |
michael@0 | 192 | }, |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * Returns an iterator over all the keys in the dictionary in an arbitrary |
michael@0 | 196 | * order. No guarantees are made about what happens if the dictionary is |
michael@0 | 197 | * mutated during iteration. |
michael@0 | 198 | */ |
michael@0 | 199 | get keys() { |
michael@0 | 200 | // If we don't capture this._state.items here then the this-binding will be |
michael@0 | 201 | // incorrect when the generator is executed |
michael@0 | 202 | var items = this._state.items; |
michael@0 | 203 | return (unconvert(k) for (k in items)); |
michael@0 | 204 | }, |
michael@0 | 205 | |
michael@0 | 206 | /** |
michael@0 | 207 | * Returns an iterator over all the values in the dictionary in an arbitrary |
michael@0 | 208 | * order. No guarantees are made about what happens if the dictionary is |
michael@0 | 209 | * mutated during iteration. |
michael@0 | 210 | */ |
michael@0 | 211 | get values() { |
michael@0 | 212 | // If we don't capture this._state.items here then the this-binding will be |
michael@0 | 213 | // incorrect when the generator is executed |
michael@0 | 214 | var items = this._state.items; |
michael@0 | 215 | return (items[k] for (k in items)); |
michael@0 | 216 | }, |
michael@0 | 217 | |
michael@0 | 218 | /** |
michael@0 | 219 | * Returns an iterator over all the items in the dictionary as key-value pairs |
michael@0 | 220 | * in an arbitrary order. No guarantees are made about what happens if the |
michael@0 | 221 | * dictionary is mutated during iteration. |
michael@0 | 222 | */ |
michael@0 | 223 | get items() { |
michael@0 | 224 | // If we don't capture this._state.items here then the this-binding will be |
michael@0 | 225 | // incorrect when the generator is executed |
michael@0 | 226 | var items = this._state.items; |
michael@0 | 227 | return ([unconvert(k), items[k]] for (k in items)); |
michael@0 | 228 | }, |
michael@0 | 229 | |
michael@0 | 230 | /** |
michael@0 | 231 | * Returns a String representation of this dictionary. |
michael@0 | 232 | */ |
michael@0 | 233 | toString: function Dict_toString() { |
michael@0 | 234 | return "{" + |
michael@0 | 235 | [(key + ": " + val) for ([key, val] in this.items)].join(", ") + |
michael@0 | 236 | "}"; |
michael@0 | 237 | }, |
michael@0 | 238 | |
michael@0 | 239 | /** |
michael@0 | 240 | * Returns a JSON representation of this dictionary. |
michael@0 | 241 | */ |
michael@0 | 242 | toJSON: function Dict_toJSON() { |
michael@0 | 243 | let obj = {}; |
michael@0 | 244 | for (let [key, item] of Iterator(this._state.items)) { |
michael@0 | 245 | obj[unconvert(key)] = item; |
michael@0 | 246 | } |
michael@0 | 247 | return JSON.stringify(obj); |
michael@0 | 248 | }, |
michael@0 | 249 | }); |