From 1baf10fa87b6b8e13aa2e51195ad2bf70daa200e Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 20 Aug 2023 20:05:58 -0500 Subject: [PATCH] Fix custom typeof function being recursivve --- src/lune/globals/typeof.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/lune/globals/typeof.rs b/src/lune/globals/typeof.rs index fb1099c..2d9b91a 100644 --- a/src/lune/globals/typeof.rs +++ b/src/lune/globals/typeof.rs @@ -2,14 +2,26 @@ use mlua::prelude::*; use crate::roblox::datatypes::extension::RobloxUserdataTypenameExt; +const REGISTRY_KEY: &str = "NetClient"; + pub fn create(lua: &Lua) -> LuaResult> { - lua.create_function(|lua, value: LuaValue| { - #[cfg(feature = "roblox")] - if let LuaValue::UserData(u) = &value { - if let Some(type_name) = u.roblox_type_name() { - return lua.create_string(type_name); + let original = lua.globals().get::<_, LuaFunction>("typeof")?; + #[cfg(feature = "roblox")] + { + lua.set_named_registry_value(REGISTRY_KEY, original) + .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); + } } - } - lua.globals().get::<_, LuaFunction>("typeof")?.call(value) - }) + let original_fn: LuaFunction = lua + .named_registry_value(REGISTRY_KEY) + .expect("Missing typeof function in registry"); + original_fn.call(value) + }) + } + #[cfg(not(feature = "roblox"))] + original }