@@ -10,6 +10,7 @@ const Elem = @import("store/elem.zig").Elem;
1010const Data = @import ("store/data.zig" ).Data ;
1111const Import = @import ("module.zig" ).Import ;
1212const Tag = @import ("module.zig" ).Tag ;
13+ const Mutability = @import ("module.zig" ).Mutability ;
1314const RefType = @import ("valtype.zig" ).RefType ;
1415const ValType = @import ("valtype.zig" ).ValType ;
1516const 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};
0 commit comments