Chromium Code Reviews| Index: dart/sdk/lib/mirrors/mirrors.dart |
| diff --git a/dart/sdk/lib/mirrors/mirrors.dart b/dart/sdk/lib/mirrors/mirrors.dart |
| index 313026f26ee74379a00199a0e78258661820fd51..fc9b03fb70f2d2204b6ac070679491fef15aa245 100644 |
| --- a/dart/sdk/lib/mirrors/mirrors.dart |
| +++ b/dart/sdk/lib/mirrors/mirrors.dart |
| @@ -12,6 +12,10 @@ |
| // 'myField='. This allows us to assign unique names to getters and |
| // setters for the purposes of member lookup. |
| +// Open questions: |
| +// Turn all getters into final fields? |
|
Johnni Winther
2013/09/03 12:36:57
Why?
|
| +// Need a way to invoke super methods. |
| + |
| /** |
| * Basic reflection in Dart, |
| * with support for introspection and dynamic evaluation. |
| @@ -74,8 +78,8 @@ abstract class MirrorSystem { |
| * Returns an iterable of all libraries in the mirror system whose library |
| * name is [libraryName]. |
|
Johnni Winther
2013/09/03 12:36:57
Comment needs update.
gbracha
2013/10/03 22:11:25
Yes, can we please fix these issues in the CL so t
ahe
2013/10/30 07:48:31
Done.
|
| */ |
| - Iterable<LibraryMirror> findLibrary(Symbol libraryName) { |
| - return libraries.values.where( |
| + LibraryMirror findLibrary(Symbol libraryName) { |
| + return libraries.values.singleWhere( |
|
Alan Knight
2013/09/03 20:08:31
Might this be more useful if it used firstWhere so
ahe
2013/09/03 20:14:30
What I don't like about firstWhere is that it does
Alan Knight
2013/09/03 20:30:56
Does the error distinguish between the case of no
|
| (library) => library.simpleName == libraryName); |
| } |
| @@ -112,12 +116,6 @@ abstract class MirrorSystem { |
| external MirrorSystem currentMirrorSystem(); |
| /** |
| - * Creates a [MirrorSystem] for the isolate which is listening on |
| - * the [SendPort]. |
| - */ |
| -external Future<MirrorSystem> mirrorSystemOf(SendPort port); |
| - |
| -/** |
| * Returns an [InstanceMirror] reflecting [reflectee]. |
| * If [reflectee] is function or an instance of a class |
| * that has a [:call:] method, the returned instance mirror |
| @@ -149,12 +147,7 @@ external ClassMirror reflectClass(Type key); |
| * |
| * Every [Mirror] originates from some [MirrorSystem]. |
| */ |
| -abstract class Mirror { |
| - /** |
| - * The [MirrorSystem] that contains this mirror. |
| - */ |
| - MirrorSystem get mirrors; |
| -} |
| +abstract class Mirror {} |
| /** |
| * An [IsolateMirror] reflects an isolate. |
| @@ -266,6 +259,8 @@ abstract class DeclarationMirror implements Mirror { |
| /** |
| * The source location of this Dart language entity. |
| + * |
| + * This operation is optional and may return [:null:]. |
| */ |
| SourceLocation get location; |
| @@ -327,6 +322,7 @@ abstract class ObjectMirror implements Mirror { |
| List positionalArguments, |
| [Map<Symbol,dynamic> namedArguments]); |
| + |
|
Johnni Winther
2013/09/03 12:36:57
Extra line.
|
| /** |
| * Invokes a getter and returns a mirror on the result. The getter |
| * can be the implicit getter for a field or a user-defined getter |
| @@ -339,6 +335,19 @@ abstract class ObjectMirror implements Mirror { |
| * in a scope that has access to the private members |
| * of *o* (if *o* is a class or library) or the private members of the |
| * class of *o* (otherwise). |
| + * |
| + * If this mirror is an [InstanceMirror], and [fieldName] denotes an instance |
| + * method on its reflectee, the result of the invocation is an instance |
| + * mirror on a closure corresponding to that method. |
| + * |
| + * If this mirror is a [LibraryMirror], and [fieldName] denotes a top-level |
| + * method in the corresponding library, the result of the invocation is an |
| + * instance mirror on a closure corresponding to that method. |
| + * |
| + * If this mirror is a [ClassMirror], and [fieldName] denotes a static method |
| + * in the corresponding class, the result of the invocation is an instance |
| + * mirror on a closure corresponding to that method. |
|
gbracha
2013/10/03 22:11:25
I'd tweak the wording a bit at some point, because
ahe
2013/10/30 07:48:31
A kind soul fixed that for me in another CL :-)
|
| + * |
| * If the invocation returns a result *r*, this method returns |
| * the result of calling [reflect](*r*). |
| * If the invocation causes a compilation error |
| @@ -346,7 +355,8 @@ abstract class ObjectMirror implements Mirror { |
| * If the invocation throws an exception *e* (that it does not catch) |
| * this method throws *e*. |
| */ |
| - /* TODO(turnidge): Handle ambiguous names.*/ |
| + // TODO(ahe): Remove stuff about scope and private members. [fieldName] is a |
| + // capability giving access to private members. |
| InstanceMirror getField(Symbol fieldName); |
| /** |
| @@ -421,6 +431,20 @@ abstract class ObjectMirror implements Mirror { |
| * in a scope that has access to the private members |
| * of *o* (if *o* is a class or library) or the private members of the |
| * class of *o*(otherwise). |
| + * |
|
Johnni Winther
2013/09/03 12:36:57
Extra line.
|
| + * |
| + * If this mirror is an [InstanceMirror], and [fieldName] denotes an instance |
| + * method on its reflectee, the result of the invocation is an instance |
| + * mirror on a closure corresponding to that method. |
| + * |
| + * If this mirror is a [LibraryMirror], and [fieldName] denotes a top-level |
| + * method in the corresponding library, the result of the invocation is an |
| + * instance mirror on a closure corresponding to that method. |
| + * |
| + * If this mirror is a [ClassMirror], and [fieldName] denotes a static method |
| + * in the corresponding class, the result of the invocation is an instance |
| + * mirror on a closure corresponding to that method. |
| + * |
| * The method returns a future *k*. |
| * If the invocation returns a result *r*, *k* will be completed |
| * with the result of calling [reflect](*r*). |
| @@ -521,6 +545,28 @@ abstract class InstanceMirror implements ObjectMirror { |
| * invocation.namedArguments); |
| */ |
| delegate(Invocation invocation); |
| + |
| + /** |
| + * Returns closure for invoking the regular method named [name]. |
| + * |
| + * If [:type.instanceLookup(name:] is a regular method, the result of this |
|
Johnni Winther
2013/09/03 12:36:57
'(name' -> '(name)'.
'is a regular method' -> 'ret
gbracha
2013/10/03 22:11:25
We have made some tweaks to this wording in other
ahe
2013/10/30 07:48:31
I assume the other CL will land this change.
|
| + * method is a closure equivalent to: |
| + * |
| + * (r1, .., rn, {p1: d1, ..., pk: dk}) { |
| + * return this.invoke(name, [r1, .., rn], {#p1: p1, .., #pk: pk}); |
| + * } |
| + * |
| + * if m has required parameters r1, ..., rn, and named parameters p1, ..., pk |
| + * with defaults d1, ..., dk. |
| + * |
| + * (r1, .., rn, [p1 = d1, …, pk = dk]) { |
| + * return this.invoke(name, [r1, .., rn, p1, .., pk]); |
| + * } |
| + * |
| + * if m has required parameters r1, ..., rn, and optional positional |
| + * parameters p1, ..., pk with defaults d1, ..., dk. |
| + */ |
| + Function operator [](Symbol name); |
| } |
| /** |
| @@ -532,6 +578,12 @@ abstract class InstanceMirror implements ObjectMirror { |
| abstract class ClosureMirror implements InstanceMirror { |
| /** |
| * A mirror on the function associated with this closure. |
| + * |
| + * The function associated with an implicit closure of a function is that |
| + * function. |
|
Johnni Winther
2013/09/03 12:36:57
'of a function is that function' -> 'of a method i
|
| + * |
| + * The function associated with an instance of a class that has a [:call:] |
| + * method is that [:call:] method. |
| */ |
| MethodMirror get function; |
| @@ -600,6 +652,7 @@ abstract class ClosureMirror implements InstanceMirror { |
| * The returned value is the result of invoking the method [reflect] on |
| * *v*. |
| */ |
| + // TODO(ahe): Drop from release 1.0? |
|
Michael Lippautz (Google)
2013/09/03 17:24:50
Or "This operation is optional and may return [:nu
gbracha
2013/10/03 22:11:25
We have real demand for this. What are its prospec
rmacnak
2013/10/03 22:26:41
This should be doable in dart2js because it compil
|
| Future<InstanceMirror> findInContext(Symbol name); |
| } |
| @@ -615,43 +668,35 @@ abstract class LibraryMirror implements DeclarationMirror, ObjectMirror { |
| Uri get uri; |
| /** |
| - * An immutable map from from names to mirrors for all members in |
| + * An immutable map from from names to mirrors for all members declared in |
| * this library. |
| * |
| - * The members of a library are its top-level classes, |
| - * functions, variables, getters, and setters. |
| - */ |
| - Map<Symbol, Mirror> get members; |
| - |
| - /** |
| - * An immutable map from names to mirrors for all class |
| - * declarations in this library. |
| - */ |
| - Map<Symbol, ClassMirror> get classes; |
| - |
| - /** |
| - * An immutable map from names to mirrors for all function, getter, |
| - * and setter declarations in this library. |
| - */ |
| - Map<Symbol, MethodMirror> get functions; |
| - |
| - /** |
| - * An immutable map from names to mirrors for all getter |
| - * declarations in this library. |
| - */ |
| - Map<Symbol, MethodMirror> get getters; |
| - |
| - /** |
| - * An immutable map from names to mirrors for all setter |
| - * declarations in this library. |
| + * The members of a library are its top-level classes, functions, variables, |
|
gbracha
2013/10/03 22:11:25
also typedefs
|
| + * getters, and setters. |
| */ |
| - Map<Symbol, MethodMirror> get setters; |
| + Map<Symbol, DeclarationMirror> get declarations; |
|
gbracha
2013/09/17 18:27:53
See comments on declarations in ClassMirror below
|
| /** |
| - * An immutable map from names to mirrors for all variable |
| - * declarations in this library. |
| + * Returns closure for invoking the regular method named [name]. |
| + * |
| + * If [:declarations[name]:] is a regular method, the result of this method |
| + * is a closure equivalent to: |
| + * |
| + * (r1, .., rn, {p1: d1, ..., pk: dk}) { |
| + * return this.invoke(name, [r1, .., rn], {#p1: p1, .., #pk: pk}); |
| + * } |
| + * |
| + * if m has required parameters r1, ..., rn, and named parameters p1, ..., pk |
| + * with defaults d1, ..., dk. |
| + * |
| + * (r1, .., rn, [p1 = d1, …, pk = dk]) { |
| + * return this.invoke(name, [r1, .., rn, p1, .., pk]); |
| + * } |
| + * |
| + * if m has required parameters r1, ..., rn, and optional positional |
| + * parameters p1, ..., pk with defaults d1, ..., dk. |
| */ |
| - Map<Symbol, VariableMirror> get variables; |
| + Function operator [](Symbol name); |
| /** |
| * Returns [:true:] if this mirror is equal to [other]. |
| @@ -665,7 +710,7 @@ abstract class LibraryMirror implements DeclarationMirror, ObjectMirror { |
| * are |
| * the same library in the same isolate. |
| */ |
| - bool operator == (other); |
| + bool operator ==(other); |
| } |
| /** |
| @@ -705,7 +750,7 @@ abstract class ClassMirror implements TypeMirror, ObjectMirror { |
| List<ClassMirror> get superinterfaces; |
| /** |
| - * An immutable map from from names to mirrors for all members of |
| + * An immutable map from from names to mirrors for all declared members of |
| * this type. |
| * |
| * The members of a type are its methods, fields, getters, and |
| @@ -714,38 +759,33 @@ abstract class ClassMirror implements TypeMirror, ObjectMirror { |
| * |
| * This does not include inherited members. |
| */ |
| - Map<Symbol, Mirror> get members; |
| + Map<Symbol, DeclarationMirror> get declarations; |
|
gbracha
2013/09/17 18:27:53
I am slightly confused. I thought the plan was to
|
|
gbracha
2013/09/26 19:50:35
In our latest discussion, we assume that all decla
gbracha
2013/10/03 22:11:25
We now decided not to include type variables, beca
|
| /** |
| - * An immutable map from names to mirrors for all method, |
| - * declarations for this type. This does not include getters and |
| - * setters. |
| - */ |
| - Map<Symbol, MethodMirror> get methods; |
| - |
| - /** |
| - * An immutable map from names to mirrors for all getter |
| - * declarations for this type. |
| - */ |
| - Map<Symbol, MethodMirror> get getters; |
| - |
| - /** |
| - * An immutable map from names to mirrors for all setter |
| - * declarations for this type. |
| - */ |
| - Map<Symbol, MethodMirror> get setters; |
| - |
| - /** |
| - * An immutable map from names to mirrors for all variable |
| - * declarations for this type. |
| + * Returns closure for invoking the regular method named [name]. |
| + * |
| + * If [:declarations[name]:] is a regular static method, the result of this |
| + * method is a closure equivalent to: |
| + * |
| + * (r1, .., rn, {p1: d1, ..., pk: dk}) { |
| + * return this.invoke(name, [r1, .., rn], {#p1: p1, .., #pk: pk}); |
| + * } |
| + * |
| + * if m has required parameters r1, ..., rn, and named parameters p1, ..., pk |
| + * with defaults d1, ..., dk. |
| + * |
| + * (r1, .., rn, [p1 = d1, …, pk = dk]) { |
| + * return this.invoke(name, [r1, .., rn, p1, .., pk]); |
| + * } |
| + * |
| + * if m has required parameters r1, ..., rn, and optional positional |
| + * parameters p1, ..., pk with defaults d1, ..., dk. |
| */ |
| - Map<Symbol, VariableMirror> get variables; |
| + Function operator [](Symbol name); |
|
gbracha
2013/09/17 18:27:53
nits in wording:
Returns *a* closure ...
if m h
|
|
gbracha
2013/09/26 19:50:35
Also, why is this restricted to static methods? Wh
rmacnak
2013/09/26 22:47:52
From a subscript operator on InstanceMirror? Hois
|
| - /** |
| - * An immutable map from names to mirrors for all constructor |
| - * declarations for this type. |
| - */ |
| - Map<Symbol, MethodMirror> get constructors; |
| + /// Finds the instance member named [name] declared or inherited in the |
|
rmacnak
2013/09/03 18:42:37
Does this include getters and setters? If so, how
ahe
2013/09/03 18:47:20
Yes.
rmacnak
2013/09/03 19:44:26
I was thinking something like this, but the overri
ahe
2013/09/03 19:52:36
Yes. We have tried very hard to ensure a single na
|
| + /// reflected class. |
| + DeclarationMirror instanceLookup(Symbol name); |
|
ahe
2013/09/26 18:03:36
We also need a way to get a list or map of all ins
rmacnak
2013/09/26 22:47:52
And perhaps add DeclarationMirror.isSynthetic
|
| /** |
| * An immutable map from names to mirrors for all type variables for |
|
Michael Lippautz (Google)
2013/09/03 17:24:50
comment out of sync
|
| @@ -758,7 +798,7 @@ abstract class ClassMirror implements TypeMirror, ObjectMirror { |
| * |
| * This map preserves the order of declaration of the type variables. |
| */ |
| - Map<Symbol, TypeVariableMirror> get typeVariables; |
| + List<TypeVariableMirror> get typeVariables; |
|
Alan Knight
2013/09/03 20:08:31
Would Iterable be better than List?
ahe
2013/09/03 20:14:30
I think length and operator[] are important proper
Alan Knight
2013/09/03 20:30:56
Important for what in this context? Are we expecti
|
| /** |
| * An immutable map from names to mirrors for all type arguments for |
|
Michael Lippautz (Google)
2013/09/03 17:24:50
comment out of sync
|
| @@ -773,7 +813,7 @@ abstract class ClassMirror implements TypeMirror, ObjectMirror { |
| * it has no type arguments and this method returns an empty map. |
| * This map preserves the order of declaration of the type variables. |
| */ |
| - Map<Symbol, TypeMirror> get typeArguments; |
| + List<TypeMirror> get typeArguments; |
| /** |
| * Is this the original declaration of this type? |
| @@ -862,7 +902,7 @@ abstract class ClassMirror implements TypeMirror, ObjectMirror { |
| * with the result of calling [reflect](*r*). |
| * If the invocation throws an exception *e* (that it does not catch) |
| * then *k* is completed with a [MirrorError] wrapping *e*. |
| -*/ |
| + */ |
| Future<InstanceMirror> newInstanceAsync(Symbol constructorName, |
| List positionalArguments, |
| [Map<Symbol, dynamic> namedArguments]); |
| @@ -1123,88 +1163,6 @@ abstract class SourceLocation { |
| } |
| /** |
| - * When an error occurs during the mirrored execution of code, a |
| - * [MirroredError] is thrown. |
| - * |
| - * In general, there are three main classes of failure that can happen |
| - * during mirrored execution of code in some isolate: |
| - * |
| - * - An exception is thrown but not caught. This is caught by the |
| - * mirrors framework and a [MirroredUncaughtExceptionError] is |
| - * created and thrown. |
| - * |
| - * - A compile-time error occurs, such as a syntax error. This is |
| - * suppressed by the mirrors framework and a |
| - * [MirroredCompilationError] is created and thrown. |
| - * |
| - * - A truly fatal error occurs, causing the isolate to be exited. If |
| - * the reflector and reflectee share the same isolate, then they |
| - * will both suffer. If the reflector and reflectee are in distinct |
| - * isolates, then we hope to provide some information about the |
| - * isolate death, but this has yet to be implemented. |
| - * |
| - * TODO(turnidge): Specify the behavior for remote fatal errors. |
| - */ |
| -abstract class MirroredError implements Exception { |
| -} |
| - |
| -/** |
| - * When an uncaught exception occurs during the mirrored execution |
| - * of code, a [MirroredUncaughtExceptionError] is thrown. |
| - * |
| - * This exception contains a mirror on the original exception object. |
| - * It also contains an object which can be used to recover the |
| - * stacktrace. |
| - */ |
| -class MirroredUncaughtExceptionError extends MirroredError { |
|
rmacnak
2013/09/03 18:42:37
We will need this for cross-isolate reflection, bu
|
| - MirroredUncaughtExceptionError(this.exception_mirror, |
| - this.exception_string, |
| - this.stacktrace) {} |
| - |
| - /** A mirror on the exception object. */ |
| - final InstanceMirror exception_mirror; |
| - |
| - /** The result of toString() for the exception object. */ |
| - final String exception_string; |
| - |
| - /** A stacktrace object for the uncaught exception. */ |
| - final Object stacktrace; |
| - |
| - String toString() { |
| - return |
| - "Uncaught exception during mirrored execution: <${exception_string}>"; |
| - } |
| -} |
| - |
| -/** |
| - * When a compile-time error occurs during the mirrored execution |
| - * of code, a [MirroredCompilationError] is thrown. |
| - * |
| - * This exception includes the compile-time error message that would |
| - * have been displayed to the user, if the function had not been |
| - * invoked via mirror. |
| - */ |
| -class MirroredCompilationError extends MirroredError { |
|
Michael Lippautz (Google)
2013/09/03 17:24:50
Note: I suspect that removing this classes will br
rmacnak
2013/09/03 18:42:37
We should keep this. Catching compilation errors i
ahe
2013/09/03 18:47:20
Yes, I think I would start by moving these classes
|
| - MirroredCompilationError(this.message) {} |
| - |
| - final String message; |
| - |
| - String toString() { |
| - return "Compile-time error during mirrored execution: <$message>"; |
| - } |
| -} |
| - |
| -/** |
| - * A [MirrorException] is used to indicate errors within the mirrors |
| - * framework. |
| - */ |
| -class MirrorException implements Exception { |
| - const MirrorException(String this._message); |
| - String toString() => "MirrorException: '$_message'"; |
| - final String _message; |
| -} |
| - |
| -/** |
| * Class used for encoding comments as metadata annotations. |
| */ |
| class Comment { |