Skip to content

Commit

Permalink
[macro] build metadata with basic types from current context (#11336)
Browse files Browse the repository at this point in the history
* [macro] build metadata with basic types from current context

* [tests] add test for runtime metadata in init macros

* Use basic types from current context when typing metadata

* [tests] make sure rtti still works for macros
  • Loading branch information
kLabz authored Oct 18, 2023
1 parent 5a3b9d5 commit c5d3faa
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/macro/eval/evalPrototype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ let is_persistent cf =
let create_static_prototype ctx mt =
let path = (t_infos mt).mt_path in
let key = path_hash path in
let com = ctx.curapi.MacroApi.get_com() in
let com = if ctx.is_macro then ctx.curapi.MacroApi.get_macro_com() else ctx.curapi.MacroApi.get_com() in
let meta = Texpr.build_metadata com.Common.basic mt in
let o = match mt with
| TClassDecl c ->
Expand Down
1 change: 1 addition & 0 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type compiler_options = {
type 'value compiler_api = {
pos : Globals.pos;
get_com : unit -> Common.context;
get_macro_com : unit -> Common.context;
get_macro_stack : unit -> pos list;
init_macros_done : unit -> bool;
get_type : string -> Type.t option;
Expand Down
20 changes: 11 additions & 9 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ let typing_timer ctx need_type f =
exit();
raise e

let make_macro_com_api com p =
let make_macro_com_api com mcom p =
let parse_metadata s p =
try
match ParserEntry.parse_string Grammar.parse_meta com.defines s null_pos raise_typing_error false with
Expand All @@ -128,6 +128,7 @@ let make_macro_com_api com p =
{
MacroApi.pos = p;
get_com = (fun () -> com);
get_macro_com = (fun () -> mcom);
get_macro_stack = (fun () ->
let envs = Interp.call_stack (Interp.get_eval (Interp.get_ctx ())) in
let envs = match envs with
Expand Down Expand Up @@ -316,7 +317,7 @@ let make_macro_com_api com p =
);
}

let make_macro_api ctx p =
let make_macro_api ctx mctx p =
let parse_metadata s p =
try
match ParserEntry.parse_string Grammar.parse_meta ctx.com.defines s null_pos raise_typing_error false with
Expand All @@ -325,7 +326,7 @@ let make_macro_api ctx p =
with _ ->
raise_typing_error "Malformed metadata string" p
in
let com_api = make_macro_com_api ctx.com p in
let com_api = make_macro_com_api ctx.com mctx.com p in
{
com_api with
MacroApi.get_type = (fun s ->
Expand Down Expand Up @@ -736,7 +737,7 @@ let get_macro_context ctx =
ctx
| None ->
let mctx = create_macro_context ctx.com in
let api = make_macro_api ctx null_pos in
let api = make_macro_api ctx mctx null_pos in
let init,_ = create_macro_interp api mctx in
ctx.g.macros <- Some (init,mctx);
mctx.g.macros <- Some (init,mctx);
Expand Down Expand Up @@ -830,8 +831,8 @@ type macro_arg_type =
| MAOther

let type_macro ctx mode cpath f (el:Ast.expr list) p =
let api = make_macro_api ctx p in
let mctx = get_macro_context ctx in
let api = make_macro_api ctx mctx p in
let mctx, (margs,mret,mclass,mfield), call_macro = load_macro ctx ctx.com mctx api (mode = MDisplay) cpath f p in
let margs =
(*
Expand Down Expand Up @@ -1047,11 +1048,11 @@ let call_init_macro com mctx e =
let (path,meth,args,p) = resolve_init_macro com e in
let (mctx, api) = match mctx with
| Some mctx ->
let api = make_macro_com_api com p in
let api = make_macro_com_api com mctx.com p in
(mctx, api)
| None ->
let mctx = create_macro_context com in
let api = make_macro_com_api com p in
let api = make_macro_com_api com mctx.com p in
let init,_ = create_macro_interp api mctx in
mctx.g.macros <- Some (init,mctx);
(mctx, api)
Expand All @@ -1062,13 +1063,14 @@ let call_init_macro com mctx e =
mctx

let finalize_macro_api tctx mctx =
let api = make_macro_api tctx null_pos in
let api = make_macro_api tctx mctx null_pos in
match !macro_interp_cache with
| None -> ignore(create_macro_interp api mctx)
| Some mint -> mint.curapi <- api

let interpret ctx =
let mctx = Interp.create ctx.com (make_macro_api ctx null_pos) false in
let mctx = get_macro_context ctx in
let mctx = Interp.create ctx.com (make_macro_api ctx mctx null_pos) false in
Interp.add_types mctx ctx.com.types (fun t -> ());
match ctx.com.main with
| None -> ()
Expand Down
7 changes: 7 additions & 0 deletions tests/misc/projects/Issue11323/Macro.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@RuntimeMeta([42,0])
class Macro {
public static function init() {
var m = haxe.rtti.Meta.getType(Macro);
if (m.RuntimeMeta[0][0] != 42) throw "runtime meta failure";
}
}
1 change: 1 addition & 0 deletions tests/misc/projects/Issue11323/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function main() {}
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue11323/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--macro Macro.init()
--main Main

0 comments on commit c5d3faa

Please sign in to comment.