Implement metatype proc-macro

This commit is contained in:
2025-06-19 20:38:40 +10:00
parent af81879b42
commit 1c821d8804
8 changed files with 325 additions and 142 deletions

View File

@@ -9,3 +9,4 @@ luaffi_impl = { version = "0.1.0", path = "../luaffi_impl" }
luaify = { version = "0.1.0", path = "../luaify" }
rustc-hash = "2.1.1"
simdutf8 = "0.1.5"
static_assertions = "1.1.0"

View File

@@ -1,4 +1,6 @@
pub use luaify::*;
use rustc_hash::FxHasher;
pub use static_assertions::*;
use std::{
any::TypeId,
fmt::{self, Display, Formatter},

View File

@@ -1,114 +0,0 @@
-- automatically generated by luaffi 0.1.0
local table, string, math, coroutine, package, debug, jit, bit, ffi, new, clear =
table,
string,
math,
coroutine,
package,
debug,
jit,
require("bit"),
require("ffi"),
require("table.new"),
require("table.clear")
local assert, error, type, print, pcall, xpcall, getfenv, setfenv, getmetatable, setmetatable, pairs, ipairs, next, rawget, rawset, rawequal, select, tonumber, tostring, require, concat, insert, maxn, remove, sort, strlen, format, strsub, gsub, gmatch, dump, random, yield, traceback, C, cdef, typeof, metatype, cast, gc, tobit, tohex, bnot, band, bor, bxor, lshift, rshift, arshift, rol, ror, bswap =
assert,
error,
type,
print,
pcall,
xpcall,
getfenv,
setfenv,
getmetatable,
setmetatable,
pairs,
ipairs,
next,
rawget,
rawset,
rawequal,
select,
tonumber,
tostring,
require,
table.concat,
table.insert,
table.maxn,
table.remove,
table.sort,
string.len,
string.format,
string.sub,
string.gsub,
string.gmatch,
string.dump,
math.random,
coroutine.yield,
debug.traceback,
ffi.C,
ffi.cdef,
ffi.typeof,
ffi.metatype,
ffi.cast,
ffi.gc,
bit.tobit,
bit.tohex,
bit.bnot,
bit.band,
bit.bor,
bit.bxor,
bit.lshift,
bit.rshift,
bit.arshift,
bit.rol,
bit.ror,
bit.bswap
cdef([[void (*luaffi_keep)(void const *);
bool (*luaffi_is_utf8)(uint8_t const *, uintptr_t );
struct Test { } __attribute__((aligned(1)));
struct lua_buf { uint8_t *__ptr; uintptr_t __len; } __attribute__((aligned(8)));
struct option__lua_buf { int32_t __tag; struct lua_buf __value; } __attribute__((aligned(8)));
struct option__uint32_t { int32_t __tag; uint32_t __value; } __attribute__((aligned(4)));
struct option__void { int32_t __tag; void __value; } __attribute__((aligned(4)));
]])
local Test = typeof("struct Test ")
local lua_buf = typeof("struct lua_buf ")
do
local __mt, __idx = {}, {}
__mt.__index = __idx
metatype(lua_buf, __mt)
end
local option__lua_buf = typeof("struct option__lua_buf ")
local option__uint32_t = typeof("struct option__uint32_t ")
local option__void = typeof("struct option__void ")
do
local __mt, __idx = {}, {}
__mt.__index = __idx
__idx.method = function(my_arg, other)
local __keep_my_arg = my_arg
if my_arg == nil then
my_arg = option__lua_buf()
else
assert(type(my_arg) == "string", "expected string in argument 'my_arg', got " .. type(my_arg))
assert(C.luaffi_is_utf8(my_arg, #my_arg), "argument 'my_arg' must be a valid utf8 string")
my_arg = lua_buf(my_arg, #my_arg)
my_arg = option__lua_buf(1, my_arg)
end
if other == nil then
other = option__uint32_t()
else
assert(type(other) == "number", "expected number in argument 'other', got " .. type(other))
other = option__uint32_t(1, other)
end
local res = C.test_fn_impl(my_arg, other)
if res.__tag == 0 then
res = nil
else
res = res.__value
end
C.luaffi_keep(__keep_my_arg)
return res
end
metatype(Test, __mt)
end