Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up .throwError #15433

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3382,9 +3382,17 @@ pub const JSGlobalObject = opaque {
return this.throwOutOfMemory();
}

var str = ZigString.init(try std.fmt.allocPrint(this.bunVM().allocator, "{s} " ++ fmt, .{@errorName(err)}));
defer this.bunVM().allocator.free(ZigString.untagged(str._unsafe_ptr_do_not_use)[0..str.len]);
str.markUTF8();
// If we're throwing JSError, that means either:
// - We're throwing an exception while another exception is already active
// - We're incorrectly returning JSError from a function that did not throw.
bun.debugAssert(err != error.JSError);

// Avoid tiny extra allocation
var stack = std.heap.stackFallback(128, bun.default_allocator);
const allocator_ = stack.get();
const buffer = try std.fmt.allocPrint(allocator_, comptime "{s} " ++ fmt, .{@errorName(err)});
defer allocator_.free(buffer);
const str = ZigString.initUTF8(buffer);
const err_value = str.toErrorInstance(this);
return this.vm().throwError2(this, err_value);
}
Expand Down