瀏覽代碼

[macro] support explicit module dependency for Context.defineType (closes #5613) (#6189)

Dan Korostelev 8 年之前
父節點
當前提交
795ec1c5dc
共有 3 個文件被更改,包括 13 次插入8 次删除
  1. 3 3
      src/macro/macroApi.ml
  2. 4 3
      src/macro/macroContext.ml
  3. 6 2
      std/haxe/macro/Context.hx

+ 3 - 3
src/macro/macroApi.ml

@@ -36,7 +36,7 @@ type 'value compiler_api = {
 	get_local_vars : unit -> (string, Type.tvar) PMap.t;
 	get_build_fields : unit -> 'value;
 	get_pattern_locals : Ast.expr -> Type.t -> (string,Type.tvar * Globals.pos) PMap.t;
-	define_type : 'value -> unit;
+	define_type : 'value -> string option -> unit;
 	define_module : string -> 'value list -> ((string * Globals.pos) list * Ast.import_mode) list -> Ast.type_path list -> unit;
 	module_dependency : string -> string -> bool -> unit;
 	current_module : unit -> module_def;
@@ -1735,8 +1735,8 @@ let macro_api ccom get_api =
 		"get_build_fields", vfun0 (fun() ->
 			(get_api()).get_build_fields()
 		);
-		"define_type", vfun1 (fun v ->
-			(get_api()).define_type v;
+		"define_type", vfun2 (fun v m ->
+			(get_api()).define_type v (opt decode_string m);
 			vnull
 		);
 		"define_module", vfun4 (fun path vl ui ul ->

+ 4 - 3
src/macro/macroContext.ml

@@ -264,12 +264,13 @@ let make_macro_api ctx p =
 		MacroApi.get_pattern_locals = (fun e t ->
 			!get_pattern_locals_ref ctx e t
 		);
-		MacroApi.define_type = (fun v ->
+		MacroApi.define_type = (fun v mdep ->
 			let m, tdef, pos = (try Interp.decode_type_def v with MacroApi.Invalid_expr -> Interp.exc_string "Invalid type definition") in
 			let add is_macro ctx =
-				let mnew = Typeload.type_module ctx m ctx.m.curmod.m_extra.m_file [tdef,pos] pos in
+				let mdep = Option.map_default (fun s -> Typeload.load_module ctx (parse_path s) pos) ctx.m.curmod mdep in
+				let mnew = Typeload.type_module ctx m mdep.m_extra.m_file [tdef,pos] pos in
 				mnew.m_extra.m_kind <- if is_macro then MMacro else MFake;
-				add_dependency mnew ctx.m.curmod;
+				add_dependency mnew mdep;
 			in
 			add false ctx;
 			(* if we are adding a class which has a macro field, we also have to add it to the macro context (issue #1497) *)

+ 6 - 2
std/haxe/macro/Context.hx

@@ -466,9 +466,13 @@ class Context {
 
 	/**
 		Defines a new type from `TypeDefinition` `t`.
+
+		If `moduleDependency` is given and is not `null`, it should contain
+		a module path that will be used as a dependency for the newly defined module
+		instead of the current module.
 	**/
-	public static function defineType( t : TypeDefinition ) : Void {
-		load("define_type", 1)(t);
+	public static function defineType( t : TypeDefinition, ?moduleDependency : String ) : Void {
+		load("define_type", 2)(t, moduleDependency);
 	}
 
 	/**