Skip to content

API reference / @jsprismarine/native-library / ffi

ffi ​

Interfaces ​

BoundLibrary ​

Defined in: script-host/src/ffi.ts:60

A bound library, its callbacks, and how to take a pointer to shared memory for it.

Properties ​

symbols ​

readonly symbols: NativeSymbols

Defined in: script-host/src/ffi.ts:61

Methods ​

close() ​

close(): void

Defined in: script-host/src/ffi.ts:67

Returns ​

void

pointerOf() ​

pointerOf(memory): bigint

Defined in: script-host/src/ffi.ts:66

The address of a shared buffer's memory, which never moves while the buffer lives.

Parameters ​
memory ​

SharedArrayBuffer

Returns ​

bigint

registerHostCallback() ​

registerHostCallback(callback): bigint

Defined in: script-host/src/ffi.ts:63

Makes a native function pointer that runs callback on this thread.

Parameters ​
callback ​

HostCallback

Returns ​

bigint

unregisterHostCallback() ​

unregisterHostCallback(pointer): void

Defined in: script-host/src/ffi.ts:64

Parameters ​
pointer ​

bigint

Returns ​

void


BoundNative ​

Defined in: ffi.ts:22

A library opened with bindNative.

Type Parameters ​

Symbols ​

Symbols

Properties ​

symbols ​

readonly symbols: Symbols

Defined in: ffi.ts:24

One function per symbol; an optional symbol the library lacks is undefined.

Methods ​

close() ​

close(): void

Defined in: ffi.ts:31

Returns ​

void

pointerOf() ​

pointerOf(memory): NativePointer

Defined in: ffi.ts:30

The address of a buffer's memory. It stays valid only while the buffer is alive and is neither transferred nor resized, so keep it unchanged until native code is done with the address.

Parameters ​
memory ​

ArrayBufferLike

Returns ​

NativePointer


BunFfi ​

Defined in: script-host/src/ffi.ts:115

The part of bun:ffi this binding uses.

Properties ​

JSCallback ​

JSCallback: new (callback, definition) => object

Defined in: script-host/src/ffi.ts:121

Parameters ​
callback ​

(...args) => number

definition ​
args ​

string[]

returns ​

string

Returns ​

object

ptr ​

readonly ptr: number | null

close() ​

close(): void

Returns ​

void

Methods ​

dlopen() ​

dlopen(path, definitions): object

Defined in: script-host/src/ffi.ts:116

Parameters ​
path ​

string

definitions ​

Record<string, { args: string[]; returns: string; }>

Returns ​

object

symbols ​

symbols: Record<string, (...args) => unknown>

close() ​

close(): void

Returns ​

void

ptr() ​

ptr(view): number

Defined in: script-host/src/ffi.ts:120

Parameters ​
view ​

Uint8Array

Returns ​

number


NativeSymbols ​

Defined in: script-host/src/ffi.ts:14

The C interface, one method per exported function.

Methods ​

jsh_abi_version() ​

jsh_abi_version(): number

Defined in: script-host/src/ffi.ts:15

Returns ​

number

jsh_call() ​

jsh_call(engine, fn, thisValue, length): number

Defined in: script-host/src/ffi.ts:33

Parameters ​
engine ​

bigint

fn ​

number

thisValue ​

number

length ​

number

Returns ​

number

jsh_context_create() ​

jsh_context_create(engine): number

Defined in: script-host/src/ffi.ts:24

Parameters ​
engine ​

bigint

Returns ​

number

jsh_context_select() ​

jsh_context_select(engine, context): number

Defined in: script-host/src/ffi.ts:25

Parameters ​
engine ​

bigint

context ​

number

Returns ​

number

jsh_debug_attach() ​

jsh_debug_attach(engine, context, pollLines): number

Defined in: script-host/src/ffi.ts:42

Parameters ​
engine ​

bigint

context ​

number

pollLines ​

number

Returns ​

number

jsh_debug_describe() ​

jsh_debug_describe(engine, handle): number

Defined in: script-host/src/ffi.ts:52

Parameters ​
engine ​

bigint

handle ​

number

Returns ​

number

jsh_debug_detach() ​

jsh_debug_detach(engine): void

Defined in: script-host/src/ffi.ts:43

Parameters ​
engine ​

bigint

Returns ​

void

jsh_debug_evaluate() ​

jsh_debug_evaluate(engine, frame, source, length): number

Defined in: script-host/src/ffi.ts:51

Parameters ​
engine ​

bigint

frame ​

number

source ​

Uint8Array

length ​

number

Returns ​

number

jsh_debug_exception() ​

jsh_debug_exception(engine): number

Defined in: script-host/src/ffi.ts:50

Parameters ​
engine ​

bigint

Returns ​

number

jsh_debug_frames() ​

jsh_debug_frames(engine): number

Defined in: script-host/src/ffi.ts:48

Parameters ​
engine ​

bigint

Returns ​

number

jsh_debug_pause() ​

jsh_debug_pause(engine): void

Defined in: script-host/src/ffi.ts:46

Parameters ​
engine ​

bigint

Returns ​

void

jsh_debug_properties() ​

jsh_debug_properties(engine, handle, start, count): number

Defined in: script-host/src/ffi.ts:53

Parameters ​
engine ​

bigint

handle ​

number

start ​

number

count ​

number

Returns ​

number

jsh_debug_resume() ​

jsh_debug_resume(engine, mode): number

Defined in: script-host/src/ffi.ts:47

Parameters ​
engine ​

bigint

mode ​

number

Returns ​

number

jsh_debug_scope() ​

jsh_debug_scope(engine, frame, scope): number

Defined in: script-host/src/ffi.ts:49

Parameters ​
engine ​

bigint

frame ​

number

scope ​

number

Returns ​

number

jsh_debug_set_breakpoints() ​

jsh_debug_set_breakpoints(engine, path, length): number

Defined in: script-host/src/ffi.ts:44

Parameters ​
engine ​

bigint

path ​

string

length ​

number

Returns ​

number

jsh_debug_set_stop_on_exception() ​

jsh_debug_set_stop_on_exception(engine, stop): void

Defined in: script-host/src/ffi.ts:45

Parameters ​
engine ​

bigint

stop ​

number

Returns ​

void

jsh_decode() ​

jsh_decode(engine, length): number

Defined in: script-host/src/ffi.ts:38

Parameters ​
engine ​

bigint

length ​

number

Returns ​

number

jsh_dup() ​

jsh_dup(engine, handle): number

Defined in: script-host/src/ffi.ts:40

Parameters ​
engine ​

bigint

handle ​

number

Returns ​

number

jsh_encode() ​

jsh_encode(engine, handle): number

Defined in: script-host/src/ffi.ts:39

Parameters ​
engine ​

bigint

handle ​

number

Returns ​

number

jsh_engine_begin_tick() ​

jsh_engine_begin_tick(engine): void

Defined in: script-host/src/ffi.ts:28

Parameters ​
engine ​

bigint

Returns ​

void

jsh_engine_create() ​

jsh_engine_create(host, buffer, capacity, memoryLimit, stackSize): bigint

Defined in: script-host/src/ffi.ts:16

Parameters ​
host ​

bigint

buffer ​

bigint

capacity ​

number

memoryLimit ​

number

stackSize ​

number

Returns ​

bigint

jsh_engine_destroy() ​

jsh_engine_destroy(engine): void

Defined in: script-host/src/ffi.ts:23

Parameters ​
engine ​

bigint

Returns ​

void

jsh_engine_set_budget() ​

jsh_engine_set_budget(engine, perCallback, perTick): void

Defined in: script-host/src/ffi.ts:27

Parameters ​
engine ​

bigint

perCallback ​

number

perTick ​

number

Returns ​

void

jsh_engine_set_buffer() ​

jsh_engine_set_buffer(engine, buffer, capacity): void

Defined in: script-host/src/ffi.ts:26

Parameters ​
engine ​

bigint

buffer ​

bigint

capacity ​

number

Returns ​

void

jsh_engine_stat() ​

jsh_engine_stat(engine, stat): number

Defined in: script-host/src/ffi.ts:29

Parameters ​
engine ​

bigint

stat ​

number

Returns ​

number

jsh_eval_module() ​

jsh_eval_module(engine, name, source, length): number

Defined in: script-host/src/ffi.ts:31

Parameters ​
engine ​

bigint

name ​

string

source ​

Uint8Array

length ​

number

Returns ​

number

jsh_eval_script() ​

jsh_eval_script(engine, name, source, length): number

Defined in: script-host/src/ffi.ts:30

Parameters ​
engine ​

bigint

name ​

string

source ​

Uint8Array

length ​

number

Returns ​

number

jsh_free() ​

jsh_free(engine, handle): void

Defined in: script-host/src/ffi.ts:41

Parameters ​
engine ​

bigint

handle ​

number

Returns ​

void

jsh_get() ​

jsh_get(engine, object, key): number

Defined in: script-host/src/ffi.ts:35

Parameters ​
engine ​

bigint

object ​

number

key ​

string

Returns ​

number

jsh_global() ​

jsh_global(engine): number

Defined in: script-host/src/ffi.ts:34

Parameters ​
engine ​

bigint

Returns ​

number

jsh_host_function() ​

jsh_host_function(engine, id, name, length): number

Defined in: script-host/src/ffi.ts:37

Parameters ​
engine ​

bigint

id ​

number

name ​

string

length ​

number

Returns ​

number

jsh_run_jobs() ​

jsh_run_jobs(engine, context): number

Defined in: script-host/src/ffi.ts:32

Parameters ​
engine ​

bigint

context ​

number

Returns ​

number

jsh_set() ​

jsh_set(engine, object, key, value): number

Defined in: script-host/src/ffi.ts:36

Parameters ​
engine ​

bigint

object ​

number

key ​

string

value ​

number

Returns ​

number


NodeFfi ​

Defined in: ffi.ts:35

The part of node:ffi this binding uses; Node.js 26 ships no types for it yet.

Methods ​

dlopen() ​

dlopen(path, definitions): object

Defined in: ffi.ts:36

Parameters ​
path ​

string

definitions ​

Record<string, { arguments: readonly string[]; return: string; }>

Returns ​

object

functions ​

functions: Record<string, (...args) => unknown>

lib ​

lib: object

lib.close() ​

close(): void

Returns ​

void

lib.getFunction() ​

getFunction(name, definition): (...args) => unknown

Binds one more symbol; throws when the library does not export it.

Parameters ​
name ​

string

definition ​
arguments ​

readonly string[]

return ​

string

Returns ​

(...args) => unknown

lib.registerCallback() ​

registerCallback(signature, callback): bigint

Makes a native function pointer that runs callback on this thread.

Parameters ​
signature ​
arguments ​

readonly string[]

return ​

string

callback ​

(...args) => number

Returns ​

bigint

lib.unregisterCallback() ​

unregisterCallback(pointer): void

Parameters ​
pointer ​

bigint

Returns ​

void

getRawPointer() ​

getRawPointer(memory): bigint

Defined in: ffi.ts:56

Parameters ​
memory ​

ArrayBufferLike

Returns ​

bigint

Type Aliases ​

FfiKind ​

FfiKind = "i32" | "f64" | "void" | "pointer" | "buffer"

Defined in: ffi.ts:13

An argument or result kind. buffer passes a typed array's memory by address for the length of the call; pointer is an address the library returned.


FfiSignature ​

FfiSignature = readonly [result: FfiKind, parameters: readonly FfiKind[]]

Defined in: ffi.ts:16

One symbol's C signature: its result, then its parameters in order.


HostCallback ​

HostCallback = (op, id, length) => number

Defined in: script-host/src/ffi.ts:57

The engine's callback: an operation, an id and a byte length in, a length or a status out.

Parameters ​

op ​

number

id ​

number

length ​

number

Returns ​

number


NativePointer ​

NativePointer = bigint | number

Defined in: ffi.ts:19

A pointer the FFI hands to native code: a bigint under Node.js, a number under Bun.


NativePointer ​

NativePointer = bigint

Defined in: script-host/src/ffi.ts:11

A native address, as node:ffi passes one. The Bun binding converts its numbers to and from it.

Functions ​

bindBunLibrary() ​

bindBunLibrary(path, bun?): BoundLibrary

Defined in: script-host/src/ffi.ts:166

Opens the library with bun:ffi.

Parameters ​

path ​

string

The shared library's file.

bun? ​

BunFfi = ...

The bun:ffi module; a parameter so a test under Node.js can pass its own.

Returns ​

BoundLibrary

The bound library.


bindLibrary() ​

bindLibrary(path): BoundLibrary

Defined in: script-host/src/ffi.ts:217

Opens the library with the running runtime's FFI.

Parameters ​

path ​

string

The shared library's file.

Returns ​

BoundLibrary

The bound library.

Throws ​

When Node.js has no node:ffi.

Throws ​

When the library does not load.


bindNative() ​

bindNative<Symbols>(path, signatures, optional?): BoundNative<Symbols>

Defined in: ffi.ts:126

Opens a library with the running runtime's FFI.

Type Parameters ​

Symbols ​

Symbols

Parameters ​

path ​

string

The shared library's file.

signatures ​

{ readonly [Name in string]: FfiSignature }

Every symbol to bind, by name.

optional? ​

ReadonlySet<string> = ...

Symbols a library built from older sources may lack; each stays undefined when it is missing, where a missing required symbol fails the load.

Returns ​

BoundNative<Symbols>

The bound library.

Throws ​

When Node.js has no node:ffi.

Throws ​

When the library does not load or lacks a required symbol.


nodeFfi() ​

nodeFfi(): NodeFfi

Defined in: ffi.ts:101

Loads node:ffi without its experimental warning, which would otherwise print on every start. Only that one warning is held back. A binding that needs more than bindNative gives, such as callbacks, opens its library with this.

Returns ​

NodeFfi

The module.

Throws ​

When the runtime has no node:ffi.


nodeFfiOf() ​

nodeFfiOf(ffi, version): NodeFfi

Defined in: ffi.ts:88

The node:ffi module as this file calls it.

process.getBuiltinModule answers undefined on a Node.js built without FFI or started with --no-experimental-ffi, which would otherwise fail later at dlopen with a TypeError that names nothing.

Parameters ​

ffi ​

unknown

What the runtime answered for node:ffi.

version ​

string

The running Node.js version, for the message.

Returns ​

NodeFfi

The module.

Throws ​

When the runtime has no node:ffi.