Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
zig_version: ["0.14.0"] # eventually use multiple versions once stable
zig_version: ["0.15.2"] # eventually use multiple versions once stable
rust:
- stable
steps:
- name: Checkout sources
uses: actions/checkout@v3
- uses: ./.github/actions/libextism
- name: Setup Zig env
uses: goto-bus-stop/setup-zig@v2
uses: https://github.com/mlugg/setup-zig@v2
with:
version: ${{ matrix.zig_version }}
- name: Test Zig Host SDK
Expand Down
36 changes: 28 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,52 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Generate C bindings from extism.h using translateC
const translate_c = b.addTranslateC(.{
.root_source_file = .{ .cwd_relative = "/usr/local/include/extism.h" },
.target = target,
.optimize = optimize,
.link_libc = true,
});

// Create a module from the generated bindings
const extism_c = translate_c.createModule();

const extism_module = b.addModule("extism", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "extism_c", .module = extism_c },
},
});
extism_module.addIncludePath(.{ .cwd_relative = "/usr/local/include" });
extism_module.addLibraryPath(.{ .cwd_relative = "/usr/local/lib" });

var tests = b.addTest(.{
.name = "Library Tests",
.root_source_file = b.path("test.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("test.zig"),
.target = target,
.optimize = optimize,
}),
});

tests.root_module.addImport("extism", extism_module);
tests.linkLibC();
tests.linkSystemLibrary("extism");
tests.root_module.link_libc = true;
tests.root_module.linkSystemLibrary("extism", .{});
const tests_run_step = b.addRunArtifact(tests);

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&tests_run_step.step);

var example = b.addExecutable(.{
.name = "Example",
.root_source_file = b.path("examples/basic.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("examples/basic.zig"),
.target = target,
.optimize = optimize,
}),
});

example.root_module.addImport("extism", extism_module);
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.{
.name = .extism,
.version = "1.0.0-rc3",
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.2",
.fingerprint = 0xb41364f00afbe9db,
.paths = .{""},
}
2 changes: 1 addition & 1 deletion src/cancel_handle.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const c = @import("ffi.zig");
const c = @import("extism_c");
const Self = @This();

handle: ?*const c.ExtismCancelHandle,
Expand Down
2 changes: 1 addition & 1 deletion src/compiled_plugin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const Manifest = @import("manifest.zig").Manifest;
const Function = @import("function.zig");
const CancelHandle = @import("cancel_handle.zig");
const c = @import("ffi.zig");
const c = @import("extism_c");

const Self = @This();

Expand Down
2 changes: 1 addition & 1 deletion src/current_plugin.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const c = @import("ffi.zig");
const c = @import("extism_c");

c_currplugin: *c.ExtismCurrentPlugin,

Expand Down
3 changes: 0 additions & 3 deletions src/ffi.zig

This file was deleted.

2 changes: 1 addition & 1 deletion src/function.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const c = @import("ffi.zig");
const c = @import("extism_c");

const Self = @This();
c_func: ?*c.ExtismFunction,
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const testing = std.testing;
pub const c = @import("ffi.zig");
pub const c = @import("extism_c");

pub const Plugin = @import("plugin.zig");
pub const CompiledPlugin = @import("compiled_plugin.zig");
Expand Down
6 changes: 3 additions & 3 deletions src/plugin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const Manifest = @import("manifest.zig").Manifest;
const Function = @import("function.zig");
const CancelHandle = @import("cancel_handle.zig");
const c = @import("ffi.zig");
const c = @import("extism_c");
const CompiledPlugin = @import("compiled_plugin.zig");

const Self = @This();
Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn init(allocator: std.mem.Allocator, data: []const u8, functions: []const F

/// Create a new plugin from the given manifest
pub fn initFromManifest(allocator: std.mem.Allocator, manifest: Manifest, functions: []const Function, wasi: bool) !Self {
const json = try std.json.stringifyAlloc(allocator, manifest, .{ .emit_null_optional_fields = false });
const json = try std.json.Stringify.valueAlloc(allocator, manifest, .{ .emit_null_optional_fields = false });
defer allocator.free(json);
return init(allocator, json, functions, wasi);
}
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn callWithContext(self: *Self, function_name: []const u8, input: []const u8

/// Set configuration values
pub fn setConfig(self: *Self, allocator: std.mem.Allocator, config: std.json.ArrayHashMap([]const u8)) !void {
const config_json = try std.json.stringifyAlloc(allocator, config, .{ .emit_null_optional_fields = false });
const config_json = try std.json.Stringify.valueAlloc(allocator, config, .{ .emit_null_optional_fields = false });
defer allocator.free(config_json);
_ = c.extism_plugin_config(self.ptr, config_json.ptr, @as(u64, config_json.len));
}
Expand Down
14 changes: 7 additions & 7 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CurrentPlugin = sdk.CurrentPlugin;
const Function = sdk.Function;
const manifest = sdk.manifest;

export fn hello_world(plugin_ptr: ?*sdk.c.ExtismCurrentPlugin, inputs: [*c]const sdk.c.ExtismVal, n_inputs: u64, outputs: [*c]sdk.c.ExtismVal, n_outputs: u64, user_data: ?*anyopaque) callconv(.C) void {
export fn hello_world(plugin_ptr: ?*sdk.c.ExtismCurrentPlugin, inputs: [*c]const sdk.c.ExtismVal, n_inputs: u64, outputs: [*c]sdk.c.ExtismVal, n_outputs: u64, user_data: ?*anyopaque) callconv(.c) void {
std.debug.print("Hello from Zig!\n", .{});
const str_ud = @as([*:0]const u8, @ptrCast(user_data orelse unreachable));
std.debug.print("User data: {s}\n", .{str_ud});
Expand Down Expand Up @@ -41,12 +41,12 @@ test "Single threaded tests" {
var plugin = try Plugin.initFromManifest(testing.allocator, man, &[_]Function{f}, true);
defer plugin.deinit();

std.debug.print("\nregister loaded plugin: {}\n", .{std.fmt.fmtDuration(wasm_start.read())});
std.debug.print("\nregister loaded plugin: {}\n", .{wasm_start.read()});
const repeat = 1182;
const input = "aeiouAEIOU____________________________________&smtms_y?" ** repeat;
var data = try plugin.call("count_vowels", input);
try testing.expectEqualStrings("{\"count\": 11820}", data);
std.debug.print("register plugin + function call: {}, sent input size: {} bytes\n", .{ std.fmt.fmtDuration(wasm_start.read()), input.len });
std.debug.print("register plugin + function call: {}, sent input size: {} bytes\n", .{ wasm_start.read(), input.len });
var ctx: u64 = 12345;
data = try plugin.callWithContext("count_vowels", input, @ptrCast(&ctx));
try testing.expectEqualStrings("{\"count\": 11820}", data);
Expand Down Expand Up @@ -74,8 +74,8 @@ test "Single threaded tests" {
native_elapsed += native_start.read();
}
const native_avg = native_elapsed / i;
std.debug.print("native function call (avg, N = {}): {}\n", .{ i, std.fmt.fmtDuration(native_avg) });
std.debug.print("wasm function call (avg, N = {}): {}\n", .{ i, std.fmt.fmtDuration(wasm_avg) });
std.debug.print("native function call (avg, N = {}): {}\n", .{ i, native_avg });
std.debug.print("wasm function call (avg, N = {}): {}\n", .{ i, wasm_avg });
}

test "Multi threaded tests" {
Expand Down Expand Up @@ -135,7 +135,7 @@ test "Plugin Cancellation" {
var handle = plugin.cancelHandle();
const S = struct {
fn _test(h: *sdk.CancelHandle) void {
std.time.sleep(1 * std.time.ns_per_s);
std.Thread.sleep(1 * std.time.ns_per_s);
_ = h.cancel();
}
};
Expand All @@ -144,5 +144,5 @@ test "Plugin Cancellation" {
const output = plugin.call("infinite_loop", "abc123");
const call_end = call_start.read();
try std.testing.expectError(error.PluginCallFailed, output);
std.debug.print("Cancelled plugin ran for {}\n", .{std.fmt.fmtDuration(call_end)});
std.debug.print("Cancelled plugin ran for {}\n", .{call_end});
}