Browse Source

[macro] add Context.typeAndStoreExpr

closes #10239
Simon Krajewski 4 years ago
parent
commit
d31ab67231
2 changed files with 15 additions and 0 deletions
  1. 7 0
      src/macro/macroApi.ml
  2. 8 0
      std/haxe/macro/Context.hx

+ 7 - 0
src/macro/macroApi.ml

@@ -1904,6 +1904,13 @@ let macro_api ccom get_api =
 			let e = decode_texpr e in
 			encode_expr ((get_api()).store_typed_expr e)
 		);
+		"type_and_store_expr", vfun1 (fun e ->
+			let api = get_api() in
+			let te = (api.type_expr (decode_expr e)) in
+			let v_e = encode_expr (api.store_typed_expr te) in
+			let v_to_string () = (s_type (print_context())) te.etype in
+			encode_obj ["expr",v_e;"type",encode_ref te.etype encode_type v_to_string]
+		);
 		"get_output", vfun0 (fun() ->
 			encode_string (ccom()).file
 		);

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

@@ -587,6 +587,14 @@ class Context {
 		return load("store_expr", 1)(e);
 	}
 
+	/**
+		This function works like `storeExpr`, but also returns access to the expression's
+		type through the `type` field of the return value.
+	**/
+	public static function typeAndStoreExpr(e:Expr):{final type:Type.Ref<Type>; final expr:Expr;} {
+		return load("type_and_store_expr", 1)(e);
+	}
+
 	/**
 		Manually adds a dependency between module `modulePath` and an external
 		file `externFile`.