From 895f8f74868917e902dd908bfa78f5b38da737d4 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Tue, 26 Nov 2024 14:11:37 -0800 Subject: [PATCH] Clean up .throwError --- src/bun.js/bindings/bindings.zig | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 63f5715076d2c8..e8d917aacb72b1 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -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); }