diff --git a/assets/mods/core.wasm b/assets/mods/core.wasm index bc550f7..da6e6d1 100755 Binary files a/assets/mods/core.wasm and b/assets/mods/core.wasm differ diff --git a/modding/log.zig b/modding/log.zig deleted file mode 100644 index 3ccdee6..0000000 --- a/modding/log.zig +++ /dev/null @@ -1,19 +0,0 @@ -pub extern fn logErr( - string: *const u8, - len: u64, -) callconv(.c) void; - -pub extern fn logWarn( - string: *const u8, - len: u64, -) callconv(.c) void; - -pub extern fn logInfo( - string: *const u8, - len: u64, -) callconv(.c) void; - -pub extern fn logDebug( - string: *const u8, - len: u64, -) callconv(.c) void; diff --git a/src/mods/Parser.zig b/src/mods/Parser.zig index 7327f0d..441d0b3 100644 --- a/src/mods/Parser.zig +++ b/src/mods/Parser.zig @@ -318,8 +318,8 @@ pub fn parseModule(self: *Parser) !void { else => return Error.invalid_section, }; } - if (self.exports.preinit != null and self.exports.preinit.? != 0){ - self.exports.preinit.? -= self.importCount; + if (self.exports.init != null and self.exports.init.? != 0){ + self.exports.init.? -= self.importCount; } } @@ -385,7 +385,7 @@ fn parseImportsec(self: *Parser) !void { } else if (std.mem.eql(u8, i.name, "logErr")) { self.exports.logErr = index; } else { - std.log.warn("imported function {s} not supported\n", .{i.name}); + std.debug.panic("imported function {s} not supported\n", .{i.name}); } index += 1; }, @@ -546,8 +546,8 @@ fn parseExportsec(self: *Parser) !void { for (exports) |e| { switch (e.exportdesc) { .func => { - if (std.mem.eql(u8, e.name, "preinit")) { - self.exports.preinit = e.exportdesc.func; + if (std.mem.eql(u8, e.name, "init")) { + self.exports.init = e.exportdesc.func; } else { std.log.warn("exported function {s} not supported\n", .{e.name}); } diff --git a/src/mods/vm.zig b/src/mods/vm.zig index ee621b2..9757e27 100644 --- a/src/mods/vm.zig +++ b/src/mods/vm.zig @@ -33,14 +33,14 @@ pub const Function = struct { func_type: Functype, typ: union(enum) { } }; pub const ExportFunction = enum { - preinit, + init, logErr, logWarn, logInfo, logDebug, }; pub const Exports = struct { - preinit: ?u32 = null, + init: ?u32 = null, logErr: ?u32 = null, logWarn: ?u32 = null, logInfo: ?u32 = null, @@ -372,13 +372,21 @@ pub const Runtime = struct { const b = self.stack.pop().?.i32; try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b > a))) }); }, - .i32_le_s => @panic("UNIMPLEMENTED"), + .i32_le_s => { + const a = self.stack.pop().?.i32; + const b = self.stack.pop().?.i32; + try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b <= a))) }); + }, .i32_le_u => { const a = self.stack.pop().?.i32; const b = self.stack.pop().?.i32; try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b <= a))) }); }, - .i32_ge_s => @panic("UNIMPLEMENTED"), + .i32_ge_s => { + const a = self.stack.pop().?.i32; + const b = self.stack.pop().?.i32; + try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b >= a))) }); + }, .i32_ge_u => { const a = self.stack.pop().?.i32; const b = self.stack.pop().?.i32; @@ -471,8 +479,16 @@ pub const Runtime = struct { const b = self.stack.pop().?.i32; try self.stack.append(Value{ .i32 = (b << @as(u5, @intCast(a))) }); }, - .i32_shr_s => @panic("UNIMPLEMENTED"), - .i32_shr_u => @panic("UNIMPLEMENTED"), + .i32_shr_s => { + const a = self.stack.pop().?.i32; + const b = self.stack.pop().?.i32; + try self.stack.append(Value{ .i32 = (b >> @as(u5, @intCast(a))) }); + }, + .i32_shr_u => { + const a = self.stack.pop().?.i32; + const b = self.stack.pop().?.i32; + try self.stack.append(Value{ .i32 = (b >> @as(u5, @intCast(a))) }); + }, .i32_rotl => @panic("UNIMPLEMENTED"), .i32_rotr => @panic("UNIMPLEMENTED"), @@ -581,11 +597,11 @@ pub const Runtime = struct { // TODO: Do name resolution at parseTime pub fn callExternal(self: *Runtime, allocator: Allocator, name: ExportFunction, parameters: []Value) !void { switch (name) { - .preinit => { - if (self.module.exports.preinit) |func| { + .init => { + if (self.module.exports.init) |func| { try self.call(allocator, func, parameters); } else { - std.debug.panic("Function preinit unavailable\n", .{}); + std.debug.panic("Function init unavailable\n", .{}); } }, else => { diff --git a/src/sideros.zig b/src/sideros.zig index 7a7ea57..532e4e1 100644 --- a/src/sideros.zig +++ b/src/sideros.zig @@ -18,8 +18,8 @@ fn init_mods() void { var global_runtime = mods.GlobalRuntime.init(allocator); defer global_runtime.deinit(); - // const file = std.fs.cwd().openFile("assets/mods/core.wasm", .{}) catch @panic("Couldn't open assets/mods/core.wasm"); - const file = std.fs.cwd().openFile("./test.wasm", .{}) catch @panic("Couldn't open test.wasm"); + const file = std.fs.cwd().openFile("assets/mods/core.wasm", .{}) catch @panic("Couldn't open assets/mods/core.wasm"); + // const file = std.fs.cwd().openFile("./test.wasm", .{}) catch @panic("Couldn't open test.wasm"); const all = file.readToEndAlloc(allocator, 1_000_000) catch @panic("Unable to read the file"); // 1 MB defer allocator.free(all); var parser = mods.Parser.init(allocator, all) catch @panic("Failed to init parser"); @@ -38,9 +38,9 @@ fn init_mods() void { defer runtime.deinit(allocator); var parameters = [_]mods.VM.Value{.{ .i32 = 17 }}; - runtime.callExternal(allocator, .preinit, ¶meters) catch @panic("Failed to call to preinit"); + runtime.callExternal(allocator, .init, ¶meters) catch @panic("Failed to call to init"); const result = runtime.stack.pop().?; - std.debug.print("Result of preinit: {any}\n", .{result}); + std.debug.print("Result of init: {any}\n", .{result}); } export fn sideros_init(init: api.GameInit) callconv(.c) void {