Skip to content

Commit c656a48

Browse files
committed
Add tests for ffi (#243)
1 parent e23aaef commit c656a48

15 files changed

+94
-42
lines changed

crates/lune-std-ffi/src/ffi/ffi_ref/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl FfiRefFlag {
2222
pub struct FfiRefFlagList(u8);
2323
#[allow(unused)]
2424
impl FfiRefFlagList {
25-
pub fn zero() -> Self {
25+
pub const fn zero() -> Self {
2626
Self(0)
2727
}
2828
pub const fn new(flags: u8) -> Self {

crates/lune-std-ffi/src/ffi/ffi_ref/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub use self::{
1616
flags::{FfiRefFlag, FfiRefFlagList},
1717
};
1818

19+
// Box:ref():ref() should not be able to modify, Only for external
20+
const BOX_REF_REF_FLAGS: FfiRefFlagList = FfiRefFlagList::zero();
21+
1922
// A referenced space. It is possible to read and write through types.
2023
// This operation is not safe. This may cause a memory error in Lua
2124
// if use it incorrectly.
@@ -46,15 +49,10 @@ impl FfiRef {
4649
this: LuaAnyUserData<'lua>,
4750
) -> LuaResult<LuaAnyUserData<'lua>> {
4851
let target = this.borrow::<FfiRef>()?;
49-
let mut flags = target.flags.clone();
50-
51-
// FIXME:
52-
// We cannot dereference ref which created by lua, in lua
53-
flags.set_dereferenceable(false);
5452

5553
let luaref = lua.create_userdata(FfiRef::new(
5654
ptr::from_ref(&target.ptr) as *mut (),
57-
flags,
55+
BOX_REF_REF_FLAGS,
5856
FfiRefBounds {
5957
below: 0,
6058
above: size_of::<usize>(),

tests/ffi/box-recursion-gc.luau

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--!nocheck
2+
--!nolint
3+
4+
local ffi = require("@lune/ffi")
5+
6+
local box = ffi.box(ffi.u8:ptr().size)
7+
local ref = box:ref()
8+
ffi.u8:ptr():into(box, ref)
9+
10+
local wt = setmetatable({}, { __mode = "v" })
11+
12+
wt[1] = box
13+
wt[2] = ref
14+
15+
box = nil
16+
ref = nil
17+
18+
collectgarbage("collect")
19+
20+
assert(wt[1] == nil and wt[2] == nil, "Box - ref recursion GC test failed")

tests/ffi/cast.luau

Whitespace-only changes.

tests/ffi/external.luau

Whitespace-only changes.

tests/ffi/from-boundary.luau

Whitespace-only changes.

tests/ffi/into-boundary.luau

Whitespace-only changes.

tests/ffi/isInteger.luau

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--!nocheck
2+
--!nolint
3+
4+
local ffi = require("@lune/ffi")
5+
6+
local int = 0b1
7+
local float = 0.5
8+
9+
assert(ffi.isInteger(int) == true, "ffi.isInteger(int) == true failed")
10+
assert(ffi.isInteger(float) == false, "ffi.isInteger(float) == false failed")

tests/ffi/ptr.luau

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/ffi/ref-hold-box.luau

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--!nocheck
2+
--!nolint
3+
4+
local ffi = require("@lune/ffi")
5+
local box = ffi.box(ffi.i32.size)
6+
local ref = box:ref()
7+
8+
local wt = setmetatable({}, { __mode = "v" })
9+
10+
wt[1] = box
11+
box = nll
12+
13+
collectgarbage("collect")
14+
15+
assert(wt[1] ~= nil, "ref hold box failed")

tests/ffi/struct.luau

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/ffi/types/arr.luau

Whitespace-only changes.

tests/ffi/types/fn.luau

Whitespace-only changes.

tests/ffi/types/ptr.luau

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--!nocheck
2+
--!nolint
3+
4+
local ffi = require("@lune/ffi")
5+
6+
-- ptr size test
7+
assert(
8+
ffi.i32:ptr().size == ffi.i64:ptr().size,
9+
"All of Ptr.size must be same.\n" .. "ffi.i32:ptr().size == ffi.i64:ptr().size failed"
10+
)
11+
12+
-- inner test
13+
local i32ptr = ffi.i32:ptr()
14+
assert(
15+
rawequal(ffi.i32, i32ptr.inner),
16+
"Ptr.inner must be same with their parent\n" .. "raweq ffi.i32 == ffi.i32:ptr().inner failed"
17+
)
18+
assert(
19+
rawequal(i32ptr, i32ptr:ptr().inner),
20+
"Ptr.inner must be same with their parent\n" .. "raweq i32ptr == i32ptr:ptr().inner failed"
21+
)
22+
assert(
23+
rawequal(i32ptr, i32ptr:ptr().inner:ptr().inner:ptr().inner),
24+
"Ptr.inner must be same with their parent\n"
25+
.. "raweq i32ptr == i32ptr:ptr().inner:ptr().inner:ptr().inner failed"
26+
)
27+
28+
-- deep ptr test
29+
local ok, err = pcall(function()
30+
i32ptr:ptr():ptr():ptr():ptr():ptr():ptr():ptr()
31+
end)
32+
assert(ok, `Deep ptr test failed.\n{err}`)

tests/ffi/types/struct.luau

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--!nocheck
2+
--!nolint
3+
4+
local ffi = require("@lune/ffi")
5+
6+
local i32ptr = ffi.i32:ptr()
7+
local struct = ffi.struct({ i32ptr, ffi.i32 })
8+
9+
assert(rawequal(struct:field(0), i32ptr), "Struct get field failed")
10+
assert(rawequal(struct:field(1), ffi.i32), "Struct get field failed")
11+
12+
-- offset(2) should fail

0 commit comments

Comments
 (0)