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 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

View File

@ -473,11 +473,6 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> {
}
pub fn param<T: FromFfi>(&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::<T::From>();
(!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 {