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
This commit is contained in:
Minus committed 2026-11-16 03:42:43 +00:00
1 parent ef305e7824
commit c319a0986d
10 files changed
+687 -1

No files matched your search

+21
View File
@@ -250,6 +250,27 @@ local d = Dog("Rex") -- constrói via `new`
print(d:speak()) -- Rex barks
```
## `ffi` — chamar código nativo (estilo ctypes/CDLL)
A biblioteca `ffi` carrega um `.so`/DLL e chama símbolos por nome com
uma assinatura declarada, usando **libffi** para a chamada ABI-correta
(é o mesmo mecanismo por trás do ctypes do Python):
```lua
local m = ffi.load("libm.so.6")
local pow = m:func("pow", "double(double,double)")
print(pow(2, 10)) -- 1024
local c = ffi.load("libc.so.6")
local strlen = c:func("strlen", "long(string)")
```
Gramática da assinatura: `RET(ARG,ARG,...)`, com tipos `void`, `bool`,
`char`, `int8/uint8`, `int16/uint16`, `int/long/int32/uint32`,
`int64/uint64/longlong`, `float`, `double`, `string`, ponteiros.
Requer `-lffi -ldl` (já no Makefile).
## cpk — Cat Package Manager
O primeiro programa em catscript. Lê o `package.toml` de um pacote