Browse Source

[eval] catch `Invalid_expr` when handling an API call

closes #7007
Simon Krajewski 7 years ago
parent
commit
dfe1795e4b
2 changed files with 27 additions and 1 deletions
  1. 9 1
      src/macro/eval/evalMain.ml
  2. 18 0
      tests/unit/src/unit/issues/Issue7007.hx

+ 9 - 1
src/macro/eval/evalMain.ml

@@ -341,7 +341,15 @@ let setup get_api =
 	let api = get_api (fun() -> (get_ctx()).curapi.get_com()) (fun() -> (get_ctx()).curapi) in
 	let api = get_api (fun() -> (get_ctx()).curapi.get_com()) (fun() -> (get_ctx()).curapi) in
 	List.iter (fun (n,v) -> match v with
 	List.iter (fun (n,v) -> match v with
 		| VFunction(f,b) ->
 		| VFunction(f,b) ->
-			let v = VFunction ((fun vl -> try f vl with Sys_error msg | Failure msg -> exc_string msg),b) in
+			let f vl = try
+				f vl
+			with
+			| Sys_error msg | Failure msg ->
+				exc_string msg
+			| MacroApi.Invalid_expr ->
+				exc_string "Invalid expression"
+			in
+			let v = VFunction (f,b) in
 			Hashtbl.replace EvalStdLib.macro_lib n v
 			Hashtbl.replace EvalStdLib.macro_lib n v
 		| _ -> assert false
 		| _ -> assert false
 	) api;
 	) api;

+ 18 - 0
tests/unit/src/unit/issues/Issue7007.hx

@@ -0,0 +1,18 @@
+package unit.issues;
+
+class Issue7007 extends unit.Test {
+	function test() {
+		#if !macro
+		eq("Exception caught: Invalid expression", catchMe());
+		#end
+	}
+
+	macro static function catchMe() {
+		try {
+			trace(haxe.macro.Context.getTypedExpr(null));
+		} catch (e:Dynamic) {
+			return macro $v{'Exception caught: $e'};
+		}
+		return macro null;
+	}
+}