ProcessMacro.hx 856 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package issue7877;
  2. import haxe.macro.Expr;
  3. import haxe.macro.Context;
  4. class ProcessMacro {
  5. public static macro function build():Array<Field> {
  6. var fields = Context.getBuildFields();
  7. var toInit = [
  8. for (field in fields) {
  9. switch (field) {
  10. case {name: name, kind: FVar(t, e), access: [AFinal]}:
  11. {name: name, type: t, def: e};
  12. case _:
  13. continue;
  14. }
  15. }
  16. ];
  17. var args:Array<FunctionArg> = [];
  18. var exprs = [];
  19. for (init in toInit) {
  20. args.push({
  21. name: init.name,
  22. opt: init.def != null,
  23. type: init.type,
  24. value: init.def
  25. });
  26. var n = init.name;
  27. exprs.push(macro this.$n = $i{n});
  28. }
  29. fields.push({
  30. pos: Context.currentPos(),
  31. name: 'new',
  32. access: [APublic],
  33. kind: FFun({ret: null, args: args, expr: {pos: Context.currentPos(), expr: EBlock(exprs)}})
  34. });
  35. return fields;
  36. }
  37. }