Skip to content

Commit 7962d70

Browse files
authored
cleanup: host takes ownership of memory blocks it gets as arguments (#34)
* cleanup: host takes ownership of memory blocks it gets as arguments * fix: use statusCode for method and status for field name
1 parent 1d75537 commit 7962d70

File tree

2 files changed

+1
-10
lines changed

2 files changed

+1
-10
lines changed

src/http.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub const HttpResponse = struct {
1717
self.memory.free();
1818
}
1919

20-
pub fn status(self: HttpResponse) u16 {
20+
pub fn statusCode(self: HttpResponse) u16 {
2121
return self.status;
2222
}
2323
};

src/main.zig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ pub const Plugin = struct {
125125
/// IMPORTANT: it's the caller's responsibility to free the returned string
126126
pub fn getConfig(self: Plugin, key: []const u8) !?[]u8 {
127127
const key_mem = self.allocateBytes(key);
128-
defer key_mem.free();
129128
const offset = extism.config_get(key_mem.offset);
130129
const c_len = extism.length(offset);
131130
if (offset == 0 or c_len == 0) {
@@ -149,22 +148,19 @@ pub const Plugin = struct {
149148

150149
pub fn log(self: Plugin, level: LogLevel, data: []const u8) void {
151150
const mem = self.allocateBytes(data);
152-
defer mem.free();
153151
self.logMemory(level, mem);
154152
}
155153

156154
/// IMPORTANT: it's the caller's responsibility to free the returned string
157155
pub fn getVar(self: Plugin, key: []const u8) !?[]u8 {
158156
const key_mem = self.allocateBytes(key);
159-
defer key_mem.free();
160157
const offset = extism.var_get(key_mem.offset);
161158
const c_len = extism.length(offset);
162159
if (offset == 0 or c_len == 0) {
163160
return null;
164161
}
165162
const memory = Memory.init(offset, c_len);
166163
defer memory.free();
167-
defer memory.free();
168164
const value = try memory.loadAlloc(self.allocator);
169165
return value;
170166
}
@@ -181,9 +177,7 @@ pub const Plugin = struct {
181177

182178
pub fn setVar(self: Plugin, key: []const u8, value: []const u8) void {
183179
const key_mem = self.allocateBytes(key);
184-
defer key_mem.free();
185180
const val_mem = self.allocateBytes(value);
186-
defer val_mem.free();
187181
extism.var_set(key_mem.offset, val_mem.offset);
188182
}
189183

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

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

204197
pub fn request(self: Plugin, http_request: http.HttpRequest, body: ?[]const u8) !http.HttpResponse {
205198
const json = try std.json.stringifyAlloc(self.allocator, http_request, .{ .emit_null_optional_fields = false });
206199
defer self.allocator.free(json);
207200
const req = self.allocateBytes(json);
208-
defer req.free();
209201
const req_body = b: {
210202
if (body) |bdy| {
211203
break :b self.allocateBytes(bdy);
212204
} else {
213205
break :b self.allocate(0);
214206
}
215207
};
216-
defer req_body.free();
217208
const offset = extism.http_request(req.offset, req_body.offset);
218209
const length = extism.length_unsafe(offset);
219210
const status: u16 = @intCast(extism.http_status_code());

0 commit comments

Comments
 (0)