Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rid nearly all use of ExceptionRef in zig #15100

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 18 additions & 52 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -938,27 +938,26 @@ pub fn shrink(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue
fn doResolve(
globalThis: *JSC.JSGlobalObject,
arguments: []const JSValue,
exception: js.ExceptionRef,
) ?JSC.JSValue {
nektro marked this conversation as resolved.
Show resolved Hide resolved
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);
globalThis.throwInvalidArguments("Expected a specifier and a from path", .{});
return null;
};

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

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

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

Expand All @@ -967,7 +966,7 @@ fn doResolve(
if (next.isBoolean()) {
is_esm = next.toBoolean();
} else {
JSC.throwInvalidArguments("esm must be a boolean", .{}, globalThis, exception);
globalThis.throwInvalidArguments("esm must be a boolean", .{});
return null;
}
}
Expand All @@ -980,7 +979,6 @@ fn doResolve(
globalThis,
specifier_str,
from_str,
exception,
is_esm,
false,
);
Expand All @@ -990,7 +988,6 @@ fn doResolveWithArgs(
ctx: js.JSContextRef,
specifier: bun.String,
from: bun.String,
exception: js.ExceptionRef,
is_esm: bool,
comptime is_file_path: bool,
) ?JSC.JSValue {
nektro marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -1024,7 +1021,7 @@ fn doResolveWithArgs(
}

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

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

Expand All @@ -1049,24 +1045,17 @@ 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());
}
const result = doResolve(globalObject, arguments.slice());

return result orelse .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()) orelse {
nektro marked this conversation as resolved.
Show resolved Hide resolved
const err = globalObject.tryTakeException().?;
return JSC.JSPromise.rejectedPromiseValue(globalObject, err);
};
return JSC.JSPromise.resolvedPromiseValue(globalObject, value);
}
Expand All @@ -1077,16 +1066,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) orelse {
const err = global.tryTakeException().?;
return JSC.JSPromise.rejectedPromiseValue(global, err);
};

return JSC.JSPromise.resolvedPromiseValue(global, value);
Expand All @@ -1098,18 +1086,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) orelse return .zero;
}

export fn Bun__resolveSyncWithStrings(
Expand All @@ -1119,10 +1102,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) orelse return .zero;
}

export fn Bun__resolveSyncWithSource(
Expand All @@ -1131,14 +1111,10 @@ 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) orelse return .zero;
}

extern fn dump_zone_malloc_stats() void;
Expand Down Expand Up @@ -3326,22 +3302,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