瀏覽代碼

added Compiler.getOutput,setOutput
neko.Sys.args() now return the compilation parameters in macro mode

Nicolas Cannasse 13 年之前
父節點
當前提交
0fdba1b6b6
共有 4 個文件被更改,包括 34 次插入12 次删除
  1. 3 1
      common.ml
  2. 21 7
      interp.ml
  3. 2 4
      main.ml
  4. 8 0
      std/haxe/macro/Compiler.hx

+ 3 - 1
common.ml

@@ -47,6 +47,7 @@ type basic_types = {
 type context = {
 	(* config *)
 	version : int;
+	args : string list;
 	mutable display : bool;
 	mutable debug : bool;
 	mutable verbose : bool;
@@ -87,10 +88,11 @@ exception Abort of string * Ast.pos
 let display_default = ref false
 let default_print = ref print_string
 
-let create v =
+let create v args =
 	let m = Type.mk_mono() in
 	{
 		version = v;
+		args = args;
 		debug = false;
 		display = !display_default;
 		verbose = false;

+ 21 - 7
interp.ml

@@ -124,6 +124,8 @@ type context = {
 	mutable do_string : value -> string;
 	mutable do_loadprim : value -> value -> value;
 	mutable do_compare : value -> value -> cmp;
+	mutable loader : value;
+	mutable exports : value;
 	(* runtime *)
 	mutable stack : value DynArray.t;
 	mutable callstack : callstack list;
@@ -727,13 +729,6 @@ let builtins =
 	let h = Hashtbl.create 0 in
 	List.iter (fun (n,f) -> Hashtbl.add h n (VFunction f)) funcs;
 	List.iter (fun (n,v) -> Hashtbl.add h n v) vals;
-	let loader = obj hash [
-		"args",VArray [||];
-		"loadprim",VFunction (Fun2 (fun a b -> (get_ctx()).do_loadprim a b));
-		"loadmodule",VFunction (Fun2 (fun a b -> assert false));
-	] in
-	Hashtbl.add h "loader" (VObject loader);
-	Hashtbl.add h "exports" (VObject { ofields = [||]; oproto = None });
 	h
 
 (* ---------------------------------------------------------------------- *)
@@ -1977,6 +1972,14 @@ let macro_lib =
 				encode_expr (make_ast e)
 			| _ -> error()
 		);
+		"get_output", Fun0 (fun() ->
+			VString (ccom()).file
+		);
+		"set_output", Fun1 (fun s ->
+			match s with
+			| VString s -> (ccom()).file <- s; VNull
+			| _ -> error()
+		);
 	]
 
 (* ---------------------------------------------------------------------- *)
@@ -2033,6 +2036,10 @@ let rec eval ctx (e,p) =
 			let f = float_of_string f in
 			(fun() -> VFloat f)
 		| String s -> (fun() -> VString s)
+		| Builtin "loader" ->
+			(fun() -> ctx.loader)
+		| Builtin "exports" ->
+			(fun() -> ctx.exports)
 		| Builtin s ->
 			let b = (try Hashtbl.find builtins s with Not_found -> throw ctx p ("Builtin not found '" ^ s ^ "'")) in
 			(fun() -> b)
@@ -2800,6 +2807,11 @@ let alloc_delayed ctx f =
 	pos
 
 let create com api =
+	let loader = obj hash [
+		"args",VArray (Array.of_list (List.map (fun s -> VString s) com.args));
+		"loadprim",VFunction (Fun2 (fun a b -> (get_ctx()).do_loadprim a b));
+		"loadmodule",VFunction (Fun2 (fun a b -> assert false));
+	] in
 	let ctx = {
 		com = com;
 		gen = Genneko.new_context com true;
@@ -2830,6 +2842,8 @@ let create com api =
 		(* context *)
 		curapi = api;
 		delayed = DynArray.create();
+		loader = VObject loader;
+		exports = VObject { ofields = [||]; oproto = None };
 	} in
 	ctx.do_call <- call ctx;
 	ctx.do_string <- to_string ctx 0;

+ 2 - 4
main.ml

@@ -26,7 +26,6 @@ type context = {
 	mutable flush : unit -> unit;
 	mutable setup : unit -> unit;
 	mutable messages : string list;
-	mutable params : string list;
 	mutable has_next : bool;
 	mutable has_error : bool;
 }
@@ -327,8 +326,7 @@ let default_flush ctx =
 
 let create_context params =
 	let ctx = {
-		com = Common.create version;
-		params = params;
+		com = Common.create version params;
 		flush = (fun()->());
 		setup = (fun()->());
 		messages = [];
@@ -809,7 +807,7 @@ try
 		),"<file> : [deprecated] compile code to Flash9 SWF file");
 	] in
 	let current = ref 0 in
-	let args = Array.of_list ("" :: ctx.params) in
+	let args = Array.of_list ("" :: ctx.com.args) in
 	let args_callback cl = classes := make_path cl :: !classes in
 	Arg.parse_argv ~current args (basic_args_spec @ adv_args_spec) args_callback usage;
 	add_libs com (!cp_libs);

+ 8 - 0
std/haxe/macro/Compiler.hx

@@ -61,6 +61,14 @@ class Compiler {
 		untyped load("add_class_path",1)(path.__s);
 	}
 
+	public static function getOutput() : String {
+		return new String(untyped load("get_output",0)());
+	}
+
+	public static function setOutput( fileOrDir : String ) {
+		untyped load("set_output",1)(untyped fileOrDir.__s);
+	}
+
 	/**
 		Include for compilation all classes defined in the given package excluding the ones referenced in the ignore list.
 	**/