Skip to content

Commit

Permalink
cleanup: host takes ownership of memory blocks it gets as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Sep 19, 2024
1 parent 67465e4 commit f90d9ee
Showing 1 changed file with 0 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ pub const Plugin = struct {
/// IMPORTANT: it's the caller's responsibility to free the returned string
pub fn getConfig(self: Plugin, key: []const u8) !?[]u8 {
const key_mem = self.allocateBytes(key);
defer key_mem.free();
const offset = extism.config_get(key_mem.offset);
const c_len = extism.length(offset);
if (offset == 0 or c_len == 0) {
Expand All @@ -149,22 +148,19 @@ pub const Plugin = struct {

pub fn log(self: Plugin, level: LogLevel, data: []const u8) void {
const mem = self.allocateBytes(data);
defer mem.free();
self.logMemory(level, mem);
}

/// IMPORTANT: it's the caller's responsibility to free the returned string
pub fn getVar(self: Plugin, key: []const u8) !?[]u8 {
const key_mem = self.allocateBytes(key);
defer key_mem.free();
const offset = extism.var_get(key_mem.offset);
const c_len = extism.length(offset);
if (offset == 0 or c_len == 0) {
return null;
}
const memory = Memory.init(offset, c_len);
defer memory.free();
defer memory.free();
const value = try memory.loadAlloc(self.allocator);
return value;
}
Expand All @@ -181,9 +177,7 @@ pub const Plugin = struct {

pub fn setVar(self: Plugin, key: []const u8, value: []const u8) void {
const key_mem = self.allocateBytes(key);
defer key_mem.free();
const val_mem = self.allocateBytes(value);
defer val_mem.free();
extism.var_set(key_mem.offset, val_mem.offset);
}

Expand All @@ -197,23 +191,20 @@ pub const Plugin = struct {

pub fn removeVar(self: Plugin, key: []const u8) void {
const mem = self.allocateBytes(key);
defer mem.free();
extism.var_set(mem.offset, 0);
}

pub fn request(self: Plugin, http_request: http.HttpRequest, body: ?[]const u8) !http.HttpResponse {
const json = try std.json.stringifyAlloc(self.allocator, http_request, .{ .emit_null_optional_fields = false });
defer self.allocator.free(json);
const req = self.allocateBytes(json);
defer req.free();
const req_body = b: {
if (body) |bdy| {
break :b self.allocateBytes(bdy);
} else {
break :b self.allocate(0);
}
};
defer req_body.free();
const offset = extism.http_request(req.offset, req_body.offset);
const length = extism.length_unsafe(offset);
const status: u16 = @intCast(extism.http_status_code());
Expand Down

0 comments on commit f90d9ee

Please sign in to comment.