Skip to content

Commit b905120

Browse files
zshipkonilslice
andauthored
feat: add CompiledPlugin (#19)
* feat: add CompiledPlugin * ci: pin zig 0.13.0 * Apply suggestions from code review Co-authored-by: Steve Manuel <[email protected]> --------- Co-authored-by: Steve Manuel <[email protected]>
1 parent 7497959 commit b905120

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
zig_version: ["master"] # eventually use multiple versions once stable
15+
zig_version: ["0.13.0"] # eventually use multiple versions once stable
1616
rust:
1717
- stable
1818
steps:

examples/basic.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std");
22
const sdk = @import("extism");
33
const Plugin = sdk.Plugin;
44
const CurrentPlugin = sdk.CurrentPlugin;
5+
const CompiledPlugin = sdk.CompiledPlugin;
56
const Function = sdk.Function;
67
const manifest = sdk.manifest;
78

@@ -24,7 +25,7 @@ pub fn main() !void {
2425
_ = sdk.setLogFile("extism.log", .Debug);
2526

2627
const wasmfile_manifest = manifest.WasmFile{ .path = "wasm/code-functions.wasm" };
27-
const man = .{ .wasm = &[_]manifest.Wasm{.{ .wasm_file = wasmfile_manifest }} };
28+
const man = manifest.Manifest{ .wasm = &[_]manifest.Wasm{.{ .wasm_file = wasmfile_manifest }} };
2829
var f = Function.init(
2930
"hello_world",
3031
&[_]sdk.c.ExtismValType{sdk.PTR},
@@ -33,7 +34,10 @@ pub fn main() !void {
3334
@constCast(@as(*const anyopaque, @ptrCast("user data"))),
3435
);
3536
defer f.deinit();
36-
var my_plugin = try Plugin.initFromManifest(allocator, man, &[_]Function{f}, true);
37+
var c = try CompiledPlugin.initFromManifest(allocator, man, &[_]Function{f}, true);
38+
defer c.deinit();
39+
40+
var my_plugin = try Plugin.initFromCompiled(c);
3741
defer my_plugin.deinit();
3842

3943
var config = std.json.ArrayHashMap([]const u8){};

src/compiled_plugin.zig

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const std = @import("std");
2+
const Manifest = @import("manifest.zig").Manifest;
3+
const Function = @import("function.zig");
4+
const CancelHandle = @import("cancel_handle.zig");
5+
const c = @import("ffi.zig");
6+
7+
const Self = @This();
8+
9+
ptr: *c.ExtismCompiledPlugin,
10+
11+
// We have to use this until ziglang/zig#2647 is resolved.
12+
error_info: ?[]const u8,
13+
14+
/// Create a new plugin from a WASM module
15+
pub fn init(allocator: std.mem.Allocator, data: []const u8, functions: []const Function, wasi: bool) !Self {
16+
var plugin: ?*c.ExtismCompiledPlugin = null;
17+
var errmsg: [*c]u8 = null;
18+
if (functions.len > 0) {
19+
var funcPtrs = try allocator.alloc(?*c.ExtismFunction, functions.len);
20+
defer allocator.free(funcPtrs);
21+
var i: usize = 0;
22+
for (functions) |function| {
23+
funcPtrs[i] = function.c_func;
24+
i += 1;
25+
}
26+
plugin = c.extism_compiled_plugin_new(data.ptr, @as(u64, data.len), &funcPtrs[0], functions.len, wasi, &errmsg);
27+
} else {
28+
plugin = c.extism_compiled_plugin_new(data.ptr, @as(u64, data.len), null, 0, wasi, &errmsg);
29+
}
30+
31+
if (plugin == null) {
32+
// TODO: figure out what to do with this error
33+
std.debug.print("extism_compiled_plugin_new: {s}\n", .{
34+
errmsg,
35+
});
36+
c.extism_plugin_new_error_free(errmsg);
37+
return error.PluginLoadFailed;
38+
}
39+
return Self{
40+
.ptr = plugin.?,
41+
.error_info = null,
42+
};
43+
}
44+
45+
/// Create a new plugin from the given manifest
46+
pub fn initFromManifest(allocator: std.mem.Allocator, manifest: Manifest, functions: []const Function, wasi: bool) !Self {
47+
const json = try std.json.stringifyAlloc(allocator, manifest, .{ .emit_null_optional_fields = false });
48+
defer allocator.free(json);
49+
return init(allocator, json, functions, wasi);
50+
}
51+
52+
pub fn deinit(self: *Self) void {
53+
c.extism_compiled_plugin_free(self.ptr);
54+
}

src/main.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const testing = std.testing;
33
pub const c = @import("ffi.zig");
44

55
pub const Plugin = @import("plugin.zig");
6+
pub const CompiledPlugin = @import("compiled_plugin.zig");
67
pub const CurrentPlugin = @import("current_plugin.zig");
78
pub const CancelHandle = @import("cancel_handle.zig");
89
pub const Function = @import("function.zig");

src/plugin.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Manifest = @import("manifest.zig").Manifest;
33
const Function = @import("function.zig");
44
const CancelHandle = @import("cancel_handle.zig");
55
const c = @import("ffi.zig");
6+
const CompiledPlugin = @import("compiled_plugin.zig");
67

78
const Self = @This();
89

@@ -49,6 +50,21 @@ pub fn initFromManifest(allocator: std.mem.Allocator, manifest: Manifest, functi
4950
return init(allocator, json, functions, wasi);
5051
}
5152

53+
/// Create a new plugin from a pre-compiled plugin
54+
pub fn initFromCompiled(compiled: *CompiledPlugin) !Self {
55+
var errmsg: [*c]u8 = null;
56+
const plugin = c.extism_plugin_new_from_compiled(compiled.ptr, &errmsg);
57+
if (plugin == null) {
58+
// TODO: figure out what to do with this error
59+
std.debug.print("extism_plugin_new: {s}\n", .{
60+
errmsg,
61+
});
62+
c.extism_plugin_new_error_free(errmsg);
63+
return error.PluginLoadFailed;
64+
}
65+
return plugin;
66+
}
67+
5268
pub fn deinit(self: *Self) void {
5369
c.extism_plugin_free(self.ptr);
5470
}

0 commit comments

Comments
 (0)