diff --git a/src/machine/lib_machine/mod.rs b/src/machine/lib_machine/mod.rs index 24f22fe7..be5c6008 100644 --- a/src/machine/lib_machine/mod.rs +++ b/src/machine/lib_machine/mod.rs @@ -544,7 +544,7 @@ impl Machine { self.run_module_predicate(atom!("loader"), (atom!("consult_stream"), 2)); } - fn allocate_stub_choice_point(&mut self) { + pub(crate) fn allocate_stub_choice_point(&mut self) { // NOTE: create a choice point to terminate the dispatch_loop // if an exception is thrown. diff --git a/src/machine/mod.rs b/src/machine/mod.rs index a62c0caa..cbd99c53 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -279,6 +279,7 @@ impl Machine { if let Some(module) = self.indices.modules.get(&module_name) { if let Some(code_index) = module.code_dir.get(&key) { let p = code_index.local().unwrap(); + self.allocate_stub_choice_point(); self.machine_st.cp = BREAK_FROM_DISPATCH_LOOP_LOC; self.machine_st.p = p; @@ -1264,3 +1265,25 @@ impl Machine { } } } + +#[cfg(test)] +mod tests { + use super::config::*; + use super::*; + + #[test] + fn test_run_module_predicate_throw() { + let mut machine = MachineBuilder::default() + .with_toplevel( + r#" + :- module('$toplevel', []). + repl :- throw(kaboom). + "#, + ) + .build(); + + let query = machine.run_module_predicate(atom!("$toplevel"), (atom!("repl"), 0)); + + assert_eq!(query, std::process::ExitCode::SUCCESS); + } +}