utils.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. var nativeDeepCopy = requireNative('utils').deepCopy;
  5. /**
  6. * An object forEach. Calls |f| with each (key, value) pair of |obj|, using
  7. * |self| as the target.
  8. * @param {Object} obj The object to iterate over.
  9. * @param {function} f The function to call in each iteration.
  10. * @param {Object} self The object to use as |this| in each function call.
  11. */
  12. function forEach(obj, f, self) {
  13. for (var key in obj) {
  14. if ($Object.hasOwnProperty(obj, key))
  15. $Function.call(f, self, key, obj[key]);
  16. }
  17. }
  18. /**
  19. * Assuming |array_of_dictionaries| is structured like this:
  20. * [{id: 1, ... }, {id: 2, ...}, ...], you can use
  21. * lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2.
  22. * @param {Array<Object<?>>} array_of_dictionaries
  23. * @param {string} field
  24. * @param {?} value
  25. */
  26. function lookup(array_of_dictionaries, field, value) {
  27. var filter = function (dict) {return dict[field] == value;};
  28. var matches = $Array.filter(array_of_dictionaries, filter);
  29. if (matches.length == 0) {
  30. return undefined;
  31. } else if (matches.length == 1) {
  32. return matches[0]
  33. } else {
  34. throw new Error("Failed lookup of field '" + field + "' with value '" +
  35. value + "'");
  36. }
  37. }
  38. /**
  39. * Sets a property |value| on |obj| with property name |key|. Like
  40. *
  41. * obj[key] = value;
  42. *
  43. * but without triggering setters.
  44. */
  45. function defineProperty(obj, key, value) {
  46. $Object.defineProperty(obj, key, {
  47. __proto__: null,
  48. configurable: true,
  49. enumerable: true,
  50. writable: true,
  51. value: value,
  52. });
  53. }
  54. /**
  55. * Takes a private class implementation |privateClass| and exposes a subset of
  56. * its methods |functions| and properties |properties| and |readonly| to a
  57. * public wrapper class that should be passed in. Within bindings code, you can
  58. * access the implementation from an instance of the wrapper class using
  59. * privates(instance).impl, and from the implementation class you can access
  60. * the wrapper using this.wrapper (or implInstance.wrapper if you have another
  61. * instance of the implementation class).
  62. *
  63. * |publicClass| should be a constructor that calls constructPrivate() like so:
  64. *
  65. * privates(publicClass).constructPrivate(this, arguments);
  66. *
  67. * @param {function} publicClass The publicly exposed wrapper class. This must
  68. * be a named function, and the name appears in stack traces.
  69. * @param {Object} privateClass The class implementation.
  70. * @param {{superclass: ?Function,
  71. * functions: ?Array<string>,
  72. * properties: ?Array<string>,
  73. * readonly: ?Array<string>}} exposed The names of properties on the
  74. * implementation class to be exposed. |superclass| represents the
  75. * constructor of the class to be used as the superclass of the exposed
  76. * class; |functions| represents the names of functions which should be
  77. * delegated to the implementation; |properties| are gettable/settable
  78. * properties and |readonly| are read-only properties.
  79. */
  80. function expose(publicClass, privateClass, exposed) {
  81. $Object.setPrototypeOf(exposed, null);
  82. // This should be called by publicClass.
  83. privates(publicClass).constructPrivate = function(self, args) {
  84. if (!(self instanceof publicClass)) {
  85. throw new Error('Please use "new ' + publicClass.name + '"');
  86. }
  87. // The "instanceof publicClass" check can easily be spoofed, so we check
  88. // whether the private impl is already set before continuing.
  89. var privateSelf = privates(self);
  90. if ('impl' in privateSelf) {
  91. throw new Error('Object ' + publicClass.name + ' is already constructed');
  92. }
  93. var privateObj = $Object.create(privateClass.prototype);
  94. $Function.apply(privateClass, privateObj, args);
  95. privateObj.wrapper = self;
  96. privateSelf.impl = privateObj;
  97. };
  98. function getPrivateImpl(self) {
  99. var impl = privates(self).impl;
  100. if (!(impl instanceof privateClass)) {
  101. // Either the object is not constructed, or the property descriptor is
  102. // used on a target that is not an instance of publicClass.
  103. throw new Error('impl is not an instance of ' + privateClass.name);
  104. }
  105. return impl;
  106. }
  107. var publicClassPrototype = {
  108. // The final prototype will be assigned at the end of this method.
  109. __proto__: null,
  110. constructor: publicClass,
  111. };
  112. if ('functions' in exposed) {
  113. $Array.forEach(exposed.functions, function(func) {
  114. publicClassPrototype[func] = function() {
  115. var impl = getPrivateImpl(this);
  116. return $Function.apply(impl[func], impl, arguments);
  117. };
  118. });
  119. }
  120. if ('properties' in exposed) {
  121. $Array.forEach(exposed.properties, function(prop) {
  122. $Object.defineProperty(publicClassPrototype, prop, {
  123. __proto__: null,
  124. enumerable: true,
  125. get: function() {
  126. return getPrivateImpl(this)[prop];
  127. },
  128. set: function(value) {
  129. var impl = getPrivateImpl(this);
  130. delete impl[prop];
  131. impl[prop] = value;
  132. }
  133. });
  134. });
  135. }
  136. if ('readonly' in exposed) {
  137. $Array.forEach(exposed.readonly, function(readonly) {
  138. $Object.defineProperty(publicClassPrototype, readonly, {
  139. __proto__: null,
  140. enumerable: true,
  141. get: function() {
  142. return getPrivateImpl(this)[readonly];
  143. },
  144. });
  145. });
  146. }
  147. // The prototype properties have been installed. Now we can safely assign an
  148. // unsafe prototype and export the class to the public.
  149. var superclass = exposed.superclass || $Object.self;
  150. $Object.setPrototypeOf(publicClassPrototype, superclass.prototype);
  151. publicClass.prototype = publicClassPrototype;
  152. return publicClass;
  153. }
  154. /**
  155. * Returns a deep copy of |value|. The copy will have no references to nested
  156. * values of |value|.
  157. */
  158. function deepCopy(value) {
  159. return nativeDeepCopy(value);
  160. }
  161. exports.$set('forEach', forEach);
  162. exports.$set('lookup', lookup);
  163. exports.$set('defineProperty', defineProperty);
  164. exports.$set('expose', expose);
  165. exports.$set('deepCopy', deepCopy);