Skip to content

Commit 140131e

Browse files
nektrosnoglobe
authored andcommitted
allow zig js host functions to return JSError (#15120)
1 parent 9ba4b0f commit 140131e

70 files changed

Lines changed: 827 additions & 876 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/bake/FrameworkRouter.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ pub const JSFrameworkRouter = struct {
10191019
InsertionContext.wrap(JSFrameworkRouter, jsfr),
10201020
) catch |err| {
10211021
global.throwError(err, "while scanning route list");
1022-
return global.jsErrorFromCPP();
1022+
return error.JSError;
10231023
};
10241024

10251025
return jsfr;
@@ -1054,7 +1054,7 @@ pub const JSFrameworkRouter = struct {
10541054
return .null;
10551055
}
10561056

1057-
pub fn toJSON(jsfr: *JSFrameworkRouter, global: *JSGlobalObject, callframe: *JSC.CallFrame) !JSValue {
1057+
pub fn toJSON(jsfr: *JSFrameworkRouter, global: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
10581058
_ = callframe;
10591059

10601060
var sfb = std.heap.stackFallback(4096, bun.default_allocator);
@@ -1129,7 +1129,7 @@ pub const JSFrameworkRouter = struct {
11291129
const parsed = style.parse(filepath.slice(), std.fs.path.extension(filepath.slice()), &log, alloc) catch |err| switch (err) {
11301130
error.InvalidRoutePattern => {
11311131
global.throw("{s} ({d}:{d})", .{ log.msg.slice(), log.cursor_at, log.cursor_len });
1132-
return global.jsErrorFromCPP();
1132+
return error.JSError;
11331133
},
11341134
else => |e| return e,
11351135
} orelse

src/bake/bake.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ pub const UserOptions = struct {
5252
bun.getcwdAlloc(alloc) catch |err| switch (err) {
5353
error.OutOfMemory => {
5454
global.throwOutOfMemory();
55-
return global.jsErrorFromCPP();
55+
return error.JSError;
5656
},
5757
else => {
5858
global.throwError(err, "while querying current working directory");
59-
return global.jsErrorFromCPP();
59+
return error.JSError;
6060
},
6161
};
6262

@@ -104,9 +104,10 @@ const BuildConfigSubset = struct {
104104

105105
/// Temporary function to invoke dev server via JavaScript. Will be
106106
/// replaced with a user-facing API. Refs the event loop forever.
107-
pub fn jsWipDevServer(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) !JSValue {
107+
pub fn jsWipDevServer(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
108108
_ = global;
109109
_ = callframe;
110+
110111
if (!bun.FeatureFlags.bake) return .undefined;
111112

112113
bun.Output.errGeneric(
@@ -562,7 +563,7 @@ fn getOptionalString(
562563

563564
export fn Bun__getTemporaryDevServer(global: *JSC.JSGlobalObject) JSValue {
564565
if (!bun.FeatureFlags.bake) return .undefined;
565-
return JSC.JSFunction.create(global, "wipDevServer", bun.JSC.toJSHostFunction(jsWipDevServer), 0, .{});
566+
return JSC.JSFunction.create(global, "wipDevServer", jsWipDevServer, 0, .{});
566567
}
567568

568569
pub inline fn getHmrRuntime(side: Side) [:0]const u8 {

src/bake/production.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ pub fn buildWithVm(ctx: bun.CLI.Command.Context, cwd: []const u8, vm: *VirtualMa
132132
const config_entry_point_string = bun.String.createUTF8(config_entry_point.pathConst().?.text);
133133
defer config_entry_point_string.deref();
134134

135-
const config_promise = bun.JSC.JSModuleLoader.loadAndEvaluateModule(global, &config_entry_point_string) orelse
136-
return global.jsErrorFromCPP();
135+
const config_promise = bun.JSC.JSModuleLoader.loadAndEvaluateModule(global, &config_entry_point_string) orelse {
136+
bun.assert(global.hasException());
137+
return error.JSError;
138+
};
137139

138140
vm.waitForPromise(.{ .internal = config_promise });
139141
var options = switch (config_promise.unwrap(vm.jsc, .mark_handled)) {

src/bun.js/BuildMessage.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ pub const BuildMessage = struct {
8181
this: *BuildMessage,
8282
globalThis: *JSC.JSGlobalObject,
8383
_: *JSC.CallFrame,
84-
) JSC.JSValue {
84+
) bun.JSError!JSC.JSValue {
8585
return this.toStringFn(globalThis);
8686
}
8787

8888
pub fn toPrimitive(
8989
this: *BuildMessage,
9090
globalThis: *JSC.JSGlobalObject,
9191
callframe: *JSC.CallFrame,
92-
) JSC.JSValue {
92+
) bun.JSError!JSC.JSValue {
9393
const args_ = callframe.arguments(1);
9494
const args = args_.ptr[0..args_.len];
9595
if (args.len > 0) {
@@ -110,7 +110,7 @@ pub const BuildMessage = struct {
110110
this: *BuildMessage,
111111
globalThis: *JSC.JSGlobalObject,
112112
_: *JSC.CallFrame,
113-
) JSC.JSValue {
113+
) bun.JSError!JSC.JSValue {
114114
var object = JSC.JSValue.createEmptyObject(globalThis, 4);
115115
object.put(globalThis, ZigString.static("name"), bun.String.static("BuildMessage").toJS(globalThis));
116116
object.put(globalThis, ZigString.static("position"), this.getPosition(globalThis));

src/bun.js/ResolveMessage.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ pub const ResolveMessage = struct {
111111
this: *ResolveMessage,
112112
globalThis: *JSC.JSGlobalObject,
113113
_: *JSC.CallFrame,
114-
) JSC.JSValue {
114+
) bun.JSError!JSC.JSValue {
115115
return this.toStringFn(globalThis);
116116
}
117117

118118
pub fn toPrimitive(
119119
this: *ResolveMessage,
120120
globalThis: *JSC.JSGlobalObject,
121121
callframe: *JSC.CallFrame,
122-
) JSC.JSValue {
122+
) bun.JSError!JSC.JSValue {
123123
const args_ = callframe.arguments(1);
124124
const args = args_.ptr[0..args_.len];
125125
if (args.len > 0) {
@@ -140,7 +140,7 @@ pub const ResolveMessage = struct {
140140
this: *ResolveMessage,
141141
globalThis: *JSC.JSGlobalObject,
142142
_: *JSC.CallFrame,
143-
) JSC.JSValue {
143+
) bun.JSError!JSC.JSValue {
144144
var object = JSC.JSValue.createEmptyObject(globalThis, 7);
145145
object.put(globalThis, ZigString.static("name"), bun.String.static("ResolveMessage").toJS(globalThis));
146146
object.put(globalThis, ZigString.static("position"), this.getPosition(globalThis));

0 commit comments

Comments
 (0)