From 4cf7faa307ab6f2ff97fa77f46452e76bc2e9ccb Mon Sep 17 00:00:00 2001 From: luccie Date: Tue, 12 Aug 2025 01:34:27 +0200 Subject: [PATCH] [MODS/VM]: Make sure i*.store* truncate the value --- src/mods/vm.zig | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mods/vm.zig b/src/mods/vm.zig index c764b88..dc466b7 100644 --- a/src/mods/vm.zig +++ b/src/mods/vm.zig @@ -348,7 +348,7 @@ pub const Runtime = struct { .f32_store => @panic("UNIMPLEMENTED"), .f64_store => @panic("UNIMPLEMENTED"), .i32_store8 => { - const val = std.mem.nativeToLittle(i8, @as(i8, @intCast(self.stack.pop().?.i32))); + const val = std.mem.nativeToLittle(i8, @as(i8, @truncate(self.stack.pop().?.i32))); const offsetVal = self.stack.pop().?.i32; if (offsetVal < 0) { std.debug.panic("offsetVal is negative (val: {any})\n", .{offsetVal}); @@ -359,7 +359,7 @@ pub const Runtime = struct { @memcpy(self.memory[start..end], std.mem.asBytes(&val)); }, .i32_store16 => { - const val = std.mem.nativeToLittle(i16, @as(i16, @intCast(self.stack.pop().?.i32))); + const val = std.mem.nativeToLittle(i16, @as(i16, @truncate(self.stack.pop().?.i32))); const offsetVal = self.stack.pop().?.i32; if (offsetVal < 0) { std.debug.panic("offsetVal is negative (val: {any})\n", .{offsetVal}); @@ -370,7 +370,7 @@ pub const Runtime = struct { @memcpy(self.memory[start..end], std.mem.asBytes(&val)); }, .i64_store8 => { - const val = std.mem.nativeToLittle(i8, @as(i8, @intCast(self.stack.pop().?.i64))); + const val = std.mem.nativeToLittle(i8, @as(i8, @truncate(self.stack.pop().?.i64))); const offsetVal = self.stack.pop().?.i32; if (offsetVal < 0) { std.debug.panic("offsetVal is negative (val: {any})\n", .{offsetVal}); @@ -381,7 +381,7 @@ pub const Runtime = struct { @memcpy(self.memory[start..end], std.mem.asBytes(&val)); }, .i64_store16 => { - const val = std.mem.nativeToLittle(i16, @as(i16, @intCast(self.stack.pop().?.i64))); + const val = std.mem.nativeToLittle(i16, @as(i16, @truncate(self.stack.pop().?.i64))); const offsetVal = self.stack.pop().?.i32; if (offsetVal < 0) { std.debug.panic("offsetVal is negative (val: {any})\n", .{offsetVal}); @@ -392,7 +392,7 @@ pub const Runtime = struct { @memcpy(self.memory[start..end], std.mem.asBytes(&val)); }, .i64_store32 => { - const val = std.mem.nativeToLittle(i32, @as(i32, @intCast(self.stack.pop().?.i64))); + const val = std.mem.nativeToLittle(i32, @as(i32, @truncate(self.stack.pop().?.i64))); const offsetVal = self.stack.pop().?.i32; if (offsetVal < 0) { std.debug.panic("offsetVal is negative (val: {any})\n", .{offsetVal}); @@ -924,6 +924,7 @@ pub const Runtime = struct { } try self.executeFrame(allocator, &frame); + // std.debug.print("Returning from {d}\n", .{function}); allocator.free(frame.locals); },