Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Unified Diff: compiler/lib/implementation/rtt.js

Issue 8566022: Function type checking (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: compiler/lib/implementation/rtt.js
diff --git a/compiler/lib/implementation/rtt.js b/compiler/lib/implementation/rtt.js
index 83179d3dfa46193a18563e04bcd62f1eaa1ee3dc..b62d361c792765a69fbc9e2cd69a994a8eeb94c8 100644
--- a/compiler/lib/implementation/rtt.js
+++ b/compiler/lib/implementation/rtt.js
@@ -10,16 +10,20 @@
* @param {string} classkey
* @param {string=} typekey
* @param {Array.<RTT>=} typeargs
+ * @param {RTT} returnType
+ * @param {bool} functionType
*/
-function RTT(classkey, typekey, typeargs) {
+function RTT(classkey, typekey, typeargs, returnType, functionType) {
this.classKey = classkey;
this.typeKey = typekey ? typekey : classkey;
this.typeArgs = typeargs;
+ this.returnType = returnType; // key for the return type
this.implementedTypes = {};
+ this.functionType = functionType;
// Add self
this.implementedTypes[classkey] = this;
// Add Object
- if (classkey != $cls('Object')) {
+ if (!functionType && classkey != $cls('Object')) {
this.implementedTypes[$cls('Object')] = RTT.objectType;
}
}
@@ -39,6 +43,7 @@ RTT.prototype.toString = function() { return this.typeKey; }
*/
RTT.prototype.implementedBy = function(value){
return (value == null) ? RTT.nullInstanceOf(this) :
+ this.functionType ? this.implementedByTypeFunc(value) :
this.implementedByType(RTT.getTypeInfo(value));
};
@@ -76,6 +81,46 @@ RTT.prototype.implementedByType = function(otherType) {
};
/**
+ * @param {!RTT} other
+ * @return {boolean} Whether this type is implement by other
+ */
+RTT.prototype.implementedByTypeFunc = function(otherType) {
+ if (otherType === this || otherType === RTT.dynamicType) {
+ return true;
+ }
+ var found = true;
+ var props = Object.getOwnPropertyNames(otherType.implementedTypes);
+ for (var i = 0 ; i < props.length; i++) {
+ found = true;
+ var mapped = otherType.implementedTypes[props[i]];
+ if (mapped.returnType && this.returnType) {
+ if (!this.returnType.implementedByType(mapped.returnType)) {
+ continue;
+ }
+ } else if (mapped.returnType || this.returnType) {
+ continue; //handles mixed undef/defined. undef/undef falls through
+ }
+ if (mapped.typeArgs && this.typeArgs) {
+ if (this.typeArgs.length != mapped.typeArgs.length) {
+ continue;
+ }
+ 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.
+ if (!this.typeArgs[x].implementedByType(mapped.typeArgs[x])) {
+ 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!
+ break; // fall through and search the next property
+ }
+ }
+ } else if (mapped.typeArgs || this.typeArgs) {
+ continue; //handles mixed undef/defined. undef/undef falls through
+ }
+ if (found == true) {
+ return true;
+ }
+ }
+ return false;
+};
+
+/**
* @return {string} the class name associated with this type
*/
RTT.prototype.getClassName = function() {
@@ -134,15 +179,37 @@ RTT.create = function(name, implementsSupplier, typeArgs) {
};
/**
+ * @param {Array.<RTT>=} typeArgs
+ * @param {<RTT>=} returnType (if defined)
+ * @return {RTT} The RTT information object
+ */
+RTT.createFunction = function(typeArgs, returnType) {
+ var name = $cls("Function");
+ var typekey = RTT.getTypeKey(name, typeArgs, returnType);
+ var rtt = $mapLookup(RTT.types, typekey);
+ if (rtt) {
+ return rtt;
+ }
+ var classkey = RTT.getTypeKey(name);
+ rtt = new RTT(classkey, typekey, typeArgs, returnType, true);
+ RTT.types[typekey] = rtt;
+ return rtt;
+};
+
+/**
* @param {string} classkey
* @param {Array.<(RTT|string)>=} typeargs
+ * @param {string} returntype
* @return {string}
*/
-RTT.getTypeKey = function(classkey, typeargs) {
+RTT.getTypeKey = function(classkey, typeargs, returntype) {
var key = classkey;
if (typeargs) {
key += "<" + typeargs.join(",") + ">";
}
+ if (returntype) {
+ key += "-><" + returntype + ">";
+ }
return key;
};

Powered by Google App Engine
This is Rietveld 408576698