From 692697f82b3c05141cbc9b63acad9575194979b0 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sun, 6 Oct 2024 11:41:52 -0700 Subject: [PATCH] split get_or_create_class_function into two parts in case you want to get multiple functions from the same class --- src/runner/instance.rs | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) 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) }