Skip to content

update to zig master 0.10.0 and gyro v0.7.0 #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ on:
pull_request:
branches:
- main
schedule:
- cron: '0 0 * * *' # run at 00:00 UTC

jobs:
build_nix:
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
zig-cache
zig-*
wasmtime*
.DS_Store
*.swp
.gyro
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# wasmtime-zig
[<img alt="github" src="https://img.shields.io/badge/github-kubkon/wasmtime--zig-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/kubkon/wasmtime-zig)
[<img alt="github" src="https://img.shields.io/badge/github-zigwasm/wasmtime--zig-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/kubkon/wasmtime-zig)
[<img alt="build status" src="https://img.shields.io/github/workflow/status/kubkon/wasmtime-zig/CI/master?style=for-the-badge" height="20">](https://github.com/kubkon/wasmtime-zig/actions?query=branch%3Amaster)

Zig embedding of [Wasmtime]
@@ -13,7 +13,7 @@ but expected, and things might just not work as expected yet.

## Building

To build this library, you will need Zig nightly 0.8.0, as well as [`gyro`] package manager.
To build this library, you will need Zig nightly 0.10.0, as well as [`gyro`] package manager (`v0.7.0`).

[`gyro`]: https://github.com/mattnite/gyro

19 changes: 14 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
@@ -30,13 +30,22 @@ pub fn build(b: *std.build.Builder) !void {
simple_exe.setBuildMode(mode);
simple_exe.addPackage(.{
.name = "wasmtime",
.path = "src/main.zig",
.source = .{ .path = "src/main.zig" },
.dependencies = &.{pkgs.wasm},
});
if (builtin.os.tag == .windows) {
simple_exe.linkSystemLibrary("wasmtime.dll");
} else {
simple_exe.linkSystemLibrary("wasmtime");
switch (builtin.os.tag) {
.windows => {
simple_exe.linkSystemLibrary("wasmtime.dll");
simple_exe.linkSystemLibrary("unwind");
},
.linux => {
simple_exe.linkSystemLibrary("wasmtime");
simple_exe.linkSystemLibrary("unwind");
},
.macos => {
simple_exe.linkSystemLibrary("wasmtime");
},
else => unreachable,
}
simple_exe.linkLibC();
simple_exe.step.dependOn(b.getInstallStep());
10 changes: 10 additions & 0 deletions ci/install_gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -x
set -e

GYRO_VERSION=$1
GYRO=$2

curl -sL "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.tar.gz" -o "$GYRO.tar.gz"
tar xf "$GYRO.tar.gz"
10 changes: 10 additions & 0 deletions ci/install_wasmtime
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -x
set -e

WASMTIME_VERSION=$1
WASMTIME=$2

curl -sL "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.tar.xz" -o "$WASMTIME.tar.xz"
tar xf "$WASMTIME.tar.xz"
35 changes: 18 additions & 17 deletions ci/linux_ci
Original file line number Diff line number Diff line change
@@ -3,28 +3,29 @@
set -x
set -e

ZIG="zig-linux-x86_64-0.8.0-dev.2667+44de88498"
ARCH="x86_64"
OS="linux"
ARCH_OS="${ARCH}-${OS}"
OS_ARCH="${OS}-${ARCH}"

ZIG_SRC="https://ziglang.org/download/index.json"
ZIG_VERSION=$(curl -s ${ZIG_SRC} | jq -r '.master.version')
ZIG="zig-${OS_ARCH}-$ZIG_VERSION"
ZIG_MASTER=$(curl -s ${ZIG_SRC} | jq -r --arg ARCH_OS "$ARCH_OS" '.master[$ARCH_OS].tarball')

WASMTIME_VERSION="v0.24.0"
WASMTIME="wasmtime-$WASMTIME_VERSION-x86_64-linux-c-api"
GYRO_VERSION="0.2.3"
GYRO="gyro-$GYRO_VERSION-linux-x86_64"
WASMTIME="wasmtime-$WASMTIME_VERSION-${ARCH_OS}-c-api"
GYRO_VERSION="0.7.0"
GYRO="gyro-$GYRO_VERSION-${OS_ARCH}"

wget -nv "https://ziglang.org/builds/$ZIG.tar.xz"
# TODO: make this nice
curl -sL "${ZIG_MASTER}" -o "$ZIG.tar.xz"
tar xf "$ZIG.tar.xz"
export PATH="$(pwd)/$ZIG:$PATH"

wget -nv "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.tar.xz"
tar xf "$WASMTIME.tar.xz"
./ci/install_wasmtime $WASMTIME_VERSION $WASMTIME

wget -nv "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.tar.gz"
tar xf "$GYRO.tar.gz"
./ci/install_gyro $GYRO_VERSION $GYRO
export PATH="$(pwd)/$GYRO/bin:$PATH"

gyro build
gyro build test
gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=gcd --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=linking --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=memory --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=interrupt --search-prefix "$(pwd)/$WASMTIME"

./ci/run_gyro $WASMTIME
30 changes: 15 additions & 15 deletions ci/macos_ci
Original file line number Diff line number Diff line change
@@ -3,28 +3,28 @@
set -x
set -e

ZIG="zig-macos-x86_64-0.8.0-dev.2670+d8d92dafe"
ARCH="x86_64"
OS="macos"
ARCH_OS="${ARCH}-${OS}"
OS_ARCH="${OS}-${ARCH}"

ZIG_SRC="https://ziglang.org/download/index.json"
ZIG_VERSION=$(curl -s ${ZIG_SRC} | jq -r '.master.version')
ZIG="zig-${OS_ARCH}-$ZIG_VERSION"
ZIG_MASTER=$(curl -s ${ZIG_SRC} | jq -r --arg ARCH_OS "$ARCH_OS" '.master[$ARCH_OS].tarball')

WASMTIME_VERSION="v0.24.0"
WASMTIME="wasmtime-$WASMTIME_VERSION-x86_64-macos-c-api"
GYRO_VERSION="0.2.3"
GYRO_VERSION="0.7.0"
GYRO="gyro-$GYRO_VERSION-macos-x86_64"

curl -L "https://ziglang.org/builds/$ZIG.tar.xz" -o "$ZIG.tar.xz"
curl -sL "${ZIG_MASTER}" -o "$ZIG.tar.xz"
tar xf "$ZIG.tar.xz"
export PATH="$(pwd)/$ZIG:$PATH"

curl -L "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.tar.xz" -o "$WASMTIME.tar.xz"
tar xf "$WASMTIME.tar.xz"
./ci/install_wasmtime $WASMTIME_VERSION $WASMTIME

curl -L "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.tar.gz" -o "$GYRO.tar.gz"
tar xf "$GYRO.tar.gz"
./ci/install_gyro $GYRO_VERSION $GYRO
export PATH="$(pwd)/$GYRO/bin:$PATH"

gyro build
gyro build test
gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=gcd --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=linking --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=memory --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=interrupt --search-prefix "$(pwd)/$WASMTIME"

./ci/run_gyro $WASMTIME
14 changes: 14 additions & 0 deletions ci/run_gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

set -x
set -e

WASMTIME=$1

gyro build
gyro build test
gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=gcd --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=linking --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=memory --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=interrupt --search-prefix "$(pwd)/$WASMTIME"
32 changes: 17 additions & 15 deletions ci/win_ci
Original file line number Diff line number Diff line change
@@ -3,28 +3,30 @@
set -x
set -e

ZIG="zig-windows-x86_64-0.8.0-dev.2667+44de88498"
ARCH="x86_64"
OS="windows"
ARCH_OS="${ARCH}-${OS}"
OS_ARCH="${OS}-${ARCH}"

ZIG_SRC="https://ziglang.org/download/index.json"
ZIG_VERSION=$(curl -s ${ZIG_SRC} | jq -r '.master.version')
ZIG="zig-${OS_ARCH}-$ZIG_VERSION"
ZIG_MASTER=$(curl -s ${ZIG_SRC} | jq -r --arg ARCH_OS "$ARCH_OS" '.master[$ARCH_OS].tarball')

WASMTIME_VERSION="v0.24.0"
WASMTIME="wasmtime-$WASMTIME_VERSION-x86_64-windows-c-api"
GYRO_VERSION="0.2.3"
GYRO="gyro-$GYRO_VERSION-windows-x86_64"
WASMTIME="wasmtime-$WASMTIME_VERSION-${ARCH_OS}-c-api"
GYRO_VERSION="0.7.0"
GYRO="gyro-$GYRO_VERSION-${OS_ARCH}"

curl -L "https://ziglang.org/builds/$ZIG.zip" -o "$ZIG.zip"
curl -sL "${ZIG_MASTER}" -o "$ZIG.zip"
7z x "$ZIG.zip"
export PATH="$(pwd)/$ZIG:$PATH"

curl -L "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.zip" -o "$WASMTIME.zip"
curl -sL "https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/$WASMTIME.zip" -o "$WASMTIME.zip"
7z x "$WASMTIME.zip"

curl -L "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.zip" -o "$GYRO.zip"
curl -sL "https://github.com/mattnite/gyro/releases/download/$GYRO_VERSION/$GYRO.zip" -o "$GYRO.zip"
7z x "$GYRO.zip"
export PATH="$(pwd)/$GYRO/bin:$PATH"

gyro build
gyro build test
gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=gcd --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=linking --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=memory --search-prefix "$(pwd)/$WASMTIME"
gyro build run -Dexample=interrupt --search-prefix "$(pwd)/$WASMTIME"

./ci/run_gyro $WASMTIME
4 changes: 2 additions & 2 deletions examples/interrupt.zig
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ pub fn main() !void {
defer instance.deinit();
std.debug.print("Instance initialized...\n", .{});

const thread = try std.Thread.spawn(interrupt, handle);
const thread = try std.Thread.spawn(.{}, interrupt, .{ handle });

if (instance.getExportFunc(module, "run")) |f| {
std.debug.print("Calling export...\n", .{});
@@ -50,5 +50,5 @@ pub fn main() !void {
std.debug.print("Export not found...\n", .{});
}

thread.wait();
thread.join();
}
2 changes: 1 addition & 1 deletion examples/linking.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const wasmtime = @import("wasmtime");
const builtin = std.builtin;
const builtin = @import("builtin");
const fs = std.fs;
const ga = std.heap.c_allocator;
const Allocator = std.mem.Allocator;
11 changes: 5 additions & 6 deletions gyro.zzz
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
pkgs:
wasmtime:
version: 0.0.0
root: src/main.zig
description: Zig embedding of Wasmtime
license: Apache-2.0
source_url: "https://github.com/zigwasm/wasmtime-zig"
tags:
wasmtime
wasm
wasi
root: src/main.zig
deps:
wasm:
src:
github:
user: zigwasm
repo: wasm-zig
ref: main
git:
url: "https://github.com/voigt/wasm-zig.git"
ref: fix_testname
root: src/main.zig
17 changes: 9 additions & 8 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -69,14 +69,14 @@ pub const Module = struct {
inner: *wasm.Module,

/// Initializes a new `Module` using the supplied engine and wasm bytecode
pub fn initFromWasm(engine: *Engine, wasm: []const u8) !Module {
var wasm_bytes = ByteVec.initWithCapacity(wasm.len);
pub fn initFromWasm(engine: *Engine, _wasm: []const u8) !Module {
var wasm_bytes = ByteVec.initWithCapacity(_wasm.len);
defer wasm_bytes.deinit();

var i: usize = 0;
var ptr = wasm_bytes.data;
while (i < wasm.len) : (i += 1) {
ptr.* = wasm[i];
while (i < _wasm.len) : (i += 1) {
ptr.* = _wasm[i];
ptr += 1;
}

@@ -163,8 +163,8 @@ pub const Func = struct {
comptime var wasm_args: [args_len]Value = undefined;
inline for (wasm_args) |*arg, i| {
arg.* = switch (@TypeOf(args[i])) {
i32, u32 => .{ .kind = .i32, .of = .{ .i32 = @intCast(i32, args[i]) } },
i64, u64 => .{ .kind = .i64, .of = .{ .i64 = @intCast(i64, args[i]) } },
i32, u32 => .{ .kind = .i32, .of = .{ .i32 = @bitCast(i32, args[i]) } },
i64, u64 => .{ .kind = .i64, .of = .{ .i64 = @bitCast(i64, args[i]) } },
f32 => .{ .kind = .f32, .of = .{ .f32 = args[i] } },
f64 => .{ .kind = .f64, .of = .{ .f64 = args[i] } },
*Func => .{ .kind = .funcref, .of = .{ .ref = args[i] } },
@@ -216,7 +216,7 @@ pub const Func = struct {
f32 => result_ty.of.f32,
f64 => result_ty.of.f64,
*Func => @ptrCast(?*Func, result_ty.of.ref).?,
*Extern => @ptrCast(?*c.Extern, result_ty.of.ref).?,
*Extern => @ptrCast(?*Extern, result_ty.of.ref).?,
else => |ty| @compileError("Unsupported result type '" ++ @typeName(ty) ++ "'"),
};
}
@@ -296,6 +296,7 @@ pub const InterruptHandle = opaque {
}
/// Invokes an interrupt in the current wasm module
pub fn interrupt(self: *InterruptHandle) void {
std.debug.print("> INTERRUPTING! \n", .{});
wasmtime_interrupt_handle_interrupt(self);
}

@@ -345,6 +346,6 @@ pub const Linker = opaque {
extern "c" fn wasmtime_linker_instantiate(*const Linker, *const wasm.Module, *?*wasm.Instance, *?*Trap) ?*WasmError;
};

test "" {
test {
testing.refAllDecls(@This());
}