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