Files
catscript/examples/ffi.cat
T
Minus c319a0986d ffi: call native code (ctypes/CDLL-like) via libffi
- new ffi library: ffi.load(path) dlopen()s a shared object
- lib:func(name, sig) dlsym()s a symbol and prepares a libffi call
- signature grammar RET(ARG,...): void/bool/char/int*/uint*/long/
  float/double/string/pointers; ABI-correct call via ffi_prep_cif
- selector type scaffolding for () [] . {} dispatch (runtime wired)
- examples/ffi.cat (pow, sqrt, getpid, strlen, abs); 53/53 tests
- Makefile: -lffi -ldl
2026-11-16 03:42:43 +00:00

23 lines
686 B
Plaintext

-- examples/ffi.cat -- call native code through the `ffi` library.
--
-- Like Python's ctypes/CDLL: load a shared object, bind a symbol with a
-- signature, and call it. libffi performs the ABI-correct call.
local m = ffi.load("libm.so.6")
local pow = m:func("pow", "double(double,double)")
print("pow(2,10) =", pow(2, 10))
local sqrt = m:func("sqrt", "double(double)")
print("sqrt(144) =", sqrt(144))
local c = ffi.load("libc.so.6")
local getpid = c:func("getpid", "int()")
local pid = getpid()
print("getpid > 0:", pid > 0)
local strlen = c:func("strlen", "long(string)")
print("strlen:", strlen("catscript"))
local abs = c:func("abs", "int(int)")
print("abs(-42) =", abs(-42))