| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 // Copyright (c) 2012 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 |  | 
| 5 var createClassWrapper = requireNative('utils').createClassWrapper; |  | 
| 6 var schemaRegistry = requireNative('schema_registry'); |  | 
| 7 var CHECK = requireNative('logging').CHECK; |  | 
| 8 var WARNING = requireNative('logging').WARNING; |  | 
| 9 |  | 
| 10 /** |  | 
| 11  * An object forEach. Calls |f| with each (key, value) pair of |obj|, using |  | 
| 12  * |self| as the target. |  | 
| 13  * @param {Object} obj The object to iterate over. |  | 
| 14  * @param {function} f The function to call in each iteration. |  | 
| 15  * @param {Object} self The object to use as |this| in each function call. |  | 
| 16  */ |  | 
| 17 function forEach(obj, f, self) { |  | 
| 18   for (var key in obj) { |  | 
| 19     if ($Object.hasOwnProperty(obj, key)) |  | 
| 20       $Function.call(f, self, key, obj[key]); |  | 
| 21   } |  | 
| 22 } |  | 
| 23 |  | 
| 24 /** |  | 
| 25  * Assuming |array_of_dictionaries| is structured like this: |  | 
| 26  * [{id: 1, ... }, {id: 2, ...}, ...], you can use |  | 
| 27  * lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2. |  | 
| 28  * @param {Array.<Object.<string, ?>>} array_of_dictionaries |  | 
| 29  * @param {string} field |  | 
| 30  * @param {?} value |  | 
| 31  */ |  | 
| 32 function lookup(array_of_dictionaries, field, value) { |  | 
| 33   var filter = function (dict) {return dict[field] == value;}; |  | 
| 34   var matches = array_of_dictionaries.filter(filter); |  | 
| 35   if (matches.length == 0) { |  | 
| 36     return undefined; |  | 
| 37   } else if (matches.length == 1) { |  | 
| 38     return matches[0] |  | 
| 39   } else { |  | 
| 40     throw new Error("Failed lookup of field '" + field + "' with value '" + |  | 
| 41                     value + "'"); |  | 
| 42   } |  | 
| 43 } |  | 
| 44 |  | 
| 45 function loadTypeSchema(typeName, defaultSchema) { |  | 
| 46   var parts = $String.split(typeName, '.'); |  | 
| 47   if (parts.length == 1) { |  | 
| 48     if (defaultSchema == null) { |  | 
| 49       WARNING('Trying to reference "' + typeName + '" ' + |  | 
| 50               'with neither namespace nor default schema.'); |  | 
| 51       return null; |  | 
| 52     } |  | 
| 53     var types = defaultSchema.types; |  | 
| 54   } else { |  | 
| 55     var schemaName = $Array.join($Array.slice(parts, 0, parts.length - 1), '.'); |  | 
| 56     var types = schemaRegistry.GetSchema(schemaName).types; |  | 
| 57   } |  | 
| 58   for (var i = 0; i < types.length; ++i) { |  | 
| 59     if (types[i].id == typeName) |  | 
| 60       return types[i]; |  | 
| 61   } |  | 
| 62   return null; |  | 
| 63 } |  | 
| 64 |  | 
| 65 /** |  | 
| 66  * Takes a private class implementation |cls| and exposes a subset of its |  | 
| 67  * methods |functions| and properties |properties| and |readonly| in a public |  | 
| 68  * wrapper class that it returns. Within bindings code, you can access the |  | 
| 69  * implementation from an instance of the wrapper class using |  | 
| 70  * privates(instance).impl, and from the implementation class you can access |  | 
| 71  * the wrapper using this.wrapper (or implInstance.wrapper if you have another |  | 
| 72  * instance of the implementation class). |  | 
| 73  * @param {string} name The name of the exposed wrapper class. |  | 
| 74  * @param {Object} cls The class implementation. |  | 
| 75  * @param {{functions: ?Array.<string>, |  | 
| 76  *          properties: ?Array.<string>, |  | 
| 77  *          readonly: ?Array.<string>}} exposed The names of properties on the |  | 
| 78  *     implementation class to be exposed. |functions| represents the names of |  | 
| 79  *     functions which should be delegated to the implementation; |properties| |  | 
| 80  *     are gettable/settable properties and |readonly| are read-only properties. |  | 
| 81  */ |  | 
| 82 function expose(name, cls, exposed) { |  | 
| 83   var publicClass = createClassWrapper(name, cls); |  | 
| 84 |  | 
| 85   if ('functions' in exposed) { |  | 
| 86     $Array.forEach(exposed.functions, function(func) { |  | 
| 87       publicClass.prototype[func] = function() { |  | 
| 88         var impl = privates(this).impl; |  | 
| 89         return $Function.apply(impl[func], impl, arguments); |  | 
| 90       }; |  | 
| 91     }); |  | 
| 92   } |  | 
| 93 |  | 
| 94   if ('properties' in exposed) { |  | 
| 95     $Array.forEach(exposed.properties, function(prop) { |  | 
| 96       $Object.defineProperty(publicClass.prototype, prop, { |  | 
| 97         enumerable: true, |  | 
| 98         get: function() { |  | 
| 99           return privates(this).impl[prop]; |  | 
| 100         }, |  | 
| 101         set: function(value) { |  | 
| 102           var impl = privates(this).impl; |  | 
| 103           delete impl[prop]; |  | 
| 104           impl[prop] = value; |  | 
| 105         } |  | 
| 106       }); |  | 
| 107     }); |  | 
| 108   } |  | 
| 109 |  | 
| 110   if ('readonly' in exposed) { |  | 
| 111     $Array.forEach(exposed.readonly, function(readonly) { |  | 
| 112       $Object.defineProperty(publicClass.prototype, readonly, { |  | 
| 113         enumerable: true, |  | 
| 114         get: function() { |  | 
| 115           return privates(this).impl[readonly]; |  | 
| 116         }, |  | 
| 117       }); |  | 
| 118     }); |  | 
| 119   } |  | 
| 120 |  | 
| 121   return publicClass; |  | 
| 122 } |  | 
| 123 |  | 
| 124 exports.forEach = forEach; |  | 
| 125 exports.loadTypeSchema = loadTypeSchema; |  | 
| 126 exports.lookup = lookup; |  | 
| 127 exports.expose = expose; |  | 
| OLD | NEW | 
|---|