Skip to content

wapc/wapc-guest-zig

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

waPC Guest Library for Zig

This is the Zig implementation of the waPC standard for WebAssembly guest modules. It allows any waPC-compliant WebAssembly host to invoke to procedures inside a Zig compiled guest and similarly for the guest to invoke procedures exposed by the host.

Example

The following is a simple example of synchronous, bi-directional procedure calls between a WebAssembly host runtime and the guest module.

hello.zig (copy wapc.zig into the same directory)

const wapc = @import("wapc.zig");
const std = @import("std");
const mem = std.mem;

export fn __guest_call(operation_size: usize, payload_size: usize) bool {
    return wapc.handleCall(operation_size, payload_size, &functions);
}

var functions = [_]wapc.Function{
    wapc.Function{.name = &"hello", .invoke = sayHello},
};

fn sayHello(allocator: *mem.Allocator, payload: []u8) ![]u8 {
    const hostHello = try wapc.hostCall(allocator, &"myBinding", &"sample", &"hello", &"Simon");
    const prefix = "Hello, ";
    const message = try allocator.alloc(u8, prefix.len + payload.len + hostHello.len + 1);
    mem.copy(u8, message, &prefix);
    mem.copy(u8, message[prefix.len..], payload);
    mem.copy(u8, message[prefix.len+payload.len..], hostHello);
    message[message.len-1] = '!';
    return message;
}
zig build-lib hello.zig -target wasm32-freestanding

About

SDK for creating waPC WebAssembly Guest Modules in Zig

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages