Zbind generates bindings for calling Zig from TypeScript

I made tooling to easily call Zig and generally include Zig code in TypeScript projects, both native and Wasm targets. Still a lot of work left to do with marshalling different types, but the API is starting to look pretty nice:

It’s a bit similar to Zigar mentioned here earlier but much lighter, provides static typing for Zig functions on TS side and doesn’t hide the Zig build config.

7 Likes

Hey! Sorry I found this project late. I’ve been doing a parallel effort to bind zig to typescript. I took the no brain method of converting a zig type to a JSON string and doing a JSON.parse on the frontend and vice-versa to communicate - then just did a zig script to generate a .d.ts file telling Typescript what to expect out of the JSON string. It’s not a fast method by any means … the only optimization is when zig finds a type that could be represented as a TypedArray, it just passes the slice info for Typescript to find manually from the Wasm memory.

I have no idea if my work is at all integratable into yours, but here’s a file reference from my game if it’s at all interesting to you – LudumDare-Games/LudumDare55-Summoning/src/type_definitions.zig at master · Nathan-Franck/LudumDare-Games · GitHub

It would definitely be awesome if there was some way to build up an interface in Typescript for pulling data from deep struct hierarchies in zig/wasm memory without basically cloning the whole structure… But probably a lot of work.

Love the logo :slight_smile:

Thanks for the heads-up! I just updated zbind to support all Zig versions 0.11.0 - 0.12.0 as far as I can tell.

We’ll see if / when I’ll get around to adding support for reading structs directly. One annoyance is that the in-memory layout of slices is undefined. Maybe in Wasm the JS side could just read them anyway using the current layout, but in native it’d have to call a Zig function because we can’t safely hard-code a cross-platform ABI for slices if Zig doesn’t want to define one.

1 Like