1 # ffi - LibFFI-based FFI Module for ucode 2 3 A Foreign Function Interface (FFI) module for the ucode scripting language, 4 providing seamless integration with C libraries through a LuaJIT-inspired API. 5 6 ## Features 7 8 - **C Declaration Parsing** - Parse C type declarations at runtime 9 - **LibFFI Integration** - Call C functions using libffi for all platform ABI handling 10 - **C Data Objects** - Create and manipulate C structs, arrays, and primitives 11 - **Function Wrapping** - Wrap C function pointers into callable ucode functions 12 - **Type Information** - Query sizeof, alignof, and offsetof at runtime 13 14 ## Usage 15 16 ```javascript 17 import * as ffi from 'ffi'; 18 19 // Declare C types and functions 20 ffi.cdef(` 21 size_t strlen(const char *); 22 int strcmp(const char *, const char *); 23 `); 24 25 // Wrap and call C functions 26 let strlen = ffi.C.wrap('size_t strlen(const char *)'); 27 let result = strlen("hello").get(); // => 5 28 29 // Create C data instances 30 ffi.cdef('struct point { int x; int y; };'); 31 let p = ffi.ctype('struct point', 10, 20); 32 print(p.get('x'), p.get('y')); // => 10 20 33 34 // Query type information 35 print(ffi.sizeof('int')); // => 4 36 print(ffi.alignof('double')); // => 8 37 38 // Load external libraries 39 let libc = ffi.dlopen('c'); 40 let atoi = libc.wrap('int atoi(const char *)'); 41 print(atoi("42").get()); // => 42 42 ``` 43 44 ## API Reference 45 46 See the inline documentation in [ffi.c](ffi.c) for the complete API reference. 47 48 ## Attribution 49 50 This project contains code derived from **LuaJIT**, created by Mike Pall. 51 The FFI implementation is adapted from LuaJIT's FFI system. 52 53 ### Copyright Notice 54 55 - **LuaJIT FFI Code**: Copyright (C) 2005-2025 Mike Pall 56 - **ucode Integration**: Copyright (C) 2023-2026 Jo-Philipp Wich 57 58 This project is released under the ISC License. See [LICENSE](../../LICENSE) for details. 59 60 For complete attribution information, see: 61 - [NOTICE](NOTICE) - Third-party dependencies and licensing 62 - [ATTRIBUTION.md](ATTRIBUTION.md) - Detailed attribution and modifications 63 64 ### Key Modifications from LuaJIT 65 66 - LuaJIT's own call infrastructure replaced with **libffi exclusively** 67 - Adapted VM interactions to use **ucode's API** instead of LuaJIT's 68 - Removed JIT-specific code and dependencies 69 - Consolidated library loading logic 70 71 This project adapted the C parser concept from LuaJIT FFI but does not 72 aim to provide native machine code generation or JIT compilation. It is 73 essentially libffi with the C parser functionality bolted on top for a 74 seamless usage experience. 75 76 ## Resources 77 78 - ucode Homepage: https://ucode.mein.io/ 79 - ucode Repository: https://github.com/jow-/ucode 80 - LuaJIT: https://luajit.org/ 81 - libffi: https://github.com/libffi/libffi
This page was automatically generated by LXR 0.3.1. • OpenWrt