소스 검색

CompilationServer.invalidate: raise error that macros can catch; add test

Rudy Ges 7 달 전
부모
커밋
ba54ffc9cf

+ 2 - 1
src/macro/macroApi.ml

@@ -2329,7 +2329,8 @@ let macro_api ccom get_api =
 			(try
 				ignore(com.module_lut#find mpath);
 				let msg = "Cannot invalidate loaded module " ^ (s_type_path mpath) in
-				(get_api()).display_error msg (get_api_call_pos())
+				let pos = get_api_call_pos() in
+				compiler_error (Error.make_error (Custom msg) pos)
 			with Not_found ->
 				com.cs#taint_module mpath ServerInvalidate);
 			vnull

+ 3 - 2
std/haxe/macro/CompilationServer.hx

@@ -70,9 +70,10 @@ class CompilationServer {
 	}
 
 	/**
-		TODO: apply some restrictions
-
 		Invalidates a module, removing it from the cache.
+
+		If the module has already been loaded in current context, a compiler
+		error will be raised which can be caught using `try ... catch`.
 	**/
 	static public function invalidateModule(path:String) {
 		@:privateAccess Compiler.load("server_invalidate_module", 1)(path);

+ 17 - 0
tests/server/src/cases/issues/Issue12001.hx

@@ -99,4 +99,21 @@ class Issue12001 extends TestCase {
 		}
 		test();
 	}
+
+	function testInvalidateError(_) {
+		vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
+		vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
+		var args = ["-main", "Empty", "--interp", "--macro", "Macro.hookInvalidateError()"];
+		runHaxe(args);
+		assertErrorMessage("Cannot invalidate loaded module Empty");
+	}
+
+	function testInvalidateCaughtError(_) {
+		vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
+		vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
+		var args = ["-main", "Empty", "--interp", "--macro", "Macro.hookInvalidateCatch()"];
+		runHaxe(args);
+		assertSuccess();
+		assertHasPrint("Cannot invalidate loaded module Empty");
+	}
 }

+ 16 - 0
tests/server/test/templates/issues/Issue12001/Macro.hx

@@ -84,3 +84,19 @@ function hookRedefine() {
 		}]);
 	});
 }
+
+function hookInvalidateError() {
+	Context.onAfterTyping((_) -> {
+		CompilationServer.invalidateModule("Empty");
+	});
+}
+
+function hookInvalidateCatch() {
+	Context.onAfterTyping((_) -> {
+		try {
+			CompilationServer.invalidateModule("Empty");
+		} catch (e:Dynamic) {
+			Sys.println(Std.string(e));
+		}
+	});
+}