Skip to content

Commit ba54ffc

Browse files
committed
CompilationServer.invalidate: raise error that macros can catch; add test
1 parent c110c27 commit ba54ffc

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/macro/macroApi.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,8 @@ let macro_api ccom get_api =
23292329
(try
23302330
ignore(com.module_lut#find mpath);
23312331
let msg = "Cannot invalidate loaded module " ^ (s_type_path mpath) in
2332-
(get_api()).display_error msg (get_api_call_pos())
2332+
let pos = get_api_call_pos() in
2333+
compiler_error (Error.make_error (Custom msg) pos)
23332334
with Not_found ->
23342335
com.cs#taint_module mpath ServerInvalidate);
23352336
vnull

std/haxe/macro/CompilationServer.hx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class CompilationServer {
7070
}
7171

7272
/**
73-
TODO: apply some restrictions
74-
7573
Invalidates a module, removing it from the cache.
74+
75+
If the module has already been loaded in current context, a compiler
76+
error will be raised which can be caught using `try ... catch`.
7677
**/
7778
static public function invalidateModule(path:String) {
7879
@:privateAccess Compiler.load("server_invalidate_module", 1)(path);

tests/server/src/cases/issues/Issue12001.hx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,21 @@ class Issue12001 extends TestCase {
9999
}
100100
test();
101101
}
102+
103+
function testInvalidateError(_) {
104+
vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
105+
vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
106+
var args = ["-main", "Empty", "--interp", "--macro", "Macro.hookInvalidateError()"];
107+
runHaxe(args);
108+
assertErrorMessage("Cannot invalidate loaded module Empty");
109+
}
110+
111+
function testInvalidateCaughtError(_) {
112+
vfs.putContent("Macro.hx", getTemplate("issues/Issue12001/Macro.hx"));
113+
vfs.putContent("Empty.hx", getTemplate("Empty.hx"));
114+
var args = ["-main", "Empty", "--interp", "--macro", "Macro.hookInvalidateCatch()"];
115+
runHaxe(args);
116+
assertSuccess();
117+
assertHasPrint("Cannot invalidate loaded module Empty");
118+
}
102119
}

tests/server/test/templates/issues/Issue12001/Macro.hx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,19 @@ function hookRedefine() {
8484
}]);
8585
});
8686
}
87+
88+
function hookInvalidateError() {
89+
Context.onAfterTyping((_) -> {
90+
CompilationServer.invalidateModule("Empty");
91+
});
92+
}
93+
94+
function hookInvalidateCatch() {
95+
Context.onAfterTyping((_) -> {
96+
try {
97+
CompilationServer.invalidateModule("Empty");
98+
} catch (e:Dynamic) {
99+
Sys.println(Std.string(e));
100+
}
101+
});
102+
}

0 commit comments

Comments
 (0)