/toolkit/content/Dict.jsm

http://github.com/zpao/v8monkey · Unknown · 268 lines · 246 code · 22 blank · 0 comment · 0 complexity · 7b84e3842b94d77b0e91531edad620b4 MD5 · raw file

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