Use raw equality for nil comparisons

This commit is contained in:
lumi 2025-06-28 09:51:50 +10:00
parent a90c36a260
commit db9d611ff7
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
2 changed files with 27 additions and 19 deletions

View File

@ -2,24 +2,22 @@
local LUA_REFNIL = -1 -- lib_aux.c local LUA_REFNIL = -1 -- lib_aux.c
local FREELIST_REF = 0 local FREELIST_REF = 0
local function __ref(value, t) local function __ref(value)
if value == nil then return LUA_REFNIL end if rawequal(value, nil) then return LUA_REFNIL end
if t == nil then t = __registry end local ref = __registry[FREELIST_REF]
local ref = t[FREELIST_REF]
if ref ~= nil and ref ~= 0 then if ref ~= nil and ref ~= 0 then
t[FREELIST_REF] = t[ref] __registry[FREELIST_REF] = __registry[ref]
else else
ref = #t + 1 ref = #__registry + 1
end end
t[ref] = value __registry[ref] = value
return ref return ref
end end
local function __unref(ref, t) local function __unref(ref)
if ref < 0 then return nil end if ref < 0 then return nil end
if t == nil then t = __registry end local value = __registry[ref]
local value = t[ref] __registry[ref] = __registry[FREELIST_REF]
t[ref] = t[FREELIST_REF] __registry[FREELIST_REF] = ref
t[FREELIST_REF] = ref
return value return value
end end

View File

@ -473,11 +473,6 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> {
} }
pub fn param<T: FromFfi>(&mut self, name: impl Display) -> &mut Self { pub fn param<T: FromFfi>(&mut self, name: impl Display) -> &mut Self {
assert!(
T::From::ty() != TypeType::Void,
"cannot declare void parameter"
);
let Self { let Self {
metatype: MetatypeBuilder { reg, .. }, metatype: MetatypeBuilder { reg, .. },
lparams, lparams,
@ -488,6 +483,14 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> {
.. ..
} = self; } = 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::<T::From>(); reg.include::<T::From>();
(!lparams.is_empty()).then(|| lparams.push_str(", ")); (!lparams.is_empty()).then(|| lparams.push_str(", "));
@ -526,6 +529,9 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> {
.. ..
} = self; } = self;
// should already be prevented by #[metatype] macro
debug_assert!(!name.to_string().starts_with("__"));
let param_ptr = <*const u8>::cdecl(&name); let param_ptr = <*const u8>::cdecl(&name);
let param_len = usize::cdecl(format!("{name}_len")); let param_len = usize::cdecl(format!("{name}_len"));
@ -536,7 +542,11 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> {
write!(lparams, "{name}").unwrap(); write!(lparams, "{name}").unwrap();
write!(cparams, "{param_ptr}, {param_len}").unwrap(); write!(cparams, "{param_ptr}, {param_len}").unwrap();
write!(cargs, "{name}, __{name}_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#"assert(type({name}) == "string", "string expected in argument '{name}', got " .. type({name})); "#).unwrap();
write!(prelude, r#"__{name}_len = #{name}; "#).unwrap(); write!(prelude, r#"__{name}_len = #{name}; "#).unwrap();
@ -946,7 +956,7 @@ macro_rules! impl_ref_fromabi {
type From = Option<$ty>; type From = Option<$ty>;
fn prelude(arg: &str) -> impl Display { 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 { fn convert(from: Self::From) -> Self {