From ed23d1721ef7f97ce33f923a69190ec41faf84fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fu=20Us?= Date: Fri, 24 Nov 2023 22:27:16 +0300 Subject: [PATCH] docs: update installation docs --- README.md | 23 ++++++++++++++++++++--- build.zig.zon | 5 +++++ examples/basic.zig | 4 ++-- src/current_plugin.zig | 6 +++--- src/plugin.zig | 2 +- 5 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 build.zig.zon diff --git a/README.md b/README.md index b14772f..ec0b975 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,24 @@ sudo extism lib install latest > **Note**: This library has breaking changes and targets 1.0 of the runtime. For the time being, install the runtime from our nightly development builds on git: `sudo extism lib install --version git`. # within your Zig project directory: -mkdir -p libs -cd libs -git clone https://github.com/extism/extism.git +This package works with the Zig package manager introduced in Zig 0.11. Create a `build.zig.zon` file like this: +```zig +.{ + .name = "my-project", + .version = "0.1.0", + .paths = .{""}, + .dependencies = .{ + .extism = .{ + .url = "https://github.com/extism/zig-sdk/archive/.tar.gz", + }, + }, +} +``` +And in your `build.zig`: +```zig +const extism_module = b.dependency("extism", .{ .target = target, .optimize = optimize }).module("extism"); +exe.addModule("extism", extism_module); +``` ## Getting Started @@ -40,11 +55,13 @@ Since you may not have an Extism plug-in on hand to test, let's load a demo plug ```zig // First require the library const extism = @import("extism"); +const std = @import("std"); const wasm_url = extism.manifest.WasmUrl{ .url = "https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm" }; const manifest = .{ .wasm = &[_]extism.manifest.Wasm{.{ .wasm_url= wasm_url }} }; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +defer std.debug.assert(gpa.deinit() == .ok); const allocator = gpa.allocator(); var plugin = try extism.Plugin.initFromManifest( diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..56ec71b --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,5 @@ +.{ + .name = "extism", + .version = "0.1.0", + .paths = .{""}, +} diff --git a/examples/basic.zig b/examples/basic.zig index 072a45b..303e7a3 100644 --- a/examples/basic.zig +++ b/examples/basic.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const testing = std.testing; const sdk = @import("extism"); const Plugin = sdk.Plugin; const CurrentPlugin = sdk.CurrentPlugin; @@ -15,11 +14,12 @@ export fn hello_world(plugin_ptr: ?*sdk.c.ExtismCurrentPlugin, inputs: [*c]const var curr_plugin = CurrentPlugin.getCurrentPlugin(plugin_ptr orelse unreachable); const input = curr_plugin.inputBytes(&input_slice[0]); std.debug.print("input: {s}\n", .{input}); - output_slice[0] = input_slice[0]; + curr_plugin.returnBytes(&output_slice[0], input); } pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer std.debug.assert(gpa.deinit() == .ok); const allocator = gpa.allocator(); _ = sdk.setLogFile("extism.log", .Debug); diff --git a/src/current_plugin.zig b/src/current_plugin.zig index 93be5f6..ec141ef 100644 --- a/src/current_plugin.zig +++ b/src/current_plugin.zig @@ -10,7 +10,7 @@ pub fn getCurrentPlugin(ptr: *c.ExtismCurrentPlugin) Self { return .{ .c_currplugin = ptr }; } -pub fn getMemory(self: Self, offset: MemoryHandle) []const u8 { +pub fn getMemory(self: Self, offset: MemoryHandle) []u8 { const len = c.extism_current_plugin_memory_length(self.c_currplugin, offset); const c_data = c.extism_current_plugin_memory(self.c_currplugin); const data: [*:0]u8 = std.mem.span(c_data); @@ -29,11 +29,11 @@ pub fn length(self: *Self, offset: MemoryHandle) u64 { return c.extism_current_plugin_memory_length(self.c_currplugin, offset); } -pub fn returnBytes(self: *Self, val: *const c.ExtismVal, data: []const u8) void { +pub fn returnBytes(self: *Self, val: *c.ExtismVal, data: []const u8) void { const mem = self.alloc(@as(u64, data.len)); const ptr = self.getMemory(mem); @memcpy(ptr, data); - val.v.i64 = @as(i64, mem); + val.v.i64 = @intCast(mem); } pub fn inputBytes(self: *Self, val: *const c.ExtismVal) []const u8 { diff --git a/src/plugin.zig b/src/plugin.zig index cb20350..23c8226 100644 --- a/src/plugin.zig +++ b/src/plugin.zig @@ -62,7 +62,7 @@ pub fn cancelHandle(self: *Self) CancelHandle { pub fn call(self: *Self, function_name: []const u8, input: []const u8) ![]const u8 { const res = c.extism_plugin_call(self.ptr, function_name.ptr, input.ptr, @as(u64, input.len)); if (res != 0) { - var err_c = c.extism_plugin_error(self.ptr); + const err_c = c.extism_plugin_error(self.ptr); const err = std.mem.span(err_c); if (!std.mem.eql(u8, err, "")) {