瀏覽代碼

[eval] don't reset GlobalState.get_ctx_ref to uninitialized state (fixes #9931)

Aleksandr Kuzmenko 4 年之前
父節點
當前提交
e405240ac2
共有 2 個文件被更改,包括 15 次插入6 次删除
  1. 4 1
      src/macro/eval/evalContext.ml
  2. 11 5
      src/macro/eval/evalExceptions.ml

+ 4 - 1
src/macro/eval/evalContext.ml

@@ -291,6 +291,7 @@ and context = {
 
 module GlobalState = struct
 	let get_ctx_ref : (unit -> context) ref = ref (fun() -> die "GlobalState.get_ctx_ref called before initialization" __LOC__)
+	let initialized = ref false
 
 	let sid : int ref = ref (-1)
 
@@ -307,7 +308,9 @@ module GlobalState = struct
 end
 
 let get_ctx () = (!GlobalState.get_ctx_ref)()
-let select ctx = GlobalState.get_ctx_ref := (fun() -> ctx)
+let select ctx =
+	GlobalState.initialized := true;
+	GlobalState.get_ctx_ref := (fun() -> ctx)
 
 let s_debug_state = function
 	| DbgRunning -> "DbgRunning"

+ 11 - 5
src/macro/eval/evalExceptions.ml

@@ -100,13 +100,19 @@ let handle_stack_overflow eval f =
 	with Stack_overflow -> exc_string "Stack overflow"
 
 let catch_exceptions ctx ?(final=(fun() -> ())) f p =
-	let prev = !GlobalState.get_ctx_ref in
+	let reset_ctx =
+		if !GlobalState.initialized then
+			let prev = !GlobalState.get_ctx_ref in
+			(fun() -> GlobalState.get_ctx_ref := prev)
+		else
+			(fun() -> ())
+	in
 	select ctx;
 	let eval = get_eval ctx in
 	let env = eval.env in
 	let r = try
 		let v = handle_stack_overflow eval f in
-		GlobalState.get_ctx_ref := prev;
+		reset_ctx();
 		final();
 		Some v
 	with
@@ -117,7 +123,7 @@ let catch_exceptions ctx ?(final=(fun() -> ())) f p =
 		if is v key_haxe_macro_Error then begin
 			let v1 = field v key_exception_message in
 			let v2 = field v key_pos in
-			GlobalState.get_ctx_ref := prev;
+			reset_ctx();
 			final();
 			match v1 with
 				| VString s ->
@@ -148,7 +154,7 @@ let catch_exceptions ctx ?(final=(fun() -> ())) f p =
 				| _ :: l -> l (* Otherwise, ignore topmost frame position. *)
 			in
 			let msg = get_exc_error_message ctx v stack (if p' = null_pos then p else p') in
-			GlobalState.get_ctx_ref := prev;
+			reset_ctx();
 			final();
 			Error.error msg null_pos
 		end
@@ -156,7 +162,7 @@ let catch_exceptions ctx ?(final=(fun() -> ())) f p =
 		final();
 		None
 	| exc ->
-		GlobalState.get_ctx_ref := prev;
+		reset_ctx();
 		final();
 		raise exc
 	in