1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
pub const Parser = @import("Parser.zig");
pub const VM = @import("vm.zig");
// TODO: is this really needed?
pub const Wasm = @import("wasm.zig");
pub const IR = @import("ir.zig");
pub const GlobalRuntime = Wasm.GlobalRuntime;
pub const Runtime = VM.Runtime;
const std = @import("std");
test "Fibonacci" {
// var gpa = std.heap.GeneralPurposeAllocator(.{}){};
// const allocator = gpa.allocator();
const allocator = std.testing.allocator;
var global_runtime = GlobalRuntime.init(allocator);
defer global_runtime.deinit();
const file = try std.fs.cwd().openFile("assets/core.wasm", .{});
const all = try file.readToEndAlloc(allocator, 1_000_000); // 1 MB
var parser = Parser{
.bytes = all,
.byte_idx = 0,
.allocator = allocator,
};
const module = parser.parseModule() catch |err| {
std.debug.print("[ERROR]: error at byte {x}(0x{x})\n", .{ parser.byte_idx, parser.bytes[parser.byte_idx] });
return err;
};
var runtime = try Runtime.init(allocator, module, &global_runtime);
defer runtime.deinit(allocator);
var parameters = [_]usize{17};
try runtime.callExternal(allocator, "preinit", ¶meters);
const result = runtime.stack.pop().?;
try std.testing.expect(result.i64 == 1597);
var parameters2 = [_]usize{1};
try runtime.callExternal(allocator, "preinit", ¶meters2);
const result2 = runtime.stack.pop().?;
try std.testing.expect(result2.i64 == 1);
var parameters3 = [_]usize{5};
try runtime.callExternal(allocator, "preinit", ¶meters3);
const result3 = runtime.stack.pop().?;
try std.testing.expect(result3.i64 == 5);
}
|