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

zig: assert there is an exception when .zero is returned #15362

Merged
merged 10 commits into from
Nov 25, 2024
16 changes: 16 additions & 0 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6770,6 +6770,14 @@ pub fn toJSHostFunction(comptime Function: JSHostZigFunction) JSC.JSHostFunction
globalThis: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
) callconv(JSC.conv) JSC.JSValue {
if (bun.Environment.allow_assert) {
nektro marked this conversation as resolved.
Show resolved Hide resolved
const value = Function(globalThis, callframe) catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => globalThis.throwOutOfMemoryValue(),
};
bun.assert((value == .zero) == globalThis.hasException());
Jarred-Sumner marked this conversation as resolved.
Show resolved Hide resolved
return value;
}
return @call(.always_inline, Function, .{ globalThis, callframe }) catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => globalThis.throwOutOfMemoryValue(),
Expand All @@ -6780,6 +6788,14 @@ pub fn toJSHostFunction(comptime Function: JSHostZigFunction) JSC.JSHostFunction

// XXX: temporary
pub fn toJSHostValue(globalThis: *JSGlobalObject, value: error{ OutOfMemory, JSError }!JSValue) JSValue {
if (bun.Environment.allow_assert) {
nektro marked this conversation as resolved.
Show resolved Hide resolved
const normal = value catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => globalThis.throwOutOfMemoryValue(),
};
bun.assert((normal == .zero) == globalThis.hasException());
Jarred-Sumner marked this conversation as resolved.
Show resolved Hide resolved
return normal;
}
return value catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => globalThis.throwOutOfMemoryValue(),
Expand Down
15 changes: 3 additions & 12 deletions src/codegen/generate-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1754,10 +1754,7 @@ const JavaScriptCoreBindings = struct {
output += `
pub fn ${classSymbolName(typeName, "call")}(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue {
if (comptime Environment.enable_logs) zig("${typeName}<d>({})<r>", .{callFrame});
return @call(.always_inline, ${typeName}.call, .{globalObject, callFrame}) catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => globalObject.throwOutOfMemoryValue(),
};
return JSC.toJSHostFunction(${typeName}.call)(globalObject, callFrame);
nektro marked this conversation as resolved.
Show resolved Hide resolved
}
`;
}
Expand Down Expand Up @@ -1810,10 +1807,7 @@ const JavaScriptCoreBindings = struct {
output += `
pub fn ${names.fn}(thisValue: *${typeName}, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame${proto[name].passThis ? ", js_this_value: JSC.JSValue" : ""}) callconv(JSC.conv) JSC.JSValue {
if (comptime Environment.enable_logs) zig("<d>${typeName}.<r>${name}<d>({})<r>", .{callFrame});
return @call(.always_inline, ${typeName}.${fn}, .{thisValue, globalObject, callFrame${proto[name].passThis ? ", js_this_value" : ""}}) catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => globalObject.throwOutOfMemoryValue(),
};
return JSC.toJSHostValue(globalObject, ${typeName}.${fn}(thisValue, globalObject, callFrame${proto[name].passThis ? ", js_this_value" : ""}));
nektro marked this conversation as resolved.
Show resolved Hide resolved
}
`;
}
Expand Down Expand Up @@ -1860,10 +1854,7 @@ const JavaScriptCoreBindings = struct {
output += `
pub fn ${names.fn}(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue {
if (comptime Environment.enable_logs) JSC.markBinding(@src());
return @call(.always_inline, ${typeName}.${fn}, .{globalObject, callFrame}) catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => globalObject.throwOutOfMemoryValue(),
};
return JSC.toJSHostFunction(${typeName}.${fn})(globalObject, callFrame);
nektro marked this conversation as resolved.
Show resolved Hide resolved
}
`;
}
Expand Down
5 changes: 1 addition & 4 deletions src/codegen/generate-js2native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,7 @@ export function getJS2NativeZig(gs2NativeZigPath: string) {
})}(global: *JSC.JSGlobalObject, call_frame: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue {`,
`
const function = @import(${JSON.stringify(path.relative(path.dirname(gs2NativeZigPath), x.filename))});
return @call(.always_inline, function.${x.symbol_target}, .{global, call_frame}) catch |err| switch (err) {
error.JSError => .zero,
error.OutOfMemory => global.throwOutOfMemoryValue(),
};`,
return JSC.toJSHostFunction(function.${x.symbol_target})(global, call_frame);`,
nektro marked this conversation as resolved.
Show resolved Hide resolved
"}",
]),
].join("\n");
Expand Down