Skip to content

Commit 999cce1

Browse files
nektrodylan-conway
authored andcommitted
zig: make throwTODO use JSError (#15264)
Co-authored-by: Dylan Conway <[email protected]>
1 parent e903c3b commit 999cce1

File tree

9 files changed

+38
-70
lines changed

9 files changed

+38
-70
lines changed

src/bun.js/api/BunObject.zig

+2-4
Original file line numberDiff line numberDiff line change
@@ -2561,8 +2561,7 @@ pub const Crypto = struct {
25612561
BoringSSL.ERR_clear_error();
25622562
return globalThis.throwValue2(instance);
25632563
} else {
2564-
globalThis.throwTODO("HMAC is not supported for this algorithm yet");
2565-
return error.JSError;
2564+
return globalThis.throwTODO("HMAC is not supported for this algorithm yet");
25662565
}
25672566
}
25682567
return error.JSError;
@@ -3366,8 +3365,7 @@ pub fn allocUnsafe(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) b
33663365

33673366
pub fn mmapFile(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue {
33683367
if (comptime Environment.isWindows) {
3369-
globalThis.throwTODO("mmapFile is not supported on Windows");
3370-
return JSC.JSValue.zero;
3368+
return globalThis.throwTODO("mmapFile is not supported on Windows");
33713369
}
33723370

33733371
const arguments_ = callframe.arguments(2);

src/bun.js/api/bun/socket.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,7 @@ fn NewSocket(comptime ssl: bool) type {
23052305
const length_value = args[2];
23062306

23072307
if (encoding_value != .undefined and (offset_value != .undefined or length_value != .undefined)) {
2308-
globalObject.throwTODO("Support encoding with offset and length altogether. Only either encoding or offset, length is supported, but not both combinations yet.");
2308+
globalObject.throwTODO("Support encoding with offset and length altogether. Only either encoding or offset, length is supported, but not both combinations yet.") catch {};
23092309
return .fail;
23102310
}
23112311

src/bun.js/api/bun/spawn/stdio.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,12 @@ pub const Stdio = union(enum) {
387387

388388
switch (req.ptr) {
389389
.File, .Blob => {
390-
globalThis.throwTODO("Support fd/blob backed ReadableStream in spawn stdin. See https://github.com/oven-sh/bun/issues/8049");
390+
globalThis.throwTODO("Support fd/blob backed ReadableStream in spawn stdin. See https://github.com/oven-sh/bun/issues/8049") catch {};
391391
return false;
392392
},
393393
.Direct, .JavaScript, .Bytes => {
394394
// out_stdio.* = .{ .connect = req };
395-
globalThis.throwTODO("Re-enable ReadableStream support in spawn stdin. ");
395+
globalThis.throwTODO("Re-enable ReadableStream support in spawn stdin. ") catch {};
396396
return false;
397397
},
398398
.Invalid => {

src/bun.js/api/server.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ const StaticRoute = struct {
226226

227227
.Blob, .InternalBlob, .WTFStringImpl => {
228228
if (response.body.value == .Blob and response.body.value.Blob.needsToReadFile()) {
229-
globalThis.throwTODO("TODO: support Bun.file(path) in static routes");
230-
return error.JSError;
229+
return globalThis.throwTODO("TODO: support Bun.file(path) in static routes");
231230
}
232231
var blob = response.body.value.use();
233232
blob.globalThis = globalThis;

src/bun.js/bindings/bindings.zig

+4-12
Original file line numberDiff line numberDiff line change
@@ -2962,10 +2962,10 @@ pub const JSGlobalObject = opaque {
29622962
return .zero;
29632963
}
29642964

2965-
pub fn throwTODO(this: *JSGlobalObject, msg: []const u8) void {
2965+
pub fn throwTODO(this: *JSGlobalObject, msg: []const u8) bun.JSError {
29662966
const err = this.createErrorInstance("{s}", .{msg});
29672967
err.put(this, ZigString.static("name"), bun.String.static("TODOError").toJS(this));
2968-
this.throwValue(err);
2968+
return this.throwValue2(err);
29692969
}
29702970

29712971
pub const throwTerminationException = JSGlobalObject__throwTerminationException;
@@ -2980,21 +2980,13 @@ pub const JSGlobalObject = opaque {
29802980
}
29812981

29822982
/// Deprecated: use `throwInvalidArguments2`
2983-
pub fn throwInvalidArguments(
2984-
this: *JSGlobalObject,
2985-
comptime fmt: [:0]const u8,
2986-
args: anytype,
2987-
) void {
2983+
pub fn throwInvalidArguments(this: *JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) void {
29882984
const err = JSC.toInvalidArguments(fmt, args, this);
29892985
this.vm().throwError(this, err);
29902986
}
29912987

29922988
/// New system for throwing errors: returning bun.JSError
2993-
pub fn throwInvalidArguments2(
2994-
this: *JSGlobalObject,
2995-
comptime fmt: [:0]const u8,
2996-
args: anytype,
2997-
) bun.JSError {
2989+
pub fn throwInvalidArguments2(this: *JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) bun.JSError {
29982990
const err = JSC.toInvalidArguments(fmt, args, this);
29992991
return this.vm().throwError2(this, err);
30002992
}

src/codegen/generate-node-errors.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ listHeader += `
8888
`;
8989

9090
zig += `
91-
91+
9292
9393
extern fn Bun__createErrorWithCode(globalThis: *JSC.JSGlobalObject, code: Error, message: *bun.String) JSC.JSValue;
94-
94+
9595
/// Creates an Error object with the given error code.
9696
/// Derefs the message string.
9797
pub fn toJS(this: Error, globalThis: *JSC.JSGlobalObject, message: *bun.String) JSC.JSValue {
@@ -110,7 +110,7 @@ zig += `
110110
}
111111
112112
pub fn throw(this: Error, globalThis: *JSC.JSGlobalObject, comptime fmt_str: [:0]const u8, args: anytype) void {
113-
globalThis.throwValue(fmt(this, globalThis, fmt_str, args));
113+
globalThis.throwValue(fmt(this, globalThis, fmt_str, args));
114114
}
115115
116116
};

src/install/dependency.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ pub const Version = struct {
354354
}
355355
},
356356
else => {
357-
globalThis.throwTODO("Unsupported dependency type");
358-
return error.JSError;
357+
return globalThis.throwTODO("Unsupported dependency type");
359358
},
360359
}
361360

src/shell/interpreter.zig

+18-38
Original file line numberDiff line numberDiff line change
@@ -1180,44 +1180,26 @@ pub const Interpreter = struct {
11801180
}
11811181
};
11821182

1183-
pub fn createShellInterpreter(
1184-
globalThis: *JSC.JSGlobalObject,
1185-
callframe: *JSC.CallFrame,
1186-
) bun.JSError!JSValue {
1183+
pub fn createShellInterpreter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
11871184
const allocator = bun.default_allocator;
11881185
const arguments_ = callframe.arguments(3);
11891186
var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice());
11901187

1191-
const resolve = arguments.nextEat() orelse {
1192-
globalThis.throw("shell: expected 3 arguments, got 0", .{});
1193-
return .undefined;
1194-
};
1188+
const resolve = arguments.nextEat() orelse return globalThis.throw2("shell: expected 3 arguments, got 0", .{});
11951189

1196-
const reject = arguments.nextEat() orelse {
1197-
globalThis.throw("shell: expected 3 arguments, got 0", .{});
1198-
return .undefined;
1199-
};
1190+
const reject = arguments.nextEat() orelse return globalThis.throw2("shell: expected 3 arguments, got 0", .{});
12001191

1201-
const parsed_shell_script_js = arguments.nextEat() orelse {
1202-
globalThis.throw("shell: expected 3 arguments, got 0", .{});
1203-
return .undefined;
1204-
};
1192+
const parsed_shell_script_js = arguments.nextEat() orelse return globalThis.throw2("shell: expected 3 arguments, got 0", .{});
12051193

1206-
const parsed_shell_script = parsed_shell_script_js.as(ParsedShellScript) orelse {
1207-
globalThis.throw("shell: expected a ParsedShellScript", .{});
1208-
return .undefined;
1209-
};
1194+
const parsed_shell_script = parsed_shell_script_js.as(ParsedShellScript) orelse return globalThis.throw2("shell: expected a ParsedShellScript", .{});
12101195

12111196
var shargs: *ShellArgs = undefined;
12121197
var jsobjs: std.ArrayList(JSValue) = std.ArrayList(JSValue).init(allocator);
12131198
var quiet: bool = false;
12141199
var cwd: ?bun.String = null;
12151200
var export_env: ?EnvMap = null;
12161201

1217-
if (parsed_shell_script.args == null) {
1218-
globalThis.throw("shell: shell args is null, this is a bug in Bun. Please file a GitHub issue.", .{});
1219-
return .undefined;
1220-
}
1202+
if (parsed_shell_script.args == null) return globalThis.throw2("shell: shell args is null, this is a bug in Bun. Please file a GitHub issue.", .{});
12211203

12221204
parsed_shell_script.take(
12231205
globalThis,
@@ -1248,8 +1230,7 @@ pub const Interpreter = struct {
12481230
if (export_env) |*ee| ee.deinit();
12491231
if (cwd) |*cc| cc.deref();
12501232
shargs.deinit();
1251-
throwShellErr(e, .{ .js = globalThis.bunVM().event_loop });
1252-
return .zero;
1233+
return try throwShellErr(e, .{ .js = globalThis.bunVM().event_loop });
12531234
},
12541235
};
12551236

@@ -1259,7 +1240,7 @@ pub const Interpreter = struct {
12591240
if (cwd) |*cc| cc.deref();
12601241
shargs.deinit();
12611242
interpreter.finalize();
1262-
return .zero;
1243+
return error.JSError;
12631244
}
12641245

12651246
interpreter.flags.quiet = quiet;
@@ -1472,8 +1453,7 @@ pub const Interpreter = struct {
14721453
null,
14731454
)) {
14741455
.err => |*e| {
1475-
throwShellErr(e, .{ .mini = mini });
1476-
return 1;
1456+
e.throwMini();
14771457
},
14781458
.result => |i| i,
14791459
};
@@ -1542,8 +1522,7 @@ pub const Interpreter = struct {
15421522
null,
15431523
)) {
15441524
.err => |*e| {
1545-
throwShellErr(e, .{ .mini = mini });
1546-
return 1;
1525+
e.throwMini();
15471526
},
15481527
.result => |i| i,
15491528
};
@@ -1643,8 +1622,7 @@ pub const Interpreter = struct {
16431622
if (this.setupIOBeforeRun().asErr()) |e| {
16441623
defer this.deinitEverything();
16451624
const shellerr = bun.shell.ShellErr.newSys(e);
1646-
throwShellErr(&shellerr, .{ .js = globalThis.bunVM().event_loop });
1647-
return .undefined;
1625+
return try throwShellErr(&shellerr, .{ .js = globalThis.bunVM().event_loop });
16481626
}
16491627
incrPendingActivityFlag(&this.has_pending_activity);
16501628

@@ -2766,7 +2744,7 @@ pub const Interpreter = struct {
27662744
}
27672745

27682746
pub fn throw(this: *const State, err: *const bun.shell.ShellErr) void {
2769-
throwShellErr(err, this.eventLoop());
2747+
throwShellErr(err, this.eventLoop()) catch {}; //TODO:
27702748
}
27712749

27722750
pub fn rootIO(this: *const State) *const IO {
@@ -5745,7 +5723,7 @@ pub const Interpreter = struct {
57455723
}
57465724

57475725
pub inline fn throw(this: *const Builtin, err: *const bun.shell.ShellErr) void {
5748-
this.parentCmd().base.throw(err);
5726+
this.parentCmd().base.throw(err) catch {};
57495727
}
57505728

57515729
pub inline fn parentCmd(this: *const Builtin) *const Cmd {
@@ -12216,11 +12194,13 @@ inline fn fastMod(val: anytype, comptime rhs: comptime_int) @TypeOf(val) {
1221612194
return val & (rhs - 1);
1221712195
}
1221812196

12219-
fn throwShellErr(e: *const bun.shell.ShellErr, event_loop: JSC.EventLoopHandle) void {
12220-
switch (event_loop) {
12197+
/// 'js' event loop will always return JSError
12198+
/// 'mini' event loop will always return noreturn and exit 1
12199+
fn throwShellErr(e: *const bun.shell.ShellErr, event_loop: JSC.EventLoopHandle) bun.JSError!noreturn {
12200+
return switch (event_loop) {
1222112201
.mini => e.throwMini(),
1222212202
.js => e.throwJS(event_loop.js.global),
12223-
}
12203+
};
1222412204
}
1222512205

1222612206
pub const ReadChunkAction = enum {

src/shell/shell.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,30 @@ pub const ShellErr = union(enum) {
8383
}
8484
}
8585

86-
pub fn throwJS(this: *const @This(), globalThis: *JSC.JSGlobalObject) void {
86+
pub fn throwJS(this: *const @This(), globalThis: *JSC.JSGlobalObject) bun.JSError {
8787
defer this.deinit(bun.default_allocator);
8888
switch (this.*) {
8989
.sys => {
9090
const err = this.sys.toErrorInstance(globalThis);
91-
globalThis.throwValue(err);
91+
return globalThis.throwValue2(err);
9292
},
9393
.custom => {
9494
var str = JSC.ZigString.init(this.custom);
9595
str.markUTF8();
9696
const err_value = str.toErrorInstance(globalThis);
97-
globalThis.throwValue(err_value);
97+
return globalThis.throwValue2(err_value);
9898
// this.bunVM().allocator.free(JSC.ZigString.untagged(str._unsafe_ptr_do_not_use)[0..str.len]);
9999
},
100100
.invalid_arguments => {
101-
globalThis.throwInvalidArguments("{s}", .{this.invalid_arguments.val});
101+
return globalThis.throwInvalidArguments2("{s}", .{this.invalid_arguments.val});
102102
},
103103
.todo => {
104-
globalThis.throwTODO(this.todo);
104+
return globalThis.throwTODO(this.todo);
105105
},
106106
}
107107
}
108108

109-
pub fn throwMini(this: @This()) void {
109+
pub fn throwMini(this: @This()) noreturn {
110110
defer this.deinit(bun.default_allocator);
111111
switch (this) {
112112
.sys => |err| {

0 commit comments

Comments
 (0)