• Stars
    star
    286
  • Rank 144,690 (Top 3 %)
  • Language
    Zig
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Zig WebAssembly Runtime Engine

zware

WebAssembly logo: a purple-blue square containing the uppercase letters W A. The square has a semicirclular notch on the top edge in the middle
Zig WebAssembly Runtime Engine

About

zware is a library for executing WebAssembly embedded in Zig programs.

Example

From examples/fib:

const std = @import("std");
const zware = @import("zware");
const Store = zware.Store;
const Module = zware.Module;
const Instance = zware.Instance;
const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator;
var gpa = GeneralPurposeAllocator(.{}){};

pub fn main() !void {
    defer _ = gpa.deinit();
    const alloc = gpa.allocator();

    const bytes = @embedFile("fib.wasm");

    var store = Store.init(alloc);
    defer store.deinit();

    var module = Module.init(alloc, bytes);
    defer module.deinit();
    try module.decode();

    var instance = Instance.init(alloc, &store, module);
    try instance.instantiate();
    defer instance.deinit();

    const n = 39;
    var in = [1]u64{n};
    var out = [1]u64{0};

    try instance.invoke("fib", in[0..], out[0..], .{});

    const result: i32 = @bitCast(@as(u32, @truncate(out[0])));
    std.debug.print("fib({}) = {}\n", .{ n, result });
}

Requirements

Compile-time

  • Zig 0.11 (master)

Run-time

  • None, zig generates static binaries:
➜  zware git:(master) ✗ ldd fib
        not a dynamic executable

Goals

  • Embed WebAssembly programs in other zig programs
  • Be fast enough to be useful

Status

  • The project is very much alpha quality
  • WebAssembly 2.0 supported (apart from the vector / SIMD support which is WIP)
  • The WebAssembly official testsuite passes (not including SIMD tests)
  • Partial WASI support

Running the testsuite

  1. Build the test runner
cd test/testrunner
zig build --prefix ./
  1. Run
sh test/run-generated.sh

Does it run doom?

Yes, yes it does