diff --git a/src/Chunk.zig b/src/Chunk.zig index 48314d8e..b9e8dd7d 100644 --- a/src/Chunk.zig +++ b/src/Chunk.zig @@ -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; @@ -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); diff --git a/src/buzz_api.zig b/src/buzz_api.zig index 4ce7d386..4b25a729 100644 --- a/src/buzz_api.zig +++ b/src/buzz_api.zig @@ -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); } diff --git a/src/vm.zig b/src/vm.zig index c52b3580..13c97665 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -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 { @@ -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( @@ -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( @@ -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"); @@ -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{