Skip to content
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

feat: support extism_current_plugin_host_context #17

Merged
merged 1 commit into from
May 22, 2024
Merged
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: 4 additions & 0 deletions src/current_plugin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ pub fn returnBytes(self: *Self, val: *c.ExtismVal, data: []const u8) void {
pub fn inputBytes(self: *Self, val: *const c.ExtismVal) []const u8 {
return self.getMemory(@intCast(val.v.i64));
}

pub fn hostContext(self: *Self) ?*anyopaque {
return c.extism_current_plugin_host_context(self.c_currplugin);
}
17 changes: 14 additions & 3 deletions src/plugin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ pub fn cancelHandle(self: *Self) CancelHandle {
return .{ .handle = ptr };
}

/// Call a function with the given input
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));
fn handleCall(self: *Self, res: i32) ![]const u8 {
if (res != 0) {
const err_c = c.extism_plugin_error(self.ptr);
const err = std.mem.span(err_c);
Expand All @@ -80,6 +78,19 @@ pub fn call(self: *Self, function_name: []const u8, input: []const u8) ![]const
}
return "";
}

/// Call a function with the given input
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));
return self.handleCall(res);
}

/// Call a function with the given input and host context
pub fn callWithContext(self: *Self, function_name: []const u8, input: []const u8, host_context: *anyopaque) ![]const u8 {
const res = c.extism_plugin_call_with_host_context(self.ptr, function_name.ptr, input.ptr, @as(u64, input.len), host_context);
return self.handleCall(res);
}

/// 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 });
Expand Down
9 changes: 8 additions & 1 deletion test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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});
if (curr_plugin.hostContext()) |ctx_ptr| {
const ctx: *u64 = @alignCast(@ptrCast(ctx_ptr));
std.debug.print("Host context={}\n", .{ctx.*});
}
output_slice[0] = input_slice[0];
}

Expand All @@ -40,9 +44,12 @@ test "Single threaded tests" {
std.debug.print("\nregister loaded plugin: {}\n", .{std.fmt.fmtDuration(wasm_start.read())});
const repeat = 1182;
const input = "aeiouAEIOU____________________________________&smtms_y?" ** repeat;
const data = try plugin.call("count_vowels", input);
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 });
var ctx: u64 = 12345;
data = try plugin.callWithContext("count_vowels", input, @ptrCast(&ctx));
try testing.expectEqualStrings("{\"count\": 11820}", data);
std.debug.print("--------------\n", .{});
var i: usize = 0;
var wasm_elapsed: u64 = 0;
Expand Down
Loading