Index: sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart |
diff --git a/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart b/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart |
index b1cb9eb7124910b4b1212e918ee9e8a854de975f..b80444cc39833153d8a7212e896cbd889c25a9a5 100644 |
--- a/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart |
+++ b/sdk/lib/_internal/compiler/implementation/js_backend/runtime_types.dart |
@@ -21,13 +21,64 @@ class RuntimeTypeInformation { |
/// instantiations and checks. |
Set<ClassElement> allArguments; |
- bool isJsNative(Element element) { |
- return (element == compiler.intClass || |
- element == compiler.boolClass || |
- element == compiler.numClass || |
- element == compiler.doubleClass || |
- element == compiler.stringClass || |
- element == compiler.listClass); |
+ Collection<ClassElement> cachedJsNatives; |
+ Collection<ClassElement> get jsNatives { |
+ if (cachedJsNatives == null) { |
+ cachedJsNatives = <ClassElement>[compiler.intClass, |
+ compiler.boolClass, |
+ compiler.numClass, |
+ compiler.doubleClass, |
+ compiler.stringClass]; |
ngeoffray
2013/02/18 09:27:58
What happened to the array class? And what about D
karlklose
2013/02/18 16:02:01
Having list here was not necessary in the first pl
ngeoffray
2013/02/19 09:00:41
Please add this as a comment.
karlklose
2013/02/19 12:39:28
Done.
|
+ } |
+ return cachedJsNatives; |
+ } |
+ |
+ bool isJsNative(Element element) => jsNatives.contains(element); |
+ |
+ String getNativeCheckName() => r'$nativeCheck'; |
ngeoffray
2013/02/18 09:27:58
Move this to the namer?
karlklose
2013/02/18 16:02:01
Done.
|
+ |
+ bool needsNativeCheck(Element element) { |
+ return element == compiler.objectClass || isJsNative(element); |
+ } |
+ |
+ /** |
+ * Construct the native check function for the class [element]. |
ngeoffray
2013/02/18 09:27:58
Construct -> Constructs
karlklose
2013/02/18 16:02:01
Done.
|
+ * |
+ * This function will be put on the type representation for native classes |
+ * (and Object) and is called by the helper function [:objectIsSubtype:], if |
+ * it is on the object that represents the type to test against. We have to |
+ * put a trivial native check on object when it is used as a type argument in |
+ * a check because the native objects cannot have [:$isObject:] bits set on |
+ * them. |
+ */ |
+ js.Expression getNativeCheck(Element element) { |
ngeoffray
2013/02/18 09:27:58
Instead of creating a function with hand-written J
karlklose
2013/02/18 16:02:01
Not at the moment, but I should perhaps move the t
ngeoffray
2013/02/19 09:00:41
Yes, please add a TODO or file a bug. We don't wan
karlklose
2013/02/19 12:39:28
Done.
|
+ final String parameterName = 'object'; |
+ final js.VariableUse variable = js.use(parameterName); |
+ |
+ js.Expression typeTest(String typeName) { |
+ return js.strictEquals( |
+ new js.LiteralExpression.withData('typeof #', [variable]), |
+ new js.LiteralString("'$typeName'")); |
+ } |
+ |
+ js.Expression test; |
+ if (element == compiler.objectClass) { |
+ test = new js.LiteralBool(true); |
+ } else if (element == compiler.intClass) { |
+ final js.Expression zero = new js.LiteralNumber('0'); |
+ test = js.strictEquals(variable, new js.Binary('|', variable, zero)); |
+ } else if (element == compiler.boolClass) { |
+ test = typeTest('boolean'); |
+ } else if (element == compiler.numClass) { |
+ test = typeTest('number'); |
+ } else if (element == compiler.doubleClass) { |
+ test = typeTest('number'); |
+ } else if (element == compiler.stringClass) { |
+ test = typeTest('string'); |
+ } else { |
+ return null; |
+ } |
+ return js.fun([parameterName], js.block1(js.return_(test))); |
} |
TypeChecks cachedRequiredChecks; |
@@ -294,4 +345,14 @@ class TypeCheckMapping implements TypeChecks { |
} |
Iterator<ClassElement> get iterator => map.keys.iterator; |
+ |
+ String toString() { |
+ StringBuffer sb = new StringBuffer(); |
+ for (ClassElement holder in this) { |
+ for (ClassElement check in [holder]) { |
+ sb.add('${holder.name.slowToString()}.${check.name.slowToString()}, '); |
+ } |
+ } |
+ return '[$sb]'; |
+ } |
} |