[WIP] make AtomTable concurrentcy ready

This commit is contained in:
Skgland
2023-08-26 01:10:49 +02:00
committed by Bennet Bleßmann
parent b2130c2a48
commit 86166dbf25
25 changed files with 528 additions and 384 deletions

View File

@@ -1240,6 +1240,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
return Some(
LS::machine_st(&mut self.payload)
.atom_tbl
.blocking_write()
.build_with(path_str),
);
}
@@ -1259,7 +1260,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
let clause = self.try_term_to_tl(term, &mut preprocessor)?;
// let queue = preprocessor.parse_queue(self)?;
let mut cg = CodeGenerator::new(&mut LS::machine_st(&mut self.payload).atom_tbl, settings);
let mut cg = CodeGenerator::new(&LS::machine_st(&mut self.payload).atom_tbl, settings);
let clause_code = cg.compile_predicate(vec![clause])?;
@@ -1289,7 +1290,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
clauses.push(self.try_term_to_tl(term, &mut preprocessor)?);
}
let mut cg = CodeGenerator::new(&mut LS::machine_st(&mut self.payload).atom_tbl, settings);
let mut cg = CodeGenerator::new(&LS::machine_st(&mut self.payload).atom_tbl, settings);
let mut code = cg.compile_predicate(clauses)?;

View File

@@ -409,7 +409,7 @@ mod tests {
let pstr_var_cell = put_partial_string(
&mut wam.machine_st.heap,
"abc ",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
let pstr_cell = wam.machine_st.heap[pstr_var_cell.get_value() as usize];
@@ -419,7 +419,7 @@ mod tests {
let pstr_second_var_cell = put_partial_string(
&mut wam.machine_st.heap,
"def",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
let pstr_second_cell = wam.machine_st.heap[pstr_second_var_cell.get_value() as usize];

View File

@@ -647,7 +647,7 @@ mod tests {
let pstr_var_cell = put_partial_string(
&mut wam.machine_st.heap,
"abc ",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
let pstr_cell = wam.machine_st.heap[pstr_var_cell.get_value() as usize];
@@ -672,7 +672,7 @@ mod tests {
let pstr_second_var_cell = put_partial_string(
&mut wam.machine_st.heap,
"def",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
let pstr_second_cell = wam.machine_st.heap[pstr_second_var_cell.get_value() as usize];

View File

@@ -1150,7 +1150,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
pub(crate) fn use_module(&mut self, module_src: ModuleSource) -> Result<(), SessionError> {
let (stream, listing_src) = match module_src {
ModuleSource::File(filename) => {
let mut path_buf = PathBuf::from(filename.as_str());
let mut path_buf = PathBuf::from(&*filename.as_str());
path_buf.set_extension("pl");
let file = File::open(&path_buf)?;
@@ -1164,7 +1164,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
ListingSource::File(filename, path_buf),
)
}
ModuleSource::Library(library) => match LIBRARIES.borrow().get(library.as_str()) {
ModuleSource::Library(library) => match LIBRARIES.borrow().get(&*library.as_str()) {
Some(code) => {
if let Some(ref module) = self.wam_prelude.indices.modules.get(&library) {
if let ListingSource::DynamicallyGenerated = &module.listing_src {
@@ -1232,7 +1232,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
) -> Result<(), SessionError> {
let (stream, listing_src) = match module_src {
ModuleSource::File(filename) => {
let mut path_buf = PathBuf::from(filename.as_str());
let mut path_buf = PathBuf::from(&*filename.as_str());
path_buf.set_extension("pl");
let file = File::open(&path_buf)?;
@@ -1245,7 +1245,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
ListingSource::File(filename, path_buf),
)
}
ModuleSource::Library(library) => match LIBRARIES.borrow().get(library.as_str()) {
ModuleSource::Library(library) => match LIBRARIES.borrow().get(&*library.as_str()) {
Some(code) => {
if self.wam_prelude.indices.modules.contains_key(&library) {
return self.import_qualified_module(library, exports);

View File

@@ -1076,7 +1076,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
let export_list = machine_st.read_term_from_heap(cell)?;
let atom_tbl = &mut LS::machine_st(&mut self.payload).atom_tbl;
let export_list = setup_module_export_list(export_list, atom_tbl)?;
let export_list = setup_module_export_list(export_list, &mut atom_tbl.blocking_write())?;
Ok(export_list.into_iter().collect())
}
@@ -1420,7 +1420,7 @@ impl MachineState {
term_stack.push(Term::PartialString(Cell::default(), string, tail));
}
Ok((string, None)) => {
let atom = self.atom_tbl.build_with(&string);
let atom = self.atom_tbl.blocking_write().build_with(&string);
term_stack.push(Term::CompleteString(Cell::default(), atom));
}
Err(cons_term) => term_stack.push(cons_term),
@@ -1855,7 +1855,7 @@ impl Machine {
.store(self.machine_st.deref(self.machine_st.registers[2])));
self.load_contexts
.push(LoadContext::new(path.as_str(), stream));
.push(LoadContext::new(&*path.as_str(), stream));
Ok(())
}
@@ -1917,7 +1917,11 @@ impl Machine {
pub(crate) fn load_context_source(&mut self) {
if let Some(load_context) = self.load_contexts.last() {
let path_str = load_context.path.to_str().unwrap();
let path_atom = self.machine_st.atom_tbl.build_with(path_str);
let path_atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(path_str);
self.machine_st
.unify_atom(path_atom, self.machine_st.registers[1]);
@@ -1931,7 +1935,11 @@ impl Machine {
match load_context.path.file_name() {
Some(file_name) if load_context.path.is_file() => {
let file_name_str = file_name.to_str().unwrap();
let file_name_atom = self.machine_st.atom_tbl.build_with(file_name_str);
let file_name_atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(file_name_str);
self.machine_st
.unify_atom(file_name_atom, self.machine_st.registers[1]);
@@ -1950,7 +1958,11 @@ impl Machine {
if let Some(load_context) = self.load_contexts.last() {
if let Some(directory) = load_context.path.parent() {
let directory_str = directory.to_str().unwrap();
let directory_atom = self.machine_st.atom_tbl.build_with(directory_str);
let directory_atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(directory_str);
self.machine_st
.unify_atom(directory_atom, self.machine_st.registers[1]);

View File

@@ -18,10 +18,12 @@ use crate::types::*;
use crate::parser::dashu::Integer;
use indexmap::IndexMap;
use tokio::sync::RwLock;
use std::convert::TryFrom;
use std::fmt;
use std::ops::{Index, IndexMut};
use std::sync::Arc;
pub(crate) type Registers = [HeapCellValue; MAX_ARITY + 1];
@@ -57,7 +59,7 @@ pub enum OnEOF {
}
pub struct MachineState {
pub atom_tbl: AtomTable,
pub atom_tbl: Arc<RwLock<AtomTable>>,
pub arena: Arena,
pub(super) pdl: Vec<HeapCellValue>,
pub(super) s: HeapPtr,
@@ -529,7 +531,7 @@ impl MachineState {
Some((var_name, var))
}
}),
&mut self.atom_tbl,
&mut self.atom_tbl.blocking_write(),
);
let singleton_addr = self.registers[3];
@@ -615,7 +617,7 @@ impl MachineState {
false
}
}),
&mut self.atom_tbl,
&mut self.atom_tbl.blocking_write(),
);
for var in term_write_result.var_dict.values_mut() {
@@ -657,12 +659,10 @@ impl MachineState {
stream: Stream,
indices: &mut IndexStore,
) -> CallResult {
let atoms_ptr = (&self.atom_tbl.table) as *const indexmap::IndexSet<Atom>;
if let Stream::Readline(ptr) = stream {
unsafe {
let readline = ptr.as_ptr().as_mut().unwrap();
readline.set_atoms_for_completion(atoms_ptr);
readline.set_atoms_for_completion(&self.atom_tbl);
return self.read_term(
stream,
indices,
@@ -776,14 +776,14 @@ impl MachineState {
}
(HeapCellValueTag::Atom, (name, _arity)) => {
debug_assert_eq!(_arity, 0);
var_names.insert(var, VarPtr::from(name.as_str()));
var_names.insert(var, VarPtr::from(&*name.as_str()));
}
(HeapCellValueTag::Str, s) => {
let (name, arity) = cell_as_atom_cell!(self.heap[s])
.get_name_and_arity();
debug_assert_eq!(arity, 0);
var_names.insert(var, VarPtr::from(name.as_str()));
var_names.insert(var, VarPtr::from(&*name.as_str()));
}
_ => {
unreachable!();
@@ -864,7 +864,7 @@ impl MachineState {
let mut printer = HCPrinter::new(
&mut self.heap,
&mut self.atom_tbl,
Arc::clone(&self.atom_tbl),
&mut self.stack,
op_dir,
PrinterOutputter::new(),

View File

@@ -503,7 +503,7 @@ impl MachineState {
} else {
self.pdl.clear();
return Some(
n1.chars().next().cmp(&Some(c2))
n1.as_str().chars().next().cmp(&Some(c2))
.then(Ordering::Greater)
);
}
@@ -533,7 +533,7 @@ impl MachineState {
} else {
self.pdl.clear();
return Some(
Some(c1).cmp(&n2.chars().next())
Some(c1).cmp(&n2.as_str().chars().next())
.then(Ordering::Less)
);
}
@@ -556,7 +556,7 @@ impl MachineState {
} else {
self.pdl.clear();
return Some(
Some(c1).cmp(&n2.chars().next())
Some(c1).cmp(&n2.as_str().chars().next())
.then(Ordering::Less)
);
}
@@ -586,7 +586,7 @@ impl MachineState {
} else {
self.pdl.clear();
return Some(
n1.chars().next().cmp(&Some(c2))
n1.as_str().chars().next().cmp(&Some(c2))
.then(Ordering::Greater)
);
}
@@ -896,7 +896,7 @@ impl MachineState {
let s = string.as_str();
match heap_pstr_iter.compare_pstr_to_string(s) {
match heap_pstr_iter.compare_pstr_to_string(&*s) {
Some(PStrPrefixCmpResult {
focus,
offset,
@@ -1003,9 +1003,9 @@ impl MachineState {
self.s_offset = 0;
self.mode = MachineMode::Read;
put_partial_string(&mut self.heap, pstr, &mut self.atom_tbl)
put_partial_string(&mut self.heap, pstr, &mut self.atom_tbl.blocking_write())
} else {
put_complete_string(&mut self.heap, pstr, &mut self.atom_tbl)
put_complete_string(&mut self.heap, pstr, &mut self.atom_tbl.blocking_write())
}
}
@@ -1097,7 +1097,7 @@ impl MachineState {
(name, 0, 0)
}
(HeapCellValueTag::Char, c) => {
(self.atom_tbl.build_with(&c.to_string()), 0, 0)
(self.atom_tbl.blocking_write().build_with(&c.to_string()), 0, 0)
}
(HeapCellValueTag::Var | HeapCellValueTag::AttrVar | HeapCellValueTag::StackVar) => {
let stub = functor_stub(atom!("call"), arity + 1);
@@ -1436,7 +1436,7 @@ impl MachineState {
}
}
(HeapCellValueTag::Char, c) => {
let c = self.atom_tbl.build_with(&c.to_string());
let c = self.atom_tbl.blocking_write().build_with(&c.to_string());
self.try_functor_fabricate_struct(
c,

View File

@@ -11,6 +11,8 @@ pub use crate::parser::ast::*;
use crate::read::*;
pub use crate::types::*;
use std::sync::Arc;
#[cfg(test)]
use crate::machine::copier::CopierTarget;
@@ -61,7 +63,7 @@ impl MockWAM {
let mut printer = HCPrinter::new(
&mut self.machine_st.heap,
&mut self.machine_st.atom_tbl,
Arc::clone(&self.machine_st.atom_tbl),
&mut self.machine_st.stack,
&self.op_dir,
PrinterOutputter::new(),

View File

@@ -230,7 +230,8 @@ impl Machine {
pub fn load_file(&mut self, path: &str, stream: Stream) {
self.machine_st.registers[1] = stream_as_cell!(stream);
self.machine_st.registers[2] = atom_as_cell!(self.machine_st.atom_tbl.build_with(path));
self.machine_st.registers[2] =
atom_as_cell!(self.machine_st.atom_tbl.blocking_write().build_with(path));
self.run_module_predicate(atom!("loader"), (atom!("file_load"), 2));
}
@@ -295,7 +296,7 @@ impl Machine {
arg_pstrs.push(put_complete_string(
&mut self.machine_st.heap,
&arg,
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
));
}

View File

@@ -55,8 +55,8 @@ impl PartialString {
}
#[inline(always)]
pub(crate) fn as_str_from(&self, n: usize) -> &str {
&self.0.as_str()[n..]
pub(crate) fn as_str_from(&self, n: usize) -> AtomString {
self.0.as_str().map(|str| &str[n..])
}
}
@@ -155,7 +155,7 @@ impl<'a> HeapPStrIter<'a> {
let s = &s[result.prefix_len..];
if s.len() >= t.len() {
if s.starts_with(t) {
if (&*s).starts_with(&*t) {
result.prefix_len += t.len();
result.offset += t.len();
} else {
@@ -229,7 +229,7 @@ impl<'a> HeapPStrIter<'a> {
}
PStrIteratee::PStrSegment(_, pstr_atom, n) => {
let pstr = PartialString::from(pstr_atom);
buf += pstr.as_str_from(n);
buf += &*pstr.as_str_from(n);
}
}
}
@@ -714,7 +714,7 @@ pub fn compare_pstr_prefixes<'a>(
let str2 = pstr2.as_str_from(n2);
match str1.len().cmp(&str2.len()) {
Ordering::Equal if str1 == str2 => {
Ordering::Equal if &*str1 == &*str2 => {
cycle_detection_step(i1, i2, &step_1);
let both_cyclic = cycle_detection_step(i2, i1, &step_2);
@@ -725,7 +725,7 @@ pub fn compare_pstr_prefixes<'a>(
continue;
}
}
Ordering::Less if str2.starts_with(str1) => {
Ordering::Less if str2.starts_with(&*str1) => {
step_2.iteratee =
PStrIteratee::PStrSegment(f2, pstr2_atom, n2 + str1.len());
let c1_result = cycle_detection_step(i1, i2, &step_1);
@@ -735,7 +735,7 @@ pub fn compare_pstr_prefixes<'a>(
continue;
}
}
Ordering::Greater if str1.starts_with(str2) => {
Ordering::Greater if str1.starts_with(&*str2) => {
step_1.iteratee =
PStrIteratee::PStrSegment(f1, pstr1_atom, n1 + str2.len());
let c2_result = cycle_detection_step(i2, i1, &step_2);
@@ -746,7 +746,7 @@ pub fn compare_pstr_prefixes<'a>(
}
}
_ => {
return PStrCmpResult::Ordered(str1.cmp(str2));
return PStrCmpResult::Ordered(str1.cmp(&*str2));
}
}
}
@@ -808,7 +808,7 @@ mod test {
let pstr_var_cell = put_partial_string(
&mut wam.machine_st.heap,
"abc ",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
let pstr_cell = wam.machine_st.heap[pstr_var_cell.get_value() as usize];
@@ -831,7 +831,7 @@ mod test {
let pstr_second_var_cell = put_partial_string(
&mut wam.machine_st.heap,
"def",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
let pstr_second_cell = wam.machine_st.heap[pstr_second_var_cell.get_value() as usize];
@@ -913,13 +913,21 @@ mod test {
// construct a structurally similar but different cyclic partial string
// matching the one beginning at wam.machine_st.heap[0].
put_partial_string(&mut wam.machine_st.heap, "ab", &mut wam.machine_st.atom_tbl);
put_partial_string(
&mut wam.machine_st.heap,
"ab",
&mut wam.machine_st.atom_tbl.blocking_write(),
);
wam.machine_st.heap.pop();
wam.machine_st.heap.push(pstr_loc_as_cell!(second_h + 2));
put_partial_string(&mut wam.machine_st.heap, "c ", &mut wam.machine_st.atom_tbl);
put_partial_string(
&mut wam.machine_st.heap,
"c ",
&mut wam.machine_st.atom_tbl.blocking_write(),
);
wam.machine_st.heap.pop();
@@ -947,7 +955,7 @@ mod test {
put_partial_string(
&mut wam.machine_st.heap,
"abc ",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
let pstr_cell = wam.machine_st.heap[0];
@@ -981,7 +989,7 @@ mod test {
let cstr_var_cell = put_complete_string(
&mut wam.machine_st.heap,
"abc",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
wam.machine_st.heap.push(list_loc_as_cell!(2));
@@ -1010,7 +1018,7 @@ mod test {
let cstr_var_cell = put_complete_string(
&mut wam.machine_st.heap,
"abc",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
wam.machine_st.heap.push(list_loc_as_cell!(2));
@@ -1040,8 +1048,11 @@ mod test {
wam.machine_st.heap.clear();
let cstr_var_cell =
put_complete_string(&mut wam.machine_st.heap, "d", &mut wam.machine_st.atom_tbl);
let cstr_var_cell = put_complete_string(
&mut wam.machine_st.heap,
"d",
&mut wam.machine_st.atom_tbl.blocking_write(),
);
wam.machine_st.heap.push(list_loc_as_cell!(2));
wam.machine_st.heap.push(char_as_cell!('d'));
@@ -1058,7 +1069,7 @@ mod test {
let cstr_var_cell = put_complete_string(
&mut wam.machine_st.heap,
"abc",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
wam.machine_st.heap.push(list_loc_as_cell!(2));
@@ -1089,7 +1100,7 @@ mod test {
put_complete_string(
&mut wam.machine_st.heap,
"abcdef",
&mut wam.machine_st.atom_tbl,
&mut wam.machine_st.atom_tbl.blocking_write(),
);
wam.machine_st.heap.push(pstr_as_cell!(atom!("abc")));

View File

@@ -313,11 +313,17 @@ pub(super) fn setup_declaration<'a, LS: LoadState<'a>>(
}
(atom!("module"), 2) => {
let atom_tbl = &mut LS::machine_st(&mut loader.payload).atom_tbl;
Ok(Declaration::Module(setup_module_decl(terms, atom_tbl)?))
Ok(Declaration::Module(setup_module_decl(
terms,
&mut atom_tbl.blocking_write(),
)?))
}
(atom!("op"), 3) => {
let atom_tbl = &mut LS::machine_st(&mut loader.payload).atom_tbl;
Ok(Declaration::Op(setup_op_decl(terms, atom_tbl)?))
Ok(Declaration::Op(setup_op_decl(
terms,
&mut atom_tbl.blocking_write(),
)?))
}
(atom!("non_counted_backtracking"), 1) => {
let (name, arity) = setup_predicate_indicator(&mut terms.pop().unwrap())?;
@@ -326,7 +332,8 @@ pub(super) fn setup_declaration<'a, LS: LoadState<'a>>(
(atom!("use_module"), 1) => Ok(Declaration::UseModule(setup_use_module_decl(terms)?)),
(atom!("use_module"), 2) => {
let atom_tbl = &mut LS::machine_st(&mut loader.payload).atom_tbl;
let (name, exports) = setup_qualified_import(terms, atom_tbl)?;
let (name, exports) =
setup_qualified_import(terms, &mut atom_tbl.blocking_write())?;
Ok(Declaration::UseQualifiedModule(name, exports))
}

View File

@@ -1853,7 +1853,7 @@ impl MachineState {
}
};
let file = match open_options.open(file_spec.as_str()) {
let file = match open_options.open(&*file_spec.as_str()) {
Ok(file) => file,
Err(err) => {
match err.kind() {

View File

@@ -1529,7 +1529,7 @@ impl Machine {
}
}
(HeapCellValueTag::Char, c) => {
let name = self.machine_st.atom_tbl.build_with(&c.to_string());
let name = self.machine_st.atom_tbl.blocking_write().build_with(&c.to_string());
let h = self.machine_st.heap.len();
self.machine_st.heap.push(atom_as_cell!(name));
@@ -1804,7 +1804,7 @@ impl Machine {
match hostname::get().ok() {
Some(host) => match host.to_str() {
Some(host) => {
let hostname = self.machine_st.atom_tbl.build_with(host);
let hostname = self.machine_st.atom_tbl.blocking_write().build_with(host);
let a1 = self.deref_register(1);
self.machine_st.unify_atom(hostname, a1);
@@ -1895,14 +1895,15 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
let path = std::path::Path::new(dir.as_str());
let str = dir.as_str();
let path = std::path::Path::new(&*str);
let mut files = Vec::new();
if let Ok(entries) = fs::read_dir(path) {
for entry in entries {
if let Ok(entry) = entry {
if let Some(name) = entry.file_name().to_str() {
let name = self.machine_st.atom_tbl.build_with(name);
let name = self.machine_st.atom_tbl.blocking_write().build_with(name);
files.push(atom_as_cstr_cell!(name));
continue;
@@ -1937,7 +1938,7 @@ impl Machine {
.value_to_str_like(self.machine_st.registers[1])
{
let len = Number::arena_from(
fs::metadata(file.as_str()).unwrap().len(),
fs::metadata(&*file.as_str()).unwrap().len(),
&mut self.machine_st.arena,
);
@@ -1963,8 +1964,8 @@ impl Machine {
{
let file_str = file.as_str();
if !std::path::Path::new(file_str).exists()
|| !fs::metadata(file_str).unwrap().is_file()
if !std::path::Path::new(&*file_str).exists()
|| !fs::metadata(&*file_str).unwrap().is_file()
{
self.machine_st.fail = true;
}
@@ -1981,7 +1982,9 @@ impl Machine {
{
let dir_str = dir.as_str();
if !std::path::Path::new(dir_str).exists() || !fs::metadata(dir_str).unwrap().is_dir() {
if !std::path::Path::new(&*dir_str).exists()
|| !fs::metadata(&*dir_str).unwrap().is_dir()
{
self.machine_st.fail = true;
}
} else {
@@ -1997,7 +2000,7 @@ impl Machine {
{
let which = cell_as_atom!(self.deref_register(2));
if let Ok(md) = fs::metadata(file.as_str()) {
if let Ok(md) = fs::metadata(&*file.as_str()) {
if let Ok(time) = match which {
atom!("modification") => md.modified(),
atom!("access") => md.accessed(),
@@ -2031,7 +2034,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
match fs::create_dir(dir.as_str()) {
match fs::create_dir(&*dir.as_str()) {
Ok(_) => {}
_ => {
self.machine_st.fail = true;
@@ -2048,7 +2051,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
match fs::create_dir_all(dir.as_str()) {
match fs::create_dir_all(&*dir.as_str()) {
Ok(_) => {}
_ => {
self.machine_st.fail = true;
@@ -2065,7 +2068,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
match fs::remove_file(file.as_str()) {
match fs::remove_file(&*file.as_str()) {
Ok(_) => {}
_ => {
self.machine_st.fail = true;
@@ -2084,7 +2087,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[2])
{
if fs::rename(file.as_str(), renamed.as_str()).is_ok() {
if fs::rename(&*file.as_str(), &*renamed.as_str()).is_ok() {
return;
}
}
@@ -2103,7 +2106,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[2])
{
if fs::copy(file.as_str(), copied.as_str()).is_ok() {
if fs::copy(&*file.as_str(), &*copied.as_str()).is_ok() {
return;
}
}
@@ -2118,7 +2121,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
match fs::remove_dir(dir.as_str()) {
match fs::remove_dir(&*dir.as_str()) {
Ok(_) => {}
_ => {
self.machine_st.fail = true;
@@ -2141,7 +2144,11 @@ impl Machine {
}
};
let current_atom = self.machine_st.atom_tbl.build_with(&current);
let current_atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&current);
let a1 = self.deref_register(1);
self.machine_st.unify_complete_string(current_atom, a1);
@@ -2153,7 +2160,7 @@ impl Machine {
let target = self.deref_register(2);
if let Some(next) = self.machine_st.value_to_str_like(target) {
if env::set_current_dir(std::path::Path::new(next.as_str())).is_ok() {
if env::set_current_dir(std::path::Path::new(&*next.as_str())).is_ok() {
return Ok(());
}
}
@@ -2169,7 +2176,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
match fs::canonicalize(path.as_str()) {
match fs::canonicalize(&*path.as_str()) {
Ok(canonical) => {
let cs = match canonical.to_str() {
Some(s) => s,
@@ -2182,7 +2189,7 @@ impl Machine {
}
};
let canonical_atom = self.machine_st.atom_tbl.build_with(cs);
let canonical_atom = self.machine_st.atom_tbl.blocking_write().build_with(cs);
let a2 = self.deref_register(2);
self.machine_st.unify_complete_string(canonical_atom, a2);
@@ -2241,13 +2248,13 @@ impl Machine {
let atom_cell = match str_like {
AtomOrString::Atom(atom) => {
atom_as_cell!(if atom == atom!("[]") {
self.machine_st.atom_tbl.build_with("")
self.machine_st.atom_tbl.blocking_write().build_with("")
} else {
atom
})
}
AtomOrString::String(string) => {
atom_as_cell!(self.machine_st.atom_tbl.build_with(&string))
atom_as_cell!(self.machine_st.atom_tbl.blocking_write().build_with(&string))
}
};
@@ -2278,6 +2285,7 @@ impl Machine {
}
(HeapCellValueTag::Atom, (name, arity)) => {
if arity == 0 {
let name = name.as_str();
let iter = name.chars()
.map(|c| fixnum_as_cell!(Fixnum::build_with(c as i64)));
@@ -2292,6 +2300,7 @@ impl Machine {
.get_name_and_arity();
if arity == 0 {
let name = name.as_str();
let iter = name.chars()
.map(|c| fixnum_as_cell!(Fixnum::build_with(c as i64)));
@@ -2307,7 +2316,7 @@ impl Machine {
match self.machine_st.try_from_list(self.machine_st.registers[2], stub_gen) {
Ok(addrs) => {
let string = self.machine_st.codes_to_string(addrs.into_iter(), stub_gen)?;
let atom = self.machine_st.atom_tbl.build_with(&string);
let atom = self.machine_st.atom_tbl.blocking_write().build_with(&string);
self.machine_st.bind(a1.as_var().unwrap(), atom_as_cell!(atom));
}
@@ -2334,7 +2343,7 @@ impl Machine {
.get_name_and_arity();
if arity == 0 {
name.chars().count() as i64
name.as_str().chars().count() as i64
} else {
self.machine_st.fail = true;
return;
@@ -2342,7 +2351,7 @@ impl Machine {
}
(HeapCellValueTag::Atom, (name, arity)) => {
if arity == 0 {
name.chars().count() as i64
name.as_str().chars().count() as i64
} else {
self.machine_st.fail = true;
return;
@@ -2392,7 +2401,7 @@ impl Machine {
let atom_or_string = self.machine_st.value_to_str_like(a1).unwrap();
self.machine_st
.parse_number_from_string(atom_or_string.as_str(), &self.indices, stub_gen)
.parse_number_from_string(&*atom_or_string.as_str(), &self.indices, stub_gen)
}
#[inline(always)]
@@ -2801,7 +2810,11 @@ impl Machine {
}
};
let chars_atom = self.machine_st.atom_tbl.build_with(&string.trim());
let chars_atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&string.trim());
self.machine_st.unify_complete_string(chars_atom, chs);
}
@@ -3027,14 +3040,14 @@ impl Machine {
match (name, arity) {
(atom!("to_upper"), 1) => {
let reg = self.machine_st.deref(self.machine_st.heap[s+1]);
let atom = self.machine_st.atom_tbl.build_with(&c.to_uppercase().to_string());
let atom = self.machine_st.atom_tbl.blocking_write().build_with(&c.to_uppercase().to_string());
let upper_str = string_as_cstr_cell!(atom);
unify!(self.machine_st, reg, upper_str);
self.machine_st.fail = false;
}
(atom!("to_lower"), 1) => {
let reg = self.machine_st.deref(self.machine_st.heap[s+1]);
let atom = self.machine_st.atom_tbl.build_with(&c.to_lowercase().to_string());
let atom = self.machine_st.atom_tbl.blocking_write().build_with(&c.to_lowercase().to_string());
let lower_str = string_as_cstr_cell!(atom);
unify!(self.machine_st, reg, lower_str);
self.machine_st.fail = false;
@@ -3557,7 +3570,11 @@ impl Machine {
};
let output = self.deref_register(3);
let atom = self.machine_st.atom_tbl.build_with(&string);
let atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&string);
self.machine_st.unify_complete_string(atom, output);
Ok(())
@@ -4052,7 +4069,7 @@ impl Machine {
cell_as_atom!(self.machine_st.heap[s])
}
(HeapCellValueTag::Char, c) => {
self.machine_st.atom_tbl.build_with(&c.to_string())
self.machine_st.atom_tbl.blocking_write().build_with(&c.to_string())
}
_ => {
unreachable!()
@@ -4374,7 +4391,7 @@ impl Machine {
(HeapCellValueTag::Str, s) => {
let name = cell_as_atom_cell!(self.machine_st.heap[s]).get_name();
let value = self.machine_st.value_to_str_like(self.machine_st.heap[s + 1]).unwrap();
header_map.insert(HeaderName::from_str(name.as_str()).unwrap(), HeaderValue::from_str(value.as_str()).unwrap());
header_map.insert(HeaderName::from_str(&*name.as_str()).unwrap(), HeaderValue::from_str(&*value.as_str()).unwrap());
}
_ => {
unreachable!()
@@ -4414,10 +4431,14 @@ impl Machine {
let h = self.machine_st.heap.len();
let header_term = functor!(
self.machine_st.atom_tbl.build_with(header_name.as_str()),
self.machine_st
.atom_tbl
.blocking_write()
.build_with(header_name.as_str()),
[cell(string_as_cstr_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(header_value.to_str().unwrap())))]
);
@@ -4437,7 +4458,10 @@ impl Machine {
let reader = resp.bytes().unwrap().reader();
let mut stream = Stream::from_http_stream(
self.machine_st.atom_tbl.build_with(&address_string),
self.machine_st
.atom_tbl
.blocking_write()
.build_with(&address_string),
Box::new(reader),
&mut self.machine_st.arena,
);
@@ -4554,14 +4578,14 @@ impl Machine {
Method::HEAD => atom!("head"),
_ => unreachable!(),
};
let path_atom = self.machine_st.atom_tbl.build_with(request.request.uri().path());
let path_atom = self.machine_st.atom_tbl.blocking_write().build_with(request.request.uri().path());
let path_cell = atom_as_cstr_cell!(path_atom);
let headers: Vec<HeapCellValue> = request.request.headers().iter().map(|(header_name, header_value)| {
let h = self.machine_st.heap.len();
let header_term = functor!(
self.machine_st.atom_tbl.build_with(header_name.as_str()),
[cell(string_as_cstr_cell!(self.machine_st.atom_tbl.build_with(header_value.to_str().unwrap())))]
self.machine_st.atom_tbl.blocking_write().build_with(header_name.as_str()),
[cell(string_as_cstr_cell!(self.machine_st.atom_tbl.blocking_write().build_with(header_value.to_str().unwrap())))]
);
self.machine_st.heap.extend(header_term.into_iter());
@@ -4571,7 +4595,7 @@ impl Machine {
let headers_list = iter_to_heap_list(&mut self.machine_st.heap, headers.into_iter());
let query_str = request.request.uri().query().unwrap_or("");
let query_atom = self.machine_st.atom_tbl.build_with(query_str);
let query_atom = self.machine_st.atom_tbl.blocking_write().build_with(query_str);
let query_cell = string_as_cstr_cell!(query_atom);
let hyper_req = request.request;
@@ -4642,7 +4666,7 @@ impl Machine {
(HeapCellValueTag::Str, s) => {
let name = cell_as_atom_cell!(self.machine_st.heap[s]).get_name();
let value = self.machine_st.value_to_str_like(self.machine_st.heap[s + 1]).unwrap();
header_map.insert(HeaderName::from_str(name.as_str()).unwrap(), HeaderValue::from_str(value.as_str()).unwrap());
header_map.insert(HeaderName::from_str(&*name.as_str()).unwrap(), HeaderValue::from_str(&*value.as_str()).unwrap());
}
_ => {
unreachable!()
@@ -4722,7 +4746,7 @@ impl Machine {
}
if let Ok(_) = self
.foreign_function_table
.load_library(library_name.as_str(), &functions)
.load_library(&*library_name.as_str(), &functions)
{
return Ok(());
}
@@ -4752,7 +4776,7 @@ impl Machine {
_ => {
let stub_gen = || functor_stub(atom!("foreign_call"), 3);
if let Some(string) = machine_st.value_to_str_like(source) {
Value::CString(CString::new(string.as_str()).unwrap())
Value::CString(CString::new(&*string.as_str()).unwrap())
} else {
match machine_st.try_from_list(source, stub_gen) {
Ok(args) => {
@@ -4785,7 +4809,7 @@ impl Machine {
.collect();
match self
.foreign_function_table
.exec(function_name.as_str(), args)
.exec(&*function_name.as_str(), args)
{
Ok(result) => {
match result {
@@ -4801,8 +4825,11 @@ impl Machine {
unify!(self.machine_st, return_value, struct_value);
}
Value::CString(cstr) => {
let cstr =
self.machine_st.atom_tbl.build_with(cstr.to_str().unwrap());
let cstr = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(cstr.to_str().unwrap());
self.machine_st.unify_complete_string(cstr, return_value);
}
}
@@ -4834,6 +4861,7 @@ impl Machine {
Value::CString(cstr) => atom_as_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&cstr.into_string().unwrap())),
Value::Struct(name, struct_args) => self.build_struct(&name, struct_args),
})
@@ -4863,7 +4891,7 @@ impl Machine {
Err(e) => return Err(e),
};
self.foreign_function_table
.define_struct(struct_name.as_str(), fields);
.define_struct(&*struct_name.as_str(), fields);
return Ok(());
}
self.machine_st.fail = true;
@@ -4890,7 +4918,7 @@ impl Machine {
let src_sink = self.deref_register(1);
if let Some(file_spec) = self.machine_st.value_to_str_like(src_sink) {
let file_spec = file_spec.as_atom(&mut self.machine_st.atom_tbl);
let file_spec = file_spec.as_atom(&mut self.machine_st.atom_tbl.blocking_write());
let mut stream =
self.machine_st
@@ -4933,7 +4961,7 @@ impl Machine {
let op = read_heap_cell!(self.deref_register(3),
(HeapCellValueTag::Char, c) => {
self.machine_st.atom_tbl.build_with(&c.to_string())
self.machine_st.atom_tbl.blocking_write().build_with(&c.to_string())
}
(HeapCellValueTag::Atom, (name, _arity)) => {
name
@@ -6103,7 +6131,7 @@ impl Machine {
write_term_to_heap(
&term,
&mut self.machine_st.heap,
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
)
});
@@ -6268,7 +6296,7 @@ impl Machine {
name
}
_ => {
self.machine_st.atom_tbl.build_with(&match Number::try_from(port) {
self.machine_st.atom_tbl.blocking_write().build_with(&match Number::try_from(port) {
Ok(Number::Fixnum(n)) => n.get_num().to_string(),
Ok(Number::Integer(n)) => n.to_string(),
_ => {
@@ -6282,7 +6310,10 @@ impl Machine {
atom!("127.0.0.1:80")
} else {
let buffer = format!("{}:{}", socket_atom.as_str(), port.as_str());
self.machine_st.atom_tbl.build_with(&buffer)
self.machine_st
.atom_tbl
.blocking_write()
.build_with(&buffer)
};
let alias = self.machine_st.registers[4];
@@ -6310,7 +6341,7 @@ impl Machine {
}
}
let stream = match TcpStream::connect(socket_addr.as_str()).map_err(|e| e.kind()) {
let stream = match TcpStream::connect(&*socket_addr.as_str()).map_err(|e| e.kind()) {
Ok(tcp_stream) => {
let mut stream =
Stream::from_tcp_stream(socket_addr, tcp_stream, &mut self.machine_st.arena);
@@ -6464,7 +6495,7 @@ impl Machine {
(ArenaHeaderTag::TcpListener, tcp_listener) => {
match tcp_listener.accept().ok() {
Some((tcp_stream, socket_addr)) => {
let client = self.machine_st.atom_tbl.build_with(&socket_addr.to_string());
let client = self.machine_st.atom_tbl.blocking_write().build_with(&socket_addr.to_string());
let mut tcp_stream = Stream::from_tcp_stream(
client,
@@ -6520,7 +6551,7 @@ impl Machine {
)?;
let connector = TlsConnector::new().unwrap();
let stream = match connector.connect(hostname.as_str(), stream0) {
let stream = match connector.connect(&*hostname.as_str(), stream0) {
Ok(tls_stream) => tls_stream,
Err(_) => {
return Err(self.machine_st.open_permission_error(
@@ -6555,7 +6586,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[2])
{
let identity = match Identity::from_pkcs12(&pkcs12, password.as_str()) {
let identity = match Identity::from_pkcs12(&pkcs12, &*password.as_str()) {
Ok(identity) => identity,
Err(_) => {
return Err(self.machine_st.open_permission_error(
@@ -7088,7 +7119,7 @@ impl Machine {
let chars = put_complete_string(
&mut self.machine_st.heap,
&result,
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
);
let result_addr = self.deref_register(1);
@@ -7107,7 +7138,7 @@ impl Machine {
use git_version::git_version;
let buffer = git_version!(cargo_prefix = "cargo:", fallback = "unknown");
let buffer_atom = self.machine_st.atom_tbl.build_with(buffer);
let buffer_atom = self.machine_st.atom_tbl.blocking_write().build_with(buffer);
let a1 = self.deref_register(1);
self.machine_st.unify_complete_string(buffer_atom, a1);
@@ -7472,7 +7503,11 @@ impl Machine {
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&buffer))
atom_as_cstr_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&buffer))
}
};
@@ -7609,7 +7644,11 @@ impl Machine {
if let Some(string) = self.machine_st.value_to_str_like(addr) {
for c in string.as_str().chars() {
if c as u32 > 255 {
let non_octet = self.machine_st.atom_tbl.build_with(&c.to_string());
let non_octet = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&c.to_string());
self.machine_st
.unify_atom(non_octet, self.machine_st.registers[2]);
return;
@@ -7641,7 +7680,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
match roxmltree::Document::parse(string.as_str()) {
match roxmltree::Document::parse(&*string.as_str()) {
Ok(doc) => {
let result = self.xml_node_to_term(doc.root_element());
unify!(self.machine_st, self.machine_st.registers[2], result);
@@ -7661,12 +7700,12 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
{
match env::var(key.as_str()) {
match env::var(&*key.as_str()) {
Ok(value) => {
let cstr = put_complete_string(
&mut self.machine_st.heap,
&value,
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
);
unify!(self.machine_st, self.machine_st.registers[2], cstr);
@@ -7691,7 +7730,7 @@ impl Machine {
.value_to_str_like(self.machine_st.registers[2])
.unwrap();
env::set_var(key.as_str(), value.as_str());
env::set_var(&*key.as_str(), &*value.as_str());
}
#[inline(always)]
@@ -7700,7 +7739,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[1])
.unwrap();
env::remove_var(key.as_str());
env::remove_var(&*key.as_str());
}
#[inline(always)]
@@ -7756,7 +7795,7 @@ impl Machine {
Ok(value) => {
let command = process::Command::new(&value)
.arg("-c")
.arg(command.as_str())
.arg(&*command.as_str())
.status();
command_result(&mut self.machine_st, command);
}
@@ -7764,7 +7803,7 @@ impl Machine {
Ok(value) => {
let command = process::Command::new(&value)
.arg("/C")
.arg(command.as_str())
.arg(&*command.as_str())
.status();
command_result(&mut self.machine_st, command);
}
@@ -7799,7 +7838,7 @@ impl Machine {
.machine_st
.value_to_str_like(self.machine_st.registers[2])
.unwrap();
let bytes = base64::decode_config(b64.as_str(), config);
let bytes = base64::decode_config(&*b64.as_str(), config);
match bytes {
Ok(bs) => {
@@ -7839,7 +7878,7 @@ impl Machine {
use crate::machine::LIBRARIES;
match LIBRARIES.borrow().get(library_name.as_str()) {
match LIBRARIES.borrow().get(&*library_name.as_str()) {
Some(library) => {
let lib_stream = Stream::from_static_string(library, &mut self.machine_st.arena);
unify!(
@@ -7851,10 +7890,14 @@ impl Machine {
let mut path_buf = machine::current_dir();
path_buf.push("/lib");
path_buf.push(library_name.as_str());
path_buf.push(&*library_name.as_str());
let library_path_str = path_buf.to_str().unwrap();
let library_path = self.machine_st.atom_tbl.build_with(library_path_str);
let library_path = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(library_path_str);
self.machine_st
.unify_atom(library_path, self.machine_st.registers[3]);
@@ -7954,7 +7997,7 @@ impl Machine {
let path_string = put_complete_string(
&mut self.machine_st.heap,
path,
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
);
unify!(self.machine_st, self.machine_st.registers[1], path_string);
@@ -8001,7 +8044,7 @@ impl Machine {
fstr.push_str("finis].");
let s = datetime.format(&fstr).to_string();
self.machine_st.atom_tbl.build_with(&s)
self.machine_st.atom_tbl.blocking_write().build_with(&s)
}
pub(super) fn string_encoding_bytes(
@@ -8025,17 +8068,21 @@ impl Machine {
put_complete_string(
&mut self.machine_st.heap,
node.text().unwrap(),
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
)
} else {
let mut avec = Vec::new();
for attr in node.attributes() {
let name = self.machine_st.atom_tbl.build_with(attr.name());
let name = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(attr.name());
let value = put_complete_string(
&mut self.machine_st.heap,
&attr.value(),
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
);
avec.push(str_loc_as_cell!(self.machine_st.heap.len()));
@@ -8061,7 +8108,11 @@ impl Machine {
cvec.into_iter()
));
let tag = self.machine_st.atom_tbl.build_with(node.tag_name().name());
let tag = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(node.tag_name().name());
let result = str_loc_as_cell!(self.machine_st.heap.len());
@@ -8081,17 +8132,17 @@ impl Machine {
None => put_complete_string(
&mut self.machine_st.heap,
&node.text(),
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
),
Some(name) => {
let mut avec = Vec::new();
for attr in node.attrs() {
let name = self.machine_st.atom_tbl.build_with(attr.0);
let name = self.machine_st.atom_tbl.blocking_write().build_with(attr.0);
let value = put_complete_string(
&mut self.machine_st.heap,
&attr.1,
&mut self.machine_st.atom_tbl,
&mut self.machine_st.atom_tbl.blocking_write(),
);
avec.push(str_loc_as_cell!(self.machine_st.heap.len()));
@@ -8117,7 +8168,7 @@ impl Machine {
cvec.into_iter()
));
let tag = self.machine_st.atom_tbl.build_with(name);
let tag = self.machine_st.atom_tbl.blocking_write().build_with(name);
let result = str_loc_as_cell!(self.machine_st.heap.len());
self.machine_st
@@ -8138,7 +8189,11 @@ impl Machine {
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&buffer))
atom_as_cstr_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&buffer))
}
}
}