Convert lua return types to stubs as well

This commit is contained in:
lumi 2025-06-24 22:49:18 +10:00
parent 8c47987a45
commit ee64b22510
Signed by: luaneko
GPG Key ID: 406809B8763FF07A

View File

@ -451,10 +451,9 @@ fn get_lua_functions(imp: &mut ItemImpl) -> Result<Vec<LuaFunction>> {
.iter()
.map(|arg| {
Ok(match arg {
FnArg::Receiver(recv) => {
syn_assert!(recv.mutability.is_none(), recv, "cannot be mut");
Pat::Type(parse_quote_spanned!(recv.span() => self: cdata))
}
FnArg::Receiver(recv) => Pat::Type(parse_quote_spanned!(recv.span() =>
self: cdata
)),
FnArg::Typed(ty) => Pat::Type(ty.clone()),
})
})
@ -482,10 +481,10 @@ fn get_lua_functions(imp: &mut ItemImpl) -> Result<Vec<LuaFunction>> {
}
fn stub_lua_function(func: &mut ImplItemFn) -> Result<()> {
// converts an extern "Lua" function into a regular function with no body to allow for
// documentation generation
let ffi = ffi_crate();
// converts an extern "Lua" function into a regular function with no body to allow for
// documentation generation
func.sig.abi = None;
func.attrs.push(parse_quote!(#[allow(unused)]));
func.block = parse_quote!({
@ -493,33 +492,22 @@ fn stub_lua_function(func: &mut ImplItemFn) -> Result<()> {
});
let inputs = &mut func.sig.inputs;
for input in inputs.iter_mut() {
if let FnArg::Typed(pat) = input {
let span = pat.ty.span();
let ty = if let Type::Infer(_) = *pat.ty {
quote_spanned!(span => any)
} else {
match ty_name(&pat.ty)?.to_string().as_str() {
"any" => quote_spanned!(span => any),
"nil" => quote_spanned!(span => nil),
"boolean" => quote_spanned!(span => boolean),
"lightuserdata" => quote_spanned!(span => lightuserdata),
"number" => quote_spanned!(span => number),
"integer" => quote_spanned!(span => integer),
"string" => quote_spanned!(span => string),
"table" => quote_spanned!(span => table),
"function" => quote_spanned!(span => function),
"userdata" => quote_spanned!(span => userdata),
"thread" => quote_spanned!(span => thread),
"cdata" => quote_spanned!(span => cdata),
_ => syn_error!(pat, "unknown lua type"),
}
};
let output = &mut func.sig.output;
pat.ty = Box::new(parse_quote!(#ffi::__internal::stub_types::#ty));
for input in inputs.iter_mut() {
if let FnArg::Typed(pat) = input
&& let Ok(stub) = stub_lua_type(&pat.ty)
{
pat.ty = stub.into();
}
}
if let ReturnType::Type(_, ty) = output
&& let Ok(stub) = stub_lua_type(ty)
{
*ty = stub.into();
}
if let Some(ref variadic) = func.sig.variadic {
let ty = quote_spanned!(variadic.span() => variadic);
inputs.push(parse_quote!(rest: #ffi::__internal::stub_types::#ty));
@ -529,6 +517,32 @@ fn stub_lua_function(func: &mut ImplItemFn) -> Result<()> {
Ok(())
}
fn stub_lua_type(ty: &Type) -> Result<Type> {
let ffi = ffi_crate();
let span = ty.span();
let ty = if let Type::Infer(_) = ty {
quote_spanned!(span => any)
} else {
match ty_name(ty)?.to_string().as_str() {
"any" => quote_spanned!(span => any),
"nil" => quote_spanned!(span => nil),
"boolean" => quote_spanned!(span => boolean),
"lightuserdata" => quote_spanned!(span => lightuserdata),
"number" => quote_spanned!(span => number),
"integer" => quote_spanned!(span => integer),
"string" => quote_spanned!(span => string),
"table" => quote_spanned!(span => table),
"function" => quote_spanned!(span => function),
"userdata" => quote_spanned!(span => userdata),
"thread" => quote_spanned!(span => thread),
"cdata" => quote_spanned!(span => cdata),
_ => syn_error!(ty, "unknown lua type"),
}
};
Ok(parse_quote!(#ffi::__internal::stub_types::#ty))
}
fn parse_lua_function_attrs(attrs: &mut Vec<Attribute>) -> Result<LuaFunctionAttrs> {
let mut parsed = LuaFunctionAttrs::default();
let mut i = 0;