From: Mark Thom Date: Sat, 6 Feb 2021 01:40:13 +0000 (-0700) Subject: gather initialization goals and call them once load succeeds (#792) X-Git-Tag: v0.9.0~150^2~65^2~11 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=a104b35ceac4803da08300fd275e1a69ef734daf;p=scryer-prolog.git gather initialization goals and call them once load succeeds (#792) --- diff --git a/src/clause_types.rs b/src/clause_types.rs index 76c70e74..6f412b78 100644 --- a/src/clause_types.rs +++ b/src/clause_types.rs @@ -387,8 +387,6 @@ impl SystemClauseType { clause_name!("$cpp_multifile_property"), &SystemClauseType::REPL(REPLCodePtr::DiscontiguousProperty) => clause_name!("$cpp_discontiguous_property"), - &SystemClauseType::REPL(REPLCodePtr::CompilePendingPredicates) => - clause_name!("$compile_pending_predicates"), &SystemClauseType::REPL(REPLCodePtr::AbolishClause) => clause_name!("$abolish_clause"), &SystemClauseType::Close => clause_name!("$close"), @@ -756,7 +754,6 @@ impl SystemClauseType { ("$cpp_dynamic_property", 3) => Some(SystemClauseType::REPL(REPLCodePtr::DynamicProperty)), ("$cpp_multifile_property", 3) => Some(SystemClauseType::REPL(REPLCodePtr::MultifileProperty)), ("$cpp_discontiguous_property", 3) => Some(SystemClauseType::REPL(REPLCodePtr::DiscontiguousProperty)), - ("$compile_pending_predicates", 1) => Some(SystemClauseType::REPL(REPLCodePtr::CompilePendingPredicates)), _ => None, } } diff --git a/src/loader.pl b/src/loader.pl index 5f499c7d..fb9d6840 100644 --- a/src/loader.pl +++ b/src/loader.pl @@ -74,6 +74,16 @@ unload_evacuable(Evacuable) :- '$pop_load_state_payload'(Evacuable), '$pop_load_context'. +run_initialization_goals :- + prolog_load_context(module, Module), + ( predicate_property(Module:'$initialization_goals'(_), dynamic) -> + findall(Goal, '$call'(builtins:retract(Module:'$initialization_goals'(Goal))), Goals), + ( maplist(Module:call, Goals) -> + true + ; true %% initialization goals can fail without thwarting the load. + ) + ; true + ). file_load(Stream, Path) :- file_load(Stream, Path, _). @@ -83,6 +93,7 @@ file_load(Stream, Path, Evacuable) :- catch(loader:load_loop(Stream, Evacuable), E, (loader:unload_evacuable(Evacuable), throw(E))), + run_initialization_goals, '$pop_load_context'. @@ -91,6 +102,7 @@ load(Stream) :- catch(loader:load_loop(Stream, Evacuable), E, (loader:unload_evacuable(Evacuable), throw(E))), + run_initialization_goals, '$pop_load_context'. load_loop(Stream, Evacuable) :- @@ -237,9 +249,8 @@ compile_declaration(dynamic(Name/Arity), Evacuable) :- '$add_dynamic_predicate'(Name, Arity, Evacuable). compile_declaration(initialization(Goal), Evacuable) :- prolog_load_context(module, Module), - '$compile_pending_predicates'(Evacuable), - expand_goal(call(Goal), Module, call(ExpandedGoal)), - call(ExpandedGoal). + assertz(Module:'$initialization_goals'(Goal)). + compile_clause(Clause, Evacuable, VNs) :- diff --git a/src/machine/loader.rs b/src/machine/loader.rs index eae4ab57..e7eb1165 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -1789,22 +1789,6 @@ impl Machine { self.machine_st.fail = true; } - - pub(crate) - fn compile_pending_predicates(&mut self) { - let (mut loader, evacuable_h) = self.loader_from_heap_evacuable(temp_v!(1)); - - let compile_pending_predicates = || { - if !loader.predicates.is_empty() { - loader.compile_and_submit()?; - } - - LiveTermStream::evacuate(loader) - }; - - let result = compile_pending_predicates(); - self.restore_load_state_payload(result, evacuable_h); - } } impl<'a> Loader<'a, LiveTermStream> { diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index 7a183ecd..b6883e77 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -524,7 +524,6 @@ pub enum REPLCodePtr { MultifileProperty, DiscontiguousProperty, DynamicProperty, - CompilePendingPredicates, AbolishClause, Asserta, Assertz, diff --git a/src/machine/mod.rs b/src/machine/mod.rs index 0b0439cd..6a73369e 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -491,9 +491,6 @@ impl Machine { REPLCodePtr::DynamicProperty => { self.dynamic_property(); } - REPLCodePtr::CompilePendingPredicates => { - self.compile_pending_predicates(); - } REPLCodePtr::Assertz => { self.compile_assert(AppendOrPrepend::Append); } diff --git a/src/write.rs b/src/write.rs index 17944e9d..00538abb 100644 --- a/src/write.rs +++ b/src/write.rs @@ -72,8 +72,6 @@ impl fmt::Display for REPLCodePtr { write!(f, "REPLCodePtr::MultifileProperty"), REPLCodePtr::DiscontiguousProperty => write!(f, "REPLCodePtr::DiscontiguousProperty"), - REPLCodePtr::CompilePendingPredicates => - write!(f, "REPLCodePtr::CompilePendingPredicates"), } } }