Browse Source

ensure that local variables are unique in macros context (fixed issue #1118)

Nicolas Cannasse 13 years ago
parent
commit
5f39f373fe
3 changed files with 13 additions and 13 deletions
  1. 2 2
      interp.ml
  2. 1 1
      main.ml
  3. 10 10
      typer.ml

+ 2 - 2
interp.ml

@@ -3174,7 +3174,7 @@ let create com api =
 	List.iter (fun e -> ignore((eval ctx e)())) (Genneko.header());
 	ctx
 
-let add_types ctx types =
+let add_types ctx types ready =
 	let types = List.filter (fun t ->
 		let path = Type.t_path t in
 		if Hashtbl.mem ctx.types path then false else begin
@@ -3182,7 +3182,7 @@ let add_types ctx types =
 			true;
 		end
 	) types in
-	Codegen.post_process types [Codegen.captured_vars ctx.com];
+	List.iter ready types;
 	let e = (EBlock (Genneko.build ctx.gen types), null_pos) in
 	ignore(catch_errors ctx (fun() -> ignore((eval ctx e)())))
 

+ 1 - 1
main.ml

@@ -1049,7 +1049,7 @@ try
 		| _ when !no_output ->
 			if !interp then begin
 				let ctx = Interp.create com (Typer.make_macro_api tctx Ast.null_pos) in
-				Interp.add_types ctx com.types;
+				Interp.add_types ctx com.types (fun t -> ());
 				(match com.main with
 				| None -> ()
 				| Some e -> ignore(Interp.eval_expr ctx e));

+ 10 - 10
typer.ml

@@ -2738,6 +2738,14 @@ let make_macro_api ctx p =
 		);
 	}
 
+let flush_macro_context mctx ctx = 
+	finalize ctx;
+	let _, types, modules = generate ctx in
+	ctx.com.types <- types;
+	ctx.com.Common.modules <- modules;
+	(* we should maybe ensure that all filters in Main are applied. Not urgent atm *)
+	Interp.add_types mctx types (fun t -> Codegen.post_process [t] [Codegen.captured_vars ctx.com; Codegen.rename_local_vars ctx.com])
+
 let get_macro_context ctx p =
 	let api = make_macro_api ctx p in
 	match ctx.g.macros with
@@ -2773,9 +2781,7 @@ let get_macro_context ctx p =
 		(* ctx2.g.core_api <- ctx.g.core_api; // causes some issues because of optional args and Null type in Flash9 *)
 		ignore(Typeload.load_module ctx2 (["haxe";"macro"],"Expr") p);
 		ignore(Typeload.load_module ctx2 (["haxe";"macro"],"Type") p);
-		finalize ctx2;
-		let _, types, _ = generate ctx2 in
-		Interp.add_types mctx types;
+		flush_macro_context mctx ctx2;
 		Interp.init mctx;
 		api, ctx2
 
@@ -2798,13 +2804,7 @@ let load_macro ctx cpath f p =
 	) in
 	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 in_macro = ctx.in_macro in
-	if not in_macro then begin
-		finalize ctx2;
-		let _, types, modules = generate ctx2 in
-		ctx2.com.types <- types;
-		ctx2.com.Common.modules <- modules;
-		Interp.add_types mctx types;
-	end;
+	if not in_macro then flush_macro_context mctx ctx2;
 	t();
 	let call args =
 		let t = macro_timer ctx (s_type_path cpath ^ "." ^ f) in