ffi: add LibFFI-based foreign function interface module#398
Open
jow- wants to merge 6 commits into
Open
Conversation
Contributor
|
I get the following after Without libffi it's the same as CI: |
fc5d64b to
7ab02ac
Compare
Owner
Author
|
After some further iterations it should work now, at least CI is green and a couple of smoke tests work as well. |
Contributor
|
Builds OK now on macos - not done any testing of ffi, however. |
5ee6a08 to
d280697
Compare
Add an `unused` macro expanding to `__attribute__((unused))` to simplify marking unused variables or function parameters. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
In order to ensure reliable freeing of resource type prototypes in complex prototype chains with per-resource prototypes, register the created type prototype object with the VM value pool. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Add support for storing an optional per-instance prototype slot in extended resource values. This allows individual resource instances to override the type-level prototype with their own prototype object. Changes: - Add hasproto flag to uc_resource_ext_t to track presence of instance proto - Add ucv_resource_new_prototyped() constructor for resources with instance proto - Add ucv_resource_hasproto(), ucv_resource_proto_get(), ucv_resource_proto_set() helpers for managing instance prototypes - Add ucv_resource_create_prototyped() convenience wrapper - Update GC mark and cleanup to handle instance prototype slot - Update ucv_prototype_get/set() to check for instance prototype before falling back to type-level prototype When an instance prototype is provided, it is chained to the type's prototype, ensuring proper prototype chain lookup. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Introduce the ffi module, providing seamless integration with C libraries
through a LuaJIT-inspired API. The module combines a C declaration parser
with libffi-based function calling for ucode/C interop.
Features:
- C declaration parsing via ffi.cdef()
- C data object creation (structs, arrays, primitives) via ffi.ctype()
- LibFFI-based function calling with full ABI handling
- Function wrapping via ffi.C.wrap() for callable C functions
- Dynamic library loading via ffi.dlopen() and ffi.C.dlsym()
- Type information queries (sizeof, alignof, offsetof)
- Pointer operations (ptr, deref, get, set, slice)
- String conversion helpers (ffi.string, cdata.slice)
Implementation:
- C parser adapted from LuaJIT FFI (lj_cparse.c -> uc_cparse.c)
- Type system and conversions ported to ucode VM conventions
- LibFFI used exclusively for all call/closure operations
- Resource types: "ffi.ctype" and "ffi.clib"
- Full JSDoc documentation for API surface
Tests:
- Full unit tests in tests/ffi/ covering all API functions
- Callback support tested with qsort (int/string/struct arrays)
- Complex nested structs (sockaddr_in, in_addr) tested
- Path-based access and dict initialization tested
- Unified test runner (run_tests.sh) for all tests
Usage:
import * as ffi from 'ffi';
ffi.cdef('size_t strlen(const char *)');
let strlen = ffi.C.wrap('size_t strlen(const char *)');
let len = strlen('hello').get(); // => 5
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce the ffi module, providing seamless integration with C libraries through a LuaJIT-inspired API. The module combines a C declaration parser with libffi-based function calling for ucode/C interop.
Features:
Implementation:
Tests:
Usage:
import * as ffi from 'ffi';
ffi.cdef('size_t strlen(const char *)');
let strlen = ffi.C.wrap('size_t strlen(const char *)');
let len = strlen('hello').get(); // => 5