Skip to content

Instantly share code, notes, and snippets.

@adam-singer
Created July 21, 2012 07:29
Show Gist options
  • Save adam-singer/3154967 to your computer and use it in GitHub Desktop.
Save adam-singer/3154967 to your computer and use it in GitHub Desktop.
Just noticed that mirrors are being compiled into the dartvm. Note mirrors implementation is still not ready :)
#!/usr/bin/env dart
#import('dart:mirrors');
#import('dart:isolate');
/*
* mirrors.h
* mirrors_impl.dart
* mirrors_sources.gypi
* mirrors.dart
*/
echo() {
port.receive((msg, reply) {
print("I received: $msg");
});
}
class UltraFunTime {
final _ultraFun = true;
bool isFun() {
return _ultraFun;
}
}
class SuperFunTime extends UltraFunTime {
final name="superfuntime";
void printme() {
print(name);
}
bool eatFood(String food) {
return false;
}
String sleep([int time=10]) {
return "";
}
String sendBack(String s) {
return s;
}
}
void main() {
/**
* Returns a [MirrorSystem] for the current isolate.
*/
// MirrorSystem currentMirrorSystem() {
// return _Mirrors.currentMirrorSystem();
// }
//currentMirrorSystem()
/**
* A mirror on the root library of the mirror system.
*/
//final LibraryMirror rootLibrary;
print(currentMirrorSystem().rootLibrary);
//Expect.equals(currentMirrorSystem().rootLibrary.toString(),"LibraryMirror on 'file:///Users/adam/dart/MirrorTests/MirrorTests.dart'");
/**
* An immutable map from from library names to mirrors for all
* libraries known to this mirror system.
*/
//Map<String, LibraryMirror> libraries();
currentMirrorSystem().libraries().forEach((k,v)=>print("Key=$k Value=$v"));
/**
* A mirror on the isolate associated with this [MirrorSystem].
* This may be null if this mirror system is not running.
*/
//IsolateMirror isolate;
print(currentMirrorSystem().isolate);
/**
* A unique name used to refer to an isolate in debugging messages.
*/
//final String debugName;
print(currentMirrorSystem().isolate.debugName);
/**
* Does this mirror reflect the currently running isolate?
*/
//final bool isCurrent;
print(currentMirrorSystem().isolate.isCurrent);
/**
* Returns an [InstanceMirror] for some Dart language object.
*
* This only works if this mirror system is associated with the
* current running isolate.
*/
//InstanceMirror mirrorOf(Object reflectee);
var sft = currentMirrorSystem().mirrorOf(new SuperFunTime());
print(sft);
/**
* Invokes the named function and returns a mirror on the result.
*
* TODO(turnidge): Properly document.
*/
// Future<InstanceMirror> invoke(String memberName,
// List<Object> positionalArguments,
// [Map<String,Object> namedArguments]);
sft.invoke("printme", []).then((v)=>print("printme invoke finished $v"));
sft.invoke("isFun", []).then((v)=>print("isFun invoke finished $v"));
sft.invoke("sendBack", ["blah blah blah"]).then((v)=>print("sendBack invoke finished $v"));
//NotImplementedException: named arguments not implemented
//sft.invoke("sendBack", [], {"s": "blah blah"}).then((v)=>print("sendBack invoke finished $v"));
/**
* Returns a mirror on the class of the reflectee.
*/
//InterfaceMirror getClass();
print(sft.getClass());
/**
* The name of this interface.
*/
//final String simpleName;
print(sft.getClass().simpleName);
/**
* Does this mirror represent a class?
*/
//final bool isClass;
print(sft.getClass().isClass);
/**
* Returns a mirror on the superclass on the reflectee.
*
* For interfaces, the superclass is Object.
*/
//InterfaceMirror superclass();
print(sft.getClass().superclass());
/**
* Returns a list of mirrors on the superinterfaces for the reflectee.
*/
//List<InterfaceMirror> superinterfaces();
print(sft.getClass().superinterfaces());
/**
* Returns a mirror on the default factory class or null if there is
* none.
*/
//InterfaceMirror defaultFactory();
print(sft.getClass().defaultFactory());
/**
* An immutable map from from names to mirrors for all members of
* this type.
*
* The members of an interface are its constructors, methods,
* fields, getters, and setters.
*
* This does not include inherited members.
*/
//Map<String, Mirror> members();
print(sft.getClass().members());
/**
* An immutable map from names to mirrors for all method,
* constructor, getter, and setter declarations in this library.
*/
//Map<String, MethodMirror> methods();
print(sft.getClass().methods());
sft.getClass().methods().forEach((k,v) {
print("key=$k");
print('v.isTopLevel=${v.isTopLevel}');
print('v.isStatic=${v.isStatic}');
print('v.isMethod=${v.isMethod}');
print('v.isAbstract=${v.isAbstract}');
print('v.isGetter=${v.isGetter}');
print('v.isSetter=${v.isSetter}');
print('v.isConstructor=${v.isConstructor}');
print('v.isConstConstructor=${v.isConstConstructor}');
print('v.isGenerativeConstructor=${v.isGenerativeConstructor}');
print('v.isRedirectingConstructor=${v.isRedirectingConstructor}');
print('v.isFactoryConstructor=${v.isFactoryConstructor}');
print('v.parameters()=${v.parameters()}');
v.parameters().forEach((p) {
print("p.type=${p.type}");
print("p.defaultValue=${p.defaultValue}");
print("p.hasDefaultValue=${p.hasDefaultValue}");
print("p.isOptional=${p.isOptional}");
});
print('v.isConstructor=${v.isConstructor}');
});
/**
* An immutable map from names to mirrors for all variable
* declarations in this library.
*/
//Map<String, VariableMirror> variables();
print(sft.getClass().variables());
sft.getClass().variables().forEach((k,v) {
print("key=$k");
print("v.simpleName=${v.simpleName}");
print("v.owner=${v.owner}");
print("v.isTopLevel=${v.isTopLevel}");
print("v.isStatic=${v.isStatic}");
print("v.isFinal=${v.isFinal}");
});
/**
* The library in which this interface is declared.
*/
//final LibraryMirror library;
print(sft.getClass().library);
/**
* Does [reflectee] contain the instance reflected by this mirror? This will
* always be true in the local case (reflecting instances in the same
* isolate), but only true in the remote case if this mirror reflects a
* simple value.
*
* A value is simple if one of the following holds:
* - the value is null
* - the value is of type [num]
* - the value is of type [bool]
* - the value is of type [String]
*/
//final bool hasReflectee;
print(sft.hasReflectee);
/**
* If the [InstanceMirror] reflects an instance it is meaningful to have a
* local reference to, we provide access to the actual instance here.
*
* If you access [reflectee] when [hasReflectee] is false, an
* exception is thrown.
*/
//final reflectee;
print(sft.reflectee);
var sendPort = spawnFunction(echo);
// sendPort.call("Hello from main").then((reply) {
// print(reply); // I received: Hello from main
// });
/**
* Creates a [MirrorSystem] for the isolate which is listening on
* the [SendPort].
*/
//Future<MirrorSystem> mirrorSystemOf(SendPort port) {
// return _Mirrors.mirrorSystemOf(port);
//}
//XXX:NotImplementedException: Remote mirrors not yet implemented
//mirrorSystemOf(sendPort).then((ms)=>print("MirrorSystem=${ms}"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment