__zigar.init([arg]) - chung-leong/zigar GitHub Wiki

Return a promise that resolves when WebAssembly code finishes compiling and all Zig functions can be called synchronously.

const std = @import("std");

pub fn hello() void {
    std.debug.print("hello\n", .{});
}
import { __zigar, hello } from './special-exports-example-1.zig';
const { init } = __zigar;
await init();
hello();
hello

Use of this function is only necessary when topLevelAwait is false. For node-zigar, this function is useless. It returns a promise that resolves immediately.

init() accepts WASI interface object as an a optional argument. Suppose you have transcoded a Zig file to WASM using Rollup. You can run it in Node.js in this manner:

import { WASI } from 'wasi';
import { __zigar, openDoc } from './zig-libx.js';
const { init } = __zigar;

const wasi = new WASI({
    version: 'preview1',
    args: [],
    env: {},
    preopens: {
        '/local': '/var/data',
    },
});
await init(wasi);
const doc = openDoc('/local/file.ext');

The file would have to be transpiled with topLevelAwait set to false.


Special exports: __zigar