From db9d611ff7cfc1e1f07ba23f3820ad468785dce1 Mon Sep 17 00:00:00 2001 From: luaneko Date: Sat, 28 Jun 2025 09:51:50 +1000 Subject: [PATCH] Use raw equality for nil comparisons --- crates/luaffi/src/lib.lua | 22 ++++++++++------------ crates/luaffi/src/lib.rs | 24 +++++++++++++++++------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/crates/luaffi/src/lib.lua b/crates/luaffi/src/lib.lua index e744284..82f5834 100644 --- a/crates/luaffi/src/lib.lua +++ b/crates/luaffi/src/lib.lua @@ -2,24 +2,22 @@ local LUA_REFNIL = -1 -- lib_aux.c local FREELIST_REF = 0 -local function __ref(value, t) - if value == nil then return LUA_REFNIL end - if t == nil then t = __registry end - local ref = t[FREELIST_REF] +local function __ref(value) + if rawequal(value, nil) then return LUA_REFNIL end + local ref = __registry[FREELIST_REF] if ref ~= nil and ref ~= 0 then - t[FREELIST_REF] = t[ref] + __registry[FREELIST_REF] = __registry[ref] else - ref = #t + 1 + ref = #__registry + 1 end - t[ref] = value + __registry[ref] = value return ref end -local function __unref(ref, t) +local function __unref(ref) if ref < 0 then return nil end - if t == nil then t = __registry end - local value = t[ref] - t[ref] = t[FREELIST_REF] - t[FREELIST_REF] = ref + local value = __registry[ref] + __registry[ref] = __registry[FREELIST_REF] + __registry[FREELIST_REF] = ref return value end diff --git a/crates/luaffi/src/lib.rs b/crates/luaffi/src/lib.rs index b3c4b6d..12cb3db 100644 --- a/crates/luaffi/src/lib.rs +++ b/crates/luaffi/src/lib.rs @@ -473,11 +473,6 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> { } pub fn param(&mut self, name: impl Display) -> &mut Self { - assert!( - T::From::ty() != TypeType::Void, - "cannot declare void parameter" - ); - let Self { metatype: MetatypeBuilder { reg, .. }, lparams, @@ -488,6 +483,14 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> { .. } = self; + assert!( + T::From::ty() != TypeType::Void, + "cannot declare void parameter" + ); + + // should already be prevented by #[metatype] macro + debug_assert!(!name.to_string().starts_with("__")); + reg.include::(); (!lparams.is_empty()).then(|| lparams.push_str(", ")); @@ -526,6 +529,9 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> { .. } = self; + // should already be prevented by #[metatype] macro + debug_assert!(!name.to_string().starts_with("__")); + let param_ptr = <*const u8>::cdecl(&name); let param_len = usize::cdecl(format!("{name}_len")); @@ -536,7 +542,11 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> { write!(lparams, "{name}").unwrap(); write!(cparams, "{param_ptr}, {param_len}").unwrap(); write!(cargs, "{name}, __{name}_len").unwrap(); - write!(prelude, "local __{name}_len = 0; if {name} ~= nil then ").unwrap(); + write!( + prelude, + "local __{name}_len = 0; if not rawequal({name}, nil) then " + ) + .unwrap(); write!(prelude, r#"assert(type({name}) == "string", "string expected in argument '{name}', got " .. type({name})); "#).unwrap(); write!(prelude, r#"__{name}_len = #{name}; "#).unwrap(); @@ -946,7 +956,7 @@ macro_rules! impl_ref_fromabi { type From = Option<$ty>; fn prelude(arg: &str) -> impl Display { - display!(r#"assert({arg} ~= nil, "argument '{arg}' cannot be nil"); "#) + display!(r#"assert(not rawequal({arg}, nil), "argument '{arg}' cannot be nil"); "#) } fn convert(from: Self::From) -> Self {