Updating table.rs and vm.rs for changes in Deimos. In progress.

This commit is contained in:
Christian Lincoln 2026-09-28 21:29:15 +01:00
parent f8deddcf2c
commit 662213fb94
2 changed files with 30 additions and 25 deletions

View file

@ -1,6 +1,6 @@
use deimos::gc::{Allocates, Id, MaybeId, Traverse};
use crate::vm::{Callable, MarsAllocator, RunError, Value}; use crate::vm::{Callable, MarsAllocator, RunError, Value};
use deimos::map::{Equivalent, Hashed, Hashes, TableEntry, Map, Entry}; use deimos::gc::{Allocates, Id, MaybeId, Traverse};
use deimos::map::{DisplacementSet, Entry, Equivalent, Hashed, Hashes, Map, TableEntry};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct KeyValue { struct KeyValue {
@ -21,12 +21,10 @@ impl Hashes for KeyValue {
} }
impl TableEntry for KeyValue { impl TableEntry for KeyValue {
fn new_vacant() -> Self { const VACANT: Self = KeyValue {
KeyValue { index: Value::Nil,
index: Value::Nil, value: Value::Nil,
value: Value::Nil, };
}
}
fn is_vacant(&self) -> bool { fn is_vacant(&self) -> bool {
matches!( matches!(
self, self,
@ -64,7 +62,7 @@ impl Hashes for Value {
} }
} }
Value::Table(table) => table.hashed(), Value::Table(table) => table.hashed(),
Value::Object(object,..) => object.hashed(), Value::Object(object, ..) => object.hashed(),
#[cfg(feature = "vector3")] #[cfg(feature = "vector3")]
Value::Vector(vec) => { Value::Vector(vec) => {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
@ -78,12 +76,18 @@ impl Hashes for Value {
// todo: displaced items don't ever get shuffled closer to their homes unless the table is resized // todo: displaced items don't ever get shuffled closer to their homes unless the table is resized
#[derive(Debug)] #[derive(Debug)]
pub struct Table { pub struct Table {
table: Map<KeyValue>, table: DisplacementSet<KeyValue>,
array: Vec<Value>, array: Vec<Value>,
pub meta: MaybeId<Table>, pub meta: MaybeId<Table>,
} }
impl<C> Traverse<C> for Table where C: MarsAllocator { impl<C> Traverse<C> for Table
where
C: MarsAllocator,
{
fn collect(self) {
todo!()
}
fn traverse(id: &Id<Table>, context: &mut C) { fn traverse(id: &Id<Table>, context: &mut C) {
for i in 0..context.freelist().get(id).table.map.len() { for i in 0..context.freelist().get(id).table.map.len() {
let entry = context.freelist().get(id).table.map[i].entry.clone(); let entry = context.freelist().get(id).table.map[i].entry.clone();
@ -122,7 +126,7 @@ impl Table {
self.array[index as usize - 1] = Value::Nil self.array[index as usize - 1] = Value::Nil
} }
} }
self.table.rem_table(Value::Integer(index)); self.table.remove(Value::Integer(index));
} }
item => { item => {
if (0..self.array.len() + 1).contains(&(index as usize - 1)) { if (0..self.array.len() + 1).contains(&(index as usize - 1)) {
@ -132,7 +136,7 @@ impl Table {
self.array[index as usize - 1] = item; self.array[index as usize - 1] = item;
} }
} else { } else {
self.table.set_table(KeyValue { self.table.set(KeyValue {
index: Value::Integer(index), index: Value::Integer(index),
value: item, value: item,
}) })
@ -144,7 +148,7 @@ impl Table {
Value::Nil => Err(RunError( Value::Nil => Err(RunError(
"Attempt to set new index of table with key: nil".to_string(), "Attempt to set new index of table with key: nil".to_string(),
)), )),
index => Ok(self.table.set_table(KeyValue { index, value })), index => Ok(self.table.set(KeyValue { index, value })),
} }
} }
pub fn get(&mut self, index: Value) -> Result<Value, RunError> { pub fn get(&mut self, index: Value) -> Result<Value, RunError> {
@ -159,11 +163,8 @@ impl Table {
// todo: move this into the Map // todo: move this into the Map
Ok(self Ok(self
.table .table
.get_table(index) .get(index)
.unwrap_or(&KeyValue { .unwrap_or(&KeyValue::VACANT)
index: Value::Nil,
value: Value::Nil,
})
.value .value
.clone()) .clone())
} }
@ -174,7 +175,7 @@ impl Table {
} }
pub fn new() -> Self { pub fn new() -> Self {
Table { Table {
table: Map::new(), table: DisplacementSet::new(),
array: Vec::new(), array: Vec::new(),
meta: MaybeId::NULL, meta: MaybeId::NULL,
} }

View file

@ -182,6 +182,12 @@ const SHORT_STRING_LEN: usize = 40;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct CachedString(Rc<str>, pub(crate) Hashed); pub(crate) struct CachedString(Rc<str>, pub(crate) Hashed);
impl Hashes for CachedString {
fn hashed(&self) -> Hashed {
self.1
}
}
impl PartialEq for CachedString { impl PartialEq for CachedString {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.1 == other.1 && self.0 == other.0 self.1 == other.1 && self.0 == other.0
@ -606,13 +612,11 @@ impl<S> Machine<S> {
pub fn new_string(&mut self, string: &str) -> Result<Value, RunError> { pub fn new_string(&mut self, string: &str) -> Result<Value, RunError> {
// todo: why Result of RunError? // todo: why Result of RunError?
if string.len() < SHORT_STRING_LEN { if string.len() < SHORT_STRING_LEN {
let existing = self.cache.get_table(string); match self.cache.get(string) {
match existing { Some(ref entry) => Ok(Value::CachedString(entry.clone())),
Some(ref entry) => Ok(Value::CachedString(entry.0.clone().unwrap())),
None => { None => {
let cached = CachedString(Rc::from(string), string.hashed()); let cached = CachedString(Rc::from(string), string.hashed());
self.cache self.cache.set(cached.clone());
.set_table(CachedStringEntry(Some(cached.clone())));
Ok(Value::CachedString(cached)) Ok(Value::CachedString(cached))
} }
} }