diff --git a/src/runner/instance.rs b/src/runner/instance.rs index ed94bac..2d96e06 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -257,7 +257,10 @@ impl mlua::UserData for Instance{ descriptor:Some(class), }; iter.find_map(|class| - cf.get_or_create_class_function(lua,&class.name,index_str).transpose() + cf.get_or_create_class_methods(&class.name) + .and_then(|mut class_methods| + class_methods.get_or_create_function(lua,index_str).transpose() + ) ).transpose() })?{ return function.into_lua(lua); @@ -378,20 +381,33 @@ struct ClassFunctions{ > } impl ClassFunctions{ - /// return self.classes[class][index] or create the function in the hashmap and then return it - fn get_or_create_class_function(&mut self,lua:&mlua::Lua,class:&str,index:&str)->mlua::Result>{ + /// return self.classes[class] or create the ClassMethods and then return it + fn get_or_create_class_methods(&mut self,class:&str)->Option{ // Use get_entry to get the &'static str keys of the database // and use it as a key for the classes hashmap - if let Some((&static_class_str,class_functions))=CLASS_FUNCTION_DATABASE.get_entry(class){ - if let Some((&static_index_str,function_pointer))=class_functions.get_entry(index){ - return Ok(Some( - self.classes.entry(static_class_str) - .or_insert_with(||HashMap::new()) - .entry(static_index_str) - .or_insert(lua.create_function(function_pointer)?) - .clone() - )) - } + CLASS_FUNCTION_DATABASE.get_entry(class) + .map(|(&static_class_str,method_pointers)| + ClassMethods{ + method_pointers, + methods:self.classes.entry(static_class_str) + .or_insert_with(||HashMap::new()), + } + ) + } +} +struct ClassMethods<'a>{ + method_pointers:&'static phf::Map<&'static str,ClassFunctionPointer>, + methods:&'a mut HashMap<&'static str,mlua::Function>, +} +impl ClassMethods<'_>{ + /// return self.methods[index] or create the function in the hashmap and then return it + fn get_or_create_function(&mut self,lua:&mlua::Lua,index:&str)->mlua::Result>{ + if let Some((&static_index_str,function_pointer))=self.method_pointers.get_entry(index){ + return Ok(Some( + self.methods.entry(static_index_str) + .or_insert(lua.create_function(function_pointer)?) + .clone() + )) } return Ok(None) }