Example.macro.hx 804 B

1234567891011121314151617181920212223242526272829303132
  1. import haxe.PosInfos;
  2. using haxe.io.Path;
  3. typedef ExamplePluginApi = {
  4. function hello():Void;
  5. function stringifyPosition(p:haxe.macro.Expr.Position):String;
  6. function hijackStaticTest():Void;
  7. }
  8. class Example {
  9. /** Access plugin API */
  10. static public var plugin(get,never):ExamplePluginApi;
  11. static var _plugin:ExamplePluginApi;
  12. static function get_plugin():ExamplePluginApi {
  13. if(_plugin == null) {
  14. try {
  15. _plugin = eval.vm.Context.loadPlugin(getPluginPath());
  16. } catch(e:Dynamic) {
  17. throw 'Failed to load plugin: $e';
  18. }
  19. }
  20. return _plugin;
  21. }
  22. static function getPluginPath():String {
  23. var currentFile = (function(?p:PosInfos) return p.fileName)();
  24. var srcDir = currentFile.directory().directory();
  25. return Path.join([srcDir, 'cmxs', Sys.systemName(), 'plugin.cmxs']);
  26. }
  27. }