diff --git a/src/extern.zig b/src/extern.zig index d643119..019aef8 100644 --- a/src/extern.zig +++ b/src/extern.zig @@ -72,12 +72,47 @@ pub const Extern = extern struct { extern "c" fn wasmtime_extern_type(*const Store.Context, *const Extern) *ExternType; }; -// pub const ExternVec = Vec(*Extern, wasm_extern_vec_new_empty, wasm_extern_vec_new_uninitialized, wasm_extern_vec_new, wasm_extern_vec_copy, wasm_extern_vec_delete); -// extern "c" fn wasm_extern_vec_new_empty(*anyopaque, usize) void; -// extern "c" fn wasm_extern_vec_new_uninitialized(*anyopaque, usize) void; -// extern "c" fn wasm_extern_vec_new(*anyopaque, usize, [*]const ?*Extern) void; -// extern "c" fn wasm_extern_vec_copy(*anyopaque, *const anyopaque) void; -// extern "c" fn wasm_extern_vec_delete(*anyopaque) void; +test Extern { + const std = @import("std"); + const expect = std.testing.expect; + + const wasm = @import("./main.zig"); + + const wat_bytes = + \\(module + \\ (func (export "some_function")) + \\ (global (export "some_global") (mut i32) (i32.const 0)) + \\ (memory (export "some_memory") 1) + \\) + ; + + // If an error *does* occur, settings up Diagnostics with debug_print will + // make sure that the error will actually be visible in the test log. + var diag = try wasm.Diagnostics.init(std.testing.allocator, true); + defer diag.deinit(); + + var engine = try wasm.Engine.initDefault(); + defer engine.deinit(); + var module = try wasm.Module.initFromWat(engine, wat_bytes, diag); + defer module.deinit(); + var store = wasm.Store.init(engine); + defer store.deinit(); + var context = store.getContext(); + + var instance = try wasm.Instance.init(context, module, &.{}, null); + + const some_function = try instance.getByIndex(context, 0, wasm.Func); + const some_global = try instance.getByIndex(context, 1, wasm.Global); + const some_memory = try instance.getByIndex(context, 2, wasm.Memory); + + try expect(std.mem.eql(u8, some_function.name, "some_function")); + try expect(std.mem.eql(u8, some_global.name, "some_global")); + try expect(std.mem.eql(u8, some_memory.name, "some_memory")); + + try expect(instance.getByIndex(context, 3, wasm.Func) == error.ExportNotFound); + try expect(instance.get(context, "no_such_func", wasm.Func) == error.ExportNotFound); + try expect(instance.get(context, "some_function", wasm.Global) == error.IncorrectType); +} pub const ExternType = opaque { pub fn copy(self: *const ExternType) *ExternType { @@ -132,10 +167,3 @@ pub const ExternType = opaque { // extern "c" fn wasm_externtype_as_globaltype_const(*const ExternType) ?*const GlobalType; extern "c" fn wasm_externtype_as_memorytype_const(*const ExternType) ?*const MemoryType; }; - -// pub const ExternTypeVec = Vec(*ExternType, wasm_externtype_vec_new_empty, wasm_externtype_vec_new_uninitialized, wasm_externtype_vec_new, wasm_externtype_vec_copy, wasm_externtype_vec_delete); -// extern "c" fn wasm_externtype_vec_new_empty(*anyopaque, usize) void; -// extern "c" fn wasm_externtype_vec_new_uninitialized(*anyopaque, usize) void; -// extern "c" fn wasm_externtype_vec_new(*anyopaque, usize, [*]const ?*ExternType) void; -// extern "c" fn wasm_externtype_vec_copy(*anyopaque, *const anyopaque) void; -// extern "c" fn wasm_externtype_vec_delete(*anyopaque) void;