Add __type to Error's userdata metatable.

Close #585
This commit is contained in:
Alex Orlenko
2025-05-27 01:31:54 +01:00
parent 13395e9c3d
commit 76a8f8cc71
3 changed files with 21 additions and 6 deletions
+5 -1
View File
@@ -349,7 +349,11 @@ pub(crate) unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(
state,
Some(|state| {
ffi::lua_pushcfunction(state, error_tostring);
rawset_field(state, -2, "__tostring")
ffi::lua_setfield(state, -2, cstr!("__tostring"));
// This is mostly for Luau typeof() function
ffi::lua_pushstring(state, cstr!("error"));
ffi::lua_setfield(state, -2, cstr!("__type"));
}),
)?;
+5 -5
View File
@@ -47,7 +47,7 @@ pub(crate) unsafe fn get_internal_metatable<T: TypeKey>(state: *mut ffi::lua_Sta
// Uses 6 stack spaces and calls checkstack.
pub(crate) unsafe fn init_internal_metatable<T: TypeKey>(
state: *mut ffi::lua_State,
customize_fn: Option<fn(*mut ffi::lua_State) -> Result<()>>,
customize_fn: Option<fn(*mut ffi::lua_State)>,
) -> Result<()> {
check_stack(state, 6)?;
@@ -62,11 +62,11 @@ pub(crate) unsafe fn init_internal_metatable<T: TypeKey>(
ffi::lua_pushboolean(state, 0);
rawset_field(state, -2, "__metatable")?;
if let Some(f) = customize_fn {
f(state)?;
}
protect_lua!(state, 1, 0, |state| {
if let Some(f) = customize_fn {
f(state);
}
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, T::type_key());
})?;
+11
View File
@@ -436,5 +436,16 @@ fn test_loadstring() -> Result<()> {
Ok(())
}
#[test]
fn test_typeof_error() -> Result<()> {
let lua = Lua::new();
let err = Error::runtime("just a test error");
let res = lua.load("return typeof(...)").call::<String>(err)?;
assert_eq!(res, "error");
Ok(())
}
#[path = "luau/require.rs"]
mod require;