commit 00cf1d7e9a45844c1e2ab7a856f148326349dee7
parent 21c58c2489df36e524fda9c3b2cad7f122d51751
Author: Szymon Mikulicz <szymon.mikulicz@posteo.net>
Date: Wed, 2 Sep 2026 14:22:15 +0200
Packed with alloc n set|
Diffstat:
8 files changed, 36 insertions(+), 16 deletions(-)
diff --git a/zlox/src/lib::packed.zig b/zlox/src/lib::packed.zig
@@ -28,6 +28,12 @@ pub fn Packed(Type: type) type {
Self.init(try allocator.create(Child));
}
+ pub fn create2(allocator: std.mem.Allocator, val: Child) !Self {
+ const self = try Self.create(allocator);
+ self.set(val);
+ return self;
+ }
+
pub fn alloc(allocator: std.mem.Allocator, count: usize) !Self {
return if (slice)
Self.init(try allocator.alloc(Child, count))
@@ -39,6 +45,14 @@ pub fn Packed(Type: type) type {
@panic("Cannot alloc() a single-item pointer with a count of more than one");
}
+ pub fn alloc2(allocator: std.mem.Allocator, count: usize, val: Child) !Self {
+ const self = try Self.alloc(allocator, count);
+ const p = self.ptr();
+ for (0..count) |i|
+ p[i] = val;
+ return self;
+ }
+
pub fn init(arg: Type) Self {
return Self{
._ptr = if (optional)
@@ -118,7 +132,7 @@ pub fn Packed(Type: type) type {
}
}
- pub fn set(self: Self, val: if (many or slice) utils.mod_ptr_t(Ptr, "const", true) else Child) void {
+ pub fn set(self: Self, val: if (many or slice) utils.mod_ptr_t(ptr, "const", true) else Child) void {
if (many or slice)
@memcpy(self.ptr(), val)
else
diff --git a/zlox/src/obj::class.zig b/zlox/src/obj::class.zig
@@ -1,7 +1,10 @@
const std = @import("std");
const utils = @import("lib::utils.zig");
+const table = @import("lib::table.zig");
+const hash = @import("hash.zig");
+const Packed = @import("lib::packed.zig").Packed;
const Obj = @import("obj.zig").Obj;
pub fn Class(fields: anytype) type {
@@ -13,12 +16,16 @@ pub fn Class(fields: anytype) type {
pub const Arg = void;
pub const Error = error{OutOfMemory};
+ pub const Methods = table.Table(*Super.String, *Super.Closure, hash.hash_t(*Super.String), Super.String.eql);
+
obj: Super,
+ methods: Packed(*Methods),
pub fn init(_: Arg, allocator: std.mem.Allocator) Error!*Self {
const self: *Self = try allocator.create(Self);
self.* = Self{
.obj = Super.make(Self),
+ .methods = try Packed(*Methods).create2(allocator, Methods.init(allocator)),
};
return self;
}
@@ -36,6 +43,8 @@ pub fn Class(fields: anytype) type {
}
pub fn free(self: *const Self, allocator: std.mem.Allocator) void {
+ self.methods.ptr().deinit();
+ self.methods.destroy(allocator);
allocator.destroy(self);
}
};
diff --git a/zlox/src/obj::closure.zig b/zlox/src/obj::closure.zig
@@ -22,10 +22,9 @@ pub fn Closure(fields: anytype) type {
const self: *Self = try allocator.create(Self);
self.* = Self{
.obj = Super.make(Self),
- .upvalues = try Packed([]?*Super.Upvalue).alloc(allocator, arg.upvalue_count),
+ .upvalues = try Packed([]?*Super.Upvalue).alloc2(allocator, arg.upvalue_count, null),
.function = Packed(*Super.Function).init(arg),
};
- for (self.upvalues.ptr()) |*upvalue| upvalue.* = null;
return self;
}
diff --git a/zlox/src/obj::function.zig b/zlox/src/obj::function.zig
@@ -27,12 +27,11 @@ pub fn Function(fields: anytype) type {
const self: *Self = try allocator.create(Self);
self.* = Self{
.obj = Super.make(Self),
- .chunk = try Packed(*chunk.Chunk).create(allocator),
+ .chunk = try Packed(*chunk.Chunk).create2(allocator, try chunk.Chunk.init(allocator)),
.arity = 0,
.type = tpe,
.upvalue_count = 0,
};
- self.chunk.set(try chunk.Chunk.init(allocator));
return self;
}
diff --git a/zlox/src/obj::instance.zig b/zlox/src/obj::instance.zig
@@ -28,9 +28,8 @@ pub fn Instance(fields: anytype) type {
self.* = Self{
.obj = Super.make(Self),
.cls = Packed(*Super.Class).init(cls),
- .fields = try Packed(*Self.Fields).create(allocator),
+ .fields = try Packed(*Self.Fields).create2(allocator, Fields.init(allocator)),
};
- self.fields.set(Self.Fields.init(allocator));
return self;
}
@@ -47,7 +46,6 @@ pub fn Instance(fields: anytype) type {
}
pub fn free(self: *const Self, allocator: std.mem.Allocator) void {
- self.fields.ptr().deinit();
self.fields.destroy(allocator);
allocator.destroy(self);
}
diff --git a/zlox/src/obj::list.zig b/zlox/src/obj::list.zig
@@ -24,9 +24,8 @@ pub fn List(fields: anytype) type {
const self: *Self = try allocator.create(Self);
self.* = Self{
.obj = Super.make(Self),
- .list = try Packed(*Self.List).create(allocator),
+ .list = try Packed(*Self.List).create2(allocator, Self.List.init(allocator)),
};
- self.list.set(Self.List.init(allocator));
return self;
}
diff --git a/zlox/src/obj::table.zig b/zlox/src/obj::table.zig
@@ -24,9 +24,8 @@ pub fn Table(fields: anytype) type {
const self: *Self = try allocator.create(Self);
self.* = Self{
.obj = Super.make(Self),
- .table = try Packed(*Self.Table).create(allocator),
+ .table = try Packed(*Self.Table).create2(allocator, Self.Table.init(allocator)),
};
- self.table.set(Self.Table.init(allocator));
return self;
}
diff --git a/zlox/src/obj::upvalue.zig b/zlox/src/obj::upvalue.zig
@@ -22,16 +22,19 @@ pub fn Upvalue(fields: anytype) type {
pub fn init(arg: Arg, allocator: std.mem.Allocator) Error!*Self {
const self: *Self = try allocator.create(Self);
- self.* = Self{ .obj = Super.make(Self), .location = Packed(*Value).init(arg.val), .closed = false, .slot = arg.slot };
+ self.* = Self{
+ .obj = Super.make(Self),
+ .location = Packed(*Value).init(arg.val),
+ .closed = false,
+ .slot = arg.slot,
+ };
return self;
}
pub fn close(self: *Self, allocator: std.mem.Allocator) Error!void {
if (!self.closed) {
const old = self.location.get();
-
- self.location = try Packed(*Value).create(allocator);
- self.location.set(old);
+ self.location = try Packed(*Value).create2(allocator, old);
self.closed = true;
}
}