Skip to content

Commit

Permalink
fix(value): Object doesn't need to hold its name
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jun 13, 2024
1 parent f70f879 commit f809081
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/Codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2948,12 +2948,8 @@ fn generateObjectDeclaration(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks
}
}

const name_constant = try self.makeConstant(object_def.name.toValue());
const object_type_constant = try self.makeConstant(object_type.toValue());

// Put object on the stack and define global with it
try self.emitCodeArg(location, .OP_OBJECT, name_constant);
try self.emit(location, @intCast(object_type_constant));
try self.emitCodeArg(location, .OP_OBJECT, try self.makeConstant(object_type.toValue()));
try self.emitCodeArg(location, .OP_DEFINE_GLOBAL, @intCast(components.slot));

// Put the object on the stack to set its fields
Expand Down
19 changes: 9 additions & 10 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ pub const Obj = struct {
"object instance: 0x{x} `{s}`",
.{
@intFromPtr(instance),
object.name.string,
object.type_def.resolved_type.?.Object.name.string,
},
);
} else {
Expand Down Expand Up @@ -588,10 +588,13 @@ pub const Obj = struct {
try writer.writeAll("}");
}
},
.Object => try writer.print("object: 0x{x} `{s}`", .{
@intFromPtr(ObjObject.cast(obj).?),
ObjObject.cast(obj).?.name.string,
}),
.Object => try writer.print(
"object: 0x{x} `{s}`",
.{
@intFromPtr(ObjObject.cast(obj).?),
ObjObject.cast(obj).?.type_def.resolved_type.?.Object.name.string,
},
),
.Range => {
const range = ObjRange.cast(obj).?;

Expand Down Expand Up @@ -1566,8 +1569,6 @@ pub const ObjObject = struct {

type_def: *ObjTypeDef,

/// Object name
name: *ObjString,
/// Static fields and methods
fields: []Value,
/// Properties default values (null if none)
Expand All @@ -1576,9 +1577,8 @@ pub const ObjObject = struct {
/// To avoid counting object fields that are instance properties
property_count: ?usize = 0,

pub fn init(allocator: Allocator, name: *ObjString, type_def: *ObjTypeDef) !Self {
pub fn init(allocator: Allocator, type_def: *ObjTypeDef) !Self {
const self = Self{
.name = name,
.fields = try allocator.alloc(
Value,
type_def.resolved_type.?.Object.fields.count(),
Expand Down Expand Up @@ -1629,7 +1629,6 @@ pub const ObjObject = struct {

pub fn mark(self: *Self, gc: *GarbageCollector) !void {
try gc.markObj(@constCast(self.type_def.toObj()));
try gc.markObj(self.name.toObj());

for (self.fields) |field| {
try gc.markValue(field);
Expand Down
7 changes: 2 additions & 5 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2796,15 +2796,12 @@ pub const VM = struct {
);
}

fn OP_OBJECT(self: *Self, _: *CallFrame, _: u32, _: OpCode, arg: u24) void {
fn OP_OBJECT(self: *Self, _: *CallFrame, _: u32, _: OpCode, type_def_constant: u24) void {
var object: *ObjObject = self.gc.allocateObject(
ObjObject,
ObjObject.init(
self.gc.allocator,
self.readConstant(arg).obj().access(ObjString, .String, self.gc).?,
self.readConstant(
@intCast(self.readInstruction()),
)
self.readConstant(type_def_constant)
.obj().access(ObjTypeDef, .Type, self.gc).?,
) catch {
self.panic("Out of memory");
Expand Down

0 comments on commit f809081

Please sign in to comment.