Skip to content

Commit

Permalink
Add free test (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwreey committed Nov 9, 2024
1 parent c457737 commit 809fd56
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 3 deletions.
3 changes: 1 addition & 2 deletions crates/lune-std-ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ See [tests/ffi](../../tests/ffi/README.md)
- Add varargs support
- Array argument in cfn
- Ref boundary fix

## Code structure

Expand All @@ -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<T>`:**](./src/c/type_info.rs) Represents C type, extended in `/c/types`

Expand Down
1 change: 1 addition & 0 deletions crates/lune-std-ffi/src/c/arr_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl FfiSize for CArrInfo {
self.size
}
}

impl FfiConvert for CArrInfo {
unsafe fn value_into_data<'lua>(
&self,
Expand Down
1 change: 1 addition & 0 deletions crates/lune-std-ffi/src/c/fn_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl FfiSignedness for CFnInfo {
false
}
}

impl FfiSize for CFnInfo {
fn get_size(&self) -> usize {
SIZE_OF_POINTER
Expand Down
2 changes: 2 additions & 0 deletions crates/lune-std-ffi/src/c/ptr_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
2 changes: 2 additions & 0 deletions crates/lune-std-ffi/src/c/struct_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/lune-std-ffi/src/c/void_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ impl FfiSignedness for CVoidInfo {
false
}
}

impl FfiSize for CVoidInfo {
fn get_size(&self) -> usize {
0
Expand Down
3 changes: 2 additions & 1 deletion crates/lune/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions tests/ffi/free.luau
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 809fd56

Please sign in to comment.