Zigar 0.14.0: A major step forward in Zig-JavaScript interoperability

The biggest improvement in the latest version is the support for function pointers. Call marshalling is now two-way: JavaScript code can call Zig functions and Zig code can call JavaScript functions. This opens up a lot of new possibilities. Zig can now operate independently in its own thread, initiating communication with JavaScript only when the need arises. Imagine something like an in-process HTTP server.

Version 0.14.0 also brings support for promise and async generator, allowing you to perform time consuming tasks in separate threads.

Support for WebAssembly thread has been added. You can now spawn Web Workers directly from Zig using std.Thread.

Handling of allocators has improved greatly courtesy of function pointer support.

The JavaScript runtime was largely rewritten. Dead code removal is much better. Code for particular features isn’t included anymore when they aren’t actually in use.

Overall, you can do a lot more than before. I’ve added a couple new tutorials: one involving accessing a MySQL database, and the other on how to use zzz. I’m planning to add a couple more in the near future.

The project’s Github page is here. At the project website you’ll find a number of demos that run in the web browser. Have a look!

12 Likes

This is remarkable!

Which is the best example to study if I want a vanilla JavaScript application in the browser that doesn’t use any frameworks but can still marshall calls back and forth to zig code?

Cheers

1 Like

Look at the tutorial for Rollup. If you use the UMD format, the resulting JS file can be loaded using just a <script> tag.

What would be a good example of running zig code in the backend, with node / deno / bun?

The “Getting started” tutorial have separate tracks for Node.js and Bun.js. The MySQL example also runs a server. The zzz example can also be of interest.

I haven’t looked into supporting Deno. I’ll add that to the TODO list.

1 Like