Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // The following methods are used to handle type information | 5 // The following methods are used to handle type information |
| 6 // | 6 // |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * @constructor | 9 * @constructor |
| 10 * @param {string} classkey | 10 * @param {string} classkey |
| 11 * @param {string=} typekey | 11 * @param {string=} typekey |
| 12 * @param {Array.<RTT>=} typeargs | 12 * @param {Array.<RTT>=} typeargs |
| 13 * @param {RTT} returnType | |
| 14 * @param {bool} functionType | |
| 13 */ | 15 */ |
| 14 function RTT(classkey, typekey, typeargs) { | 16 function RTT(classkey, typekey, typeargs, returnType, functionType) { |
| 15 this.classKey = classkey; | 17 this.classKey = classkey; |
| 16 this.typeKey = typekey ? typekey : classkey; | 18 this.typeKey = typekey ? typekey : classkey; |
| 17 this.typeArgs = typeargs; | 19 this.typeArgs = typeargs; |
| 20 this.returnType = returnType; // key for the return type | |
| 18 this.implementedTypes = {}; | 21 this.implementedTypes = {}; |
| 22 this.functionType = functionType; | |
| 19 // Add self | 23 // Add self |
| 20 this.implementedTypes[classkey] = this; | 24 this.implementedTypes[classkey] = this; |
| 21 // Add Object | 25 // Add Object |
| 22 if (classkey != $cls('Object')) { | 26 if (!functionType && classkey != $cls('Object')) { |
| 23 this.implementedTypes[$cls('Object')] = RTT.objectType; | 27 this.implementedTypes[$cls('Object')] = RTT.objectType; |
| 24 } | 28 } |
| 25 } | 29 } |
| 26 | 30 |
| 27 /** @type {Object.<string, Object>} */ | 31 /** @type {Object.<string, Object>} */ |
| 28 RTT.types = {}; | 32 RTT.types = {}; |
| 29 | 33 |
| 30 /** @type {Array.<RTT>} */ | 34 /** @type {Array.<RTT>} */ |
| 31 RTT.prototype.derivedTypes = []; | 35 RTT.prototype.derivedTypes = []; |
| 32 | 36 |
| 33 /** @return {string} */ | 37 /** @return {string} */ |
| 34 RTT.prototype.toString = function() { return this.typeKey; } | 38 RTT.prototype.toString = function() { return this.typeKey; } |
| 35 | 39 |
| 36 /** | 40 /** |
| 37 * @param {*} value | 41 * @param {*} value |
| 38 * @return {boolean} Whether this type is implemented by the value | 42 * @return {boolean} Whether this type is implemented by the value |
| 39 */ | 43 */ |
| 40 RTT.prototype.implementedBy = function(value){ | 44 RTT.prototype.implementedBy = function(value){ |
| 41 return (value == null) ? RTT.nullInstanceOf(this) : | 45 return (value == null) ? RTT.nullInstanceOf(this) : |
| 46 this.functionType ? this.implementedByTypeFunc(value) : | |
| 42 this.implementedByType(RTT.getTypeInfo(value)); | 47 this.implementedByType(RTT.getTypeInfo(value)); |
| 43 }; | 48 }; |
| 44 | 49 |
| 45 /** | 50 /** |
| 46 * A helper function for safely looking up a value | 51 * A helper function for safely looking up a value |
| 47 * in a Object used as a map. | 52 * in a Object used as a map. |
| 48 * @param {Object.<*>} map | 53 * @param {Object.<*>} map |
| 49 * @param {srting} key | 54 * @param {srting} key |
| 50 * @return {*} the value or null; | 55 * @return {*} the value or null; |
| 51 */ | 56 */ |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 69 for(var i = this.typeArgs.length - 1; i >= 0; i--) { | 74 for(var i = this.typeArgs.length - 1; i >= 0; i--) { |
| 70 if (!this.typeArgs[i].implementedByType(targetTypeInfo.typeArgs[i])) { | 75 if (!this.typeArgs[i].implementedByType(targetTypeInfo.typeArgs[i])) { |
| 71 return false; | 76 return false; |
| 72 } | 77 } |
| 73 } | 78 } |
| 74 } | 79 } |
| 75 return true; | 80 return true; |
| 76 }; | 81 }; |
| 77 | 82 |
| 78 /** | 83 /** |
| 84 * @param {!RTT} other | |
| 85 * @return {boolean} Whether this type is implement by other | |
| 86 */ | |
| 87 RTT.prototype.implementedByTypeFunc = function(otherType) { | |
| 88 if (otherType === this || otherType === RTT.dynamicType) { | |
| 89 return true; | |
| 90 } | |
| 91 var found = true; | |
| 92 var props = Object.getOwnPropertyNames(otherType.implementedTypes); | |
| 93 for (var i = 0 ; i < props.length; i++) { | |
| 94 found = true; | |
| 95 var mapped = otherType.implementedTypes[props[i]]; | |
| 96 if (mapped.returnType && this.returnType) { | |
| 97 if (!this.returnType.implementedByType(mapped.returnType)) { | |
| 98 continue; | |
| 99 } | |
| 100 } else if (mapped.returnType || this.returnType) { | |
| 101 continue; //handles mixed undef/defined. undef/undef falls through | |
| 102 } | |
| 103 if (mapped.typeArgs && this.typeArgs) { | |
| 104 if (this.typeArgs.length != mapped.typeArgs.length) { | |
| 105 continue; | |
| 106 } | |
| 107 for(var x = this.typeArgs.length - 1; x >= 0; x--) { | |
|
zundel
2011/11/16 23:54:56
style: space between for and (
codefu
2011/11/17 21:17:10
Done.
| |
| 108 if (!this.typeArgs[x].implementedByType(mapped.typeArgs[x])) { | |
| 109 found = false; | |
|
zundel
2011/11/16 23:54:56
I find the use of 'found' in this method a bit con
codefu
2011/11/17 21:17:10
Nice!
| |
| 110 break; // fall through and search the next property | |
| 111 } | |
| 112 } | |
| 113 } else if (mapped.typeArgs || this.typeArgs) { | |
| 114 continue; //handles mixed undef/defined. undef/undef falls through | |
| 115 } | |
| 116 if (found == true) { | |
| 117 return true; | |
| 118 } | |
| 119 } | |
| 120 return false; | |
| 121 }; | |
| 122 | |
| 123 /** | |
| 79 * @return {string} the class name associated with this type | 124 * @return {string} the class name associated with this type |
| 80 */ | 125 */ |
| 81 RTT.prototype.getClassName = function() { | 126 RTT.prototype.getClassName = function() { |
| 82 var name = this.classKey; | 127 var name = this.classKey; |
| 83 if (name.substr(0, 4) == "cls:") { | 128 if (name.substr(0, 4) == "cls:") { |
| 84 name = name.substr(4); | 129 name = name.substr(4); |
| 85 } | 130 } |
| 86 if (name.substr(-5) == "$Dart") { | 131 if (name.substr(-5) == "$Dart") { |
| 87 name = name.substr(0, name.length - 5); | 132 name = name.substr(0, name.length - 5); |
| 88 } | 133 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 var classkey = RTT.getTypeKey(name); | 172 var classkey = RTT.getTypeKey(name); |
| 128 rtt = new RTT(classkey, typekey, typeArgs); | 173 rtt = new RTT(classkey, typekey, typeArgs); |
| 129 RTT.types[typekey] = rtt; | 174 RTT.types[typekey] = rtt; |
| 130 if (implementsSupplier) { | 175 if (implementsSupplier) { |
| 131 implementsSupplier(rtt, typeArgs); | 176 implementsSupplier(rtt, typeArgs); |
| 132 } | 177 } |
| 133 return rtt; | 178 return rtt; |
| 134 }; | 179 }; |
| 135 | 180 |
| 136 /** | 181 /** |
| 182 * @param {Array.<RTT>=} typeArgs | |
| 183 * @param {<RTT>=} returnType (if defined) | |
| 184 * @return {RTT} The RTT information object | |
| 185 */ | |
| 186 RTT.createFunction = function(typeArgs, returnType) { | |
| 187 var name = $cls("Function"); | |
| 188 var typekey = RTT.getTypeKey(name, typeArgs, returnType); | |
| 189 var rtt = $mapLookup(RTT.types, typekey); | |
| 190 if (rtt) { | |
| 191 return rtt; | |
| 192 } | |
| 193 var classkey = RTT.getTypeKey(name); | |
| 194 rtt = new RTT(classkey, typekey, typeArgs, returnType, true); | |
| 195 RTT.types[typekey] = rtt; | |
| 196 return rtt; | |
| 197 }; | |
| 198 | |
| 199 /** | |
| 137 * @param {string} classkey | 200 * @param {string} classkey |
| 138 * @param {Array.<(RTT|string)>=} typeargs | 201 * @param {Array.<(RTT|string)>=} typeargs |
| 202 * @param {string} returntype | |
| 139 * @return {string} | 203 * @return {string} |
| 140 */ | 204 */ |
| 141 RTT.getTypeKey = function(classkey, typeargs) { | 205 RTT.getTypeKey = function(classkey, typeargs, returntype) { |
| 142 var key = classkey; | 206 var key = classkey; |
| 143 if (typeargs) { | 207 if (typeargs) { |
| 144 key += "<" + typeargs.join(",") + ">"; | 208 key += "<" + typeargs.join(",") + ">"; |
| 145 } | 209 } |
| 210 if (returntype) { | |
| 211 key += "-><" + returntype + ">"; | |
| 212 } | |
| 146 return key; | 213 return key; |
| 147 }; | 214 }; |
| 148 | 215 |
| 149 /** | 216 /** |
| 150 * @return {*} value | 217 * @return {*} value |
| 151 * @return {RTT} return the RTT information object for the value | 218 * @return {RTT} return the RTT information object for the value |
| 152 */ | 219 */ |
| 153 RTT.getTypeInfo = function(value) { | 220 RTT.getTypeInfo = function(value) { |
| 154 return (value.$typeInfo) ? value.$typeInfo : RTT.getNativeTypeInfo(value); | 221 return (value.$typeInfo) ? value.$typeInfo : RTT.getNativeTypeInfo(value); |
| 155 }; | 222 }; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 return typeof o == 'number'; | 347 return typeof o == 'number'; |
| 281 } | 348 } |
| 282 | 349 |
| 283 /** | 350 /** |
| 284 * @param {*} o | 351 * @param {*} o |
| 285 * @return {boolean} | 352 * @return {boolean} |
| 286 */ | 353 */ |
| 287 function $isString(o) { | 354 function $isString(o) { |
| 288 return typeof o == 'string'; | 355 return typeof o == 'string'; |
| 289 } | 356 } |
| OLD | NEW |