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

+67
View File
@@ -0,0 +1,67 @@
/*
* cat/types/ffi.h -- the `ffi` library: call native code (ctypes/CDLL-like).
*
* Naming rule: symbols here are cat_types_ffi_* / CAT_TYPES_FFI_*.
*
* catscript can load a shared library (dlopen) and call its symbols with
* a declared signature, using libffi to perform the ABI-correct call.
*
* local lib = ffi.load("libm.so.6")
* local pow = lib:func("pow", "double(double,double)")
* print(pow(2, 10)) -- 1024
*
* local c = ffi.load("libc.so.6")
* local pid = c:func("getpid", "int()")
*
* Signature grammar: "RET(ARG,ARG,...)" where each type is one of
* void, bool, char, int8/uint8, int16/uint16, int/long/int32/uint32,
* int64/uint64/longlong, float, double, string, pointer spellings, or
* any base name suffixed with a star.
*/
#ifndef CAT_TYPES_FFI_H
#define CAT_TYPES_FFI_H
#include "cat/value.h"
/* CAT_TYPES_FFI_Library -- a dlopen() handle as a first-class value.
* `lib:func(name, sig)` / `lib[name]` resolve a symbol. */
typedef struct CAT_TYPES_FFI_Library {
CAT_VALUE_Value base;
void *handle;
char *path;
} CAT_TYPES_FFI_Library;
/* CAT_TYPES_FFI_Function -- a resolved symbol + prepared libffi call. */
typedef struct CAT_TYPES_FFI_Function {
CAT_VALUE_Value base;
void *sym; /* the dlsym pointer */
char *name;
char *sig; /* original signature text */
void *cif; /* ffi_cif * (kept opaque here) */
void *argtypes; /* ffi_type ** */
int nargs;
void *rtype; /* ffi_type * */
int ret_kind; /* CAT_TYPES_FFI_RET_* */
} CAT_TYPES_FFI_Function;
enum {
CAT_TYPES_FFI_RET_VOID,
CAT_TYPES_FFI_RET_INT,
CAT_TYPES_FFI_RET_DOUBLE,
CAT_TYPES_FFI_RET_STRING
};
/* Load a shared library by path; returns a Library value (nil on error). */
CAT_VALUE_Value *cat_types_ffi_load(const char *path);
/* Bind a symbol with a signature; returns a Function value (nil on error). */
CAT_VALUE_Value *cat_types_ffi_bind(CAT_VALUE_Value *lib, const char *name,
const char *sig);
/* Build the `ffi` module table (with `load`). */
CAT_VALUE_Value *cat_types_ffi_module(void);
CAT_VALUE_Vtable *cat_types_ffi_library_vtable(void);
CAT_VALUE_Vtable *cat_types_ffi_function_vtable(void);
#endif /* CAT_TYPES_FFI_H */
+49
View File
@@ -0,0 +1,49 @@
/*
* cat/types/selector.h -- syntax selectors.
*
* Naming rule: symbols here are cat_types_selector_* / CAT_TYPES_SELECTOR_*.
*
* catscript has no special-case operators for member access. Instead the
* compiler lowers surface syntax to ordinary method sends against a small
* set of well-known selector names:
*
* obj(a, b) -> obj:_parenthesis_(a, b)
* obj[i] -> obj:_squarebrackets_(i)
* obj[i] = v -> obj:_squarebrackets_set_(i, v)
* obj.k -> obj:_dot_("k")
* obj.k = v -> obj:_dot_set_("k", v)
* { ... } -> _curlybrackets_ hook on the new object
*
* Every object bound to these names inherits a default implementation
* (a `selector` value) that performs the raw, built-in behaviour. A type
* can override any selector by defining a method of that name, so new
* indexing/attribute semantics need no compiler change at all.
*/
#ifndef CAT_TYPES_SELECTOR_H
#define CAT_TYPES_SELECTOR_H
#include "cat/value.h"
/* The selector kinds. */
enum {
CAT_TYPES_SELECTOR_DOT = 1, /* obj._dot_(key) */
CAT_TYPES_SELECTOR_DOT_SET, /* obj._dot_set_(key, value) */
CAT_TYPES_SELECTOR_SQ, /* obj._squarebrackets_(idx) */
CAT_TYPES_SELECTOR_SQ_SET, /* obj._squarebrackets_set_(idx, v) */
CAT_TYPES_SELECTOR_PAREN, /* obj._parenthesis_(args...) */
CAT_TYPES_SELECTOR_CURLY /* obj._curlybrackets_() */
};
/* CAT_TYPES_SELECTOR_Selector -- a bound default selector method. */
typedef struct CAT_TYPES_SELECTOR_Selector {
CAT_VALUE_Value base;
int kind;
} CAT_TYPES_SELECTOR_Selector;
/* Return a fresh selector value for a selector name, or NULL if `name`
* is not one of the reserved selector names. */
CAT_VALUE_Value *cat_types_selector_lookup(const char *name);
CAT_VALUE_Vtable *cat_types_selector_vtable(void);
#endif /* CAT_TYPES_SELECTOR_H */