Skip to content

Commit

Permalink
[macro] add dependency to loaded macros
Browse files Browse the repository at this point in the history
Please work...

closes #7448
  • Loading branch information
Simn committed Feb 26, 2019
1 parent 4cbc7ac commit e0ddd85
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type context = {
file_lookup_cache : (string,string option) Hashtbl.t;
parser_cache : (string,(type_def * pos) list) Hashtbl.t;
module_to_file : (path,string) Hashtbl.t;
cached_macros : (path * string,((string * bool * t) list * t * tclass * Type.tclass_field)) Hashtbl.t;
cached_macros : (path * string,(((string * bool * t) list * t * tclass * Type.tclass_field) * module_def)) Hashtbl.t;
mutable stored_typed_exprs : (int, texpr) PMap.t;
(* output *)
mutable file : string;
Expand Down
5 changes: 3 additions & 2 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ let load_macro ctx display cpath f p =
| name :: pack when name.[0] >= 'A' && name.[0] <= 'Z' -> (List.rev pack,name), Some (snd cpath)
| _ -> cpath, None
) in
let meth = try Hashtbl.find mctx.com.cached_macros (cpath,f) with Not_found ->
let (meth,mloaded) = try Hashtbl.find mctx.com.cached_macros (cpath,f) with Not_found ->
let t = macro_timer ctx ["typing";s_type_path cpath ^ "." ^ f] in
let mloaded = load_macro_module ctx cpath display p in
let mt = Typeload.load_type_def mctx p { tpackage = fst cpath; tname = snd cpath; tparams = []; tsub = sub } in
Expand All @@ -529,7 +529,7 @@ let load_macro ctx display cpath f p =
api.MacroApi.current_macro_module <- (fun() -> mloaded);
if not (Common.defined ctx.com Define.NoDeprecationWarnings) then
DeprecationCheck.check_cf mctx.com meth p;
let meth = (match follow meth.cf_type with TFun (args,ret) -> args,ret,cl,meth | _ -> error "Macro call should be a method" p) in
let meth = (match follow meth.cf_type with TFun (args,ret) -> (args,ret,cl,meth),mloaded | _ -> error "Macro call should be a method" p) in
mctx.com.display <- DisplayTypes.DisplayMode.create DMNone;
if not ctx.in_macro then flush_macro_context mint ctx;
Hashtbl.add mctx.com.cached_macros (cpath,f) meth;
Expand All @@ -544,6 +544,7 @@ let load_macro ctx display cpath f p =
t();
meth
in
add_dependency ctx.m.curmod mloaded;
let call args =
if ctx.com.verbose then Common.log ctx.com ("Calling macro " ^ s_type_path cpath ^ "." ^ f ^ " (" ^ p.pfile ^ ":" ^ string_of_int (Lexer.get_error_line p) ^ ")");
let t = macro_timer ctx ["execution";s_type_path cpath ^ "." ^ f] in
Expand Down
14 changes: 14 additions & 0 deletions tests/server/src/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ class ServerTests extends HaxeServerTestCase {
runHaxe(args, true);
assertHasField("", "Type", "enumIndex", true);
}

function testBuildMacro() {
vfs.putContent("BuildMacro.hx", getTemplate("BuildMacro.hx"));
vfs.putContent("BuiltClass.hx", getTemplate("BuiltClass.hx"));
var args = ["-main", "BuiltClass.hx", "--interp"];
runHaxe(args);
runHaxe(args);
assertReuse("BuiltClass");
vfs.touchFile("BuildMacro.hx");
runHaxe(args);
assertNotCacheModified("BuildMacro");
assertSkipping("BuiltClass", "BuildMacro");
assertSkipping("BuildMacro");
}
}

class Main {
Expand Down
5 changes: 5 additions & 0 deletions tests/server/test/templates/BuildMacro.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class BuildMacro {
public static function build() {
return null;
}
}
4 changes: 4 additions & 0 deletions tests/server/test/templates/BuiltClass.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@:build(BuildMacro.build())
class BuiltClass {
static function main() {}
}

1 comment on commit e0ddd85

@nadako
Copy link
Member

@nadako nadako commented on e0ddd85 Feb 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please work...

:D

Please sign in to comment.