Skip to content

Commit

Permalink
cleanup: rename namespaced functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Oct 13, 2023
1 parent c309994 commit cdca73e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
16 changes: 8 additions & 8 deletions src/Memory.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const c = @import("ffi.zig");
const extism = @import("ffi.zig");

const Self = @This();

Expand All @@ -19,12 +19,12 @@ pub fn load(self: Self, buf: []u8) void {
var i: usize = 0;
while (i < len) {
if (len - i < 8) {
buf[i] = c.extism_load_u8(self.offset + @as(u64, i));
buf[i] = extism.load_u8(self.offset + @as(u64, i));
i += 1;
continue;
}

const x = c.extism_load_u64(self.offset + @as(u64, i));
const x = extism.load_u64(self.offset + @as(u64, i));
std.mem.writeIntLittle(u64, buf[i..][0..8], x);
i += 8;
}
Expand All @@ -35,31 +35,31 @@ pub fn store(self: Self, buf: []const u8) void {
var i: usize = 0;
while (i < len) {
if (len - i < 8) {
c.extism_store_u8(self.offset + @as(u64, i), buf[i]);
extism.store_u8(self.offset + @as(u64, i), buf[i]);
i += 1;
continue;
}
const data = std.mem.readIntLittle(u64, buf[i..][0..8]);
c.extism_store_u64(self.offset + @as(u64, i), data);
extism.store_u64(self.offset + @as(u64, i), data);
i += 8;
}
}

pub fn allocate(length: usize) Self {
const c_len = @as(u64, length);
const offset = c.extism_alloc(c_len);
const offset = extism.alloc(c_len);

return .{ .offset = @as(u64, offset), .length = @as(u64, c_len) };
}

pub fn allocateBytes(data: []const u8) Self {
const c_len = @as(u64, data.len);
const offset = c.extism_alloc(c_len);
const offset = extism.alloc(c_len);
const mem = init(offset, c_len);
mem.store(data);
return .{ .offset = offset, .length = c_len };
}

pub fn free(self: Self) void {
c.extism_free(self.offset);
extism.free(self.offset);
}
42 changes: 21 additions & 21 deletions src/ffi.zig
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
pub const ExtismPointer = u64;
pub extern "extism:env" fn extism_input_length() u64;
pub extern "extism:env" fn extism_length(ExtismPointer) u64;
pub extern "extism:env" fn extism_alloc(u64) ExtismPointer;
pub extern "extism:env" fn extism_free(ExtismPointer) void;
pub extern "extism:env" fn extism_input_load_u8(ExtismPointer) u8;
pub extern "extism:env" fn extism_input_load_u64(ExtismPointer) u64;
pub extern "extism:env" fn extism_output_set(ExtismPointer, u64) void;
pub extern "extism:env" fn extism_error_set(ExtismPointer) void;
pub extern "extism:env" fn extism_config_get(ExtismPointer) ExtismPointer;
pub extern "extism:env" fn extism_var_get(ExtismPointer) ExtismPointer;
pub extern "extism:env" fn extism_var_set(ExtismPointer, ExtismPointer) void;
pub extern "extism:env" fn extism_store_u8(ExtismPointer, u8) void;
pub extern "extism:env" fn extism_load_u8(ExtismPointer) u8;
pub extern "extism:env" fn extism_store_u64(ExtismPointer, u64) void;
pub extern "extism:env" fn extism_load_u64(ExtismPointer) u64;
pub extern "extism:env" fn extism_http_request(ExtismPointer, ExtismPointer) ExtismPointer;
pub extern "extism:env" fn extism_http_status_code() i32;
pub extern "extism:env" fn extism_log_info(ExtismPointer) void;
pub extern "extism:env" fn extism_log_debug(ExtismPointer) void;
pub extern "extism:env" fn extism_log_warn(ExtismPointer) void;
pub extern "extism:env" fn extism_log_error(ExtismPointer) void;
pub extern "extism:env" fn input_length() u64;
pub extern "extism:env" fn length(ExtismPointer) u64;
pub extern "extism:env" fn alloc(u64) ExtismPointer;
pub extern "extism:env" fn free(ExtismPointer) void;
pub extern "extism:env" fn input_load_u8(ExtismPointer) u8;
pub extern "extism:env" fn input_load_u64(ExtismPointer) u64;
pub extern "extism:env" fn output_set(ExtismPointer, u64) void;
pub extern "extism:env" fn error_set(ExtismPointer) void;
pub extern "extism:env" fn config_get(ExtismPointer) ExtismPointer;
pub extern "extism:env" fn var_get(ExtismPointer) ExtismPointer;
pub extern "extism:env" fn var_set(ExtismPointer, ExtismPointer) void;
pub extern "extism:env" fn store_u8(ExtismPointer, u8) void;
pub extern "extism:env" fn load_u8(ExtismPointer) u8;
pub extern "extism:env" fn store_u64(ExtismPointer, u64) void;
pub extern "extism:env" fn load_u64(ExtismPointer) u64;
pub extern "extism:env" fn http_request(ExtismPointer, ExtismPointer) ExtismPointer;
pub extern "extism:env" fn http_status_code() i32;
pub extern "extism:env" fn log_info(ExtismPointer) void;
pub extern "extism:env" fn log_debug(ExtismPointer) void;
pub extern "extism:env" fn log_warn(ExtismPointer) void;
pub extern "extism:env" fn log_error(ExtismPointer) void;
40 changes: 20 additions & 20 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const c = @import("ffi.zig");
const extism = @import("ffi.zig");
const Memory = @import("Memory.zig");
pub const http = @import("http.zig");

Expand All @@ -16,18 +16,18 @@ pub const Plugin = struct {

// IMPORTANT: It's the caller's responsibility to free the returned slice
pub fn getInput(self: Plugin) ![]u8 {
const len = c.extism_input_length();
const len = extism.input_length();
var buf = try self.allocator.alloc(u8, @intCast(len));
errdefer self.allocator.free(buf);
var i: usize = 0;
while (i < len) {
if (len - i < 8) {
buf[i] = c.extism_input_load_u8(@as(u64, i));
buf[i] = extism.input_load_u8(@as(u64, i));
i += 1;
continue;
}

const x = c.extism_input_load_u64(@as(u64, i));
const x = extism.input_load_u64(@as(u64, i));
std.mem.writeIntLittle(u64, buf[i..][0..8], x);
i += 8;
}
Expand All @@ -36,25 +36,25 @@ pub const Plugin = struct {

pub fn outputMemory(self: Plugin, mem: Memory) void {
_ = self; // to make the interface consistent
c.extism_output_set(mem.offset, mem.length);
extism.output_set(mem.offset, mem.length);
}

pub fn output(self: Plugin, data: []const u8) void {
_ = self; // to make the interface consistent
const c_len = @as(u64, data.len);
const offset = c.extism_alloc(c_len);
const offset = extism.alloc(c_len);
const memory = Memory.init(offset, c_len);
defer memory.free();
memory.store(data);
c.extism_output_set(offset, c_len);
extism.output_set(offset, c_len);
}

/// IMPORTANT: it's the caller's responsibility to free the returned string
pub fn getConfig(self: Plugin, key: []const u8) !?[]u8 {
const key_mem = Memory.allocateBytes(key);
defer key_mem.free();
const offset = c.extism_config_get(key_mem.offset);
const c_len = c.extism_length(offset);
const offset = extism.config_get(key_mem.offset);
const c_len = extism.length(offset);
if (offset == 0 or c_len == 0) {
return null;
}
Expand All @@ -69,10 +69,10 @@ pub const Plugin = struct {
pub fn logMemory(self: Plugin, level: LogLevel, memory: Memory) void {
_ = self; // to make the interface consistent
switch (level) {
.Info => c.extism_log_info(memory.offset),
.Debug => c.extism_log_debug(memory.offset),
.Warn => c.extism_log_warn(memory.offset),
.Error => c.extism_log_error(memory.offset),
.Info => extism.log_info(memory.offset),
.Debug => extism.log_debug(memory.offset),
.Warn => extism.log_warn(memory.offset),
.Error => extism.log_error(memory.offset),
}
}

Expand All @@ -86,8 +86,8 @@ pub const Plugin = struct {
pub fn getVar(self: Plugin, key: []const u8) !?[]u8 {
const key_mem = Memory.allocateBytes(key);
defer key_mem.free();
const offset = c.extism_var_get(key_mem.offset);
const c_len = c.extism_length(offset);
const offset = extism.var_get(key_mem.offset);
const c_len = extism.length(offset);
if (offset == 0 or c_len == 0) {
return null;
}
Expand All @@ -105,14 +105,14 @@ pub const Plugin = struct {
defer key_mem.free();
const val_mem = Memory.allocateBytes(value);
defer val_mem.free();
c.extism_var_set(key_mem.offset, val_mem.offset);
extism.var_set(key_mem.offset, val_mem.offset);
}

pub fn removeVar(self: Plugin, key: []const u8) void {
_ = self; // to make the interface consistent
const mem = Memory.allocateBytes(key);
defer mem.free();
c.extism_var_set(mem.offset, 0);
extism.extism_var_set(mem.offset, 0);
}

pub fn request(self: Plugin, http_request: http.HttpRequest, body: ?[]const u8) !http.HttpResponse {
Expand All @@ -128,9 +128,9 @@ pub const Plugin = struct {
}
};
defer req_body.free();
const offset = c.extism_http_request(req.offset, req_body.offset);
const length = c.extism_length(offset);
const status: u16 = @intCast(c.extism_http_status_code());
const offset = extism.http_request(req.offset, req_body.offset);
const length = extism.length(offset);
const status: u16 = @intCast(extism.http_status_code());

const mem = Memory.init(offset, length);
defer mem.free();
Expand Down

0 comments on commit cdca73e

Please sign in to comment.