Rename MetatypeMethodBuilder to MetatypeFunctionBuilder

This commit is contained in:
lumi 2025-06-26 17:12:00 +10:00
parent 31b5ff5ab9
commit 1c1753234d
Signed by: luaneko
GPG Key ID: 406809B8763FF07A

View File

@ -38,7 +38,7 @@ const CACHE_LIBS: &[(&str, &str)] = &[
("package", "package"),
("debug", "debug"),
("jit", "jit"),
// require
// requires
("bit", r#"require("bit")"#),
("ffi", r#"require("ffi")"#),
("__tnew", r#"require("table.new")"#),
@ -47,7 +47,7 @@ const CACHE_LIBS: &[(&str, &str)] = &[
// https://www.lua.org/manual/5.1/manual.html#5.1
const CACHE_LOCALS: &[(&str, &str)] = &[
// base
// baselib
("assert", "assert"),
("error", "error"),
("type", "type"),
@ -68,7 +68,8 @@ const CACHE_LOCALS: &[(&str, &str)] = &[
("tonumber", "tonumber"),
("tostring", "tostring"),
("require", "require"),
// table
("__yield", "coroutine.yield"), // (used in future.rs)
// tablib
("__tconcat", "table.concat"),
("__tinsert", "table.insert"),
("__tmaxn", "table.maxn"),
@ -76,23 +77,21 @@ const CACHE_LOCALS: &[(&str, &str)] = &[
("__tsort", "table.sort"),
("__tpack", "table.pack"),
("__tunpack", "table.unpack"),
// string
// strlib
("__slen", "string.len"),
("__sprintf", "string.format"),
("__ssub", "string.sub"),
("__sgsub", "string.gsub"),
("__sgmatch", "string.gmatch"),
("__sdump", "string.dump"),
// math (used in luaify! macro)
// mathlib (used in luaify! macro)
("__fmod", "math.fmod"),
// coroutine (used in future.rs)
("__yield", "coroutine.yield"),
// package
// loadlib
("__preload", "package.preload"),
// debug
// dblib
("__traceback", "debug.traceback"),
("__registry", "debug.getregistry()"), // (used in lib.lua)
// ffi
// ffilib
("__C", "ffi.C"),
("__ct", "{}"),
("__cdef", "ffi.cdef"),
@ -105,7 +104,7 @@ const CACHE_LOCALS: &[(&str, &str)] = &[
("__sizeof", "ffi.sizeof"),
("__alignof", "ffi.alignof"),
("__intern", "ffi.string"), // (used in string.rs)
// bit (used in luaify! macro)
// bitlib (used in luaify! macro)
("__bnot", "bit.bnot"),
("__band", "bit.band"),
("__bor", "bit.bor"),
@ -341,10 +340,10 @@ impl<'r> MetatypeBuilder<'r> {
pub fn index(
&mut self,
name: impl Display,
f: impl FnOnce(&mut MetatypeMethodBuilder),
f: impl FnOnce(&mut MetatypeFunctionBuilder),
) -> &mut Self {
write!(self.lua, "__idx.{name} = ").unwrap();
f(&mut MetatypeMethodBuilder::new(self));
f(&mut MetatypeFunctionBuilder::new(self));
writeln!(self.lua, ";").unwrap();
self
}
@ -357,10 +356,10 @@ impl<'r> MetatypeBuilder<'r> {
pub fn metatable(
&mut self,
name: impl Display,
f: impl FnOnce(&mut MetatypeMethodBuilder),
f: impl FnOnce(&mut MetatypeFunctionBuilder),
) -> &mut Self {
write!(self.lua, "__mt.__{name} = ").unwrap();
f(&mut MetatypeMethodBuilder::new(self));
f(&mut MetatypeFunctionBuilder::new(self));
writeln!(self.lua, ";").unwrap();
self
}
@ -429,16 +428,16 @@ pub unsafe trait IntoFfi: Sized {
}
#[derive(Debug)]
pub struct MetatypeMethodBuilder<'r, 'm> {
pub struct MetatypeFunctionBuilder<'r, 'm> {
metatype: &'m mut MetatypeBuilder<'r>,
lparams: String, // parameters to the lua function
cparams: String, // parameters to the lua function
cargs: String, // arguments to the C call
prelude: String, // function body prelude
postlude: String, // function body postlude
lparams: String, // lua function parameters
cparams: String, // C function parameters
cargs: String, // C call arguments
prelude: String, // lua function body prelude
postlude: String, // lua function body postlude
}
impl<'r, 'm> MetatypeMethodBuilder<'r, 'm> {
impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> {
pub fn new(metatype: &'m mut MetatypeBuilder<'r>) -> Self {
Self {
metatype,
@ -517,12 +516,15 @@ impl<'r, 'm> MetatypeMethodBuilder<'r, 'm> {
write!(prelude, "local __{name}_len = 0; if {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();
if check_utf8 {
write!(prelude, r#"assert(__C.{IS_UTF8_FN}({name}, __{name}_len), "argument '{name}' must be a valid utf-8 string"); "#).unwrap();
}
if !allow_nil {
write!(prelude, r#"else return error("string expected in argument '{name}', got " .. type({name})); "#).unwrap();
}
write!(prelude, r#"end; "#).unwrap();
self
}