Bar.hx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package;
  2. #if macro
  3. import haxe.macro.Context;
  4. import haxe.macro.Expr;
  5. import haxe.macro.Type;
  6. using haxe.macro.ExprTools;
  7. using haxe.macro.TypeTools;
  8. #end
  9. class Bar
  10. {
  11. public function new()
  12. {
  13. trace('New Bar');
  14. }
  15. public function nonmacro_func(val:String)
  16. {
  17. trace('Hello runtime: $val');
  18. }
  19. public macro function macro_func(this_expr:Expr) // :this (should refer to the Bar instance on the Foo)
  20. {
  21. var this_ident:String = get_this_ident(this_expr);
  22. trace('${ this_expr.toString() } computed this_ident as: ${ this_ident }');
  23. var code = '${ this_ident }.nonmacro_func("${ this_ident }")';
  24. return Context.parse(code, Context.currentPos());
  25. }
  26. #if macro
  27. static function get_this_ident(this_expr:Expr):String
  28. {
  29. // Read the ident from the source code
  30. return switch (this_expr.expr) {
  31. case EMeta(_, e):
  32. var info = Context.getPosInfos(this_expr.pos);
  33. var bytes = sys.io.File.getBytes(info.file);
  34. bytes.getString(info.min, info.max-info.min);
  35. default:
  36. // Not in the above form? Hmm...
  37. trace('Unexpected this resolution: ${ this_expr.toString() }');
  38. this_expr.toString();
  39. }
  40. }
  41. #end
  42. }