Skip to content

Commit

Permalink
fix: OP_DEFINE_GLOBAL doesn't always add global at the end of the list
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Nov 20, 2024
1 parent 20324b3 commit 618ba9a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1031,11 +1031,15 @@ pub const VM = struct {
}

fn OP_DEFINE_GLOBAL(self: *Self, _: *CallFrame, _: u32, _: OpCode, arg: u24) void {
self.globals.ensureTotalCapacity(arg + 1) catch {
const new_len = @max(arg + 1, self.globals.items.len);

self.globals.ensureTotalCapacity(new_len) catch {
self.panic("Out of memory");
unreachable;
};
self.globals.items.len = arg + 1;

// We don't always define a new global at the end of the list
self.globals.items.len = new_len;
self.globals.items[arg] = self.peek(0);

self.globals_count = @max(self.globals_count, arg);
Expand Down

0 comments on commit 618ba9a

Please sign in to comment.