Forráskód Böngészése

support imports and usings arguments on Context.defineModule

Simon Krajewski 11 éve
szülő
commit
fb6f2ac383
4 módosított fájl, 39 hozzáadás és 9 törlés
  1. 20 5
      interp.ml
  2. 4 2
      std/haxe/macro/Context.hx
  3. 11 0
      std/haxe/macro/Expr.hx
  4. 4 2
      typer.ml

+ 20 - 5
interp.ml

@@ -117,7 +117,7 @@ type extern_api = {
 	get_build_fields : unit -> value;
 	get_pattern_locals : Ast.expr -> Type.t -> (string,Type.tvar * Ast.pos) PMap.t;
 	define_type : value -> unit;
-	define_module : string -> value list -> unit;
+	define_module : string -> value list -> ((string * Ast.pos) list * Ast.import_mode) list -> Ast.type_path list -> unit;
 	module_dependency : string -> string -> bool -> unit;
 	current_module : unit -> module_def;
 	delayed_macro : int -> (unit -> (unit -> value));
@@ -204,6 +204,8 @@ let enc_string_ref = ref (fun s -> assert false)
 let make_ast_ref = ref (fun _ -> assert false)
 let make_complex_type_ref = ref (fun _ -> assert false)
 let encode_tvar_ref = ref (fun _ -> assert false)
+let decode_path_ref = ref (fun _ -> assert false)
+let decode_import_ref = ref (fun _ -> assert false)
 let get_ctx() = (!get_ctx_ref)()
 let enc_array (l:value list) : value = (!enc_array_ref) l
 let dec_array (l:value) : value list = (!dec_array_ref) l
@@ -220,6 +222,8 @@ let make_ast (e:texpr) : Ast.expr = (!make_ast_ref) e
 let enc_string (s:string) : value = (!enc_string_ref) s
 let make_complex_type (t:Type.t) : Ast.complex_type = (!make_complex_type_ref) t
 let encode_tvar (v:tvar) : value = (!encode_tvar_ref) v
+let decode_path (v:value) : Ast.type_path = (!decode_path_ref) v
+let decode_import (v:value) : ((string * Ast.pos) list * Ast.import_mode) = (!decode_import_ref) v
 
 let to_int f = Int32.of_float (mod_float f 2147483648.0)
 let need_32_bits i = Int32.compare (Int32.logand (Int32.add i 0x40000000l) 0x80000000l) Int32.zero <> 0
@@ -2487,10 +2491,10 @@ let macro_lib =
 			(get_ctx()).curapi.define_type v;
 			VNull
 		);
-		"define_module", Fun2 (fun p v ->
-			match p, v with
-			| VString path, VArray vl ->
-				(get_ctx()).curapi.define_module path (Array.to_list vl);
+		"define_module", Fun4 (fun p v i u ->
+			match p, v, i, u with
+			| VString path, VArray vl, VArray ui, VArray ul ->
+				(get_ctx()).curapi.define_module path (Array.to_list vl) (List.map decode_import (Array.to_list ui)) (List.map decode_path (Array.to_list ul));
 				VNull
 			| _ ->
 				error()
@@ -3998,6 +4002,15 @@ let decode_unop op =
 	| 4, [] -> NegBits
 	| _ -> raise Invalid_expr
 
+let decode_import_mode t = 
+	match decode_enum t with
+	| 0, [] -> INormal
+	| 1, [alias] -> IAsName (dec_string alias)
+	| 2, [] -> IAll
+	| _ -> raise Invalid_expr
+
+let decode_import t = (List.map (fun o -> ((dec_string (field o "name")), (decode_pos (field o "pos")))) (dec_array (field t "path")), decode_import_mode (field t "mode"))
+
 let rec decode_path t =
 	{
 		tpackage = List.map dec_string (dec_array (field t "pack"));
@@ -4996,3 +5009,5 @@ enc_hash_ref := enc_hash;
 encode_texpr_ref := encode_texpr;
 decode_texpr_ref := decode_texpr;
 encode_tvar_ref := encode_tvar;
+decode_path_ref := decode_path;
+decode_import_ref := decode_import;

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

@@ -434,8 +434,10 @@ class Context {
 	/**
 		Defines a new module with several `TypeDefinition` `types`.
 	**/
-	public static function defineModule( modulePath : String, types : Array<TypeDefinition> ) : Void {
-		load("define_module", 2)(untyped modulePath.__s,untyped types.__neko());
+	public static function defineModule( modulePath : String, types : Array<TypeDefinition>, ?imports: Array<ImportExpr>, ?usings : Array<TypePath> ) : Void {
+		if (imports == null) imports = [];
+		if (usings == null) usings = [];
+		load("define_module", 4)(untyped modulePath.__s, untyped types.__neko(), untyped imports.__neko(), untyped usings.__neko());
 	}
 
 	/**

+ 11 - 0
std/haxe/macro/Expr.hx

@@ -334,3 +334,14 @@ class Error {
 		return message;
 	}
 }
+
+enum ImportMode {
+	INormal;
+	IAsName(alias:String);
+	IAll;
+}
+
+typedef ImportExpr = {
+	var path: Array< { pos: Position, name: String } >;
+	var mode: ImportMode;
+}

+ 4 - 2
typer.ml

@@ -4341,13 +4341,15 @@ let make_macro_api ctx p =
 			| _ ->
 				()
 		);
-		Interp.define_module = (fun m types ->
+		Interp.define_module = (fun m types imports usings ->
 			let types = List.map (fun v ->
 				let _, tdef, pos = (try Interp.decode_type_def v with Interp.Invalid_expr -> Interp.exc (Interp.VString "Invalid type definition")) in
 				tdef, pos
 			) types in
-			let m = Ast.parse_path m in
 			let pos = (match types with [] -> Ast.null_pos | (_,p) :: _ -> p) in
+			let types = types @ (List.map (fun (il,ik) -> EImport(il,ik),pos)) imports in
+			let types = types @ (List.map (fun tp -> EUsing tp,pos)) usings in
+			let m = Ast.parse_path m in
 			let prev = (try Some (Hashtbl.find ctx.g.modules m) with Not_found -> None) in
 			let mnew = Typeload.type_module ctx m ctx.m.curmod.m_extra.m_file types pos in
 			add_dependency mnew ctx.m.curmod;