Janet 1.28.0-358f5a0 Documentation
(Other Versions:
1.27.0
1.26.0
1.25.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.0
1.19.0
1.18.1
1.17.1
1.16.1
1.15.0
1.13.1
1.12.2
1.11.1
1.10.1
1.9.1
1.8.1
1.7.0
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
)
FFI Module
Janet's Foreign Function Interface module is used to interface with native code in a way that does not require compiling native "glue" code. The tradeoff is the FFI module is "unsafe" - there is no guarantee or even much protection from crashing your program or triggering nonsensical behavior. This departs from Janet's usual memory safe programming model.
Index
ffi/align ffi/call ffi/close ffi/context ffi/defbind ffi/free ffi/jitfn ffi/lookup ffi/malloc ffi/native ffi/pointer-buffer ffi/read ffi/signature ffi/size ffi/struct ffi/trampoline ffi/write
ffi/align cfunction source
(ffi/align type) Get the align of an ffi type in bytes.Community Examples
ffi/call cfunction source
(ffi/call pointer signature & args) Call a raw pointer as a function pointer. The function signature specifies how Janet values in `args` are converted to native machine types.Community Examples
ffi/close cfunction source
(ffi/close native) Free a native object. Dereferencing pointers to symbols in the object will have undefined behavior after freeing.Community Examples
ffi/context function source
(ffi/context &opt native-path &named map-symbols lazy) Set the path of the dynamic library to implictly bind, as well as other global state for ease of creating native bindings.Community Examples
ffi/defbind macro source
(ffi/defbind name ret-type & body) Generate bindings for native functions in a convenient manner.Community Examples
ffi/free cfunction source
(ffi/free pointer) Free memory allocated with `ffi/malloc`. Returns nil.Community Examples
ffi/jitfn cfunction source
(ffi/jitfn bytes) Create an abstract type that can be used as the pointer argument to `ffi/call`. The content of `bytes` is architecture specific machine code that will be copied into executable memory.Community Examples
ffi/lookup cfunction source
(ffi/lookup native symbol-name) Lookup a symbol from a native object. All symbol lookups will return a raw pointer if the symbol is found, else nil.Community Examples
ffi/malloc cfunction source
(ffi/malloc size) Allocates memory directly using the janet memory allocator. Memory allocated in this way must be freed manually! Returns a raw pointer, or nil if size = 0.Community Examples
ffi/native cfunction source
(ffi/native &opt path) Load a shared object or dll from the given path, and do not extract or run any code from it. This is different than `native`, which will run initialization code to get a module table. If `path` is nil, opens the current running binary. Returns a `core/native`.Community Examples
ffi/pointer-buffer cfunction source
(ffi/pointer-buffer pointer capacity &opt count offset) Create a buffer from a pointer. The underlying memory of the buffer will not be reallocated or freed by the garbage collector, allowing unmanaged, mutable memory to be manipulated with buffer functions. Attempts to resize or extend the buffer beyond its initial capacity will raise an error. As with many FFI functions, this is memory unsafe and can potentially allow out of bounds memory access. Returns a new buffer.Community Examples
ffi/read cfunction source
(ffi/read ffi-type bytes &opt offset) Parse a native struct out of a buffer and convert it to normal Janet data structures. This function is the inverse of `ffi/write`. `bytes` can also be a raw pointer, although this is unsafe.Community Examples
ffi/signature cfunction source
(ffi/signature calling-convention ret-type & arg-types) Create a function signature object that can be used to make calls with raw function pointers.Community Examples
ffi/struct cfunction source
(ffi/struct & types) Create a struct type definition that can be used to pass structs into native functions.Community Examples
ffi/trampoline cfunction source
(ffi/trampoline cc) Get a native function pointer that can be used as a callback and passed to C libraries. This callback trampoline has the signature `void trampoline(void \*ctx, void \*userdata)` in the given calling convention. This is the only function signature supported. It is up to the programmer to ensure that the `userdata` argument contains a janet function the will be called with one argument, `ctx` which is an opaque pointer. This pointer can be further inspected with `ffi/read`.Community Examples
ffi/write cfunction source
(ffi/write ffi-type data &opt buffer index) Append a native type to a buffer such as it would appear in memory. This can be used to pass pointers to structs in the ffi, or send C/C++/native structs over the network or to files. Returns a modifed buffer or a new buffer if one is not supplied.Community Examples