Compare commits

..

No commits in common. "d5ea8de1922836636e5ff1ecfb10679409e22320" and "420907a14ac772871b8c193786ac6d703fbcb95c" have entirely different histories.

View File

@ -8,7 +8,7 @@ use super::vector3::Vector3;
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
//class functions store //class functions store
lua.set_app_data(ClassMethodsStore::default()); lua.set_app_data(ClassFunctions::default());
let instance_table=lua.create_table()?; let instance_table=lua.create_table()?;
@ -34,8 +34,8 @@ fn dom_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->m
let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?; let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?;
f(&mut *dom) f(&mut *dom)
} }
fn class_functions_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut ClassMethodsStore)->mlua::Result<T>)->mlua::Result<T>{ fn class_functions_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut ClassFunctions)->mlua::Result<T>)->mlua::Result<T>{
let mut cf=lua.app_data_mut::<ClassMethodsStore>().ok_or(mlua::Error::runtime("ClassFunctions missing"))?; let mut cf=lua.app_data_mut::<ClassFunctions>().ok_or(mlua::Error::runtime("ClassFunctions missing"))?;
f(&mut *cf) f(&mut *cf)
} }
@ -392,14 +392,14 @@ static CLASS_FUNCTION_DATABASE:CFD=phf::phf_map!{
/// A store of created functions for each Roblox class. /// A store of created functions for each Roblox class.
/// Functions are created the first time they are accessed and stored in this data structure. /// Functions are created the first time they are accessed and stored in this data structure.
#[derive(Default)] #[derive(Default)]
struct ClassMethodsStore{ struct ClassFunctions{
classes:HashMap<&'static str,//ClassName classes:HashMap<&'static str,//ClassName
HashMap<&'static str,//Method name HashMap<&'static str,//Method name
mlua::Function mlua::Function
> >
> >
} }
impl ClassMethodsStore{ impl ClassFunctions{
/// return self.classes[class] or create the ClassMethods and then return it /// return self.classes[class] or create the ClassMethods and then return it
fn get_or_create_class_methods(&mut self,class:&str)->Option<ClassMethods>{ fn get_or_create_class_methods(&mut self,class:&str)->Option<ClassMethods>{
// Use get_entry to get the &'static str keys of the database // Use get_entry to get the &'static str keys of the database
@ -482,56 +482,3 @@ fn find_virtual_property(
//Transform Source property with provided function //Transform Source property with provided function
(virtual_property.pointer)(variant) (virtual_property.pointer)(variant)
} }
// lazy-loaded per-instance userdata values
type CreateUserData=fn(&mlua::Lua)->mlua::Result<mlua::AnyUserData>;
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<Ref,
HashMap<&'static str,
mlua::AnyUserData
>
>,
}
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<InstanceValues>{
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<Option<mlua::AnyUserData>>{
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,
})
}
}