Requires the
ffi permission in your
manifest:FFI module lets you load native libraries (.dll, .so) and call their
functions directly from JavaScript - no bindings, no wrappers.
Pointers are represented as BigInt values. A
0n BigInt is a null
pointer. The engine never uses raw number for addresses.Overview
Libraries & functions
Load a DLL/SO, resolve symbols, call native functions.
Structs & unions
Define C-compatible data layouts and access fields directly.
Memory
Allocate, read, write, and convert pointers.
Callbacks
Pass JavaScript functions where native code expects a function pointer.
Types
Supported type names and how to define aliases.
Resource management
Auto-cleanup scopes and disposable handles.
Libraries & functions
Load a native library, then pull functions out of it. Two styles: quickproc calls for simple signatures, or C-style func declarations when
you want readable code.
open
LoadLibraryA, on Linux/macOS dlopen.
close
close() will crash.
proc
func
proc for complex declarations.
The signature format is
returnType [abi] functionName(paramTypes...):
- Basic
- Calling convention
- Variadic
funcs
funcAsync / funcsAsync
func and funcs. The native call runs on a worker
thread and returns a Promise that resolves with the result.
Structs & unions
Define C-compatible memory layouts. Struct instances are backed by anArrayBuffer - fields are live accessors into the buffer, not copies.
Write a field and the native memory updates immediately.
struct
char[N] fields are treated as C strings - they read/write as JS
strings, auto null-terminated at N-1.- alloc + write
- from
- view
- decode
- array
- offsetOf
union
struct, but all fields share the same memory (offset 0). The
total size equals the largest field.
alloc(), from(), and view() - same as struct.
Memory
Raw memory operations. Use these when you need to go below struct-level - reading strings from pointers, allocating scratch buffers, or converting between pointer representations.alloc
ArrayBuffer of size bytes. Maximum 256 MB.
free
free). Silent no-op on null / undefined.
toPointer
readString
null if the
pointer is null. Default maxLen is 4096, capped at 64 MB.
writeString
sizeOf
errno
errno value. Check it right after a native call.
nullptr
NULL.
Callbacks
Pass a JavaScript function where native code expects a function pointer. The engine creates a native trampoline that invokes your JS function when the native side calls through the pointer.callback
- Signature style
- Explicit types
Callbacks have a re-entry depth limit of 16. If native code calls
your callback recursively beyond that, the engine returns zero/null
instead of calling JS.
freeCallback
Type system
Type names are strings used throughout the FFI API - in signatures, struct fields,proc calls, and sizeOf. The engine recognizes
C-style names, Rust-style names, and Windows typedefs.
Primitive types
Pointer types
When a function returns
string or wstring, the engine reads
the C string and gives you a JS string. When you pass a JS string
as a pointer argument, the engine auto-converts it to a temporary
null-terminated C string for the duration of the call.Composite types
Struct and union names become valid type names after definition:typedef
Resource management
Native resources (allocated memory, file handles, callbacks) don’t get garbage collected. These helpers prevent leaks.disposable
func / proc has a .disposable() method.
It returns a new function that wraps return values in a managed handle
which auto-closes inside FFI.using() scopes.
using
fn in a managed scope. Any managed handles (from .disposable())
created during the scope are automatically closed when fn returns or
throws.
- Memory safety
- Nested scopes
Out parameters
Some native APIs write results through pointer parameters (likeGetCursorPos(POINT*)). The FFI supports this with _Out_ and
_Inout_ annotations in signatures.
{ $: initialValue }:
Signature cheat sheet
A quick reference for thefunc signature format: