From 809fd566a15dad579a08d8d2f46fdc2557978a3e Mon Sep 17 00:00:00 2001 From: qwreey Date: Sat, 9 Nov 2024 10:07:26 +0000 Subject: [PATCH] Add free test (#243) --- crates/lune-std-ffi/README.md | 3 +-- crates/lune-std-ffi/src/c/arr_info.rs | 1 + crates/lune-std-ffi/src/c/fn_info.rs | 1 + crates/lune-std-ffi/src/c/ptr_info.rs | 2 ++ crates/lune-std-ffi/src/c/struct_info.rs | 2 ++ crates/lune-std-ffi/src/c/void_info.rs | 1 + crates/lune/src/tests.rs | 3 ++- tests/ffi/free.luau | 18 ++++++++++++++++++ 8 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 tests/ffi/free.luau diff --git a/crates/lune-std-ffi/README.md b/crates/lune-std-ffi/README.md index e7b3fbcd..1140c04a 100644 --- a/crates/lune-std-ffi/README.md +++ b/crates/lune-std-ffi/README.md @@ -24,7 +24,6 @@ See [tests/ffi](../../tests/ffi/README.md) - Add varargs support - Array argument in cfn -- Ref boundary fix ## Code structure @@ -35,7 +34,7 @@ Define C-ABI type information and provide conversion and casting - [**Struct ` CArrInfo`:**](./src/c/struct_info.rs) Represents C Array type - [**Struct ` CPtrInfo`:**](./src/c/ptr_info.rs) Represents C Pointer type - [**Struct ` CFnInfo`:**](./src/c/fn_info.rs) Represents C Function signature - > provide CallableData and ClosureData creation + > provide `CallableData` and `ClosureData` creator - [**Struct ` CStructInfo`:**](./src/c/struct_info.rs) Represents C Struct type - [**Struct ` CTypeInfo`:**](./src/c/type_info.rs) Represents C type, extended in `/c/types` diff --git a/crates/lune-std-ffi/src/c/arr_info.rs b/crates/lune-std-ffi/src/c/arr_info.rs index 0362f264..fd76dce1 100644 --- a/crates/lune-std-ffi/src/c/arr_info.rs +++ b/crates/lune-std-ffi/src/c/arr_info.rs @@ -83,6 +83,7 @@ impl FfiSize for CArrInfo { self.size } } + impl FfiConvert for CArrInfo { unsafe fn value_into_data<'lua>( &self, diff --git a/crates/lune-std-ffi/src/c/fn_info.rs b/crates/lune-std-ffi/src/c/fn_info.rs index 21ce3f0d..eaf566e6 100644 --- a/crates/lune-std-ffi/src/c/fn_info.rs +++ b/crates/lune-std-ffi/src/c/fn_info.rs @@ -43,6 +43,7 @@ impl FfiSignedness for CFnInfo { false } } + impl FfiSize for CFnInfo { fn get_size(&self) -> usize { SIZE_OF_POINTER diff --git a/crates/lune-std-ffi/src/c/ptr_info.rs b/crates/lune-std-ffi/src/c/ptr_info.rs index 12ad7b37..f1efc9e3 100644 --- a/crates/lune-std-ffi/src/c/ptr_info.rs +++ b/crates/lune-std-ffi/src/c/ptr_info.rs @@ -25,11 +25,13 @@ impl FfiSignedness for CPtrInfo { false } } + impl FfiSize for CPtrInfo { fn get_size(&self) -> usize { SIZE_OF_POINTER } } + impl FfiConvert for CPtrInfo { // Convert luavalue into data, then write into ptr unsafe fn value_into_data<'lua>( diff --git a/crates/lune-std-ffi/src/c/struct_info.rs b/crates/lune-std-ffi/src/c/struct_info.rs index 55b8a429..f97f89a0 100644 --- a/crates/lune-std-ffi/src/c/struct_info.rs +++ b/crates/lune-std-ffi/src/c/struct_info.rs @@ -116,11 +116,13 @@ impl FfiSize for CStructInfo { self.size } } + impl FfiSignedness for CStructInfo { fn get_signedness(&self) -> bool { false } } + impl FfiConvert for CStructInfo { unsafe fn value_into_data<'lua>( &self, diff --git a/crates/lune-std-ffi/src/c/void_info.rs b/crates/lune-std-ffi/src/c/void_info.rs index 3c0ed880..9fd1968e 100644 --- a/crates/lune-std-ffi/src/c/void_info.rs +++ b/crates/lune-std-ffi/src/c/void_info.rs @@ -12,6 +12,7 @@ impl FfiSignedness for CVoidInfo { false } } + impl FfiSize for CVoidInfo { fn get_size(&self) -> usize { 0 diff --git a/crates/lune/src/tests.rs b/crates/lune/src/tests.rs index acce10ec..0599f4a3 100644 --- a/crates/lune/src/tests.rs +++ b/crates/lune/src/tests.rs @@ -31,7 +31,7 @@ macro_rules! create_tests { // The rest of the test logic can continue as normal let full_name = format!("{}/tests/{}.luau", workspace_dir.display(), $value); let script = read_to_string(&full_name).await?; - let mut lune = Runtime::new().with_args( + let mut lune = Runtime::new().set_unsafe_library_enabled(true).with_args( ARGS .clone() .iter() @@ -111,6 +111,7 @@ create_tests! { ffi_external_print_hello_world: "ffi/external_print/helloWorld", ffi_external_struct_ab: "ffi/external_struct/ab", ffi_cast: "ffi/cast", + ffi_free: "ffi/free", ffi_is_integer: "ffi/isInteger", ffi_pretty_print: "ffi/prettyPrint", ffi_read_boundary: "ffi/readBoundary", diff --git a/tests/ffi/free.luau b/tests/ffi/free.luau new file mode 100644 index 00000000..5ab91da0 --- /dev/null +++ b/tests/ffi/free.luau @@ -0,0 +1,18 @@ +--!nocheck +--!nolint +local ffi = require("@lune/ffi") + +local box = ffi.box(ffi.i32.size) +local ref = box:leak() + +box = nil + +collectgarbage("collect") +collectgarbage("collect") +collectgarbage("collect") + +ffi.free(ref) + +collectgarbage("collect") +collectgarbage("collect") +collectgarbage("collect")