Browse Source

Add Context.getDefines()

Gama11 11 năm trước cách đây
mục cha
commit
959063d5a7
2 tập tin đã thay đổi với 20 bổ sung1 xóa
  1. 6 0
      interp.ml
  2. 14 1
      std/haxe/macro/Context.hx

+ 6 - 0
interp.ml

@@ -2137,6 +2137,12 @@ let macro_lib =
 			| VString s -> (try VString (Common.raw_defined_value (ccom()) s) with Not_found -> VNull)
 			| _ -> error();
 		);
+		"get_defines", Fun0 (fun() ->
+			let defines = (ccom()).defines in
+			let h = Hashtbl.create 0 in
+			PMap.iter (fun n v -> Hashtbl.replace h (VString n) (VString v)) defines;
+			enc_hash h
+		);
 		"get_type", Fun1 (fun s ->
 			match s with
 			| VString s ->

+ 14 - 1
std/haxe/macro/Context.hx

@@ -21,6 +21,7 @@
  */
 package haxe.macro;
 
+import haxe.ds.StringMap;
 import haxe.macro.Expr;
 import haxe.macro.Type.TypedExpr;
 
@@ -225,7 +226,19 @@ class Context {
 		var d = load("defined_value", 1)(untyped key.__s);
 		return d == null ? null : new String(d);
 	}
-
+	
+	/**
+		Returns a map of all compiler directives that have been set.
+		
+		Compiler directives are set using the `-D` command line parameter, or
+		by calling `haxe.macro.Compiler.define`.
+		
+		Modifying the returned map has no effect on the compiler.
+	 */
+	public static function getDefines() : haxe.ds.StringMap<String> {
+		return load("get_defines", 0)();
+	}
+	
 	/**
 		Resolves a type identified by `name`.