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

Unified Diff: sdk/lib/_internal/compiler/implementation/lib/js_helper.dart

Issue 11448009: Represent runtime type information as nested lists. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years 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: 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 7ed7b2d1177b7f4d399b8e8f7091a6e8fdaf45ea..6ab95d52ab540cd349bb2bc81be1d588f0df552a 100644
--- a/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart
+++ b/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart
@@ -1631,22 +1631,62 @@ String getClassName(var object) {
return JS('String', r'#.constructor.builtin$cls', object);
}
+String getTypeArgumentAsString(List runtimeType) {
+ String className = runtimeTypeToString(runtimeType[0]);
+ if (runtimeType.length = 1) return className;
ngeoffray 2012/12/06 23:21:45 == 1
karlklose 2012/12/07 07:46:35 Argh, this is bad. Good catch.
+ return '$className<${joinArguments(runtimeType, 1)}>';
+}
+
+String runtimeTypeToString(type) {
+ if (type == null) {
+ return 'dynamic';
+ } else if (type is String) {
+ // A native class. The string is the unique name.
+ return type;
+ } else if (isJsArray(type)) {
+ // A list representing a type with arguments.
+ return getTypeArgumentAsString(type);
+ } else {
+ // A reference to the constructor.
+ return JS('String', r'#.builtin$cls', type);
+ }
+}
+
+String joinArguments(var types, int startIndex) {
+ bool firstArgument = true;
+ StringBuffer buffer = new StringBuffer();
+ for (int index = startIndex; index < types.length; index++) {
+ if (firstArgument) {
+ firstArgument = false;
+ } else {
+ buffer. add(', ');
+ }
+ var argument = types[index];
+ buffer.add(runtimeTypeToString(argument));
+ }
+ return buffer.toString();
+}
+
String getRuntimeTypeString(var object) {
String className = isJsArray(object) ? 'List' : getClassName(object);
var typeInfo = JS('var', r'#.builtin$typeInfo', object);
if (typeInfo == null) return className;
- StringBuffer arguments = new StringBuffer();
- for (var i = 0; i < typeInfo.length; i++) {
- if (i > 0) {
- arguments.add(', ');
- }
- var argument = typeInfo[i];
- if (argument == null) {
- argument = 'dynamic';
+ return "$className<${joinArguments(typeInfo, 0)}>";
+}
+
+bool isSubtype(var s, var t) {
+ if (s == null || t == null) return true;
+ if (!isJsArray(s)) return s == t;
+ // TODO(karlklose): support subtyping: if s[0] != t[0], check if there is
+ // a function is$s[0] on t[0] and call it with substitutes type arguments.
ngeoffray 2012/12/06 23:21:45 Please don't use JS in this method. The compiler k
karlklose 2012/12/07 07:46:35 Done.
+ if (JS('var', '#[0]', s) != JS('var', '#[0]', t)) return false;
+ int len = JS('int', '#.length', s);
+ for (int i = 1; i < len; i++) {
+ if (!isSubtype(JS('var', '#[0]', s), JS('var', '#[0]', s))) {
+ return false;
}
- arguments.add(argument);
}
- return '$className<$arguments>';
+ return true;
}
createRuntimeType(String name) => new TypeImpl(name);

Powered by Google App Engine
This is Rietveld 408576698