Skip to content

Commit

Permalink
Merge pull request #10 from usdogu/update-installation-docs
Browse files Browse the repository at this point in the history
docs: update installation docs
  • Loading branch information
nilslice authored Dec 1, 2023
2 parents ad08d28 + ed23d17 commit eb6fade
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<git-ref-here>.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

Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.{
.name = "extism",
.version = "0.1.0",
.paths = .{""},
}
4 changes: 2 additions & 2 deletions examples/basic.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const std = @import("std");
const testing = std.testing;
const sdk = @import("extism");
const Plugin = sdk.Plugin;
const CurrentPlugin = sdk.CurrentPlugin;
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/current_plugin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")) {
Expand Down

0 comments on commit eb6fade

Please sign in to comment.