Skip to content

Commit

Permalink
fix: debug.dump would mistakenly output non-existent object default v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
giann committed Jul 19, 2024
1 parent f524082 commit 171795a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
30 changes: 16 additions & 14 deletions src/Chunk.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Ast = @import("Ast.zig");
const Value = @import("value.zig").Value;
const VM = @import("vm.zig").VM;
Expand Down Expand Up @@ -141,37 +140,40 @@ const Self = @This();

pub const max_constants: u24 = std.math.maxInt(u24);

allocator: std.mem.Allocator,
/// AST
ast: Ast,
/// List of opcodes to execute
code: std.ArrayList(u32),
code: std.ArrayListUnmanaged(u32),
/// List of locations
lines: std.ArrayList(Ast.TokenIndex),
lines: std.ArrayListUnmanaged(Ast.TokenIndex),
/// List of constants defined in this chunk
constants: std.ArrayList(Value),
constants: std.ArrayListUnmanaged(Value),

pub fn init(allocator: Allocator, ast: Ast) Self {
pub fn init(allocator: std.mem.Allocator, ast: Ast) Self {
return Self{
.allocator = allocator,
.ast = ast,
.code = std.ArrayList(u32).init(allocator),
.constants = std.ArrayList(Value).init(allocator),
.lines = std.ArrayList(Ast.TokenIndex).init(allocator),
.code = std.ArrayListUnmanaged(u32){},
.constants = std.ArrayListUnmanaged(Value){},
.lines = std.ArrayListUnmanaged(Ast.TokenIndex){},
};
}

pub fn deinit(self: *Self) void {
self.code.deinit();
self.constants.deinit();
self.lines.deinit();
self.code.deinit(self.allocator);
self.constants.deinit(self.allocator);
self.lines.deinit(self.allocator);
}

pub fn write(self: *Self, code: u32, where: Ast.TokenIndex) !void {
try self.code.append(code);
try self.lines.append(where);
try self.code.append(self.allocator, code);
try self.lines.append(self.allocator, where);
}

pub fn addConstant(self: *Self, vm: ?*VM, value: Value) !u24 {
if (vm) |uvm| uvm.push(value);
try self.constants.append(value);
try self.constants.append(self.allocator, value);
if (vm) |uvm| _ = uvm.pop();

return @intCast(self.constants.items.len - 1);
Expand Down
8 changes: 7 additions & 1 deletion src/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ fn valueDump(value: Value, vm: *VM, seen: *std.AutoHashMap(*_obj.Obj, void), dep
},
);

if (if (field.static) object.fields[field.index] else object.defaults[field.index]) |v| {
if (if (field.static)
object.fields[field.index]
else if (!field.method)
object.defaults[field.index]
else
null) |v|
{
io.print(" = ", .{});
valueDump(v, vm, seen, depth + 1);
}
Expand Down
35 changes: 19 additions & 16 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -555,17 +555,10 @@ pub const VM = struct {
}

pub inline fn cloneValue(self: *Self, value: Value) !Value {
return if (value.isObj()) try cloneObject(value.obj(), self) else value;
}

inline fn clone(self: *Self) !void {
self.push(try self.cloneValue(self.pop()));
}

inline fn swap(self: *Self, from: u8, to: u8) void {
const temp: Value = (self.current_fiber.stack_top - to - 1)[0];
(self.current_fiber.stack_top - to - 1)[0] = (self.current_fiber.stack_top - from - 1)[0];
(self.current_fiber.stack_top - from - 1)[0] = temp;
return if (value.isObj())
try cloneObject(value.obj(), self)
else
value;
}

pub inline fn currentFrame(self: *Self) ?*CallFrame {
Expand Down Expand Up @@ -993,10 +986,12 @@ pub const VM = struct {
}

fn OP_CLONE(self: *Self, _: *CallFrame, _: u32, _: OpCode, _: u24) void {
self.clone() catch {
self.panic("Out of memory");
unreachable;
};
self.push(
self.cloneValue(self.pop()) catch {
self.panic("Out of memory");
unreachable;
},
);

const next_full_instruction: u32 = self.readInstruction();
@call(
Expand All @@ -1013,7 +1008,12 @@ pub const VM = struct {
}

fn OP_SWAP(self: *Self, _: *CallFrame, _: u32, _: OpCode, arg: u24) void {
self.swap(@as(u8, @intCast(arg)), self.readByte());
const from = @as(u8, @intCast(arg));
const to = self.readByte();

const temp: Value = (self.current_fiber.stack_top - to - 1)[0];
(self.current_fiber.stack_top - to - 1)[0] = (self.current_fiber.stack_top - from - 1)[0];
(self.current_fiber.stack_top - from - 1)[0] = temp;

const next_full_instruction: u32 = self.readInstruction();
@call(
Expand Down Expand Up @@ -4333,6 +4333,7 @@ pub const VM = struct {

// The now compile hotspot must be a new constant for the current function
self.currentFrame().?.closure.function.chunk.constants.append(
self.currentFrame().?.closure.function.chunk.allocator,
obj_native.toValue(),
) catch {
self.panic("Out of memory");
Expand Down Expand Up @@ -5212,12 +5213,14 @@ pub const VM = struct {
};

try chunk.code.replaceRange(
chunk.allocator,
to - hotspot_call.len,
hotspot_call.len,
&hotspot_call,
);

try chunk.lines.replaceRange(
chunk.allocator,
to - hotspot_call.len,
hotspot_call.len,
&[_]Ast.TokenIndex{
Expand Down

0 comments on commit 171795a

Please sign in to comment.