Fix custom typeof function being recursivve

This commit is contained in:
Filip Tibell 2023-08-20 20:05:58 -05:00
parent 02c9bce645
commit 1baf10fa87

View file

@ -2,14 +2,26 @@ use mlua::prelude::*;
use crate::roblox::datatypes::extension::RobloxUserdataTypenameExt; use crate::roblox::datatypes::extension::RobloxUserdataTypenameExt;
const REGISTRY_KEY: &str = "NetClient";
pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> { pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_function(|lua, value: LuaValue| { let original = lua.globals().get::<_, LuaFunction>("typeof")?;
#[cfg(feature = "roblox")] #[cfg(feature = "roblox")]
if let LuaValue::UserData(u) = &value { {
if let Some(type_name) = u.roblox_type_name() { lua.set_named_registry_value(REGISTRY_KEY, original)
return lua.create_string(type_name); .expect("Failed to store typeof function in registry");
lua.create_function(|lua, value: LuaValue| {
if let LuaValue::UserData(u) = &value {
if let Some(type_name) = u.roblox_type_name() {
return lua.create_string(type_name);
}
} }
} let original_fn: LuaFunction = lua
lua.globals().get::<_, LuaFunction>("typeof")?.call(value) .named_registry_value(REGISTRY_KEY)
}) .expect("Missing typeof function in registry");
original_fn.call(value)
})
}
#[cfg(not(feature = "roblox"))]
original
} }