Skip to content

Binding Advanced Signatures

While executing simple numeric buffers is direct, production pipelines often require string interfaces, structured objects, specialized tuple mappings, or massive multi-threaded parallel computation.

WebAssembly operates natively with numbers. To manage text data and structured payloads, the ZigBindRegistry embeds helper wrappers around TextEncoder and TextDecoder instances.

The registry offers utility tools to write and read character arrays directly to and from linear memory:

// 1. Write text content into the WASM arena
const { ptr, len } = registry.writeString("Hello from Javascript!");
// 2. Call a Zig export expecting raw pointer boundaries
const processText = registry.bind("process_text");
processText(ptr, len);
// 3. Extract text responses out of WASM memory via pointer coordinates
const outputString = registry.readString(ptr, len);

For complex data models, use writeObject to quickly stringify structural trees over memory boundaries:

const payload = { id: 42, active: true, matrix: [1, 2, 3] };
const { ptr, len } = registry.writeObject(payload);
// Pass ptr and len into a Zig JSON parser layer
registry.bind("parse_config")(ptr, len);

The function binder (registry.bind) features a built-in arity analyzer. When binding functions, it maps array descriptors and automates argument injection:

  • Automated Length Splicing: If a Zig function signature expects a pointer followed by its element length, you only need to pass the ZigVector instance. The registry detects the mismatch and appends .length automatically.
  • Implicit Unwrapping: If primitives or standard parameters are passed alongside structured vectors, zig-bind isolates and feeds raw .ptr references to the WebAssembly instruction pipeline dynamically.

The CLI parser parses specialized function signatures and converts unique compound data definitions into explicit TypeScript shapes.

The compiler maps spatial objects and geometry structures to predictable runtime arrays:

Zig Structural FootprintGenerated TypeScript Typings
Vec2, Vec2Result, or struct{f64,f64}[number, number] (2D Tuple Array)
Vec3, Vec3Result, or struct{f64,f64,f64}[number, number, number] (3D Tuple Array)
Pointers ([*]f32, *u32)number (Memory Offset Pointers)

For multi-threaded executions, zig-bind provides thread-safe primitives, thread synchronization channels, and worker engines built directly on Shared WebAssembly Memory.