Janet 1.27.0-01aab66 Documentation
(Other Versions: 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

cfunction (ffi/align type)
Get the align of an ffi type in bytes.
Community Examples / source
cfunction (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 / source
cfunction (ffi/close native)
Free a native object. Dereferencing pointers to symbols in the object will have undefined behavior after freeing.
Community Examples / source
function (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 / source
macro (ffi/defbind name ret-type & body)
Generate bindings for native functions in a convenient manner.
Community Examples / source
cfunction (ffi/free pointer)
Free memory allocated with `ffi/malloc`. Returns nil.
Community Examples / source
cfunction (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 / source
cfunction (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 / source
cfunction (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 / source
cfunction (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 / source
cfunction (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 / source
cfunction (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 / source
cfunction (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 / source
cfunction (ffi/size type)
Get the size of an ffi type in bytes.
Community Examples / source
cfunction (ffi/struct & types)
Create a struct type definition that can be used to pass structs into native functions. 
Community Examples / source
cfunction (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 / source
cfunction (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 / source