Skip to content

IO redirection

Chung Leong edited this page Jul 16, 2026 · 2 revisions

When Zig code is compiled for native execution, hooks are install on the system functions listed below. Upon being called, a hook checks if the given file descriptor/handle points to a virtual file. If so, the call is redirected to JavaScript for handling. Otherwise it gets passed to the original function.

POSIX functions

Lib C functions:

MSVC-specific functions

Windows API functions

When the write target is stdout or stderr, output is redirect to the JavaScript console. You can use __zigar.redirect() to redirect it to a stream of your choice.

By default, redirection only occurs for the code within the module's binary. Calls made by shared libraries that the module links to are unaffected. You have to use zigar.io.redirect() to manually activate redirection for individual libraries. This can be done safely only when library isn't actually shared.

When compiled for WebAssembly, output is sent to JavaScript through WASI.

Clone this wiki locally