Skip to content

Commit

Permalink
allow zig js host functions to return JSError (#15120)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored and snoglobe committed Nov 21, 2024
1 parent 9ba4b0f commit 140131e
Show file tree
Hide file tree
Showing 70 changed files with 827 additions and 876 deletions.
6 changes: 3 additions & 3 deletions src/bake/FrameworkRouter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ pub const JSFrameworkRouter = struct {
InsertionContext.wrap(JSFrameworkRouter, jsfr),
) catch |err| {
global.throwError(err, "while scanning route list");
return global.jsErrorFromCPP();
return error.JSError;
};

return jsfr;
Expand Down Expand Up @@ -1054,7 +1054,7 @@ pub const JSFrameworkRouter = struct {
return .null;
}

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

var sfb = std.heap.stackFallback(4096, bun.default_allocator);
Expand Down Expand Up @@ -1129,7 +1129,7 @@ pub const JSFrameworkRouter = struct {
const parsed = style.parse(filepath.slice(), std.fs.path.extension(filepath.slice()), &log, alloc) catch |err| switch (err) {
error.InvalidRoutePattern => {
global.throw("{s} ({d}:{d})", .{ log.msg.slice(), log.cursor_at, log.cursor_len });
return global.jsErrorFromCPP();
return error.JSError;
},
else => |e| return e,
} orelse
Expand Down
9 changes: 5 additions & 4 deletions src/bake/bake.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ pub const UserOptions = struct {
bun.getcwdAlloc(alloc) catch |err| switch (err) {
error.OutOfMemory => {
global.throwOutOfMemory();
return global.jsErrorFromCPP();
return error.JSError;
},
else => {
global.throwError(err, "while querying current working directory");
return global.jsErrorFromCPP();
return error.JSError;
},
};

Expand Down Expand Up @@ -104,9 +104,10 @@ const BuildConfigSubset = struct {

/// Temporary function to invoke dev server via JavaScript. Will be
/// replaced with a user-facing API. Refs the event loop forever.
pub fn jsWipDevServer(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) !JSValue {
pub fn jsWipDevServer(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
_ = global;
_ = callframe;

if (!bun.FeatureFlags.bake) return .undefined;

bun.Output.errGeneric(
Expand Down Expand Up @@ -562,7 +563,7 @@ fn getOptionalString(

export fn Bun__getTemporaryDevServer(global: *JSC.JSGlobalObject) JSValue {
if (!bun.FeatureFlags.bake) return .undefined;
return JSC.JSFunction.create(global, "wipDevServer", bun.JSC.toJSHostFunction(jsWipDevServer), 0, .{});
return JSC.JSFunction.create(global, "wipDevServer", jsWipDevServer, 0, .{});
}

pub inline fn getHmrRuntime(side: Side) [:0]const u8 {
Expand Down
6 changes: 4 additions & 2 deletions src/bake/production.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ pub fn buildWithVm(ctx: bun.CLI.Command.Context, cwd: []const u8, vm: *VirtualMa
const config_entry_point_string = bun.String.createUTF8(config_entry_point.pathConst().?.text);
defer config_entry_point_string.deref();

const config_promise = bun.JSC.JSModuleLoader.loadAndEvaluateModule(global, &config_entry_point_string) orelse
return global.jsErrorFromCPP();
const config_promise = bun.JSC.JSModuleLoader.loadAndEvaluateModule(global, &config_entry_point_string) orelse {
bun.assert(global.hasException());
return error.JSError;
};

vm.waitForPromise(.{ .internal = config_promise });
var options = switch (config_promise.unwrap(vm.jsc, .mark_handled)) {
Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/BuildMessage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ pub const BuildMessage = struct {
this: *BuildMessage,
globalThis: *JSC.JSGlobalObject,
_: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
return this.toStringFn(globalThis);
}

pub fn toPrimitive(
this: *BuildMessage,
globalThis: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
const args_ = callframe.arguments(1);
const args = args_.ptr[0..args_.len];
if (args.len > 0) {
Expand All @@ -110,7 +110,7 @@ pub const BuildMessage = struct {
this: *BuildMessage,
globalThis: *JSC.JSGlobalObject,
_: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
var object = JSC.JSValue.createEmptyObject(globalThis, 4);
object.put(globalThis, ZigString.static("name"), bun.String.static("BuildMessage").toJS(globalThis));
object.put(globalThis, ZigString.static("position"), this.getPosition(globalThis));
Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/ResolveMessage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ pub const ResolveMessage = struct {
this: *ResolveMessage,
globalThis: *JSC.JSGlobalObject,
_: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
return this.toStringFn(globalThis);
}

pub fn toPrimitive(
this: *ResolveMessage,
globalThis: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
const args_ = callframe.arguments(1);
const args = args_.ptr[0..args_.len];
if (args.len > 0) {
Expand All @@ -140,7 +140,7 @@ pub const ResolveMessage = struct {
this: *ResolveMessage,
globalThis: *JSC.JSGlobalObject,
_: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
var object = JSC.JSValue.createEmptyObject(globalThis, 7);
object.put(globalThis, ZigString.static("name"), bun.String.static("ResolveMessage").toJS(globalThis));
object.put(globalThis, ZigString.static("position"), this.getPosition(globalThis));
Expand Down
Loading

0 comments on commit 140131e

Please sign in to comment.