Files
catscript/tests/ffi.cat
T
Minus 4dae4ee1cd ffi: canonical static access ffi::load (module namespace)
- '::' accesses a static/class member (here the module namespace ffi),
  '.'/':' sends to an instance.  ffi is a module, so ffi::load is the
  canonical form; the native now accepts the receiver passed by '::'.
- example, tests and README use ffi::load; '.load' still works (compat).
- 69/69 tests
2026-11-16 03:44:59 +00:00

51 lines
1.5 KiB
Plaintext

-- tests/ffi.cat -- edge cases for the `ffi` library.
--
-- Deterministic output (no getpid): loads, symbol binding, signatures,
-- return conversions, and the nil-on-error contract.
local m = ffi::load("libm.so.6")
local c = ffi::load("libc.so.6")
-- load failure / bad library -> nil
local bad = ffi::load("no_such_library_xyz.so.999")
print("bad load nil:", bad == nil)
print("bad load type:", type(bad))
-- symbol binding
local pow = m:func("pow", "double(double,double)")
print("pow bound:", pow ~= nil)
print("missing sym nil:", m:func("no_such_symbol_xyz", "int()") == nil)
-- invalid signature -> nil
print("bad sig nil:", m:func("pow", "not a signature") == nil)
print("bad rettype nil:", m:func("pow", "nope(double)") == nil)
-- return conversions
print("pow 2,10:", pow(2, 10))
print("pow 3,4:", pow(3, 4))
local floor = m:func("floor", "double(double)")
print("floor 3.7:", floor(3.7))
local sqrt = m:func("sqrt", "double(double)")
print("sqrt 144:", sqrt(144))
-- int return
local abs = c:func("abs", "int(int)")
print("abs -42:", abs(-42))
-- long + string arg
local strlen = c:func("strlen", "long(string)")
print("strlen abc:", strlen("abc"))
print("strlen empty:", strlen(""))
-- void return -> nil
local srand = c:func("srand", "void(uint32)")
print("void ret nil:", srand(1) == nil)
-- pointer-ish spelling accepted
local strchr = c:func("strchr", "string(string,int)")
print("strchr bound:", strchr ~= nil)
-- library is callable like :func
local cosf = m("cos", "double(double)")
print("cos 0:", cosf(0))