Skip to content

Commit

Permalink
rid nearly all use of ExceptionRef in zig (#15100)
Browse files Browse the repository at this point in the history
Co-authored-by: nektro <[email protected]>
  • Loading branch information
2 people authored and snoglobe committed Nov 18, 2024
1 parent 609e438 commit ec6d98b
Show file tree
Hide file tree
Showing 26 changed files with 811 additions and 1,932 deletions.
92 changes: 28 additions & 64 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -938,37 +938,36 @@ pub fn shrink(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue
fn doResolve(
globalThis: *JSC.JSGlobalObject,
arguments: []const JSValue,
exception: js.ExceptionRef,
) ?JSC.JSValue {
) bun.JSError!JSC.JSValue {
var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments);
defer args.deinit();
const specifier = args.protectEatNext() orelse {
JSC.throwInvalidArguments("Expected a specifier and a from path", .{}, globalThis, exception);
return null;
globalThis.throwInvalidArguments("Expected a specifier and a from path", .{});
return error.JSError;
};

if (specifier.isUndefinedOrNull()) {
JSC.throwInvalidArguments("specifier must be a string", .{}, globalThis, exception);
return null;
globalThis.throwInvalidArguments("specifier must be a string", .{});
return error.JSError;
}

const from = args.protectEatNext() orelse {
JSC.throwInvalidArguments("Expected a from path", .{}, globalThis, exception);
return null;
globalThis.throwInvalidArguments("Expected a from path", .{});
return error.JSError;
};

if (from.isUndefinedOrNull()) {
JSC.throwInvalidArguments("from must be a string", .{}, globalThis, exception);
return null;
globalThis.throwInvalidArguments("from must be a string", .{});
return error.JSError;
}

var is_esm = true;
if (args.nextEat()) |next| {
if (next.isBoolean()) {
is_esm = next.toBoolean();
} else {
JSC.throwInvalidArguments("esm must be a boolean", .{}, globalThis, exception);
return null;
globalThis.throwInvalidArguments("esm must be a boolean", .{});
return error.JSError;
}
}

Expand All @@ -980,7 +979,6 @@ fn doResolve(
globalThis,
specifier_str,
from_str,
exception,
is_esm,
false,
);
Expand All @@ -990,10 +988,9 @@ fn doResolveWithArgs(
ctx: js.JSContextRef,
specifier: bun.String,
from: bun.String,
exception: js.ExceptionRef,
is_esm: bool,
comptime is_file_path: bool,
) ?JSC.JSValue {
) bun.JSError!JSC.JSValue {
var errorable: ErrorableString = undefined;
var query_string = ZigString.Empty;

Expand Down Expand Up @@ -1024,8 +1021,8 @@ fn doResolveWithArgs(
}

if (!errorable.success) {
exception.* = bun.cast(JSC.JSValueRef, errorable.result.err.ptr.?);
return null;
ctx.throwValue(bun.cast(JSC.JSValueRef, errorable.result.err.ptr.?).?.value());
return error.JSError;
}

if (query_string.len > 0) {
Expand All @@ -1037,9 +1034,8 @@ fn doResolveWithArgs(
errorable.result.value,
query_string,
}) catch {
// TODO: binding for createOutOfMemoryError
exception.* = JSC.createError(ctx, "Out of memory", .{}).asObjectRef();
return null;
ctx.throwOutOfMemory();
return error.JSError;
};

return ZigString.initUTF8(arraylist.items).toJS(ctx);
Expand All @@ -1049,24 +1045,16 @@ fn doResolveWithArgs(
}

pub fn resolveSync(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue {
var exception_ = [1]JSC.JSValueRef{null};
const exception = &exception_;
const arguments = callframe.arguments(3);
const result = doResolve(globalObject, arguments.slice(), exception);

if (exception_[0] != null) {
globalObject.throwValue(exception_[0].?.value());
}

return result orelse .zero;
const result = doResolve(globalObject, arguments.slice());
return result catch .zero;
}

pub fn resolve(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue {
var exception_ = [1]JSC.JSValueRef{null};
const exception = &exception_;
const arguments = callframe.arguments(3);
const value = doResolve(globalObject, arguments.slice(), exception) orelse {
return JSC.JSPromise.rejectedPromiseValue(globalObject, exception_[0].?.value());
const value = doResolve(globalObject, arguments.slice()) catch {
const err = globalObject.tryTakeException().?;
return JSC.JSPromise.rejectedPromiseValue(globalObject, err);
};
return JSC.JSPromise.resolvedPromiseValue(globalObject, value);
}
Expand All @@ -1077,16 +1065,15 @@ export fn Bun__resolve(
source: JSValue,
is_esm: bool,
) JSC.JSValue {
var exception_ = [1]JSC.JSValueRef{null};
const exception = &exception_;
const specifier_str = specifier.toBunString(global);
defer specifier_str.deref();

const source_str = source.toBunString(global);
defer source_str.deref();

const value = doResolveWithArgs(global, specifier_str, source_str, exception, is_esm, true) orelse {
return JSC.JSPromise.rejectedPromiseValue(global, exception_[0].?.value());
const value = doResolveWithArgs(global, specifier_str, source_str, is_esm, true) catch {
const err = global.tryTakeException().?;
return JSC.JSPromise.rejectedPromiseValue(global, err);
};

return JSC.JSPromise.resolvedPromiseValue(global, value);
Expand All @@ -1098,18 +1085,13 @@ export fn Bun__resolveSync(
source: JSValue,
is_esm: bool,
) JSC.JSValue {
var exception_ = [1]JSC.JSValueRef{null};
const exception = &exception_;

const specifier_str = specifier.toBunString(global);
defer specifier_str.deref();

const source_str = source.toBunString(global);
defer source_str.deref();

return doResolveWithArgs(global, specifier_str, source_str, exception, is_esm, true) orelse {
return JSC.JSValue.fromRef(exception[0]);
};
return doResolveWithArgs(global, specifier_str, source_str, is_esm, true) catch return .zero;
}

export fn Bun__resolveSyncWithStrings(
Expand All @@ -1119,10 +1101,7 @@ export fn Bun__resolveSyncWithStrings(
is_esm: bool,
) JSC.JSValue {
Output.scoped(.importMetaResolve, false)("source: {s}, specifier: {s}", .{ source.*, specifier.* });
var exception = [1]JSC.JSValueRef{null};
return doResolveWithArgs(global, specifier.*, source.*, &exception, is_esm, true) orelse {
return JSC.JSValue.fromRef(exception[0]);
};
return doResolveWithArgs(global, specifier.*, source.*, is_esm, true) catch return .zero;
}

export fn Bun__resolveSyncWithSource(
Expand All @@ -1131,14 +1110,9 @@ export fn Bun__resolveSyncWithSource(
source: *bun.String,
is_esm: bool,
) JSC.JSValue {
var exception_ = [1]JSC.JSValueRef{null};
const specifier_str = specifier.toBunString(global);
defer specifier_str.deref();

const exception = &exception_;
return doResolveWithArgs(global, specifier_str, source.*, exception, is_esm, true) orelse {
return JSC.JSValue.fromRef(exception[0]);
};
return doResolveWithArgs(global, specifier_str, source.*, is_esm, true) catch return .zero;
}

extern fn dump_zone_malloc_stats() void;
Expand Down Expand Up @@ -3326,22 +3300,12 @@ pub fn serve(
) JSC.JSValue {
const arguments = callframe.arguments(2).slice();
var config: JSC.API.ServerConfig = brk: {
var exception_ = [1]JSC.JSValueRef{null};
const exception = &exception_;

var args = JSC.Node.ArgumentsSlice.init(globalObject.bunVM(), arguments);
var config: JSC.API.ServerConfig = .{};
JSC.API.ServerConfig.fromJS(globalObject, &config, &args, exception);
if (exception[0] != null) {
config.deinit();

globalObject.throwValue(exception_[0].?.value());
return .zero;
}
JSC.API.ServerConfig.fromJS(globalObject, &config, &args) catch return .zero;

if (globalObject.hasException()) {
config.deinit();

return .zero;
}

Expand Down
Loading

0 comments on commit ec6d98b

Please sign in to comment.