Skip to content

Commit

Permalink
Restruct tests/ffi and add std-ffi tests (lune-org#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwreey committed Oct 24, 2024
1 parent 00319f0 commit 90c0987
Show file tree
Hide file tree
Showing 35 changed files with 350 additions and 302 deletions.
4 changes: 3 additions & 1 deletion crates/lune-std-ffi/src/c/fn_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ impl FfiSignedness for CFnInfo {
false
}
}

impl FfiSize for CFnInfo {
fn get_size(&self) -> usize {
SIZE_OF_POINTER
Expand Down Expand Up @@ -203,6 +202,9 @@ impl CFnInfo {
}

impl LuaUserData for CFnInfo {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("size", |_, _| Ok(SIZE_OF_POINTER));
}
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
// Subtype
method_provider::provide_ptr(methods);
Expand Down
2 changes: 1 addition & 1 deletion crates/lune-std-ffi/src/c/ptr_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl CPtrInfo {

impl LuaUserData for CPtrInfo {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("size", |_, _| Ok(size_of::<usize>()));
fields.add_field_method_get("size", |_, _| Ok(SIZE_OF_POINTER));
fields.add_field_function_get("inner", |lua, this| {
let inner = association::get(lua, CPTR_INNER, this)?
.ok_or_else(|| LuaError::external("inner type not found"))?;
Expand Down
3 changes: 3 additions & 0 deletions crates/lune-std-ffi/src/c/void_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ impl CVoidInfo {
}

impl LuaUserData for CVoidInfo {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("size", |_, _| Ok(0));
}
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
method_provider::provide_to_string(methods);
method_provider::provide_ptr(methods);
Expand Down
18 changes: 18 additions & 0 deletions crates/lune/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ create_tests! {
datetime_to_universal_time: "datetime/toUniversalTime",
}

#[cfg(feature = "std-ffi")]
create_tests! {
ffi_external_closure_call_closure: "ffi/external_closure/callClosure",
ffi_external_closure_call_closure_with_pointer: "ffi/external_closure/callClosureWithPointer",
ffi_external_closure_call_hello_world: "ffi/external_closure/callHelloWorld",
ffi_external_math_add_int: "ffi/external_math/addInt",
ffi_external_math_mul_int: "ffi/external_math/mulInt",
ffi_external_pointer_pointer_read: "ffi/external_pointer/pointerRead",
ffi_external_pointer_pointer_write: "ffi/external_pointer/pointerWrite",
ffi_external_print_hello_world: "ffi/external_print/helloWorld",
ffi_external_struct_ab: "ffi/external_struct/ab",
ffi_cast: "ffi/cast",
ffi_is_integer: "ffi/isInteger",
ffi_pretty_print: "ffi/prettyPrint",
ffi_read_boundary: "ffi/readBoundary",
ffi_write_boundary: "ffi/writeBoundary",
}

#[cfg(feature = "std-fs")]
create_tests! {
fs_files: "fs/files",
Expand Down
12 changes: 0 additions & 12 deletions print-ignore-me.luau

This file was deleted.

4 changes: 2 additions & 2 deletions tests/ffi/benchmark/external_call/deno.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { libSuffix } from "../../utility/deno.ts";
import { get_clock, get_offset } from "../../utility/proc_clock/deno.ts";
import { libSuffix } from "../../utils/libSuffix.ts";
import { get_clock, get_offset } from "../../utils/proc_clock/deno.ts";

const library_file = "./tests/ffi/benchmark/external_call/lib."+libSuffix;
// @ts-ignore
Expand Down
46 changes: 19 additions & 27 deletions tests/ffi/benchmark/external_call/init.luau
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
local ffi = require("@lune/ffi")
local lib = require("../../utility/compile")("./tests/ffi/benchmark/external_call/lib.c")
local process = require("@lune/process")
local c = ffi.c
local BENCH_SCALE: number = tonumber(process.env.BENCH_SCALE) or 1000000

local proc_clock = require("../../utility/proc_clock")
local before, after = proc_clock.newBox()
local get_clock = proc_clock.getClock
-- Get clock provider
local procClock = require("../../utility/proc_clock")
local before, after = procClock.newBox()
local getClock = procClock.getClock

local testdir = "./tests/ffi/benchmark/external_call"
local compile = require("../../utility/compile")
compile(`{testdir}/lib.c`, `{testdir}/lib.so`)
local lib = ffi.open(`{testdir}/lib.so`)
local add = c.fn({ c.int, c.int }, c.int):callable(lib:find("add"))

local function bench_add(bench_scale: number)
local add_info = c.fn({ c.int, c.int }, c.int)
local a = c.int:box(0)
local delta = c.int:box(1)
local a_ref = a:ref()
local delta_ref = delta:ref()

local add_callable = add_info:callable(lib:find("add"))

local a = c.int:box(0)
local delta = c.int:box(1)

local a_ref = a:ref()
local delta_ref = delta:ref()

get_clock(before)
for i = 1, bench_scale do
add_callable(a, a_ref, delta_ref)
end
get_clock(after)
print(proc_clock.getOffset(before, after))

local result = c.int:readData(a)
assert(result == bench_scale, `bench_add failed. result expected {bench_scale}, got {result}`)
getClock(before)
for i = 1, BENCH_SCALE do
add(a, a_ref, delta_ref)
end
getClock(after)

bench_add(1000000)
print(procClock.getOffset(before, after))
local result = c.int:readData(a)
assert(result == BENCH_SCALE, `bench_add failed. result expected {BENCH_SCALE}, got {result}`)
35 changes: 16 additions & 19 deletions tests/ffi/benchmark/external_call/luajit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@
--!nocheck

local ffi = require("ffi")
local BENCH_SCALE = 1000000

local function bench_add(bench_scale)
ffi.cdef([[
int add(int a, int b);
]])
local lib = ffi.load("./tests/ffi/benchmark/external_call/lib.so")
local add = lib.add
local a = 0
ffi.cdef([[
int add(int a, int b);
]])
local lib = ffi.load("./tests/ffi/benchmark/external_call/lib.so")
local add = lib.add
local a = 0

local before = os.clock()
for i = 1, bench_scale do
a = add(a, 1)
end
local after = os.clock()

print(after - before)
assert(
a == bench_scale,
string.format("bench_add failed. result expected %d, got %d", bench_scale, a)
)
local before = os.clock()
for i = 1, BENCH_SCALE do
a = add(a, 1)
end
local after = os.clock()

bench_add(1000000)
print(after - before)
assert(
a == BENCH_SCALE,
string.format("bench_add failed. result expected %d, got %d", BENCH_SCALE, a)
)
14 changes: 14 additions & 0 deletions tests/ffi/external_closure/callClosure.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local callableWrapper = require("../utils/callableWrapper")
local ffi = require("@lune/ffi")
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)
c.int:writeData(ret, c.int:readData(a) + c.int:readData(b))
end)

local callClosure = callableWrapper(lib:find("call_closure"), { closureInfo }, c.int)
local result = callClosure(closure:ref())
assert(result == 72, `callClosure failed. result expected 20000, got {result}`)
15 changes: 15 additions & 0 deletions tests/ffi/external_closure/callClosureWithPointer.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local callableWrapper = require("../utils/callableWrapper")
local ffi = require("@lune/ffi")
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 callClosureWithPointer =
callableWrapper(lib:find("call_closure_with_pointer"), { closureWithPointerInfo }, c.int)
local result = callClosureWithPointer(closureWithPointer:ref())
assert(result == 72, `closureWithPointer failed. result expected 20000, got {result}`)
12 changes: 12 additions & 0 deletions tests/ffi/external_closure/callHelloWorld.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local ffi = require("@lune/ffi")
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()
print("Hello world in lua closure!")
end)

local callHelloWorld = c.fn({ helloWorldInfo }, c.void):callable(lib:find("call_hello_world"))
callHelloWorld(nil, helloWorld:ref())
52 changes: 0 additions & 52 deletions tests/ffi/external_closure/init.luau

This file was deleted.

9 changes: 9 additions & 0 deletions tests/ffi/external_math/addInt.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local callableWrapper = require("../utils/callableWrapper")
local ffi = require("@lune/ffi")
local lib = require("../utils/compile")("./tests/ffi/external_math/lib.c")
local c = ffi.c

local addInt = callableWrapper(lib:find("add_int"), { c.int, c.int }, c.int)
local result = addInt(100, 200)

assert(result == 300, `test_addInt failed. result expected 300, got {result}`)
37 changes: 0 additions & 37 deletions tests/ffi/external_math/init.luau

This file was deleted.

8 changes: 8 additions & 0 deletions tests/ffi/external_math/mulInt.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local callableWrapper = require("../utils/callableWrapper")
local ffi = require("@lune/ffi")
local lib = require("../utils/compile")("./tests/ffi/external_math/lib.c")
local c = ffi.c

local mulInt = callableWrapper(lib:find("mul_int"), { c.int, c.int }, c.int)
local result = mulInt(100, 200)
assert(result == 20000, `test_mulInt failed. result expected 20000, got {result}`)
20 changes: 0 additions & 20 deletions tests/ffi/external_pointer/init.luau

This file was deleted.

4 changes: 4 additions & 0 deletions tests/ffi/external_pointer/lib.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
void pointer_write(int *a) {
*a = 123;
}

int pointer_read(int *a) {
return *a;
}
8 changes: 8 additions & 0 deletions tests/ffi/external_pointer/pointerRead.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local callableWrapper = require("../utils/callableWrapper")
local ffi = require("@lune/ffi")
local lib = require("../utils/compile")("./tests/ffi/external_pointer/lib.c")
local c = ffi.c

local pointerRead = callableWrapper(lib:find("pointer_read"), { c.int:ptr() }, c.int)
local result = pointerRead(c.int:box(123):ref():ref())
assert(result == 123, `pointerRead failed. result expected 123, got {result}`)
9 changes: 9 additions & 0 deletions tests/ffi/external_pointer/pointerWrite.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local ffi = require("@lune/ffi")
local lib = require("../utils/compile")("./tests/ffi/external_pointer/lib.c")
local c = ffi.c

local pointerWrite = c.fn({ c.int:ptr() }, c.void):callable(lib:find("pointer_write"))
local aBox = ffi.box(c.int.size)
pointerWrite(nil, aBox:ref():ref())
local result = c.int:readData(aBox)
assert(result == 123, `pointerWrite failed. result expected 123, got {result}`)
5 changes: 5 additions & 0 deletions tests/ffi/external_print/helloWorld.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local ffi = require("@lune/ffi")
local lib = require("../utils/compile")("./tests/ffi/external_print/lib.c")
local c = ffi.c

c.fn({}, c.void):callable(lib:find("hello_world"))(nil)
Loading

0 comments on commit 90c0987

Please sign in to comment.