Skip to content

Commit

Permalink
fix(value): Function 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 f809081 commit 621a1ca
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/Codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,6 @@ fn generateFunction(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?
self.gc.allocator,
self.ast,
node,
node_type_def.resolved_type.?.Function.name,
);

function.type_def = node_type_def;
Expand Down
11 changes: 4 additions & 7 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,11 @@ pub const Obj = struct {
},
.Closure => try writer.print("closure: 0x{x} `{s}`", .{
@intFromPtr(ObjClosure.cast(obj).?),
ObjClosure.cast(obj).?.function.name.string,
ObjClosure.cast(obj).?.function.type_def.resolved_type.?.Function.name.string,
}),
.Function => try writer.print("function: 0x{x} `{s}`", .{
@intFromPtr(ObjFunction.cast(obj).?),
ObjFunction.cast(obj).?.name.string,
ObjFunction.cast(obj).?.type_def.resolved_type.?.Function.name.string,
}),
.ObjectInstance => {
const instance = ObjObjectInstance.cast(obj).?;
Expand Down Expand Up @@ -648,7 +648,7 @@ pub const Obj = struct {
const bound: *ObjBoundMethod = ObjBoundMethod.cast(obj).?;

if (bound.closure) |closure| {
const closure_name: []const u8 = closure.function.name.string;
const closure_name: []const u8 = closure.function.type_def.resolved_type.?.Function.name.string;
try writer.writeAll("bound method: ");

try (bound.receiver).toString(writer);
Expand Down Expand Up @@ -1267,7 +1267,6 @@ pub const ObjFunction = struct {

type_def: *ObjTypeDef = undefined, // Undefined because function initialization is in several steps

name: *ObjString,
chunk: Chunk,
upvalue_count: u8 = 0,

Expand All @@ -1282,9 +1281,8 @@ pub const ObjFunction = struct {
// JIT compiled function callable by buzz VM
native: ?*anyopaque = null,

pub fn init(allocator: Allocator, ast: Ast, node: Ast.Node.Index, name: *ObjString) !Self {
pub fn init(allocator: Allocator, ast: Ast, node: Ast.Node.Index) !Self {
return Self{
.name = name,
.node = node,
.chunk = Chunk.init(allocator, ast),
};
Expand All @@ -1295,7 +1293,6 @@ pub const ObjFunction = struct {
}

pub fn mark(self: *Self, gc: *GarbageCollector) !void {
try gc.markObj(self.name.toObj());
try gc.markObj(@constCast(self.type_def.toObj()));
if (BuildOptions.gc_debug) {
io.print("MARKING CONSTANTS OF FUNCTION @{} {s}\n", .{ @intFromPtr(self), self.name.string });
Expand Down
5 changes: 3 additions & 2 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4558,6 +4558,7 @@ pub const VM = struct {
var writer = msg.writer();

if (next) |unext| {
const function_name = unext.closure.function.type_def.resolved_type.?.Function.name.string;
writer.print(
"\t{s} in \x1b[36m{s}\x1b[0m at {s}",
.{
Expand All @@ -4568,9 +4569,9 @@ pub const VM = struct {
else
" ╰─",
if (unext.closure.function.type_def.resolved_type.?.Function.function_type == .Test)
unext.closure.function.name.string[(std.mem.indexOfScalar(u8, unext.closure.function.name.string, ' ').? + 1)..]
function_name[(std.mem.indexOfScalar(u8, function_name, ' ').? + 1)..]
else
unext.closure.function.name.string,
function_name,
if (frame.call_site) |call_site|
self.current_ast.tokens.items(.script_name)[call_site]
else
Expand Down

0 comments on commit 621a1ca

Please sign in to comment.