From dbb7092e34fed48548fc4ede66b06f58465a7b4a Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 17 Oct 2024 14:16:19 -0700 Subject: [PATCH] wip: associated values store --- src/runner/instance.rs | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index a2e3023..ffc29ff 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -482,3 +482,58 @@ fn find_virtual_property( //Transform Source property with provided function (virtual_property.pointer)(variant) } + +// lazy-loaded per-instance userdata values +// This whole thing is a bad idea and a garbage collection nightmare. +// TODO: recreate rbx_dom_weak with my own instance type that owns this data. +type CreateUserData=fn(&mlua::Lua)->mlua::Result; +type LUD=phf::Map<&'static str,// Class name + phf::Map<&'static str,// Value name + CreateUserData + > +>; +static LAZY_USER_DATA:LUD=phf::phf_map!{ + "RunService"=>phf::phf_map!{ + "RenderStepped"=>|lua|{ + lua.create_any_userdata(super::script_signal::ScriptSignal::new()) + }, + }, +}; +struct InstanceValueStore{ + values:HashMap + >, +} +struct InstanceValues<'a>{ + named_values:&'static phf::Map<&'static str,CreateUserData>, + values:&'a mut HashMap<&'static str,mlua::AnyUserData>, +} +impl InstanceValueStore{ + fn get_or_create_instance_values(&mut self,instance:&rbx_dom_weak::Instance)->Option{ + LAZY_USER_DATA.get(instance.class.as_str()) + .map(|named_values| + InstanceValues{ + named_values, + values:self.values.entry(instance.referent()) + .or_insert_with(||HashMap::new()), + } + ) + } +} +impl InstanceValues<'_>{ + fn get_or_create_value(&mut self,lua:&mlua::Lua,index:&str)->mlua::Result>{ + Ok(match self.named_values.get_entry(index){ + Some((&static_index_str,&function_pointer))=>Some( + match self.values.entry(static_index_str){ + Entry::Occupied(entry)=>entry.get().clone(), + Entry::Vacant(entry)=>entry.insert( + function_pointer(lua)? + ).clone(), + } + ), + None=>None, + }) + } +}