Skip to content

WIP: Update wasmtime 0.37.0 #22

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 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -7,6 +7,8 @@ on:
pull_request:
branches:
- main
schedule:
- cron: '0 0 * * *' # run at 00:00 UTC

jobs:
build_nix:
Expand Down
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
Expand Down
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]
Expand All @@ -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

Expand Down
19 changes: 14 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
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"
37 changes: 19 additions & 18 deletions ci/linux_ci
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
set -x
set -e

ZIG="zig-linux-x86_64-0.8.0-dev.2667+44de88498"
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"
ARCH="x86_64"
OS="linux"
ARCH_OS="${ARCH}-${OS}"
OS_ARCH="${OS}-${ARCH}"

wget -nv "https://ziglang.org/builds/$ZIG.tar.xz"
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.38.1"
WASMTIME="wasmtime-$WASMTIME_VERSION-${ARCH_OS}-c-api"
GYRO_VERSION="0.7.0"
GYRO="gyro-$GYRO_VERSION-${OS_ARCH}"

# 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
32 changes: 16 additions & 16 deletions ci/macos_ci
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
set -x
set -e

ZIG="zig-macos-x86_64-0.8.0-dev.2670+d8d92dafe"
WASMTIME_VERSION="v0.24.0"
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.38.1"
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"
34 changes: 18 additions & 16 deletions ci/win_ci
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
set -x
set -e

ZIG="zig-windows-x86_64-0.8.0-dev.2667+44de88498"
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"
ARCH="x86_64"
OS="windows"
ARCH_OS="${ARCH}-${OS}"
OS_ARCH="${OS}-${ARCH}"

curl -L "https://ziglang.org/builds/$ZIG.zip" -o "$ZIG.zip"
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.38.1"
WASMTIME="wasmtime-$WASMTIME_VERSION-${ARCH_OS}-c-api"
GYRO_VERSION="0.7.0"
GYRO="gyro-$GYRO_VERSION-${OS_ARCH}"

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
Expand Up @@ -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", .{});
Expand All @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions examples/simple_new.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const builtin = @import("builtin");
const std = @import("std");
const wasmtime = @import("wasmtime");
const fs = std.fs;
const ga = std.heap.c_allocator;
const Allocator = std.mem.Allocator;

const log = std.log.scoped(.wasmtime_zig);

fn hello() void {
std.debug.print("Calling back...\n", .{});
std.debug.print("> Hello World!\n", .{});
}

pub fn main() !void {
const wasm_path = if (builtin.os.tag == .windows) "examples\\simple.wat" else "examples/simple.wat";
const wasm_file = try fs.cwd().openFile(wasm_path, .{});
const wasm = try wasm_file.readToEndAlloc(ga, std.math.maxInt(u64));
defer ga.free(wasm);

var engine = try wasmtime.Engine.init();
defer engine.deinit();
std.debug.print("Engine initialized...\n", .{});

var store = try wasmtime.Store.init(&engine);
defer store.deinit();
std.debug.print("Store initialized...\n", .{});

var module = try wasmtime.Module.initFromWat(&engine, wasm);
defer module.deinit();
std.debug.print("Wasm module compiled...\n", .{});

var func = try wasmtime.Func.init(&store, hello);
std.debug.print("Func callback prepared...\n", .{});

_ = func;

// var instance = try wasmtime.Instance.init(store, module, &.{func});
// defer instance.deinit();
// std.debug.print("Instance initialized...\n", .{});

// if (instance.getExportFunc(module, "run")) |f| {
// std.debug.print("Calling export...\n", .{});
// try f.call(void, .{});
// } else {
// std.debug.print("Export not found...\n", .{});
// }
}
32 changes: 32 additions & 0 deletions examples/wat2wasm.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const builtin = @import("builtin");
const std = @import("std");
const wasmtime = @import("wasmtime");
const fs = std.fs;
const ga = std.heap.c_allocator;
const Allocator = std.mem.Allocator;
const unicode = @import("std").unicode;

const log = std.log.scoped(.wasmtime_zig);

pub fn main() !void {
log.err("Starting...", .{});
const wat_path = if (builtin.os.tag == .windows) "examples\\simple.wat" else "examples/simple.wat";
const wat_file = try fs.cwd().openFile(wat_path, .{});
const wat = try wat_file.readToEndAlloc(ga, std.math.maxInt(u64));
log.err("Read wat:\n{s}", .{wat});
defer ga.free(wat);

const wasm = try wasmtime.Convert.wat2wasm(wat);
const wasm_slice = wasm.toSlice();
log.err("Converted wasm:\n{s}", .{wasm_slice});

const dir = try std.fs.cwd().openDir(".", .{ .iterate = true });
var out = try dir.createFile("output.wasm", .{});
defer out.close();
if (std.unicode.utf8ValidateSlice(wasm_slice)) {
try out.writeAll(wasm_slice);
log.err("File written.", .{});
} else {
log.err("No valid utf-8", .{});
}
}
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/zigwasm/wasm-zig.git"
ref: main
root: src/main.zig
Loading