From bfc418a55e012fe4a7a9468a39dcc4d3cdc54cce Mon Sep 17 00:00:00 2001 From: paladin Date: Thu, 1 Oct 2026 09:11:00 +0100 Subject: [PATCH] More tweaks on Meta. --- src/vm.rs | 70 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/src/vm.rs b/src/vm.rs index 7403b72..7b78abb 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -2,7 +2,7 @@ use crate::parser::{BinaryOp, Piece, UnaryOp, parse}; use deimos::gc::{Allocates, AnyId, FreeList, Id, MaybeId, Traverse}; -use deimos::map::{Equivalent, Hashed, Hashes, Map, Set}; +use deimos::map::{DisplacementSet, Equivalent, Hashed, Hashes, Map, TableEntry}; use std::fmt::{Debug, Display, Formatter}; use std::ops::Deref; use std::rc::Rc; @@ -193,7 +193,7 @@ impl PartialEq for CachedString { self.1 == other.1 && self.0 == other.0 } } -/*#[derive(Clone, Debug)] +#[derive(Clone, Debug)] struct CachedStringEntry(Option); impl Hashes for CachedStringEntry { fn hashed(&self) -> Hashed { @@ -212,11 +212,18 @@ impl Equivalent for CachedStringEntry { None => false, } } -}*/ +} -impl Equivalent<&str> for CachedString { +impl TableEntry for CachedStringEntry { + const VACANT: Self = CachedStringEntry(None); + fn is_vacant(&self) -> bool { + self.0.is_none() + } +} + +impl Equivalent<&str> for CachedStringEntry { fn matches(&self, string: &&str) -> bool { - self.0.as_ref().eq(*string) + self.0.as_ref().unwrap().0.as_ref().eq(*string) } } @@ -365,24 +372,39 @@ macro_rules! read_only { }; } +type MetaFieldSet = fn( + &mut Machine, + id: AnyId, + index: Value, + value: Value, + needle: Needle, +) -> Result; +type MetaFieldGet = fn(&mut Machine, id: AnyId, index: Value, needle: Needle) -> Result<(), RunError>; pub struct MetaField { - set: fn( - &mut Machine, - id: AnyId, - index: Value, - value: Value, - needle: Needle, - ) -> Result, - get: fn(&mut Machine, id: AnyId, index: Value, needle: Needle) -> Result<(), RunError>, + set: MetaFieldSet, + get: MetaFieldGet, } pub struct Meta { pub string: fn(&mut Machine, id: AnyId, needle: Needle) -> Result, pub number: fn(&mut Machine, id: AnyId, needle: Needle) -> Result, + pub get: fn(&mut Machine, id: AnyId, index: Value, needle: Needle) -> Result, + pub set: fn(&mut Machine, id: AnyId, index: Value, value: Value, needle: Needle) -> Result<(), RunError>, + pub traverse: fn(&mut Machine, id: AnyId) -> Result<(), RunError>, pub fields: Map>, - pub constants: - Map, id: AnyId, needle: Needle) -> Result>, - pub traverse: fn(id: AnyId) -> Result<(), RunError>, + pub constants: Map, +} + +impl Default for Meta { + fn default() -> Self { + Meta { + string: |machine, id, needle| Err(RunError::new("Type does not implement to string.")), + number: |machine, id, needle| Err(RunError::new("Type does not implement to number.")), + get: |machine, id, index, needle|, + set: |machine, id, index, value, needle| (), + traverse: |machine, id| (), + } + } } pub type ForeignFunction = fn(&mut Machine, needle: Needle) -> Result<(), RunError>; @@ -396,7 +418,7 @@ pub struct Machine { upvalues: FreeList, natives: FreeList>, metas: FreeList>, - cache: Set, + cache: DisplacementSet, meta_strings: MetaStrings, } @@ -613,10 +635,10 @@ impl Machine { // todo: why Result of RunError? if string.len() < SHORT_STRING_LEN { match self.cache.get(string) { - Some(ref entry) => Ok(Value::CachedString(entry.clone())), + Some(ref entry) => Ok(Value::CachedString(entry.0.as_ref().unwrap().clone())), None => { let cached = CachedString(Rc::from(string), string.hashed()); - self.cache.set(cached.clone()); + self.cache.set(CachedStringEntry(Some(cached.clone()))); Ok(Value::CachedString(cached)) } } @@ -774,9 +796,7 @@ impl Machine { table.set(index, value) } } - Value::Object(id, meta) => { - (self.metas.get(&meta.into()).set)(self, id, index, value, dispatch.needle()) - } + Value::Object(id, meta) => (self.metas.get(&meta.into()).set)(self, id, index, value, dispatch.needle()), Value::Function(_callable) => Err(RunError("Function index unimplemented".to_string())), } } @@ -804,9 +824,7 @@ impl Machine { table.get(index.clone()) } } - Value::Object(mut object, ..) => { - Err(RunError("Object index unimplemented".to_string())) - } + Value::Object(id, meta) => (self.metas.get(&meta.into()).get)(self, id, index, dispatch.needle()), Value::Function(_callable) => Err(RunError("Function index unimplemented".to_string())), } } @@ -1319,7 +1337,7 @@ impl Machine { upvalues: FreeList::new(), natives: FreeList::new(), meta_strings: MetaStrings::new(), - cache: Set::new(), + cache: DisplacementSet::new(), }; machine.init_meta_strings()?; load(&mut machine)?;