Created
November 27, 2011 22:51
-
-
Save seaneagan/1398353 to your computer and use it in GitHub Desktop.
Dart reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IsolateMirror { | |
final LibraryMirror rootLibrary; | |
StackMirror stack(); | |
MirrorGroup<LibraryMirror> imports; | |
MirrorGroup<ReceivePort> ports; // not sure what is used as the name of a port | |
} | |
// invocation | |
interface InvocationMirror { | |
final InvocationKind kind; | |
final String name; | |
final ArgumentsMirror arguments; | |
const InvocationMirror(this.kind, this.name, this.arguments); | |
} | |
class InvocationKind { | |
static final GET = const InvocationKind._('get'); | |
static final SET = const InvocationKind._('set'); | |
static final METHOD = const InvocationKind._('method'); | |
static final OPERATOR = const InvocationKind._('operator'); | |
const InvocationKind._(this.name); | |
} | |
interface Invocable { | |
// calls noSuchMethod if necessary | |
// TODO: should noSuchMethod be allowed in global, static, and local scopes ? | |
invoke(InvocationMirror invocation); | |
} | |
// variables | |
interface VariableDeclarationMirror { | |
bool isFinal; | |
TypeMirror type; | |
} | |
interface VariableMirror { | |
VariableDeclarationMirror declaration; | |
Object get value(); | |
Object set value(); // throws if final | |
} | |
// generics | |
interface GenericMirror { | |
final TypeParametersMirror typeParameters; | |
} | |
interface SpecificMirror { | |
final GenericMirror generic; | |
final TypeArgumentsMirror typeArguments; | |
} | |
interface TypeParametersMirror extends List<TypeParameterMirror> {} | |
interface TypeArgumentsMirror extends List<TypeMirror> {} | |
// objects | |
interface ObjectMirror extends ScopeMirror { | |
final Object reflectee; | |
final ClassMirror getClass(); // "class" is a reserved word | |
const ObjectMirror(this.object); | |
} | |
// functions | |
interface FunctionTemplateMirror { | |
final SignatureMirror signature(); | |
final String source; // null for abstract methods | |
final List<ScopeDeclarationMirror> subscopes; | |
} | |
interface FunctionMirror extends ObjectMirror, FunctionTemplateMirror { | |
final Function reflectee; | |
apply(ArgumentsMirror arguments); | |
const FunctionMirror(this.object); | |
} | |
interface SignatureMirror extends TypeMirror { | |
final TypeMirror returnType; | |
final ParametersMirror parameters; | |
} | |
interface ParametersMirror { | |
final List<TypeMirror> required; | |
final Map<String, TypeMirror> optional; | |
const ParametersMirror([this.required/* = []*/, this.optional/* = {}*/]); | |
} | |
interface ArgumentsMirror { | |
final List positional; | |
final Map<String, Dynamic> named; | |
const ArgumentsMirror([this.positional/* = []*/, this.named/* = {}*/]); | |
} | |
// scopes | |
interface ScopeDeclarationMirror { | |
final MirrorGroup<VariableDeclarationMirror> variables; | |
final MirrorGroup<FunctionDeclarationMirror> methods; | |
final MirrorGroup<FunctionDeclarationMirror> getters; | |
final MirrorGroup<FunctionDeclarationMirror> setters; | |
final MirrorGroup<FunctionDeclarationMirror> operators; | |
} | |
interface ScopeMirror extends Invocable { | |
final ScopeMirror parentScope; | |
final MirrorGroup<VariableMirror> variables; | |
final MirrorGroup<FunctionMirror> methods; | |
// TODO: should getters/setters be allowed in global and local scopes ? | |
final MirrorGroup<NullaryMirror> getters; | |
final MirrorGroup<UnaryMirror<void, Dynanmic>> setters; | |
} | |
// libraries | |
interface LibraryMirror extends ScopeMirror { | |
// exports | |
final Collection<ClassDeclarationMirror> classDeclarations(); | |
final Collection<TypedefMirror> typedefs(); | |
final Collection<FunctionTypedefMirror> functionTypedefs(); | |
final Collection<ImportMirror> imports; | |
final String source; | |
} | |
interface ImportMirror { | |
final String url; | |
final String prefix; | |
final LibraryMirror library; | |
} | |
// mirror groups | |
typedef Map<Object, T extends Mirror> MirrorGroup<T>; | |
// types | |
interface TypeMirror { | |
final String simpleName; | |
bool isType(TypeMirror type); | |
} | |
interface TypeParameterMirror extends TypeMirror { | |
final TypeMirror bound; | |
} | |
// should typedefs even be exposed since they are just compile time sugar ? | |
// helps with auto-completion | |
interface TypedefMirror extends TypeMirror, GenericMirror { | |
ClassMirror getClass(); | |
} | |
interface SignatureTypedefMirror extends TypedefMirror { | |
ClassMirror getClass(); // always returns the Function interface | |
SignatureMirror signature; | |
} | |
// classes and interfaces | |
interface ClassTemplateMirror { | |
final ClassMirror superClass; // interfaces -> Object (null instead?), classes -> extends clause | |
final Collection<ClassMirror> superInterfaces; // interfaces -> extends clause, classes -> implements clause | |
final InstanceScopeDeclarationMirror instanceScopeDeclaration; | |
} | |
interface ClassDeclarationMirror extends ClassTemplateMirror, ScopeMirror, GenericMirror { | |
final ClassDeclarationMirror defaultClassDeclaration; // interfaces -> default clause (null if missing), classes -> this | |
// isInterface(); // => this != defaultClassDeclaration; | |
final LibraryMirror library; | |
} | |
interface ClassMirror extends ClassTemplateMirror, TypeMirror, SpecificMirror { | |
final ClassDeclarationMirror generic; | |
} | |
interface StackMirror extends Stack<ActivationMirror> { | |
} | |
typedef R Nullary<R>(); | |
typedef R Unary<R, P>(P p); | |
typedef R Operator<R, P1, P2>([P1 p1, P2 p2]); | |
interface NullaryMirror<R> extends FunctionMirror { | |
Nullary<R> object; | |
const NullaryMirror(this.object); | |
} | |
interface UnaryMirror<R, P1> extends FunctionMirror { | |
Unary<R, P1> object; | |
const UnaryMirror(this.object); | |
} | |
interface BinaryMirror<R, P1, P2> extends FunctionMirror { | |
Binary<R, P1, P2> object; | |
const BinaryMirror(this.object); | |
} | |
interface OperatorMirror<R, P1, P2> extends FunctionMirror { | |
Binary<R, P1, P2> object; | |
const OperatorMirror(this.object); | |
} | |
/* | |
interface ConstructorMirror extends FunctionMirror {} | |
interface FactoryConstructorMirror extends ConstructorMirror {} | |
interface GenerativeConstructorMirror extends ConstructorMirror {} | |
interface ConstConstructorMirror extends GenerativeConstructorMirror {} | |
interface RedirectingConstructorMirror extends GenerativeConstructorMirror { | |
final GenerativeConstructorMirror target; | |
} | |
*/ | |
typedef void Advice(InvocationMirror im); | |
interface InvocableMirror { | |
// return success (whether advice was taken) | |
bool beforeInvoke(Advice advice); | |
bool afterInvoke(Advice advice); | |
invoke(InvocationMirror im); | |
} | |
// Examples | |
final objectMirror = new ObjectMirror(foo); | |
final functionMirror = objectMirror.methods[const Slot("get", bar)]; | |
final function = functionMirror.object; | |
final value = function(); | |
final barValue = new ObjectMirror(foo).methods[const Slot("get", bar)].object(); | |
final objectMirror = new ObjectMirror(foo); | |
final functionMirror = objectMirror.methods[const Slot("get", bar)]; | |
final function = functionMirror.object; | |
final value = function(); | |
final om = new ObjectMirror(this); | |
om["foo"]; | |
om["foo"] = "bar"; | |
om.operate("+", 2); | |
// methods are merely Function getters | |
om["m"](a, b, c: 1, d: 2); | |
noSuchMethod(bool isOperator, String name, ArgumentsMirror arguments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment