Index: sdk/lib/_internal/compiler/implementation/lib/js_helper.dart |
diff --git a/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart b/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart |
index ee23e0dbb08ec629f16073732225897c4dc55c7f..9b6b8bcb6a006559f50f76c4ef9ba53cd23b4277 100644 |
--- a/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart |
+++ b/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart |
@@ -1541,18 +1541,59 @@ getArguments(var type) => JS('var', r'#.slice(1)', type); |
getField(var object, var name) => JS('var', r'#[#]', object, name); |
/** |
+ * Tests whether the Dart object [o] is a subtype of the runtime type |
+ * representation [t], which is a type representation as described in the |
+ * comment on [isSubtype]. |
+ */ |
+bool objectIsSubtype(Object o, var t) { |
+ if (JS('bool', '# == null', o) || JS('bool', '# == null', t)) return true; |
+ // In the case of [List], [t] will be an array with the type representation |
+ // in the first entry and the type argument in the second. |
+ var typeOfT = isJsArray(t) ? getField(t, 0) : t; |
+ var nativeCheck = getField(typeOfT, '\$nativeCheck'); |
+ if (nativeCheck != null) { |
+ bool isInstance = invoke(nativeCheck, [o]); |
+ if (!isInstance) return false; |
+ if (o is List) { |
+ // If the object is a list and passed the native check, we know that [t] |
+ // is an array containing the type argument as its second entry. |
+ return isSubtype(getRuntimeTypeArgument(o, null, 0), getField(t, 1)); |
+ } |
+ return true; |
+ } else { |
+ var type; |
+ var rti = getRuntimeTypeInfo(o); |
+ // If the type has type variables (that is, [:rti != null:]), make a copy of |
+ // the type arguments and insert [o] in the first position to create a |
+ // compound type representation, otherwise use [o] itself. |
+ if (JS('bool', '# != null', rti)) { |
+ type = JS('List', '#.slice().splice(0, 0, #)', rti, o); |
+ } else { |
+ // We can use the object as its own type representation because we install |
+ // the subtype flags and the substitution on the prototype, so they are |
+ // properties of the object in JS. |
+ type = o; |
+ } |
+ return isSubtype(type, t); |
+ } |
+} |
+ |
+ |
+/** |
* Check whether the type represented by [s] is a subtype of the type |
* represented by [t]. |
* |
* Type representations can be: |
* 1) a JavaScript constructor for a class C: the represented type is the raw |
* type C. |
- * 2) a JavaScript object: this represents a class for which there is no |
+ * 2) a Dart object. |
+ * 3) a JavaScript object: this represents a class for which there is no |
* JavaScript constructor, because it is only used in type arguments or it |
* is native. The represented type is the raw type of this class. |
- * 3) a JavaScript array: the first entry is of type 1 or 2 and identifies the |
- * class of the type and the rest of the array are the type arguments. |
- * 4) [:null:]: the dynamic type. |
+ * 4) a JavaScript array: the first entry is of type 1, 2 or 3 and contains the |
+ * subtyping flags and the substitution of the type and the rest of the |
+ * array are the type arguments. |
+ * 5) [:null:]: the dynamic type. |
*/ |
bool isSubtype(var s, var t) { |
// If either type is dynamic, [s] is a subtype of [t]. |