Skip to content

Commit 9319add

Browse files
authored
More helpers (#202)
# Description Rename `addHostFunction` to `exposeHostFunction` and add similar functions for globals, tables, etc.
1 parent 6f0bec2 commit 9319add

4 files changed

Lines changed: 87 additions & 70 deletions

File tree

src/store.zig

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Elem = @import("store/elem.zig").Elem;
1010
const Data = @import("store/data.zig").Data;
1111
const Import = @import("module.zig").Import;
1212
const Tag = @import("module.zig").Tag;
13+
const Mutability = @import("module.zig").Mutability;
1314
const RefType = @import("valtype.zig").RefType;
1415
const ValType = @import("valtype.zig").ValType;
1516
const Instance = @import("instance.zig").Instance;
@@ -103,6 +104,8 @@ pub const ArrayListStore = struct {
103104
});
104105
}
105106

107+
/// Given a funcaddr return a Function if the funcaddr exists
108+
/// otherwise error with BadFunctionIndex.
106109
pub fn function(self: *ArrayListStore, funcaddr: usize) !Function {
107110
if (funcaddr >= self.functions.items.len) return error.BadFunctionIndex;
108111
return self.functions.items[funcaddr];
@@ -114,33 +117,23 @@ pub const ArrayListStore = struct {
114117
return self.functions.items.len - 1;
115118
}
116119

117-
// Helper function for adding a host function
118-
pub fn addHostFunction(self: *ArrayListStore, module: []const u8, function_name: []const u8, host_function_pointer: anytype, params: []const ValType, results: []const ValType) !void {
119-
const handle = try self.addFunction(Function{
120-
.params = params,
121-
.results = results,
122-
.subtype = .{
123-
.host_function = .{
124-
.func = host_function_pointer,
125-
},
126-
},
127-
});
128-
129-
try self.@"export"(module[0..], function_name[0..], .Func, handle);
130-
}
131-
120+
/// Given a memaddr return a pointer to the Memory if the memaddr exists
121+
/// otherwise error with BadMemoryIndex.
132122
pub fn memory(self: *ArrayListStore, memaddr: usize) !*Memory {
133123
if (memaddr >= self.memories.items.len) return error.BadMemoryIndex;
134124
return &self.memories.items[memaddr];
135125
}
136126

127+
/// Allocate a new Memory with min / max size and add to store.
137128
pub fn addMemory(self: *ArrayListStore, min: u32, max: ?u32) !usize {
138129
const mem_ptr = try self.memories.addOne();
139130
mem_ptr.* = Memory.init(self.alloc, min, max);
140131
_ = try mem_ptr.grow(min);
141132
return self.memories.items.len - 1;
142133
}
143134

135+
/// Given a tableaddr return a pointer to the Table if the tableaddr exists
136+
/// otherwise error with BadTableIndex.
144137
pub fn table(self: *ArrayListStore, tableaddr: usize) !*Table {
145138
if (tableaddr >= self.tables.items.len) return error.BadTableIndex;
146139
return &self.tables.items[tableaddr];
@@ -152,18 +145,23 @@ pub const ArrayListStore = struct {
152145
return self.tables.items.len - 1;
153146
}
154147

148+
/// Given a globaladdr return a pointer to the Global if the globaladdr exists
149+
/// otherwise error with BadGlobalIndex.
155150
pub fn global(self: *ArrayListStore, globaladdr: usize) !*Global {
156151
if (globaladdr >= self.globals.items.len) return error.BadGlobalIndex;
157152
return &self.globals.items[globaladdr];
158153
}
159154

155+
/// Add a Global to the store and return its globaladdr
160156
pub fn addGlobal(self: *ArrayListStore, value: Global) !usize {
161157
const glbl_ptr = try self.globals.addOne();
162158
glbl_ptr.* = value;
163159

164160
return self.globals.items.len - 1;
165161
}
166162

163+
/// Given a elemaddr return a pointer to the Elem if the elemaddr exists
164+
/// otherwise error with BadElemAddr.
167165
pub fn elem(self: *ArrayListStore, elemaddr: usize) !*Elem {
168166
if (elemaddr >= self.elems.items.len) return error.BadElemAddr;
169167
return &self.elems.items[elemaddr];
@@ -175,6 +173,8 @@ pub const ArrayListStore = struct {
175173
return self.elems.items.len - 1;
176174
}
177175

176+
/// Given a dataaddr return a pointer to the Data if the dataaddr exists
177+
/// otherwise error with BadDataAddr.
178178
pub fn data(self: *ArrayListStore, dataaddr: usize) !*Data {
179179
if (dataaddr >= self.datas.items.len) return error.BadDataAddr;
180180
return &self.datas.items[dataaddr];
@@ -185,4 +185,54 @@ pub const ArrayListStore = struct {
185185
data_ptr.* = try Data.init(self.alloc, count);
186186
return self.datas.items.len - 1;
187187
}
188+
189+
// Helper functions for exposing values
190+
191+
pub fn exposeHostFunction(self: *ArrayListStore, module: []const u8, function_name: []const u8, host_function_pointer: anytype, params: []const ValType, results: []const ValType) !void {
192+
const funcaddr = try self.addFunction(Function{
193+
.params = params,
194+
.results = results,
195+
.subtype = .{
196+
.host_function = .{
197+
.func = host_function_pointer,
198+
},
199+
},
200+
});
201+
202+
try self.@"export"(module[0..], function_name[0..], .Func, funcaddr);
203+
}
204+
205+
pub fn exposeMemory(self: *ArrayListStore, module: []const u8, name: []const u8, min: u32, max: ?u32) !void {
206+
const memaddr = try self.addMemory(min, max);
207+
208+
try self.@"export"(module, name, .Mem, memaddr);
209+
}
210+
211+
pub fn exposeTable(self: *ArrayListStore, module: []const u8, name: []const u8, reftype: RefType, entries: u32, max: ?u32) !void {
212+
const tableaddr = try self.addTable(reftype, entries, max);
213+
214+
try self.@"export"(module, name, .Table, tableaddr);
215+
}
216+
217+
pub fn exposeGlobal(self: *ArrayListStore, module: []const u8, name: []const u8, value: u64, valtype: ValType, mutability: Mutability) !void {
218+
const globaladdr = try self.addGlobal(.{
219+
.value = value,
220+
.valtype = valtype,
221+
.mutability = mutability,
222+
});
223+
224+
try self.@"export"(module, name, .Global, globaladdr);
225+
}
226+
227+
pub fn exposeElem(self: *ArrayListStore, module: []const u8, name: []const u8, reftype: RefType, count: u32) !void {
228+
const elemaddr = try self.addElem(reftype, count);
229+
230+
try self.@"export"(module, name, .Elem, elemaddr);
231+
}
232+
233+
pub fn exposeData(self: *ArrayListStore, module: []const u8, name: []const u8, count: u32) !void {
234+
const dataaddr = try self.addData(count);
235+
236+
try self.@"export"(module, name, .Data, dataaddr);
237+
}
188238
};

test/generate_interface/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# generate_interface
22

3-
Provide `generate_interface` with a `.wasm` and it will print out host function stubs and `addHostFunction` code for the interfacing with that `.wasm` binary.
3+
Provide `generate_interface` with a `.wasm` and it will print out host function stubs and `exposeHostFunction` code for the interfacing with that `.wasm` binary.

test/generate_interface/src/generate_interface.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn main() !void {
4242

4343
const function_type = module.types.list.items[function.typeidx];
4444

45-
try stdout.print("\ttry store.addHostFunction(\"{s}\", \"{s}\", {s}, &[_]zware.ValType{{", .{function_import.module, function_import.name, function_import.name});
45+
try stdout.print("\ttry store.exposeHostFunction(\"{s}\", \"{s}\", {s}, &[_]zware.ValType{{", .{function_import.module, function_import.name, function_import.name});
4646
for (function_type.params, 0..) |param, i| {
4747
try stdout.print(".{s}", .{@tagName(param)});
4848

test/testrunner/src/testrunner.zig

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -97,61 +97,28 @@ pub fn main() anyerror!void {
9797

9898
// Initialise a store
9999
var store: Store = Store.init(alloc);
100-
const spectest_module = "spectest";
100+
const spectest = "spectest";
101101

102102
// Init spectest memory
103-
const mem_handle = try store.addMemory(1, 2);
104-
_ = try store.memory(mem_handle);
105-
106-
const spectest_memory_name = "memory";
107-
try store.@"export"(spectest_module[0..], spectest_memory_name[0..], .Mem, mem_handle);
108-
109-
// Init spec test table
110-
const table_handle = try store.addTable(.FuncRef, 10, 20);
111-
const spectest_table_name = "table";
112-
113-
try store.@"export"(spectest_module[0..], spectest_table_name[0..], .Table, table_handle);
114-
115-
// Initiliase spectest globals
116-
const i32_handle = try store.addGlobal(Global{
117-
.value = 666,
118-
.valtype = .I32,
119-
.mutability = .Immutable,
120-
});
121-
const i32_name = "global_i32";
122-
try store.@"export"(spectest_module[0..], i32_name[0..], .Global, i32_handle);
123-
124-
const i64_handle = try store.addGlobal(Global{
125-
.value = 666,
126-
.valtype = .I64,
127-
.mutability = .Immutable,
128-
});
129-
const i64_name = "global_i64";
130-
try store.@"export"(spectest_module[0..], i64_name[0..], .Global, i64_handle);
131-
132-
const f32_handle = try store.addGlobal(Global{
133-
.value = 666,
134-
.valtype = .F32,
135-
.mutability = .Immutable,
136-
});
137-
const f32_name = "global_f32";
138-
try store.@"export"(spectest_module[0..], f32_name[0..], .Global, f32_handle);
139-
140-
const f64_handle = try store.addGlobal(Global{
141-
.value = 666,
142-
.valtype = .F64,
143-
.mutability = .Immutable,
144-
});
145-
const f64_name = "global_f64";
146-
try store.@"export"(spectest_module[0..], f64_name[0..], .Global, f64_handle);
147-
148-
try store.addHostFunction(spectest_module[0..], "print", print, &[_]ValType{}, &[_]ValType{});
149-
try store.addHostFunction(spectest_module[0..], "print_i32", print_i32, &[_]ValType{.I32}, &[_]ValType{});
150-
try store.addHostFunction(spectest_module[0..], "print_i64", print_i64, &[_]ValType{.I64}, &[_]ValType{});
151-
try store.addHostFunction(spectest_module[0..], "print_f32", print_f32, &[_]ValType{.F32}, &[_]ValType{});
152-
try store.addHostFunction(spectest_module[0..], "print_f64", print_f64, &[_]ValType{.F64}, &[_]ValType{});
153-
try store.addHostFunction(spectest_module[0..], "print_i32_f32", print_i32_f32, &[_]ValType{.I32, .F32}, &[_]ValType{});
154-
try store.addHostFunction(spectest_module[0..], "print_f64_f64", print_f64_f64, &[_]ValType{.F64, .F64}, &[_]ValType{});
103+
try store.exposeMemory(spectest, "memory", 1, 2);
104+
105+
// Init spectest table
106+
try store.exposeTable(spectest, "table", .FuncRef, 10, 20);
107+
108+
// Expose spectest globals
109+
try store.exposeGlobal(spectest, "global_i32", 666, .I32, .Immutable);
110+
try store.exposeGlobal(spectest, "global_i64", 666, .I64, .Immutable);
111+
try store.exposeGlobal(spectest, "global_f32", 666, .F32, .Immutable);
112+
try store.exposeGlobal(spectest, "global_f64", 666, .F64, .Immutable);
113+
114+
// Expose host functions
115+
try store.exposeHostFunction(spectest, "print", print, &[_]ValType{}, &[_]ValType{});
116+
try store.exposeHostFunction(spectest, "print_i32", print_i32, &[_]ValType{.I32}, &[_]ValType{});
117+
try store.exposeHostFunction(spectest, "print_i64", print_i64, &[_]ValType{.I64}, &[_]ValType{});
118+
try store.exposeHostFunction(spectest, "print_f32", print_f32, &[_]ValType{.F32}, &[_]ValType{});
119+
try store.exposeHostFunction(spectest, "print_f64", print_f64, &[_]ValType{.F64}, &[_]ValType{});
120+
try store.exposeHostFunction(spectest, "print_i32_f32", print_i32_f32, &[_]ValType{.I32, .F32}, &[_]ValType{});
121+
try store.exposeHostFunction(spectest, "print_f64_f64", print_f64_f64, &[_]ValType{.F64, .F64}, &[_]ValType{});
155122

156123
var current_instance: *Instance = undefined;
157124
var registered_names = StringHashMap(*Instance).init(alloc);

0 commit comments

Comments
 (0)