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.
Working with Strings and Objects
Section titled “Working with Strings and Objects”WebAssembly operates natively with numbers. To manage text data and structured payloads, the ZigBindRegistry embeds helper wrappers around TextEncoder and TextDecoder instances.
Passing and Extracting Strings
Section titled “Passing and Extracting Strings”The registry offers utility tools to write and read character arrays directly to and from linear memory:
// 1. Write text content into the WASM arenaconst { ptr, len } = registry.writeString("Hello from Javascript!");
// 2. Call a Zig export expecting raw pointer boundariesconst processText = registry.bind("process_text");processText(ptr, len);
// 3. Extract text responses out of WASM memory via pointer coordinatesconst outputString = registry.readString(ptr, len);JSON Data Serialization
Section titled “JSON Data Serialization”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 layerregistry.bind("parse_config")(ptr, len);Arity Inferencing & Smart Mapping
Section titled “Arity Inferencing & Smart Mapping”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
ZigVectorinstance. The registry detects the mismatch and appends.lengthautomatically. - Implicit Unwrapping: If primitives or standard parameters are passed alongside structured vectors,
zig-bindisolates and feeds raw.ptrreferences to the WebAssembly instruction pipeline dynamically.
Struct and Tuple Target Mappings
Section titled “Struct and Tuple Target Mappings”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 Footprint | Generated 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) |
Multi-Threaded Worker Pools
Section titled “Multi-Threaded Worker Pools”For multi-threaded executions, zig-bind provides thread-safe primitives, thread synchronization channels, and worker engines built directly on Shared WebAssembly Memory.