feat: interning formatted strings
This commit is contained in:
parent
d2cd7fa888
commit
874fd61a37
5 changed files with 423 additions and 292 deletions
|
|
@ -35,7 +35,7 @@ code_chunks: std.ArrayListUnmanaged(*Object.Code) = .empty,
|
|||
gc_objects: std.SinglyLinkedList = .{},
|
||||
/// Global constants pool.
|
||||
constants_pool: []const Value = &.{},
|
||||
// FIXME: This was a hack to keep string bytes alive.
|
||||
/// Interned string bytes.
|
||||
string_bytes: []const u8 = &.{},
|
||||
dump_writer: ?*std.Io.Writer = null,
|
||||
|
||||
|
|
@ -304,16 +304,19 @@ pub const Value = union(enum) {
|
|||
}
|
||||
|
||||
pub fn format(value: Value, writer: *std.Io.Writer) error{WriteFailed}!void {
|
||||
var scratch_buffer: [64]u8 = undefined;
|
||||
switch (value) {
|
||||
.nil => try writer.writeAll(value.tagBytes()),
|
||||
.bool => |boolean| try writer.writeAll(if (boolean) "true" else "false"),
|
||||
.int => |int| try writer.print("{d}", .{int}),
|
||||
.float => |float| {
|
||||
var buf: [64]u8 = undefined;
|
||||
if (std.math.isNan(float)) return writer.writeAll("NaN");
|
||||
if (std.math.isInf(float)) return writer.writeAll(if (float > 0) "Inf" else "-Inf");
|
||||
if (std.math.isNan(float))
|
||||
return writer.writeAll("NaN");
|
||||
|
||||
var str = std.fmt.bufPrint(&buf, "{d:.7}", .{float}) catch |err| switch (err) {
|
||||
if (std.math.isInf(float))
|
||||
return writer.writeAll(if (float > 0) "Inf" else "-Inf");
|
||||
|
||||
var str = std.fmt.bufPrint(&scratch_buffer, "{d:.7}", .{float}) catch |err| switch (err) {
|
||||
error.NoSpaceLeft => unreachable,
|
||||
else => |e| return e,
|
||||
};
|
||||
|
|
@ -322,14 +325,14 @@ pub const Value = union(enum) {
|
|||
while (end > dot + 2 and str[end - 1] == '0') end -= 1;
|
||||
str = str[0..end];
|
||||
}
|
||||
try writer.writeAll(str);
|
||||
return writer.writeAll(str);
|
||||
},
|
||||
.object => |object| switch (object.tag) {
|
||||
.string => {
|
||||
const typed: *const Object.String = @ptrCast(object);
|
||||
try writer.writeAll(typed.toSlice());
|
||||
return writer.writeAll(typed.toSlice());
|
||||
},
|
||||
else => try writer.print("<{s} {*}>", .{ object.tag.tagBytes(), object }),
|
||||
else => return writer.print("<{s} {*}>", .{ object.tag.tagBytes(), object }),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue