Skip to content

Commit 14144b8

Browse files
authored
feat: support extism_current_plugin_host_context (#17)
1 parent 896cb52 commit 14144b8

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/current_plugin.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ pub fn returnBytes(self: *Self, val: *c.ExtismVal, data: []const u8) void {
3939
pub fn inputBytes(self: *Self, val: *const c.ExtismVal) []const u8 {
4040
return self.getMemory(@intCast(val.v.i64));
4141
}
42+
43+
pub fn hostContext(self: *Self) ?*anyopaque {
44+
return c.extism_current_plugin_host_context(self.c_currplugin);
45+
}

src/plugin.zig

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ pub fn cancelHandle(self: *Self) CancelHandle {
5858
return .{ .handle = ptr };
5959
}
6060

61-
/// Call a function with the given input
62-
pub fn call(self: *Self, function_name: []const u8, input: []const u8) ![]const u8 {
63-
const res = c.extism_plugin_call(self.ptr, function_name.ptr, input.ptr, @as(u64, input.len));
61+
fn handleCall(self: *Self, res: i32) ![]const u8 {
6462
if (res != 0) {
6563
const err_c = c.extism_plugin_error(self.ptr);
6664
const err = std.mem.span(err_c);
@@ -80,6 +78,19 @@ pub fn call(self: *Self, function_name: []const u8, input: []const u8) ![]const
8078
}
8179
return "";
8280
}
81+
82+
/// Call a function with the given input
83+
pub fn call(self: *Self, function_name: []const u8, input: []const u8) ![]const u8 {
84+
const res = c.extism_plugin_call(self.ptr, function_name.ptr, input.ptr, @as(u64, input.len));
85+
return self.handleCall(res);
86+
}
87+
88+
/// Call a function with the given input and host context
89+
pub fn callWithContext(self: *Self, function_name: []const u8, input: []const u8, host_context: *anyopaque) ![]const u8 {
90+
const res = c.extism_plugin_call_with_host_context(self.ptr, function_name.ptr, input.ptr, @as(u64, input.len), host_context);
91+
return self.handleCall(res);
92+
}
93+
8394
/// Set configuration values
8495
pub fn setConfig(self: *Self, allocator: std.mem.Allocator, config: std.json.ArrayHashMap([]const u8)) !void {
8596
const config_json = try std.json.stringifyAlloc(allocator, config, .{ .emit_null_optional_fields = false });

test.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export fn hello_world(plugin_ptr: ?*sdk.c.ExtismCurrentPlugin, inputs: [*c]const
1515
var curr_plugin = CurrentPlugin.getCurrentPlugin(plugin_ptr orelse unreachable);
1616
const input = curr_plugin.inputBytes(&input_slice[0]);
1717
std.debug.print("input: {s}\n", .{input});
18+
if (curr_plugin.hostContext()) |ctx_ptr| {
19+
const ctx: *u64 = @alignCast(@ptrCast(ctx_ptr));
20+
std.debug.print("Host context={}\n", .{ctx.*});
21+
}
1822
output_slice[0] = input_slice[0];
1923
}
2024

@@ -40,9 +44,12 @@ test "Single threaded tests" {
4044
std.debug.print("\nregister loaded plugin: {}\n", .{std.fmt.fmtDuration(wasm_start.read())});
4145
const repeat = 1182;
4246
const input = "aeiouAEIOU____________________________________&smtms_y?" ** repeat;
43-
const data = try plugin.call("count_vowels", input);
47+
var data = try plugin.call("count_vowels", input);
4448
try testing.expectEqualStrings("{\"count\": 11820}", data);
4549
std.debug.print("register plugin + function call: {}, sent input size: {} bytes\n", .{ std.fmt.fmtDuration(wasm_start.read()), input.len });
50+
var ctx: u64 = 12345;
51+
data = try plugin.callWithContext("count_vowels", input, @ptrCast(&ctx));
52+
try testing.expectEqualStrings("{\"count\": 11820}", data);
4653
std.debug.print("--------------\n", .{});
4754
var i: usize = 0;
4855
var wasm_elapsed: u64 = 0;

0 commit comments

Comments
 (0)