From 12f890e4a23c4dd3d51379e6e89318e556ff5f84 Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 13 Jul 2023 15:03:26 -0600 Subject: [PATCH] throw permission_error in compile_assert when attempting to assert a built-in (#1872) --- src/machine/dispatch.rs | 12 ++++++++++-- src/machine/loader.rs | 22 +++++----------------- src/machine/machine_errors.rs | 2 +- src/machine/machine_indices.rs | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/machine/dispatch.rs b/src/machine/dispatch.rs index 3b30e75b..afe67fb9 100644 --- a/src/machine/dispatch.rs +++ b/src/machine/dispatch.rs @@ -4919,11 +4919,19 @@ impl Machine { self.machine_st.p = self.machine_st.cp; } &Instruction::CallBuiltInProperty => { - self.builtin_property(); + let key = self + .machine_st + .read_predicate_key(self.machine_st.registers[1], self.machine_st.registers[2]); + + self.machine_st.fail = !self.indices.builtin_property(key); step_or_fail!(self, self.machine_st.p += 1); } &Instruction::ExecuteBuiltInProperty => { - self.builtin_property(); + let key = self + .machine_st + .read_predicate_key(self.machine_st.registers[1], self.machine_st.registers[2]); + + self.machine_st.fail = !self.indices.builtin_property(key); step_or_fail!(self, self.machine_st.p = self.machine_st.cp); } &Instruction::CallMetaPredicateProperty => { diff --git a/src/machine/loader.rs b/src/machine/loader.rs index 5a309197..9da054c7 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -2016,6 +2016,7 @@ impl Machine { }; let arity = head.arity(); + let is_builtin = loader.wam_prelude.indices.builtin_property((name, arity)); let is_dynamic_predicate = loader .wam_prelude @@ -2026,7 +2027,7 @@ impl Machine { ); let no_such_predicate = - if !is_dynamic_predicate && !ClauseType::is_inbuilt(name, arity) { + if !is_dynamic_predicate && !is_builtin { let idx_tag = loader .wam_prelude .indices @@ -2038,8 +2039,9 @@ impl Machine { .map(|code_idx| code_idx.get_tag()) .unwrap_or(IndexPtrTag::DynamicUndefined); - idx_tag == IndexPtrTag::DynamicUndefined || - idx_tag == IndexPtrTag::Undefined + idx_tag == IndexPtrTag::DynamicUndefined || idx_tag == IndexPtrTag::Undefined + } else if is_builtin { + return Err(SessionError::CannotOverwriteBuiltIn((name, arity))); } else { is_dynamic_predicate }; @@ -2466,20 +2468,6 @@ impl Machine { } } } - - pub(crate) fn builtin_property(&mut self) { - let (name, arity) = self - .machine_st - .read_predicate_key(self.machine_st.registers[1], self.machine_st.registers[2]); - - if !ClauseType::is_inbuilt(name, arity) { // ClauseType::from(key.0, key.1, &mut self.machine_st.arena) { - if let Some(module) = self.indices.modules.get(&(atom!("builtins"))) { - self.machine_st.fail = !module.code_dir.contains_key(&(name, arity)); - } else { - self.machine_st.fail = true; - } - } - } } impl<'a> Loader<'a, LiveLoadAndMachineState<'a>> { diff --git a/src/machine/machine_errors.rs b/src/machine/machine_errors.rs index 62b06ca2..936200f0 100644 --- a/src/machine/machine_errors.rs +++ b/src/machine/machine_errors.rs @@ -442,7 +442,7 @@ impl MachineState { // SessionError::CannotOverwriteImport(pred_atom) => { self.permission_error( Permission::Modify, - atom!("private_procedure"), + atom!("static_procedure"), functor_stub(key.0, key.1).into_iter().collect::(), ) } diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index df880447..b19ac3bf 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -3,6 +3,7 @@ use crate::parser::ast::*; use crate::arena::*; use crate::atom_table::*; use crate::forms::*; +use crate::machine::ClauseType; use crate::machine::loader::*; use crate::machine::machine_state::*; use crate::machine::streams::Stream; @@ -259,6 +260,20 @@ pub struct IndexStore { } impl IndexStore { + pub(crate) fn builtin_property(&self, key: PredicateKey) -> bool { + let (name, arity) = key; + + if !ClauseType::is_inbuilt(name, arity) { + return if let Some(module) = self.modules.get(&(atom!("builtins"))) { + module.code_dir.contains_key(&(name, arity)) + } else { + false + }; + } else { + true + } + } + #[inline(always)] pub(crate) fn goal_expansion_defined(&self, key: PredicateKey) -> bool { self.goal_expansion_indices.contains(&key) -- 2.54.0