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

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

Issue 11264005: InvocationMirror implemented in dart2js. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Working version Created 8 years, 2 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: lib/compiler/implementation/lib/js_helper.dart
diff --git a/lib/compiler/implementation/lib/js_helper.dart b/lib/compiler/implementation/lib/js_helper.dart
index 8e0974f4ef0540aaf4efa6794eea9400ff0ba3d6..f921e832fc2be8ad7bb5fb884e7380e5b3774bac 100644
--- a/lib/compiler/implementation/lib/js_helper.dart
+++ b/lib/compiler/implementation/lib/js_helper.dart
@@ -352,6 +352,115 @@ class ListIterator<T> implements Iterator<T> {
}
}
+createInvocationMirror(name, internalName, type, arguments, argumentNames) =>
+ new JSInvocationMirror(name, internalName, type, arguments, argumentNames);
+
+class JSInvocationMirror implements InvocationMirror {
+ static const METHOD = 0;
+ static const GETTER = 1;
+ static const SETTER = 2;
+
+ final String memberName;
+ final String _internalName;
+ final int _type;
+ final List _arguments;
+ final List _namedArgumentNames;
+ /** Map from argument name to index in _arguments. */
+ Map<String,Dynamic> _namedIndices = null;
+
+ JSInvocationMirror(this.memberName,
+ this._internalName,
+ this._type,
+ this._arguments,
+ this._namedArgumentNames) {
+ if (_type == METHOD) {
+ if (_namedArgumentNames != null && _namedArgumentNames.length > 0) {
+ _namedIndices = <String,int>{};
+ int namedArgumentCount = _namedArgumentNames.length;
+ int namedArgumentsStartIndex =
+ _arguments.length - _namedArgumentNames.length;
+ for (int i = 0; i < namedArgumentCount; i++) {
+ _namedIndices[_namedArgumentNames[i]] = i + namedArgumentsStartIndex;
+ }
+ } else {
+ _namedIndices = const <String,int>{};
+ }
+ }
+ }
+
+ bool get isMethod => _type == METHOD;
+ bool get isGetter => _type == GETTER;
+ bool get isSetter => _type == SETTER;
+ bool get isAccessor => _type != METHOD;
+
+ List get positionalArguments {
+ if (_type == GETTER) return null;
+ var list = [];
+ var argumentCount =
+ _arguments.length - _namedArgumentNames.length;
+ for (var index = 0 ; index < argumentCount ; index++) {
+ list.add(_arguments[index]);
+ }
+ return list;
+ //return new JSInvocationMirrorPositionalArguments(this);
bakster 2012/10/24 13:43:04 Code in comments is wrong.
Johnni Winther 2012/10/25 13:34:56 Done.
+ }
+
+ Map<String,Dynamic> get namedArguments {
+ if (_type != METHOD) return null;
+ var map = <String,Dynamic>{};
+ _namedIndices.forEach((String name, int index) {
+ map[name] = _arguments[index];
+ });
+ return map;
+ //return new JSInvocationMirrorNamedArguments(this);
bakster 2012/10/24 13:43:04 Same here.
Johnni Winther 2012/10/25 13:34:56 Done.
+ }
+
+ // TODO(lrn): Hide subtype when class literals are implemented.
+ // Type get runtimeType => InvocationMirror;
+
+ invokeOn(Object object) {
+ List arguments = _arguments;
+ if (!isJsArray(arguments)) arguments = new List.from(arguments);
+ return JS("var", "#[#].apply(#, #)", object, _internalName, object, arguments);
+ }
+}
+
+class JSInvocationMirrorPositionalArguments implements List {
+ JSInvocationMirror _mirror;
+ JSInvocationMirrorPositionalArguments(this._mirror);
+
+ int get length =>
+ _mirror._arguments.length - _mirror._namedArgumentNames.length;
+
+ operator[](int index) {
+ if (index < 0 || index >= length) throw new IndexOutOfRangeException(index);
+ return _mirror._arguments[index];
+ }
+ // extend UnmodifiableList when available.
+}
+
+class JSInvocationMirrorNamedArguments implements Map<String, Dynamic> {
+ JSInvocationMirror _mirror;
+ JSInvocationMirrorNamedArguments(this._mirror);
+
+ int get length => _mirror._namedIndices.length;
+
+ operator[](String key) {
+ int index = _mirror._namedIndices[key];
+ if (index == null) return null;
+ return _mirror._arguments[index];
+ }
+
+ void forEach(void action(String key, var value)) {
+ _mirror._namedIndices.forEach((String key, int index) {
+ action(key, _mirror._arguments[index]);
+ });
+ }
+
+ Set<String> get keys => _mirror._namedIndices.getKeys();
+ // extend UnmodifiableMap when available.
+}
+
class Primitives {
static int hashCodeSeed = 0;

Powered by Google App Engine
This is Rietveld 408576698