diff --git a/crates/lune-std-ffi/src/c/fn_info.rs b/crates/lune-std-ffi/src/c/fn_info.rs index fb5b8bcc..963d8a0b 100644 --- a/crates/lune-std-ffi/src/c/fn_info.rs +++ b/crates/lune-std-ffi/src/c/fn_info.rs @@ -11,7 +11,7 @@ use super::{ use crate::{ data::{CallableData, ClosureData, RefData, RefFlag}, ffi::{ - association, bit_mask::*, libffi_helper::SIZE_OF_POINTER, FfiArg, FfiData, FfiResult, + association, bit_field::*, libffi_helper::SIZE_OF_POINTER, FfiArg, FfiData, FfiResult, FfiSignedness, FfiSize, }, }; diff --git a/crates/lune-std-ffi/src/data/box_data/flag.rs b/crates/lune-std-ffi/src/data/box_data/flag.rs index 445a3b3f..a983f3ba 100644 --- a/crates/lune-std-ffi/src/data/box_data/flag.rs +++ b/crates/lune-std-ffi/src/data/box_data/flag.rs @@ -1,4 +1,4 @@ -use crate::ffi::bit_mask::*; +use crate::ffi::bit_field::*; pub enum BoxFlag { Leaked, diff --git a/crates/lune-std-ffi/src/data/box_data/mod.rs b/crates/lune-std-ffi/src/data/box_data/mod.rs index e8c4b88a..1cdf9b4b 100644 --- a/crates/lune-std-ffi/src/data/box_data/mod.rs +++ b/crates/lune-std-ffi/src/data/box_data/mod.rs @@ -10,7 +10,7 @@ use mlua::prelude::*; use super::helper::method_provider; use crate::{ data::{association_names::REF_INNER, RefBounds, RefData, RefFlag}, - ffi::{association, bit_mask::*, FfiData}, + ffi::{association, bit_field::*, FfiData}, }; mod flag; diff --git a/crates/lune-std-ffi/src/data/callable_data.rs b/crates/lune-std-ffi/src/data/callable_data.rs index 47f47953..1119a2f8 100644 --- a/crates/lune-std-ffi/src/data/callable_data.rs +++ b/crates/lune-std-ffi/src/data/callable_data.rs @@ -13,6 +13,7 @@ use mlua::prelude::*; use super::{GetFfiData, RefData}; use crate::ffi::{FfiArg, FfiData, FfiResult}; +// A function pointer that luau can call. it stores libffi cif for calling convention. pub struct CallableData { cif: *mut ffi_cif, arg_info_list: Vec, diff --git a/crates/lune-std-ffi/src/data/closure_data.rs b/crates/lune-std-ffi/src/data/closure_data.rs index 26595d1c..93d21b49 100644 --- a/crates/lune-std-ffi/src/data/closure_data.rs +++ b/crates/lune-std-ffi/src/data/closure_data.rs @@ -17,6 +17,7 @@ use crate::ffi::{ FfiArg, FfiData, FfiResult, }; +// A closure that can be created with lua function. pub struct ClosureData { lua: *const Lua, closure: *mut ffi_closure, diff --git a/crates/lune-std-ffi/src/data/ref_data/flag.rs b/crates/lune-std-ffi/src/data/ref_data/flag.rs index 36a24a00..a7068adf 100644 --- a/crates/lune-std-ffi/src/data/ref_data/flag.rs +++ b/crates/lune-std-ffi/src/data/ref_data/flag.rs @@ -1,4 +1,4 @@ -use crate::ffi::bit_mask::*; +use crate::ffi::bit_field::*; pub enum RefFlag { Leaked, diff --git a/crates/lune-std-ffi/src/data/ref_data/mod.rs b/crates/lune-std-ffi/src/data/ref_data/mod.rs index 42701b30..2f26bdd1 100644 --- a/crates/lune-std-ffi/src/data/ref_data/mod.rs +++ b/crates/lune-std-ffi/src/data/ref_data/mod.rs @@ -5,7 +5,7 @@ use mlua::prelude::*; use super::helper::method_provider; use crate::{ data::association_names::REF_INNER, - ffi::{association, bit_mask::*, FfiData}, + ffi::{association, bit_field::*, FfiData}, }; mod bounds; diff --git a/crates/lune-std-ffi/src/ffi/bit_mask.rs b/crates/lune-std-ffi/src/ffi/bit_field.rs similarity index 91% rename from crates/lune-std-ffi/src/ffi/bit_mask.rs rename to crates/lune-std-ffi/src/ffi/bit_field.rs index db88474b..175d5882 100644 --- a/crates/lune-std-ffi/src/ffi/bit_mask.rs +++ b/crates/lune-std-ffi/src/ffi/bit_field.rs @@ -1,5 +1,7 @@ #![allow(unused)] +// Simple bit field library for handling data flags + pub const U8_MASK1: u8 = 1; pub const U8_MASK2: u8 = 2; pub const U8_MASK3: u8 = 4; diff --git a/crates/lune-std-ffi/src/ffi/mod.rs b/crates/lune-std-ffi/src/ffi/mod.rs index 5d6073a7..36b27d91 100644 --- a/crates/lune-std-ffi/src/ffi/mod.rs +++ b/crates/lune-std-ffi/src/ffi/mod.rs @@ -3,7 +3,7 @@ use std::cell::Ref; use mlua::prelude::*; pub mod association; -pub mod bit_mask; +pub mod bit_field; mod cast; pub mod libffi_helper; diff --git a/tests/ffi/external_closure/callClosure.luau b/tests/ffi/external_closure/callClosure.luau index b73fe6c7..f6bd73c8 100644 --- a/tests/ffi/external_closure/callClosure.luau +++ b/tests/ffi/external_closure/callClosure.luau @@ -4,11 +4,10 @@ local lib = require("../utils/compile")("./tests/ffi/external_closure/lib.c") local c = ffi.c -- Create closure -local closureInfo = c.fn({ c.int, c.int }, c.int) -local closure = closureInfo:closure(function(ret, a, b) +local closure = c.fn({ c.int, c.int }, c.int):closure(function(ret, a, b) c.int:writeData(ret, c.int:readData(a) + c.int:readData(b)) end) -local callClosure = callableWrapper(lib:find("call_closure"), { closureInfo }, c.int) +local callClosure = callableWrapper(lib:find("call_closure"), { c.void:ptr() }, c.int) local result = callClosure(closure:ref()) assert(result == 72, `callClosure failed. result expected 20000, got {result}`) diff --git a/tests/ffi/external_closure/callClosureWithPointer.luau b/tests/ffi/external_closure/callClosureWithPointer.luau index 5fd47b91..24084647 100644 --- a/tests/ffi/external_closure/callClosureWithPointer.luau +++ b/tests/ffi/external_closure/callClosureWithPointer.luau @@ -4,12 +4,12 @@ local lib = require("../utils/compile")("./tests/ffi/external_closure/lib.c") local c = ffi.c -- Create closure -local closureWithPointerInfo = c.fn({ c.int, c.int:ptr() }, c.int) -local closureWithPointer = closureWithPointerInfo:closure(function(returnRef, aRef, bRef) - c.int:writeData(returnRef, c.int:readData(aRef) + c.int:readData(bRef:deref())) -end) +local closureWithPointer = c.fn({ c.int, c.int:ptr() }, c.int) + :closure(function(returnRef, aRef, bRef) + c.int:writeData(returnRef, c.int:readData(aRef) + c.int:readData(bRef:deref())) + end) local callClosureWithPointer = - callableWrapper(lib:find("call_closure_with_pointer"), { closureWithPointerInfo }, c.int) + callableWrapper(lib:find("call_closure_with_pointer"), { c.void:ptr() }, c.int) local result = callClosureWithPointer(closureWithPointer:ref()) assert(result == 72, `closureWithPointer failed. result expected 20000, got {result}`) diff --git a/tests/ffi/external_closure/callHelloWorld.luau b/tests/ffi/external_closure/callHelloWorld.luau index 811601d1..50a8b199 100644 --- a/tests/ffi/external_closure/callHelloWorld.luau +++ b/tests/ffi/external_closure/callHelloWorld.luau @@ -3,10 +3,9 @@ local lib = require("../utils/compile")("./tests/ffi/external_closure/lib.c") local c = ffi.c -- Create closure -local helloWorldInfo = c.fn({}, c.void) -local helloWorld = helloWorldInfo:closure(function() +local helloWorld = c.fn({}, c.void):closure(function() print("Hello world in lua closure!") end) -local callHelloWorld = c.fn({ helloWorldInfo }, c.void):callable(lib:find("call_hello_world")) +local callHelloWorld = c.fn({ c.void:ptr() }, c.void):callable(lib:find("call_hello_world")) callHelloWorld(nil, helloWorld:ref()) diff --git a/types/ffi.luau b/types/ffi.luau index a4238c7d..6cc8fc1e 100644 --- a/types/ffi.luau +++ b/types/ffi.luau @@ -563,7 +563,9 @@ export type CPtrInfo = { --[=[ @class CArrInfo - A c sized array type information. + A c sized array type information. It can be used for sturct field. + + For function arguments, use CPtr instead. ]=] export type CArrInfo = { --[=[ @@ -682,6 +684,8 @@ export type CArrInfo = { @class CFnInfo A C function pointer type information, with function signature. + + For struct field, array element, or function arguments, use `void:ptr()` instead. ]=] export type CFnInfo = { --[=[