Rename CDef to Cdef

This commit is contained in:
lumi 2025-06-23 09:27:15 +10:00
parent 68f6e48043
commit fae1c6e162
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
4 changed files with 47 additions and 19 deletions

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
__internal::{display, type_id}, __internal::{display, type_id},
CDef, CDefBuilder, FfiReturnConvention, Metatype, MetatypeBuilder, ToFfi, Type, TypeBuilder, Cdef, CdefBuilder, FfiReturnConvention, Metatype, MetatypeBuilder, ToFfi, Type, TypeBuilder,
}; };
use luaify::luaify; use luaify::luaify;
use std::{ use std::{
@ -144,8 +144,8 @@ unsafe impl<F: Future<Output: ToFfi> + 'static> Type for lua_future<F> {
} }
} }
unsafe impl<F: Future<Output: ToFfi> + 'static> CDef for lua_future<F> { unsafe impl<F: Future<Output: ToFfi> + 'static> Cdef for lua_future<F> {
fn build(s: &mut CDefBuilder) { fn build(s: &mut CdefBuilder) {
s.field_opaque(mem::offset_of!(Self, take)) // opaque .sig, .poll and .state s.field_opaque(mem::offset_of!(Self, take)) // opaque .sig, .poll and .state
.field::<unsafe extern "C" fn(*mut Self) -> <F::Output as ToFfi>::To>("__take") .field::<unsafe extern "C" fn(*mut Self) -> <F::Output as ToFfi>::To>("__take")
.field::<unsafe extern "C" fn(*mut Self)>("__drop"); .field::<unsafe extern "C" fn(*mut Self)>("__drop");

24
crates/luaffi/src/lib.lua Normal file
View File

@ -0,0 +1,24 @@
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]
if ref ~= nil and ref ~= 0 then
t[FREELIST_REF] = t[ref]
else
ref = #t + 1
end
t[ref] = value
return ref
end
local function __unref(ref, t)
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
return value
end

View File

@ -77,6 +77,8 @@ const CACHE_LOCALS: &[(&str, &str)] = &[
("__tmaxn", "table.maxn"), ("__tmaxn", "table.maxn"),
("__tremove", "table.remove"), ("__tremove", "table.remove"),
("__tsort", "table.sort"), ("__tsort", "table.sort"),
("__tpack", "table.pack"),
("__tunpack", "table.unpack"),
// string // string
("__slen", "string.len"), ("__slen", "string.len"),
("__sformat", "string.format"), ("__sformat", "string.format"),
@ -92,6 +94,7 @@ const CACHE_LOCALS: &[(&str, &str)] = &[
("__preload", "package.preload"), ("__preload", "package.preload"),
// debug // debug
("__traceback", "debug.traceback"), ("__traceback", "debug.traceback"),
("__registry", "debug.getregistry()"),
// ffi // ffi
("__C", "ffi.C"), ("__C", "ffi.C"),
("__ct", "{}"), ("__ct", "{}"),
@ -126,7 +129,7 @@ fn cache_local(f: &mut Formatter, list: &[(&str, &str)]) -> fmt::Result {
writeln!(f, ";") writeln!(f, ";")
} }
#[derive(Debug, Default)] #[derive(Debug, Clone, Default)]
pub struct Registry { pub struct Registry {
types: HashSet<String>, types: HashSet<String>,
funcs: HashSet<String>, funcs: HashSet<String>,
@ -180,6 +183,7 @@ impl Display for Registry {
writeln!(f, "--- automatically generated by {name} {version}")?; writeln!(f, "--- automatically generated by {name} {version}")?;
cache_local(f, CACHE_LIBS)?; cache_local(f, CACHE_LIBS)?;
cache_local(f, CACHE_LOCALS)?; cache_local(f, CACHE_LOCALS)?;
writeln!(f, "{}", include_str!("./lib.lua"))?;
writeln!(f, "__cdef [[\n{}\n]];", self.cdef.trim())?; writeln!(f, "__cdef [[\n{}\n]];", self.cdef.trim())?;
write!(f, "{}", self.lua) write!(f, "{}", self.lua)
} }
@ -213,9 +217,9 @@ impl<'r> TypeBuilder<'r> {
self self
} }
pub fn cdef<T: CDef>(&mut self) -> &mut Self { pub fn cdef<T: Cdef>(&mut self) -> &mut Self {
let mut b = CDefBuilder::new::<T>(self.registry); let mut b = CdefBuilder::new::<T>(self.registry);
<T as CDef>::build(&mut b); <T as Cdef>::build(&mut b);
drop(b); drop(b);
self self
} }
@ -228,20 +232,20 @@ impl<'r> TypeBuilder<'r> {
} }
} }
pub unsafe trait CDef: Type { pub unsafe trait Cdef: Type {
fn build(b: &mut CDefBuilder); fn build(b: &mut CdefBuilder);
} }
#[derive(Debug)] #[derive(Debug)]
pub struct CDefBuilder<'r> { pub struct CdefBuilder<'r> {
registry: &'r mut Registry, registry: &'r mut Registry,
cdef: String, cdef: String,
align: usize, align: usize,
opaque: usize, opaque: usize,
} }
impl<'r> CDefBuilder<'r> { impl<'r> CdefBuilder<'r> {
fn new<T: CDef>(registry: &'r mut Registry) -> Self { fn new<T: Cdef>(registry: &'r mut Registry) -> Self {
Self { Self {
registry, registry,
cdef: format!("{} {{ ", T::cdecl("")), cdef: format!("{} {{ ", T::cdecl("")),
@ -267,14 +271,14 @@ impl<'r> CDefBuilder<'r> {
self self
} }
pub fn inner_struct(&mut self, f: impl FnOnce(&mut CDefBuilder)) -> &mut Self { pub fn inner_struct(&mut self, f: impl FnOnce(&mut CdefBuilder)) -> &mut Self {
self.cdef.push_str("struct { "); self.cdef.push_str("struct { ");
f(self); f(self);
self.cdef.push_str("}; "); self.cdef.push_str("}; ");
self self
} }
pub fn inner_union(&mut self, f: impl FnOnce(&mut CDefBuilder)) -> &mut Self { pub fn inner_union(&mut self, f: impl FnOnce(&mut CdefBuilder)) -> &mut Self {
self.cdef.push_str("union { "); self.cdef.push_str("union { ");
f(self); f(self);
self.cdef.push_str("}; "); self.cdef.push_str("}; ");
@ -282,7 +286,7 @@ impl<'r> CDefBuilder<'r> {
} }
} }
impl<'r> Drop for CDefBuilder<'r> { impl<'r> Drop for CdefBuilder<'r> {
fn drop(&mut self) { fn drop(&mut self) {
let Self { let Self {
registry, registry,
@ -297,7 +301,7 @@ impl<'r> Drop for CDefBuilder<'r> {
} }
pub unsafe trait Metatype { pub unsafe trait Metatype {
type Target: CDef; type Target: Cdef;
fn build(b: &mut MetatypeBuilder); fn build(b: &mut MetatypeBuilder);
} }

View File

@ -1,4 +1,4 @@
use crate::{CDef, CDefBuilder, FfiReturnConvention, FromFfi, ToFfi, Type, TypeBuilder, display}; use crate::{Cdef, CdefBuilder, FfiReturnConvention, FromFfi, ToFfi, Type, TypeBuilder, display};
use std::{ffi::c_int, fmt::Display, ptr}; use std::{ffi::c_int, fmt::Display, ptr};
#[repr(C)] #[repr(C)]
@ -22,8 +22,8 @@ unsafe impl<T: Type> Type for lua_option<T> {
} }
} }
unsafe impl<T: Type> CDef for lua_option<T> { unsafe impl<T: Type> Cdef for lua_option<T> {
fn build(b: &mut CDefBuilder) { fn build(b: &mut CdefBuilder) {
b.field::<c_int>("__tag").field::<T>("__value"); b.field::<c_int>("__tag").field::<T>("__value");
} }
} }