Use bun.String for ZigException (#3451)

* Use `bun.String` for `ZigException`

* woopsie

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2023-06-28 21:11:06 -07:00
committed by GitHub
parent 3258bed1c0
commit 68e6fe00a4
10 changed files with 198 additions and 143 deletions

View File

@@ -1805,6 +1805,7 @@ pub const VirtualMachine = struct {
if (exception) |exception_| {
var holder = ZigException.Holder.init();
var zig_exception: *ZigException = holder.zigException();
defer zig_exception.deinit();
exception_.getStackTrace(&zig_exception.stack);
if (zig_exception.stack.frames_len > 0) {
if (allow_ansi_color) {
@@ -1932,8 +1933,14 @@ pub const VirtualMachine = struct {
while (i < stack.len) : (i += 1) {
const frame = stack[@intCast(usize, i)];
const file = frame.source_url.slice();
const func = frame.function_name.slice();
const file_slice = frame.source_url.toSlice(bun.default_allocator);
defer file_slice.deinit();
const func_slice = frame.function_name.toSlice(bun.default_allocator);
defer func_slice.deinit();
const file = file_slice.slice();
const func = func_slice.slice();
if (file.len == 0 and func.len == 0) continue;
const has_name = std.fmt.count("{any}", .{frame.nameFormatter(
@@ -1985,7 +1992,7 @@ pub const VirtualMachine = struct {
pub fn remapStackFramePositions(this: *VirtualMachine, frames: [*]JSC.ZigStackFrame, frames_count: usize) void {
for (frames[0..frames_count]) |*frame| {
if (frame.position.isInvalid() or frame.remapped) continue;
var sourceURL = frame.source_url.toSlice(bun.default_allocator);
var sourceURL = frame.source_url.toUTF8(bun.default_allocator);
defer sourceURL.deinit();
if (this.source_mappings.resolveMapping(
@@ -2049,8 +2056,10 @@ pub const VirtualMachine = struct {
if (frames.len == 0) return;
var top = &frames[0];
var top_source_url = top.source_url.toUTF8(bun.default_allocator);
defer top_source_url.deinit();
if (this.source_mappings.resolveMapping(
top.source_url.slice(),
top_source_url.slice(),
@max(top.position.line, 0),
@max(top.position.column_start, 0),
)) |mapping| {
@@ -2078,18 +2087,18 @@ pub const VirtualMachine = struct {
)) |lines| {
var source_lines = exception.stack.source_lines_ptr[0..JSC.ZigException.Holder.source_lines_count];
var source_line_numbers = exception.stack.source_lines_numbers[0..JSC.ZigException.Holder.source_lines_count];
@memset(source_lines, ZigString.Empty);
@memset(source_lines, String.empty);
@memset(source_line_numbers, 0);
var lines_ = lines[0..@min(lines.len, source_lines.len)];
for (lines_, 0..) |line, j| {
source_lines[(lines_.len - 1) - j] = ZigString.init(line);
source_lines[(lines_.len - 1) - j] = String.init(line);
source_line_numbers[j] = top.position.line - @intCast(i32, j) + 1;
}
exception.stack.source_lines_len = @intCast(u8, lines_.len);
top.position.column_stop = @intCast(i32, source_lines[lines_.len - 1].len);
top.position.column_stop = @intCast(i32, source_lines[lines_.len - 1].length());
top.position.line_stop = top.position.column_stop;
// This expression range is no longer accurate
@@ -2101,8 +2110,10 @@ pub const VirtualMachine = struct {
if (frames.len > 1) {
for (frames[1..]) |*frame| {
if (frame.position.isInvalid()) continue;
const source_url = frame.source_url.toUTF8(bun.default_allocator);
defer source_url.deinit();
if (this.source_mappings.resolveMapping(
frame.source_url.slice(),
source_url.slice(),
@max(frame.position.line, 0),
@max(frame.position.column_start, 0),
)) |mapping| {
@@ -2117,6 +2128,7 @@ pub const VirtualMachine = struct {
pub fn printErrorInstance(this: *VirtualMachine, error_instance: JSValue, exception_list: ?*ExceptionList, comptime Writer: type, writer: Writer, comptime allow_ansi_color: bool, comptime allow_side_effects: bool) !void {
var exception_holder = ZigException.Holder.init();
var exception = exception_holder.zigException();
defer exception_holder.deinit();
this.remapZigException(exception, error_instance, exception_list);
this.had_errors = true;
@@ -2134,15 +2146,18 @@ pub const VirtualMachine = struct {
var source_lines = exception.stack.sourceLineIterator();
var last_pad: u64 = 0;
while (source_lines.untilLast()) |source| {
defer source.text.deinit();
const int_size = std.fmt.count("{d}", .{source.line});
const pad = max_line_number_pad - int_size;
last_pad = pad;
try writer.writeByteNTimes(' ', pad);
try writer.print(
comptime Output.prettyFmt("<r><d>{d} | <r>{s}\n", allow_ansi_color),
.{
source.line,
std.mem.trim(u8, source.text, "\n"),
std.mem.trim(u8, source.text.slice(), "\n"),
},
);
}
@@ -2158,7 +2173,8 @@ pub const VirtualMachine = struct {
const top_frame = if (exception.stack.frames_len > 0) exception.stack.frames()[0] else null;
if (top_frame == null or top_frame.?.position.isInvalid()) {
defer did_print_name = true;
var text = std.mem.trim(u8, source.text, "\n");
defer source.text.deinit();
var text = std.mem.trim(u8, source.text.slice(), "\n");
try writer.print(
comptime Output.prettyFmt(
@@ -2176,7 +2192,9 @@ pub const VirtualMachine = struct {
const int_size = std.fmt.count("{d}", .{source.line});
const pad = max_line_number_pad - int_size;
try writer.writeByteNTimes(' ', pad);
var remainder = std.mem.trim(u8, source.text, "\n");
defer source.text.deinit();
const text = source.text.slice();
var remainder = std.mem.trim(u8, text, "\n");
try writer.print(
comptime Output.prettyFmt(
@@ -2188,7 +2206,7 @@ pub const VirtualMachine = struct {
if (!top.position.isInvalid()) {
var first_non_whitespace = @intCast(u32, top.position.column_start);
while (first_non_whitespace < source.text.len and source.text[first_non_whitespace] == ' ') {
while (first_non_whitespace < text.len and text[first_non_whitespace] == ' ') {
first_non_whitespace += 1;
}
const indent = @intCast(usize, pad) + " | ".len + first_non_whitespace;
@@ -2219,10 +2237,10 @@ pub const VirtualMachine = struct {
};
var show = Show{
.system_code = exception.system_code.len > 0 and !strings.eql(exception.system_code.slice(), name.slice()),
.syscall = exception.syscall.len > 0,
.system_code = !exception.system_code.eql(name) and !exception.system_code.isEmpty(),
.syscall = !exception.syscall.isEmpty(),
.errno = exception.errno < 0,
.path = exception.path.len > 0,
.path = !exception.path.isEmpty(),
.fd = exception.fd != -1,
};
@@ -2262,7 +2280,7 @@ pub const VirtualMachine = struct {
} else if (show.errno) {
try writer.writeAll(" ");
}
try writer.print(comptime Output.prettyFmt(" path<d>: <r><cyan>\"{s}\"<r>\n", allow_ansi_color), .{exception.path});
try writer.print(comptime Output.prettyFmt(" path<d>: <r><cyan>\"{}\"<r>\n", allow_ansi_color), .{exception.path});
}
if (show.fd) {
@@ -2281,12 +2299,12 @@ pub const VirtualMachine = struct {
} else if (show.errno) {
try writer.writeAll(" ");
}
try writer.print(comptime Output.prettyFmt(" code<d>: <r><cyan>\"{s}\"<r>\n", allow_ansi_color), .{exception.system_code});
try writer.print(comptime Output.prettyFmt(" code<d>: <r><cyan>\"{}\"<r>\n", allow_ansi_color), .{exception.system_code});
add_extra_line = true;
}
if (show.syscall) {
try writer.print(comptime Output.prettyFmt(" syscall<d>: <r><cyan>\"{s}\"<r>\n", allow_ansi_color), .{exception.syscall});
try writer.print(comptime Output.prettyFmt(" syscall<d>: <r><cyan>\"{}\"<r>\n", allow_ansi_color), .{exception.syscall});
add_extra_line = true;
}
@@ -2303,22 +2321,22 @@ pub const VirtualMachine = struct {
try printStackTrace(@TypeOf(writer), writer, exception.stack, allow_ansi_color);
}
fn printErrorNameAndMessage(_: *VirtualMachine, name: ZigString, message: ZigString, comptime Writer: type, writer: Writer, comptime allow_ansi_color: bool) !void {
if (name.len > 0 and message.len > 0) {
const display_name: ZigString = if (!name.is16Bit() and strings.eqlComptime(name.slice(), "Error")) ZigString.init("error") else name;
fn printErrorNameAndMessage(_: *VirtualMachine, name: String, message: String, comptime Writer: type, writer: Writer, comptime allow_ansi_color: bool) !void {
if (!name.isEmpty() and !message.isEmpty()) {
const display_name: String = if (name.eqlComptime("Error")) String.init("error") else name;
try writer.print(comptime Output.prettyFmt("<r><red>{any}<r><d>:<r> <b>{s}<r>\n", allow_ansi_color), .{
display_name,
message,
});
} else if (name.len > 0) {
if (name.is16Bit() or !strings.hasPrefixComptime(name.slice(), "error")) {
try writer.print(comptime Output.prettyFmt("<r><red>error<r><d>:<r> <b>{s}<r>\n", allow_ansi_color), .{name});
} else if (!name.isEmpty()) {
if (!name.hasPrefixComptime("error")) {
try writer.print(comptime Output.prettyFmt("<r><red>error<r><d>:<r> <b>{}<r>\n", allow_ansi_color), .{name});
} else {
try writer.print(comptime Output.prettyFmt("<r><red>{s}<r>\n", allow_ansi_color), .{name});
try writer.print(comptime Output.prettyFmt("<r><red>{}<r>\n", allow_ansi_color), .{name});
}
} else if (message.len > 0) {
try writer.print(comptime Output.prettyFmt("<r><red>error<r><d>:<r> <b>{s}<r>\n", allow_ansi_color), .{message});
} else if (!message.isEmpty()) {
try writer.print(comptime Output.prettyFmt("<r><red>error<r><d>:<r> <b>{}<r>\n", allow_ansi_color), .{message});
} else {
try writer.print(comptime Output.prettyFmt("<r><red>error<r>\n", allow_ansi_color), .{});
}