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

Unified Diff: dart/sdk/lib/_internal/lib/js_types.dart

Issue 23996002: Use interceptors to handle runtime types. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comment Created 7 years, 3 months 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: dart/sdk/lib/_internal/lib/js_types.dart
diff --git a/dart/sdk/lib/_internal/lib/js_types.dart b/dart/sdk/lib/_internal/lib/js_types.dart
new file mode 100644
index 0000000000000000000000000000000000000000..eed0bf8f6b995984f86303f3aeb51ab8d0db691b
--- /dev/null
+++ b/dart/sdk/lib/_internal/lib/js_types.dart
@@ -0,0 +1,90 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of _interceptors;
+
+class JsType {}
+
+/**
+ * Interceptor for JavaScript constructor functions that create Dart objects
+ * (the functions created by defineClass). This is used for a variety of
+ * things, such as reflection and runtime types.
+ */
+class JsRuntimeType extends Interceptor implements JsType {
+ const JsRuntimeType();
+
+ String get mangledName => JS('String', r'#.builtin$cls', this);
+
+ String get name {
+ String m = mangledName;
+ String n = unmangleGlobalNameIfPreservedAnyways(m);
+ return n == null ? m : n;
+ }
+
+ String toString() => "JsRuntimeType on '$name'";
+}
+
+/**
+ * Interceptor for JavaScript objects that represent function types. These
+ * objects are generated by [TypeRepresentationGenerator.visitFunctionType] and
+ * is used for runtime type information about closures.
+ */
+// TODO(ahe): Shouldn't use interceptors for these function types, instead
+// use real Dart objects (possibly constants).
+class JsFunctionType extends Interceptor implements JsType {
+ const JsFunctionType();
+
+ bool get hasReturnType => JS('bool', '"ret" in #', this);
+ get returnType => JS('', '#.ret', this);
+
+ bool get isVoid => JS('bool', '!!#["void"]', this);
+
+ bool get hasArguments => JS('bool', '"args" in #', this);
+ List get arguments => JS('JSExtendableArray', '#.args', this);
+
+ bool get hasOptionalArguments => JS('bool', '"opt" in #', this);
+ List get optionalArguments => JS('JSExtendableArray', '#.opt', this);
+
+ bool get hasNamedArguments => JS('bool', '"named" in #', this);
+ get namedArguments => JS('=Object', '#.named', this);
+
+ String toString() {
+ var s = '(';
+ var sep = '';
+ if (hasArguments) {
+ for (var argument in arguments) {
+ s = '$s$sep${runtimeTypeToString(argument)}';
+ sep = ', ';
+ }
+ }
+ if (hasOptionalArguments) {
+ s = '$s$sep[';
+ sep = '';
+ for (var argument in optionalArguments) {
+ s = '$s$sep${runtimeTypeToString(argument)}';
+ sep = ', ';
+ }
+ s = '$s]';
+ }
+ if (hasNamedArguments) {
+ s = '${s}$sep{';
+ sep = '';
+ for (var name in extractKeys(namedArguments)) {
+ var type = JS('', '#[#]', namedArguments, name);
+ s = '$s$sep$name: ${runtimeTypeToString(type)}';
+ sep = ', ';
+ }
+ s = '$s}';
+ }
+ s = '${s}) -> ';
+ if (isVoid) {
+ s = '${s}void';
+ } else if (hasReturnType) {
+ s = '$s${runtimeTypeToString(returnType)}';
+ } else {
+ s = '${s}dynamic';
+ }
+ return s;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698