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