From fdd8d35845d9d3e009449f7d0574aabd5a135bba Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 13 Nov 2024 21:11:56 -0800 Subject: [PATCH] allow zig js host functions to return JSError (#15120) --- src/bake/FrameworkRouter.zig | 6 +- src/bake/bake.zig | 9 +- src/bake/production.zig | 6 +- src/bun.js/BuildMessage.zig | 6 +- src/bun.js/ResolveMessage.zig | 6 +- src/bun.js/api/BunObject.zig | 101 +++++------ src/bun.js/api/JSBundler.zig | 12 +- src/bun.js/api/JSTranspiler.zig | 16 +- src/bun.js/api/Timer.zig | 10 +- src/bun.js/api/bun/dns_resolver.zig | 157 +++++----------- src/bun.js/api/bun/h2_frame_parser.zig | 58 +++--- src/bun.js/api/bun/socket.zig | 121 ++++++------- src/bun.js/api/bun/subprocess.zig | 16 +- src/bun.js/api/bun/udp_socket.zig | 16 +- src/bun.js/api/ffi.zig | 4 +- src/bun.js/api/filesystem_router.zig | 4 +- src/bun.js/api/glob.zig | 6 +- src/bun.js/api/html_rewriter.zig | 52 +++--- src/bun.js/api/server.zig | 68 ++++--- src/bun.js/base.zig | 16 +- src/bun.js/bindings/bindings.zig | 75 ++++---- src/bun.js/bindings/shimmer.zig | 6 +- src/bun.js/javascript.zig | 12 +- src/bun.js/node/node_cluster_binding.zig | 12 +- src/bun.js/node/node_crypto_binding.zig | 16 +- src/bun.js/node/node_error_binding.zig | 4 +- src/bun.js/node/node_fs_binding.zig | 10 +- src/bun.js/node/node_fs_stat_watcher.zig | 6 +- src/bun.js/node/node_fs_watcher.zig | 8 +- src/bun.js/node/node_http_binding.zig | 6 +- src/bun.js/node/node_net_binding.zig | 8 +- src/bun.js/node/node_os.zig | 36 ++-- src/bun.js/node/node_util_binding.zig | 2 +- src/bun.js/node/node_zlib_binding.zig | 44 ++--- src/bun.js/node/types.zig | 18 +- src/bun.js/node/util/parse_args.zig | 6 +- src/bun.js/test/expect.zig | 218 +++++++++++------------ src/bun.js/test/jest.zig | 67 +++---- src/bun.js/webcore.zig | 30 ++-- src/bun.js/webcore/ObjectURLRegistry.zig | 24 ++- src/bun.js/webcore/blob.zig | 44 +++-- src/bun.js/webcore/body.zig | 26 ++- src/bun.js/webcore/encoding.zig | 6 +- src/bun.js/webcore/request.zig | 2 +- src/bun.js/webcore/response.zig | 31 ++-- src/bun.js/webcore/streams.zig | 52 +++--- src/bun.zig | 17 +- src/bun_js.zig | 2 +- src/cli/pack_command.zig | 2 +- src/cli/upgrade_command.zig | 4 +- src/codegen/generate-classes.ts | 35 ++-- src/codegen/generate-js2native.ts | 27 +-- src/crash_handler.zig | 16 +- src/css/css_internals.zig | 8 +- src/css/values/color_js.zig | 4 +- src/deps/c_ares.zig | 13 +- src/deps/uws.zig | 6 +- src/fmt.zig | 2 +- src/ini.zig | 4 +- src/install/dependency.zig | 4 +- src/install/install.zig | 4 +- src/install/npm.zig | 8 +- src/install/semver.zig | 4 +- src/patch.zig | 6 +- src/resolver/resolver.zig | 7 +- src/shell/interpreter.zig | 22 +-- src/shell/shell.zig | 6 +- src/sql/postgres.zig | 33 ++-- src/string.zig | 5 +- src/url.zig | 5 +- 70 files changed, 827 insertions(+), 876 deletions(-) diff --git a/src/bake/FrameworkRouter.zig b/src/bake/FrameworkRouter.zig index 39847ad129..f863c8b0b0 100644 --- a/src/bake/FrameworkRouter.zig +++ b/src/bake/FrameworkRouter.zig @@ -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; @@ -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); @@ -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 diff --git a/src/bake/bake.zig b/src/bake/bake.zig index 65ad80c1e6..7045bce772 100644 --- a/src/bake/bake.zig +++ b/src/bake/bake.zig @@ -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; }, }; @@ -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( @@ -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 { diff --git a/src/bake/production.zig b/src/bake/production.zig index 9260121a07..394146d849 100644 --- a/src/bake/production.zig +++ b/src/bake/production.zig @@ -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)) { diff --git a/src/bun.js/BuildMessage.zig b/src/bun.js/BuildMessage.zig index e3952df4c9..32a336a744 100644 --- a/src/bun.js/BuildMessage.zig +++ b/src/bun.js/BuildMessage.zig @@ -81,7 +81,7 @@ pub const BuildMessage = struct { this: *BuildMessage, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return this.toStringFn(globalThis); } @@ -89,7 +89,7 @@ pub const BuildMessage = struct { 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) { @@ -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)); diff --git a/src/bun.js/ResolveMessage.zig b/src/bun.js/ResolveMessage.zig index 0eddbfd9db..498519ae7f 100644 --- a/src/bun.js/ResolveMessage.zig +++ b/src/bun.js/ResolveMessage.zig @@ -111,7 +111,7 @@ pub const ResolveMessage = struct { this: *ResolveMessage, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return this.toStringFn(globalThis); } @@ -119,7 +119,7 @@ pub const ResolveMessage = struct { 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) { @@ -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)); diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index fc84aec855..088acff708 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -12,7 +12,7 @@ pub const BunObject = struct { pub const allocUnsafe = toJSCallback(Bun.allocUnsafe); pub const braces = toJSCallback(Bun.braces); pub const build = toJSCallback(Bun.JSBundler.buildFn); - pub const color = bun.css.CssColor.jsFunctionColor; + pub const color = toJSCallback(bun.css.CssColor.jsFunctionColor); pub const connect = toJSCallback(JSC.wrapStaticMethod(JSC.API.Listener, "connect", false)); pub const createParsedShellScript = toJSCallback(bun.shell.ParsedShellScript.createParsedShellScript); pub const createShellInterpreter = toJSCallback(bun.shell.Interpreter.createShellInterpreter); @@ -85,11 +85,11 @@ pub const BunObject = struct { const toJSCallback = JSC.toJSHostFunction; - const LazyPropertyCallback = fn (*JSC.JSGlobalObject, *JSC.JSObject) callconv(JSC.conv) JSC.JSValue; + const LazyPropertyCallback = fn (*JSC.JSGlobalObject, *JSC.JSObject) callconv(JSC.conv) JSValue; fn toJSGetter(comptime getter: anytype) LazyPropertyCallback { return struct { - pub fn callback(this: *JSC.JSGlobalObject, object: *JSC.JSObject) callconv(JSC.conv) JSC.JSValue { + pub fn callback(this: *JSC.JSGlobalObject, object: *JSC.JSObject) callconv(JSC.conv) JSValue { return @call(.always_inline, getter, .{ this, object }); } }.callback; @@ -262,7 +262,7 @@ const Shell = @import("../../shell/shell.zig"); pub fn shellEscape( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); if (arguments.len < 1) { globalThis.throw("shell escape expected at least 1 argument", .{}); @@ -296,7 +296,7 @@ pub fn shellEscape( pub fn braces( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); defer arguments.deinit(); @@ -403,7 +403,7 @@ pub fn braces( pub fn which( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); var path_buf: bun.PathBuffer = undefined; var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); @@ -474,7 +474,7 @@ pub fn which( pub fn inspectTable( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) callconv(JSC.conv) JSC.JSValue { +) bun.JSError!JSC.JSValue { var args_buf = callframe.argumentsUndef(5); var all_arguments = args_buf.mut(); if (all_arguments[0].isUndefined() or all_arguments[0].isNull()) @@ -506,7 +506,7 @@ pub fn inspectTable( .single_line = true, }; if (arguments[2].isObject()) { - formatOptions.fromJS(globalThis, arguments[2..]) catch return .zero; + try formatOptions.fromJS(globalThis, arguments[2..]); } const value = arguments[0]; @@ -549,7 +549,7 @@ pub fn inspectTable( pub fn inspect( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(4).slice(); if (arguments.len == 0) return bun.String.empty.toJS(globalThis); @@ -572,7 +572,7 @@ pub fn inspect( .ordered_properties = false, }; if (arguments.len > 1) { - formatOptions.fromJS(globalThis, arguments[1..]) catch return .zero; + try formatOptions.fromJS(globalThis, arguments[1..]); } const value = arguments[0]; @@ -618,7 +618,7 @@ pub fn getInspect(globalObject: *JSC.JSGlobalObject, _: *JSC.JSObject) JSC.JSVal pub fn registerMacro( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); const arguments = arguments_.slice(); if (arguments.len != 2 or !arguments[0].isNumber()) { @@ -775,7 +775,7 @@ const Editor = @import("../../open.zig").Editor; pub fn openInEditor( globalThis: js.JSContextRef, callframe: *JSC.CallFrame, -) JSValue { +) bun.JSError!JSValue { var edit = &VirtualMachine.get().rareData().editor_context; const args = callframe.arguments(4); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), args.slice()); @@ -893,7 +893,7 @@ pub fn getPublicPathWithAssetPrefix( } } -pub fn sleepSync(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn sleepSync(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); // Expect at least one argument. We allow more than one but ignore them; this @@ -921,16 +921,17 @@ pub fn sleepSync(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) J return .undefined; } -pub fn generateHeapSnapshot(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { +pub fn generateHeapSnapshot(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { return globalObject.generateHeapSnapshot(); } -pub fn runGC(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn runGC(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(1); const arguments = arguments_.slice(); return globalObject.bunVM().garbageCollect(arguments.len > 0 and arguments[0].isBoolean() and arguments[0].toBoolean()); } -pub fn shrink(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + +pub fn shrink(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { globalObject.vm().shrinkFootprint(); return .undefined; } @@ -1044,13 +1045,12 @@ fn doResolveWithArgs( return errorable.result.value.toJS(ctx); } -pub fn resolveSync(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn resolveSync(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(3); - const result = doResolve(globalObject, arguments.slice()); - return result catch .zero; + return try doResolve(globalObject, arguments.slice()); } -pub fn resolve(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn resolve(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(3); const value = doResolve(globalObject, arguments.slice()) catch { const err = globalObject.tryTakeException().?; @@ -1091,7 +1091,7 @@ export fn Bun__resolveSync( const source_str = source.toBunString(global); defer source_str.deref(); - return doResolveWithArgs(global, specifier_str, source_str, is_esm, true) catch return .zero; + return JSC.toJSHostValue(global, doResolveWithArgs(global, specifier_str, source_str, is_esm, true)); } export fn Bun__resolveSyncWithStrings( @@ -1101,7 +1101,7 @@ export fn Bun__resolveSyncWithStrings( is_esm: bool, ) JSC.JSValue { Output.scoped(.importMetaResolve, false)("source: {s}, specifier: {s}", .{ source.*, specifier.* }); - return doResolveWithArgs(global, specifier.*, source.*, is_esm, true) catch return .zero; + return JSC.toJSHostValue(global, doResolveWithArgs(global, specifier.*, source.*, is_esm, true)); } export fn Bun__resolveSyncWithSource( @@ -1112,12 +1112,12 @@ export fn Bun__resolveSyncWithSource( ) JSC.JSValue { const specifier_str = specifier.toBunString(global); defer specifier_str.deref(); - return doResolveWithArgs(global, specifier_str, source.*, is_esm, true) catch return .zero; + return JSC.toJSHostValue(global, doResolveWithArgs(global, specifier_str, source.*, is_esm, true)); } extern fn dump_zone_malloc_stats() void; -fn dump_mimalloc(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { +fn dump_mimalloc(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { globalObject.bunVM().arena.dumpStats(); if (bun.heap_breakdown.enabled) { dump_zone_malloc_stats(); @@ -1125,7 +1125,7 @@ fn dump_mimalloc(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSVal return .undefined; } -pub fn indexOfLine(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn indexOfLine(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); const arguments = arguments_.slice(); if (arguments.len == 0) { @@ -2198,7 +2198,7 @@ pub const Crypto = struct { pub fn JSPasswordObject__hash( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); const arguments = arguments_.ptr[0..arguments_.len]; @@ -2234,7 +2234,7 @@ pub const Crypto = struct { pub fn JSPasswordObject__hashSync( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); const arguments = arguments_.ptr[0..arguments_.len]; @@ -2359,7 +2359,7 @@ pub const Crypto = struct { pub fn JSPasswordObject__verify( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(3); const arguments = arguments_.ptr[0..arguments_.len]; @@ -2414,7 +2414,7 @@ pub const Crypto = struct { pub fn JSPasswordObject__verifySync( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(3); const arguments = arguments_.ptr[0..arguments_.len]; @@ -2701,7 +2701,7 @@ pub const Crypto = struct { return CryptoHasher.getConstructor(globalObject); } - pub fn update(this: *CryptoHasher, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn update(this: *CryptoHasher, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const thisValue = callframe.this(); const arguments = callframe.arguments(2); const input = arguments.ptr[0]; @@ -2759,7 +2759,7 @@ pub const Crypto = struct { this: *CryptoHasher, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var new: CryptoHasher = undefined; switch (this.*) { .evp => |*inner| { @@ -3173,7 +3173,7 @@ pub const Crypto = struct { return ThisHasher.getConstructor(globalObject); } - pub fn update(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn update(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (this.digested) { globalThis.ERR_INVALID_STATE(name ++ " hasher already digested, create a new instance to update", .{}).throw(); return .zero; @@ -3286,12 +3286,12 @@ pub const Crypto = struct { pub const SHA512_256 = StaticCryptoHasher(Hashers.SHA512_256, "SHA512_256"); }; -pub fn nanoseconds(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { +pub fn nanoseconds(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { const ns = globalThis.bunVM().origin_timer.read(); return JSC.JSValue.jsNumberFromUint64(ns); } -pub fn serve(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) !JSC.JSValue { +pub fn serve(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).slice(); var config: JSC.API.ServerConfig = brk: { var args = JSC.Node.ArgumentsSlice.init(globalObject.bunVM(), arguments); @@ -3481,7 +3481,7 @@ comptime { pub fn allocUnsafe( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); const size = arguments.ptr[0]; if (!size.isUInt32AsAnyInt()) { @@ -3495,7 +3495,7 @@ pub fn allocUnsafe( pub fn mmapFile( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { if (comptime Environment.isWindows) { globalThis.throwTODO("mmapFile is not supported on Windows"); return JSC.JSValue.zero; @@ -3603,7 +3603,7 @@ const HashObject = struct { pub const murmur64v2 = hashWrap(std.hash.murmur.Murmur2_64); pub fn create(globalThis: *JSC.JSGlobalObject) JSC.JSValue { - const function = JSC.createCallback(globalThis, ZigString.static("hash"), 1, &wyhash); + const function = JSC.createCallback(globalThis, ZigString.static("hash"), 1, wyhash); const fns = comptime .{ "wyhash", "adler32", @@ -3627,13 +3627,10 @@ const HashObject = struct { return function; } - fn hashWrap(comptime Hasher_: anytype) JSC.JSHostFunctionType { + fn hashWrap(comptime Hasher_: anytype) JSC.JSHostZigFunction { return struct { const Hasher = Hasher_; - pub fn hash( - globalThis: *JSC.JSGlobalObject, - callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + pub fn hash(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).slice(); var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments); defer args.deinit(); @@ -3794,7 +3791,7 @@ const UnsafeObject = struct { pub fn gcAggressionLevel( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const ret = JSValue.jsNumber(@as(i32, @intFromEnum(globalThis.bunVM().aggressive_garbage_collection))); const value = callframe.arguments(1).ptr[0]; @@ -3812,7 +3809,7 @@ const UnsafeObject = struct { pub fn arrayBufferToString( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const args = callframe.arguments(2).slice(); if (args.len < 1 or !args[0].isCell() or !args[0].jsType().isTypedArray()) { globalThis.throwInvalidArguments("Expected an ArrayBuffer", .{}); @@ -3857,7 +3854,7 @@ const TOMLObject = struct { pub fn parse( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var arena = bun.ArenaAllocator.init(globalThis.allocator()); const allocator = arena.allocator(); defer arena.deinit(); @@ -4538,7 +4535,7 @@ pub const FFIObject = struct { } }; -fn stringWidth(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +fn stringWidth(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).slice(); const value = if (arguments.len > 0) arguments[0] else .undefined; const options_object = if (arguments.len > 1) arguments[1] else .undefined; @@ -4674,7 +4671,7 @@ pub const JSZlib = struct { pub fn gzipSync( globalThis: *JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const buffer, const options_val = getOptions(globalThis, callframe) orelse return .zero; defer buffer.deinit(); return gzipOrDeflateSync(globalThis, buffer, options_val, true); @@ -4683,7 +4680,7 @@ pub const JSZlib = struct { pub fn inflateSync( globalThis: *JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const buffer, const options_val = getOptions(globalThis, callframe) orelse return .zero; defer buffer.deinit(); return gunzipOrInflateSync(globalThis, buffer, options_val, false); @@ -4692,7 +4689,7 @@ pub const JSZlib = struct { pub fn deflateSync( globalThis: *JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const buffer, const options_val = getOptions(globalThis, callframe) orelse return .zero; defer buffer.deinit(); return gzipOrDeflateSync(globalThis, buffer, options_val, false); @@ -4701,7 +4698,7 @@ pub const JSZlib = struct { pub fn gunzipSync( globalThis: *JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const buffer, const options_val = getOptions(globalThis, callframe) orelse return .zero; defer buffer.deinit(); return gunzipOrInflateSync(globalThis, buffer, options_val, true); @@ -4855,7 +4852,7 @@ pub const JSZlib = struct { buffer: JSC.Node.StringOrBuffer, options_val_: ?JSValue, is_gzip: bool, - ) JSValue { + ) bun.JSError!JSValue { var level: ?i32 = null; var library: Library = .zlib; var windowBits: i32 = 0; @@ -4968,7 +4965,7 @@ pub const JSZlib = struct { pub usingnamespace @import("./bun/subprocess.zig"); const InternalTestingAPIs = struct { - pub fn BunInternalFunction__syntaxHighlighter(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn BunInternalFunction__syntaxHighlighter(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const args = callframe.arguments(1); if (args.len < 1) { globalThis.throwNotEnoughArguments("code", 1, 0); diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index c22c741798..edd39ed588 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -607,7 +607,7 @@ pub const JSBundler = struct { pub fn buildFn( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); return build(globalThis, arguments.slice()); } @@ -1166,7 +1166,7 @@ pub const BuildArtifact = struct { this: *BuildArtifact, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return @call(bun.callmod_inline, Blob.getText, .{ &this.blob, globalThis, callframe }); } @@ -1174,21 +1174,21 @@ pub const BuildArtifact = struct { this: *BuildArtifact, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return @call(bun.callmod_inline, Blob.getJSON, .{ &this.blob, globalThis, callframe }); } pub fn getArrayBuffer( this: *BuildArtifact, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return @call(bun.callmod_inline, Blob.getArrayBuffer, .{ &this.blob, globalThis, callframe }); } pub fn getSlice( this: *BuildArtifact, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return @call(bun.callmod_inline, Blob.getSlice, .{ &this.blob, globalThis, callframe }); } pub fn getType( @@ -1202,7 +1202,7 @@ pub const BuildArtifact = struct { this: *BuildArtifact, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return @call(bun.callmod_inline, Blob.getStream, .{ &this.blob, globalThis, diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig index f95bd61d5d..305b179a87 100644 --- a/src/bun.js/api/JSTranspiler.zig +++ b/src/bun.js/api/JSTranspiler.zig @@ -878,7 +878,7 @@ pub fn scan( this: *Transpiler, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const arguments = callframe.arguments(3); var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments.slice()); @@ -899,7 +899,7 @@ pub fn scan( const loader: ?Loader = brk: { if (args.next()) |arg| { args.eat(); - break :brk Loader.fromJS(globalThis, arg) catch return .zero; + break :brk try Loader.fromJS(globalThis, arg); } break :brk null; @@ -959,7 +959,7 @@ pub fn transform( this: *Transpiler, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const arguments = callframe.arguments(3); var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments.slice()); @@ -979,7 +979,7 @@ pub fn transform( const loader: ?Loader = brk: { if (args.next()) |arg| { args.eat(); - break :brk Loader.fromJS(globalThis, arg) catch return .zero; + break :brk try Loader.fromJS(globalThis, arg); } break :brk null; @@ -1008,7 +1008,7 @@ pub fn transformSync( this: *Transpiler, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const arguments = callframe.arguments(3); @@ -1036,7 +1036,7 @@ pub fn transformSync( if (args.next()) |arg| { args.eat(); if (arg.isNumber() or arg.isString()) { - break :brk Loader.fromJS(globalThis, arg) catch return .zero; + break :brk try Loader.fromJS(globalThis, arg); } if (arg.isObject()) { @@ -1185,7 +1185,7 @@ pub fn scanImports( this: *Transpiler, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments.slice()); defer args.deinit(); @@ -1207,7 +1207,7 @@ pub fn scanImports( var loader: Loader = this.transpiler_options.default_loader; if (args.next()) |arg| { - if (Loader.fromJS(globalThis, arg) catch return .zero) |_loader| { + if (try Loader.fromJS(globalThis, arg)) |_loader| { loader = _loader; } args.eat(); diff --git a/src/bun.js/api/Timer.zig b/src/bun.js/api/Timer.zig index 3849d729eb..6f9dcb62e3 100644 --- a/src/bun.js/api/Timer.zig +++ b/src/bun.js/api/Timer.zig @@ -550,7 +550,7 @@ pub const TimerObject = struct { return .{ timer, timer_js }; } - pub fn doRef(this: *TimerObject, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn doRef(this: *TimerObject, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const this_value = callframe.this(); this_value.ensureStillAlive(); @@ -564,7 +564,7 @@ pub const TimerObject = struct { return this_value; } - pub fn doRefresh(this: *TimerObject, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn doRefresh(this: *TimerObject, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const this_value = callframe.this(); // setImmediate does not support refreshing and we do not support refreshing after cleanup @@ -578,7 +578,7 @@ pub const TimerObject = struct { return this_value; } - pub fn doUnref(this: *TimerObject, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn doUnref(this: *TimerObject, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const this_value = callframe.this(); this_value.ensureStillAlive(); @@ -643,10 +643,10 @@ pub const TimerObject = struct { } } - pub fn hasRef(this: *TimerObject, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn hasRef(this: *TimerObject, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { return JSValue.jsBoolean(this.is_keeping_event_loop_alive); } - pub fn toPrimitive(this: *TimerObject, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn toPrimitive(this: *TimerObject, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { if (!this.has_accessed_primitive) { this.has_accessed_primitive = true; const vm = VirtualMachine.get(); diff --git a/src/bun.js/api/bun/dns_resolver.zig b/src/bun.js/api/bun/dns_resolver.zig index 16b4f8cb84..b2ed04351b 100644 --- a/src/bun.js/api/bun/dns_resolver.zig +++ b/src/bun.js/api/bun/dns_resolver.zig @@ -1562,7 +1562,7 @@ pub const InternalDNS = struct { var dns_cache_errors: usize = 0; var getaddrinfo_calls: usize = 0; - pub fn getDNSCacheStats(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn getDNSCacheStats(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { const object = JSC.JSValue.createEmptyObject(globalObject, 6); object.put(globalObject, JSC.ZigString.static("cacheHitsCompleted"), JSC.JSValue.jsNumber(@atomicLoad(usize, &dns_cache_hits_completed, .monotonic))); object.put(globalObject, JSC.ZigString.static("cacheHitsInflight"), JSC.JSValue.jsNumber(@atomicLoad(usize, &dns_cache_hits_inflight, .monotonic))); @@ -1633,7 +1633,7 @@ pub const InternalDNS = struct { return req; } - pub fn prefetchFromJS(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn prefetchFromJS(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).slice(); if (arguments.len < 1) { @@ -2252,7 +2252,7 @@ pub const DNSResolver = struct { }); }; - pub fn resolve(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolve(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(3); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolve", 2, arguments.len); @@ -2337,7 +2337,7 @@ pub const DNSResolver = struct { } } - pub fn reverse(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn reverse(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("reverse", 2, arguments.len); @@ -2402,7 +2402,7 @@ pub const DNSResolver = struct { return promise; } - pub fn lookup(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn lookup(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("lookup", 2, arguments.len); @@ -2472,7 +2472,7 @@ pub const DNSResolver = struct { }; } - pub fn resolveSrv(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveSrv(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveSrv", 2, arguments.len); @@ -2503,7 +2503,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_ares_srv_reply, "srv", name.slice(), globalThis); } - pub fn resolveSoa(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveSoa(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveSoa", 2, arguments.len); @@ -2529,7 +2529,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_ares_soa_reply, "soa", name.slice(), globalThis); } - pub fn resolveCaa(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveCaa(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveCaa", 2, arguments.len); @@ -2560,7 +2560,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_ares_caa_reply, "caa", name.slice(), globalThis); } - pub fn resolveNs(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveNs(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveNs", 2, arguments.len); @@ -2586,7 +2586,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_hostent, "ns", name.slice(), globalThis); } - pub fn resolvePtr(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolvePtr(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolvePtr", 2, arguments.len); @@ -2617,7 +2617,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_hostent, "ptr", name.slice(), globalThis); } - pub fn resolveCname(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveCname(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveCname", 2, arguments.len); @@ -2648,7 +2648,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_hostent, "cname", name.slice(), globalThis); } - pub fn resolveMx(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveMx(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveMx", 2, arguments.len); @@ -2679,7 +2679,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_ares_mx_reply, "mx", name.slice(), globalThis); } - pub fn resolveNaptr(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveNaptr(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveNaptr", 2, arguments.len); @@ -2710,7 +2710,7 @@ pub const DNSResolver = struct { return resolver.doResolveCAres(c_ares.struct_ares_naptr_reply, "naptr", name.slice(), globalThis); } - pub fn resolveTxt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn resolveTxt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("resolveTxt", 2, arguments.len); @@ -2831,7 +2831,7 @@ pub const DNSResolver = struct { return promise; } - pub fn getServers(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn getServers(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = callframe; var vm = globalThis.bunVM(); @@ -2917,7 +2917,7 @@ pub const DNSResolver = struct { // Resolves the given address and port into a host name and service using the operating system's underlying getnameinfo implementation. // If address is not a valid IP address, a TypeError will be thrown. The port will be coerced to a number. // If it is not a legal port, a TypeError will be thrown. - pub fn lookupService(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn lookupService(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(3); if (arguments.len < 2) { globalThis.throwNotEnoughArguments("lookupService", 3, arguments.len); @@ -3004,98 +3004,37 @@ pub const DNSResolver = struct { } comptime { - @export( - resolve, - .{ - .name = "Bun__DNSResolver__resolve", - }, - ); - @export( - lookup, - .{ - .name = "Bun__DNSResolver__lookup", - }, - ); - @export( - resolveTxt, - .{ - .name = "Bun__DNSResolver__resolveTxt", - }, - ); - @export( - resolveSoa, - .{ - .name = "Bun__DNSResolver__resolveSoa", - }, - ); - @export( - resolveMx, - .{ - .name = "Bun__DNSResolver__resolveMx", - }, - ); - @export( - resolveNaptr, - .{ - .name = "Bun__DNSResolver__resolveNaptr", - }, - ); - @export( - resolveSrv, - .{ - .name = "Bun__DNSResolver__resolveSrv", - }, - ); - @export( - resolveCaa, - .{ - .name = "Bun__DNSResolver__resolveCaa", - }, - ); - @export( - resolveNs, - .{ - .name = "Bun__DNSResolver__resolveNs", - }, - ); - @export( - resolvePtr, - .{ - .name = "Bun__DNSResolver__resolvePtr", - }, - ); - @export( - resolveCname, - .{ - .name = "Bun__DNSResolver__resolveCname", - }, - ); - @export( - getServers, - .{ - .name = "Bun__DNSResolver__getServers", - }, - ); - @export( - reverse, - .{ - .name = "Bun__DNSResolver__reverse", - }, - ); - @export( - lookupService, - .{ - .name = "Bun__DNSResolver__lookupService", - }, - ); - @export( - InternalDNS.prefetchFromJS, - .{ - .name = "Bun__DNSResolver__prefetch", - }, - ); - @export(InternalDNS.getDNSCacheStats, .{ - .name = "Bun__DNSResolver__getCacheStats", - }); + const js_resolve = JSC.toJSHostFunction(resolve); + @export(js_resolve, .{ .name = "Bun__DNSResolver__resolve" }); + const js_lookup = JSC.toJSHostFunction(lookup); + @export(js_lookup, .{ .name = "Bun__DNSResolver__lookup" }); + const js_resolveTxt = JSC.toJSHostFunction(resolveTxt); + @export(js_resolveTxt, .{ .name = "Bun__DNSResolver__resolveTxt" }); + const js_resolveSoa = JSC.toJSHostFunction(resolveSoa); + @export(js_resolveSoa, .{ .name = "Bun__DNSResolver__resolveSoa" }); + const js_resolveMx = JSC.toJSHostFunction(resolveMx); + @export(js_resolveMx, .{ .name = "Bun__DNSResolver__resolveMx" }); + const js_resolveNaptr = JSC.toJSHostFunction(resolveNaptr); + @export(js_resolveNaptr, .{ .name = "Bun__DNSResolver__resolveNaptr" }); + const js_resolveSrv = JSC.toJSHostFunction(resolveSrv); + @export(js_resolveSrv, .{ .name = "Bun__DNSResolver__resolveSrv" }); + const js_resolveCaa = JSC.toJSHostFunction(resolveCaa); + @export(js_resolveCaa, .{ .name = "Bun__DNSResolver__resolveCaa" }); + const js_resolveNs = JSC.toJSHostFunction(resolveNs); + @export(js_resolveNs, .{ .name = "Bun__DNSResolver__resolveNs" }); + const js_resolvePtr = JSC.toJSHostFunction(resolvePtr); + @export(js_resolvePtr, .{ .name = "Bun__DNSResolver__resolvePtr" }); + const js_resolveCname = JSC.toJSHostFunction(resolveCname); + @export(js_resolveCname, .{ .name = "Bun__DNSResolver__resolveCname" }); + const js_getServers = JSC.toJSHostFunction(getServers); + @export(js_getServers, .{ .name = "Bun__DNSResolver__getServers" }); + const js_reverse = JSC.toJSHostFunction(reverse); + @export(js_reverse, .{ .name = "Bun__DNSResolver__reverse" }); + const js_lookupService = JSC.toJSHostFunction(lookupService); + @export(js_lookupService, .{ .name = "Bun__DNSResolver__lookupService" }); + const js_prefetchFromJS = JSC.toJSHostFunction(InternalDNS.prefetchFromJS); + @export(js_prefetchFromJS, .{ .name = "Bun__DNSResolver__prefetch" }); + const js_getDNSCacheStats = JSC.toJSHostFunction(InternalDNS.getDNSCacheStats); + @export(js_getDNSCacheStats, .{ .name = "Bun__DNSResolver__getCacheStats" }); } }; diff --git a/src/bun.js/api/bun/h2_frame_parser.zig b/src/bun.js/api/bun/h2_frame_parser.zig index 4fe99cc86d..6b7b949440 100644 --- a/src/bun.js/api/bun/h2_frame_parser.zig +++ b/src/bun.js/api/bun/h2_frame_parser.zig @@ -290,7 +290,7 @@ const SingleValueHeaders = bun.ComptimeStringMap(void, .{ .{"x-content-type-options"}, }); -fn jsGetUnpackedSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +fn jsGetUnpackedSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var settings: FullSettingsPayload = .{}; @@ -325,7 +325,7 @@ fn jsGetUnpackedSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.Call } } -fn jsAssertSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { +fn jsAssertSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args_list = callframe.arguments(1); if (args_list.len < 1) { globalObject.throw("Expected settings to be a object", .{}); @@ -427,7 +427,7 @@ fn jsAssertSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame return .undefined; } -fn jsGetPackedSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { +fn jsGetPackedSettings(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var settings: FullSettingsPayload = .{}; const args_list = callframe.arguments(1); @@ -2338,7 +2338,7 @@ pub const H2FrameParser = struct { return DirectWriterStruct{ .writer = this }; } - pub fn setEncoding(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn setEncoding(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -2455,7 +2455,7 @@ pub const H2FrameParser = struct { return true; } - pub fn updateSettings(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn updateSettings(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -2473,7 +2473,7 @@ pub const H2FrameParser = struct { return .zero; } - pub fn getCurrentState(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn getCurrentState(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); var result = JSValue.createEmptyObject(globalObject, 9); result.put(globalObject, JSC.ZigString.static("effectiveLocalWindowSize"), JSC.JSValue.jsNumber(this.windowSize)); @@ -2489,7 +2489,7 @@ pub const H2FrameParser = struct { result.put(globalObject, JSC.ZigString.static("outboundQueueSize"), JSC.JSValue.jsNumber(this.outboundQueueSize)); return result; } - pub fn goaway(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn goaway(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(3); if (args_list.len < 1) { @@ -2540,7 +2540,7 @@ pub const H2FrameParser = struct { return .undefined; } - pub fn ping(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn ping(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -2564,7 +2564,7 @@ pub const H2FrameParser = struct { return .zero; } - pub fn getEndAfterHeaders(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn getEndAfterHeaders(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -2592,7 +2592,7 @@ pub const H2FrameParser = struct { return JSC.JSValue.jsBoolean(stream.endAfterHeaders); } - pub fn isStreamAborted(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn isStreamAborted(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -2623,7 +2623,7 @@ pub const H2FrameParser = struct { // closed with cancel = aborted return JSC.JSValue.jsBoolean(stream.state == .CLOSED and stream.rstCode == @intFromEnum(ErrorCode.CANCEL)); } - pub fn getStreamState(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn getStreamState(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -2660,7 +2660,7 @@ pub const H2FrameParser = struct { return state; } - pub fn setStreamPriority(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn setStreamPriority(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(2); if (args_list.len < 2) { @@ -2759,7 +2759,7 @@ pub const H2FrameParser = struct { } return JSC.JSValue.jsBoolean(true); } - pub fn rstStream(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn rstStream(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(2); if (args_list.len < 2) { @@ -2824,7 +2824,7 @@ pub const H2FrameParser = struct { return (this.writeBuffer.len + this.queuedDataSize) / 1024 / 1024; } // get memory in bytes - pub fn getBufferSize(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn getBufferSize(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); return JSC.JSValue.jsNumber(this.writeBuffer.len + this.queuedDataSize); } @@ -2916,7 +2916,7 @@ pub const H2FrameParser = struct { } } } - pub fn noTrailers(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn noTrailers(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -2957,7 +2957,7 @@ pub const H2FrameParser = struct { return .undefined; } - pub fn sendTrailers(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn sendTrailers(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(3); if (args_list.len < 3) { @@ -3111,7 +3111,7 @@ pub const H2FrameParser = struct { this.dispatchWithExtra(.onStreamEnd, identifier, JSC.JSValue.jsNumber(@intFromEnum(stream.state))); return .undefined; } - pub fn writeStream(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn writeStream(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args = callframe.argumentsUndef(5); const stream_arg, const data_arg, const encoding_arg, const close_arg, const callback_arg = args.ptr; @@ -3188,11 +3188,11 @@ pub const H2FrameParser = struct { return stream_id; } - pub fn hasNativeRead(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn hasNativeRead(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { return JSC.JSValue.jsBoolean(this.native_socket == .tcp or this.native_socket == .tls); } - pub fn getNextStream(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn getNextStream(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const id = this.getNextStreamID(); @@ -3203,7 +3203,7 @@ pub const H2FrameParser = struct { return JSC.JSValue.jsNumber(id); } - pub fn getStreamContext(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn getStreamContext(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -3225,7 +3225,7 @@ pub const H2FrameParser = struct { return stream.jsContext.get() orelse .undefined; } - pub fn setStreamContext(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn setStreamContext(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(2); if (args_list.len < 2) { @@ -3252,7 +3252,7 @@ pub const H2FrameParser = struct { return .undefined; } - pub fn getAllStreams(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn getAllStreams(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const array = JSC.JSValue.createEmptyArray(globalObject, this.streams.count()); @@ -3265,7 +3265,7 @@ pub const H2FrameParser = struct { } return array; } - pub fn emitAbortToAllStreams(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn emitAbortToAllStreams(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var it = StreamResumableIterator.init(this); while (it.next()) |stream| { @@ -3285,7 +3285,7 @@ pub const H2FrameParser = struct { } return .undefined; } - pub fn emitErrorToAllStreams(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn emitErrorToAllStreams(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); @@ -3311,13 +3311,13 @@ pub const H2FrameParser = struct { return .undefined; } - pub fn flushFromJS(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn flushFromJS(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); return JSC.JSValue.jsNumber(this.flush()); } - pub fn request(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn request(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(5); @@ -3658,7 +3658,7 @@ pub const H2FrameParser = struct { return JSC.JSValue.jsNumber(stream_id); } - pub fn read(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn read(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -3700,7 +3700,7 @@ pub const H2FrameParser = struct { this.detachNativeSocket(); } - pub fn setNativeSocketFromJS(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn setNativeSocketFromJS(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const args_list = callframe.arguments(1); if (args_list.len < 1) { @@ -3890,7 +3890,7 @@ pub const H2FrameParser = struct { } return this; } - pub fn detachFromJS(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn detachFromJS(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); this.detach(false); return .undefined; diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 7b59bf0f47..888cd176b1 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -561,7 +561,7 @@ pub const Listener = struct { } }; - pub fn reload(this: *Listener, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn reload(this: *Listener, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const args = callframe.arguments(1); if (args.len < 1 or (this.listener == .none and this.handlers.active_connections == 0)) { @@ -580,7 +580,7 @@ pub const Listener = struct { return .zero; }; - const handlers = Handlers.fromJS(globalObject, socket_obj) catch return .zero; + const handlers = try Handlers.fromJS(globalObject, socket_obj); var prev_handlers = &this.handlers; prev_handlers.unprotect(); @@ -590,7 +590,7 @@ pub const Listener = struct { return JSValue.jsUndefined(); } - pub fn listen(globalObject: *JSC.JSGlobalObject, opts: JSValue) JSValue { + pub fn listen(globalObject: *JSC.JSGlobalObject, opts: JSValue) bun.JSError!JSValue { log("listen", .{}); if (opts.isEmptyOrUndefinedOrNull() or opts.isBoolean() or !opts.isObject()) { globalObject.throwInvalidArguments("Expected object", .{}); @@ -599,7 +599,7 @@ pub const Listener = struct { const vm = JSC.VirtualMachine.get(); - var socket_config = SocketConfig.fromJS(vm, opts, globalObject) catch return .zero; + var socket_config = try SocketConfig.fromJS(vm, opts, globalObject); var hostname_or_unix = socket_config.hostname_or_unix; const port = socket_config.port; @@ -897,7 +897,7 @@ pub const Listener = struct { socket.setTimeout(120); } - pub fn addServerName(this: *Listener, global: *JSC.JSGlobalObject, hostname: JSValue, tls: JSValue) JSValue { + pub fn addServerName(this: *Listener, global: *JSC.JSGlobalObject, hostname: JSValue, tls: JSValue) bun.JSError!JSValue { if (!this.ssl) { global.throwInvalidArguments("addServerName requires SSL support", .{}); return .zero; @@ -918,7 +918,7 @@ pub const Listener = struct { return .zero; } - if (JSC.API.ServerConfig.SSLConfig.fromJS(JSC.VirtualMachine.get(), global, tls) catch return .zero) |ssl_config| { + if (try JSC.API.ServerConfig.SSLConfig.fromJS(JSC.VirtualMachine.get(), global, tls)) |ssl_config| { // to keep nodejs compatibility, we allow to replace the server name uws.us_socket_context_remove_server_name(1, this.socket_context, server_name); uws.us_bun_socket_context_add_server_name(1, this.socket_context, server_name, ssl_config.asUSockets(), null); @@ -927,12 +927,12 @@ pub const Listener = struct { return JSValue.jsUndefined(); } - pub fn dispose(this: *Listener, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn dispose(this: *Listener, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { this.doStop(true); return .undefined; } - pub fn stop(this: *Listener, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn stop(this: *Listener, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(1); log("close", .{}); @@ -1039,7 +1039,7 @@ pub const Listener = struct { return JSValue.jsNumber(this.connection.host.port); } - pub fn ref(this: *Listener, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn ref(this: *Listener, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const this_value = callframe.this(); if (this.listener == .none) return JSValue.jsUndefined(); this.poll_ref.ref(globalObject.bunVM()); @@ -1047,7 +1047,7 @@ pub const Listener = struct { return JSValue.jsUndefined(); } - pub fn unref(this: *Listener, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn unref(this: *Listener, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { this.poll_ref.unref(globalObject.bunVM()); if (this.handlers.active_connections == 0) { this.strong_self.clear(); @@ -1055,17 +1055,14 @@ pub const Listener = struct { return JSValue.jsUndefined(); } - pub fn connect( - globalObject: *JSC.JSGlobalObject, - opts: JSValue, - ) JSValue { + pub fn connect(globalObject: *JSC.JSGlobalObject, opts: JSValue) bun.JSError!JSValue { if (opts.isEmptyOrUndefinedOrNull() or opts.isBoolean() or !opts.isObject()) { globalObject.throwInvalidArguments("Expected options object", .{}); return .zero; } const vm = globalObject.bunVM(); - const socket_config = SocketConfig.fromJS(vm, opts, globalObject) catch return .zero; + const socket_config = try SocketConfig.fromJS(vm, opts, globalObject); var hostname_or_unix = socket_config.hostname_or_unix; const port = socket_config.port; @@ -1459,7 +1456,7 @@ fn NewSocket(comptime ssl: bool) type { globalObject.throw("Cannot construct Socket", .{}); return null; } - pub fn resumeFromJS(this: *This, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn resumeFromJS(this: *This, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); log("resume", .{}); @@ -1468,7 +1465,7 @@ fn NewSocket(comptime ssl: bool) type { } return .undefined; } - pub fn pauseFromJS(this: *This, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn pauseFromJS(this: *This, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); log("pause", .{}); @@ -1478,7 +1475,7 @@ fn NewSocket(comptime ssl: bool) type { return .undefined; } - pub fn setKeepAlive(this: *This, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn setKeepAlive(this: *This, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args = callframe.arguments(2); @@ -1506,7 +1503,7 @@ fn NewSocket(comptime ssl: bool) type { return JSValue.jsBoolean(this.socket.setKeepAlive(enabled, initialDelay)); } - pub fn setNoDelay(this: *This, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn setNoDelay(this: *This, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args = callframe.arguments(1); @@ -2051,7 +2048,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); const args = callframe.arguments(1); if (this.socket.isDetached()) return JSValue.jsUndefined(); @@ -2075,7 +2072,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); if (this.socket.isDetached()) { @@ -2105,7 +2102,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); if (this.socket.isDetached()) { @@ -2182,7 +2179,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.socket.isDetached()) { this.buffered_data_for_node_net.deinitWithAllocator(bun.default_allocator); return JSValue.jsBoolean(false); @@ -2200,7 +2197,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.socket.isDetached()) { this.buffered_data_for_node_net.deinitWithAllocator(bun.default_allocator); return JSValue.jsBoolean(false); @@ -2473,7 +2470,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); this.internalFlush(); return JSValue.jsUndefined(); @@ -2483,7 +2480,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); this.closeAndDetach(.failure); return JSValue.jsUndefined(); @@ -2493,7 +2490,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); const args = callframe.arguments(1); this.buffered_data_for_node_net.deinitWithAllocator(bun.default_allocator); @@ -2510,7 +2507,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); var args = callframe.argumentsUndef(5); @@ -2534,14 +2531,14 @@ fn NewSocket(comptime ssl: bool) type { }; } - pub fn jsRef(this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn jsRef(this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); if (this.socket.isDetached()) return JSValue.jsUndefined(); this.poll_ref.ref(globalObject.bunVM()); return JSValue.jsUndefined(); } - pub fn jsUnref(this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn jsUnref(this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); this.poll_ref.unref(globalObject.bunVM()); return JSValue.jsUndefined(); @@ -2588,7 +2585,7 @@ fn NewSocket(comptime ssl: bool) type { this.deref(); } - pub fn reload(this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn reload(this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const args = callframe.arguments(1); if (args.len < 1) { @@ -2611,7 +2608,7 @@ fn NewSocket(comptime ssl: bool) type { return .zero; }; - const handlers = Handlers.fromJS(globalObject, socket_obj) catch return .zero; + const handlers = try Handlers.fromJS(globalObject, socket_obj); var prev_handlers = this.handlers; prev_handlers.unprotect(); @@ -2625,7 +2622,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -2638,7 +2635,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -2679,7 +2676,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -2696,7 +2693,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -2719,7 +2716,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -2760,7 +2757,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -2818,7 +2815,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -2901,7 +2898,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsNull(); } @@ -2967,7 +2964,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -3011,7 +3008,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -3039,7 +3036,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -3067,7 +3064,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); if (comptime ssl == false) { return JSValue.jsNull(); @@ -3155,7 +3152,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); if (comptime ssl == false) { return JSValue.jsNull(); @@ -3174,7 +3171,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); if (comptime ssl == false) { return JSValue.jsBoolean(false); @@ -3209,7 +3206,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); if (comptime ssl == false) { return JSValue.jsUndefined(); @@ -3260,7 +3257,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -3277,7 +3274,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -3293,7 +3290,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (comptime ssl == false) { return JSValue.jsUndefined(); } @@ -3347,7 +3344,7 @@ fn NewSocket(comptime ssl: bool) type { this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); const this_js = callframe.this(); @@ -3380,7 +3377,7 @@ fn NewSocket(comptime ssl: bool) type { return .zero; } - const handlers = Handlers.fromJS(globalObject, socket_obj) catch return .zero; + const handlers = try Handlers.fromJS(globalObject, socket_obj); if (globalObject.hasException()) { return .zero; @@ -3401,7 +3398,7 @@ fn NewSocket(comptime ssl: bool) type { ssl_opts = JSC.API.ServerConfig.SSLConfig.zero; } } else { - if (JSC.API.ServerConfig.SSLConfig.fromJS(JSC.VirtualMachine.get(), globalObject, tls) catch return .zero) |ssl_config| { + if (try JSC.API.ServerConfig.SSLConfig.fromJS(JSC.VirtualMachine.get(), globalObject, tls)) |ssl_config| { ssl_opts = ssl_config; } } @@ -4243,7 +4240,7 @@ pub const WindowsNamedPipeContext = if (Environment.isWindows) struct { } } else void; -pub fn jsAddServerName(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { +pub fn jsAddServerName(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const arguments = callframe.arguments(3); @@ -4259,7 +4256,7 @@ pub fn jsAddServerName(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) J return .zero; } -pub fn jsUpgradeDuplexToTLS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { +pub fn jsUpgradeDuplexToTLS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const args = callframe.arguments(2); @@ -4285,7 +4282,7 @@ pub fn jsUpgradeDuplexToTLS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.C return .zero; }; - var handlers = Handlers.fromJS(globalObject, socket_obj) catch return .zero; + var handlers = try Handlers.fromJS(globalObject, socket_obj); var ssl_opts: ?JSC.API.ServerConfig.SSLConfig = null; if (opts.getTruthy(globalObject, "tls")) |tls| { @@ -4294,7 +4291,7 @@ pub fn jsUpgradeDuplexToTLS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.C ssl_opts = JSC.API.ServerConfig.SSLConfig.zero; } } else { - if (JSC.API.ServerConfig.SSLConfig.fromJS(JSC.VirtualMachine.get(), globalObject, tls) catch return .zero) |ssl_config| { + if (try JSC.API.ServerConfig.SSLConfig.fromJS(JSC.VirtualMachine.get(), globalObject, tls)) |ssl_config| { ssl_opts = ssl_config; } } @@ -4370,7 +4367,7 @@ pub fn jsUpgradeDuplexToTLS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.C return array; } -pub fn jsIsNamedPipeSocket(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { +pub fn jsIsNamedPipeSocket(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const arguments = callframe.arguments(3); @@ -4388,13 +4385,13 @@ pub fn jsIsNamedPipeSocket(global: *JSC.JSGlobalObject, callframe: *JSC.CallFram } pub fn createNodeTLSBinding(global: *JSC.JSGlobalObject) JSC.JSValue { return JSC.JSArray.create(global, &.{ - JSC.JSFunction.create(global, "addServerName", JSC.toJSHostFunction(jsAddServerName), 3, .{}), - JSC.JSFunction.create(global, "upgradeDuplexToTLS", JSC.toJSHostFunction(jsUpgradeDuplexToTLS), 2, .{}), - JSC.JSFunction.create(global, "isNamedPipeSocket", JSC.toJSHostFunction(jsIsNamedPipeSocket), 1, .{}), + JSC.JSFunction.create(global, "addServerName", jsAddServerName, 3, .{}), + JSC.JSFunction.create(global, "upgradeDuplexToTLS", jsUpgradeDuplexToTLS, 2, .{}), + JSC.JSFunction.create(global, "isNamedPipeSocket", jsIsNamedPipeSocket, 1, .{}), }); } -pub fn jsCreateSocketPair(global: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(JSC.conv) JSValue { +pub fn jsCreateSocketPair(global: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); if (Environment.isWindows) { diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig index 0488615ee9..9cd2fd8e45 100644 --- a/src/bun.js/api/bun/subprocess.zig +++ b/src/bun.js/api/bun/subprocess.zig @@ -237,7 +237,7 @@ pub const Subprocess = struct { this: *Subprocess, globalObject: *JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return this.createResourceUsageObject(globalObject); } @@ -592,7 +592,7 @@ pub const Subprocess = struct { this: *Subprocess, global: *JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.process.hasExited()) { // rely on GC to clean everything up in this case return .undefined; @@ -620,7 +620,7 @@ pub const Subprocess = struct { this: *Subprocess, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { this.this_jsvalue = callframe.this(); var arguments = callframe.arguments(1); @@ -654,7 +654,7 @@ pub const Subprocess = struct { if (arguments.ptr[0].asString().length() == 0) { break :brk SignalCode.default; } - const signal_code = arguments.ptr[0].toEnum(globalThis, "signal", SignalCode) catch return .zero; + const signal_code = try arguments.ptr[0].toEnum(globalThis, "signal", SignalCode); break :brk @intFromEnum(signal_code); } else if (!arguments.ptr[0].isEmptyOrUndefinedOrNull()) { globalThis.throwInvalidArguments("Invalid signal: must be a string or an integer", .{}); @@ -700,12 +700,12 @@ pub const Subprocess = struct { this.process.close(); } - pub fn doRef(this: *Subprocess, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn doRef(this: *Subprocess, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { this.jsRef(); return .undefined; } - pub fn doUnref(this: *Subprocess, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn doUnref(this: *Subprocess, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { this.jsUnref(); return .undefined; } @@ -724,7 +724,7 @@ pub const Subprocess = struct { } } - pub fn doSend(this: *Subprocess, global: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) JSValue { + pub fn doSend(this: *Subprocess, global: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSValue { IPClog("Subprocess#doSend", .{}); const ipc_data = &(this.ipc_data orelse { if (this.hasExited()) { @@ -751,7 +751,7 @@ pub const Subprocess = struct { const ipc_data = this.ipc() orelse return; ipc_data.close(nextTick); } - pub fn disconnect(this: *Subprocess, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn disconnect(this: *Subprocess, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { _ = globalThis; _ = callframe; this.disconnectIPC(true); diff --git a/src/bun.js/api/bun/udp_socket.zig b/src/bun.js/api/bun/udp_socket.zig index c8252ee664..7ddbfb5f8b 100644 --- a/src/bun.js/api/bun/udp_socket.zig +++ b/src/bun.js/api/bun/udp_socket.zig @@ -370,7 +370,7 @@ pub const UDPSocket = struct { return true; } - pub fn sendMany(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn sendMany(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { if (this.closed) { globalThis.throw("Socket is closed", .{}); return .zero; @@ -460,7 +460,7 @@ pub const UDPSocket = struct { this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.closed) { globalThis.throw("Socket is closed", .{}); return .zero; @@ -563,7 +563,7 @@ pub const UDPSocket = struct { address: JSValue, }; - pub fn ref(this: *This, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn ref(this: *This, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { if (!this.closed) { this.poll_ref.ref(globalThis.bunVM()); } @@ -571,7 +571,7 @@ pub const UDPSocket = struct { return .undefined; } - pub fn unref(this: *This, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn unref(this: *This, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { this.poll_ref.unref(globalThis.bunVM()); return .undefined; @@ -581,13 +581,13 @@ pub const UDPSocket = struct { this: *This, _: *JSGlobalObject, _: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (!this.closed) this.socket.close(); return .undefined; } - pub fn reload(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn reload(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const args = callframe.arguments(1); if (args.len < 1) { @@ -693,7 +693,7 @@ pub const UDPSocket = struct { this.destroy(); } - pub fn jsConnect(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn jsConnect(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args = callFrame.arguments(2); const this = callFrame.this().as(UDPSocket) orelse { @@ -743,7 +743,7 @@ pub const UDPSocket = struct { return .undefined; } - pub fn jsDisconnect(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn jsDisconnect(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { const this = callFrame.this().as(UDPSocket) orelse { globalObject.throwInvalidArguments("Expected UDPSocket as 'this'", .{}); return .zero; diff --git a/src/bun.js/api/ffi.zig b/src/bun.js/api/ffi.zig index 4e83add580..9bfb79b61a 100644 --- a/src/bun.js/api/ffi.zig +++ b/src/bun.js/api/ffi.zig @@ -610,7 +610,7 @@ pub const FFI = struct { } }; - pub fn Bun__FFI__cc(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn Bun__FFI__cc(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len == 0 or !arguments[0].isObject()) { return JSC.toInvalidArguments("Expected object", .{}, globalThis); @@ -903,7 +903,7 @@ pub const FFI = struct { this: *FFI, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) callconv(.C) JSValue { + ) bun.JSError!JSValue { JSC.markBinding(@src()); if (this.closed) { return .undefined; diff --git a/src/bun.js/api/filesystem_router.zig b/src/bun.js/api/filesystem_router.zig index 26268c6fee..994f58482d 100644 --- a/src/bun.js/api/filesystem_router.zig +++ b/src/bun.js/api/filesystem_router.zig @@ -211,7 +211,7 @@ pub const FileSystemRouter = struct { return fs_router; } - pub fn reload(this: *FileSystemRouter, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn reload(this: *FileSystemRouter, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const this_value = callframe.this(); var arena = globalThis.allocator().create(bun.ArenaAllocator) catch unreachable; @@ -259,7 +259,7 @@ pub const FileSystemRouter = struct { return this_value; } - pub fn match(this: *FileSystemRouter, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn match(this: *FileSystemRouter, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const argument_ = callframe.arguments(2); if (argument_.len == 0) { globalThis.throwInvalidArguments("Expected string, Request or Response", .{}); diff --git a/src/bun.js/api/glob.zig b/src/bun.js/api/glob.zig index 122aff963b..afe34b22f7 100644 --- a/src/bun.js/api/glob.zig +++ b/src/bun.js/api/glob.zig @@ -384,7 +384,7 @@ fn decrPendingActivityFlag(has_pending_activity: *std.atomic.Value(usize)) void _ = has_pending_activity.fetchSub(1, .seq_cst); } -pub fn __scan(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn __scan(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const alloc = getAllocator(globalThis); const arguments_ = callframe.arguments(1); @@ -408,7 +408,7 @@ pub fn __scan(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.CallFram return task.promise.value(); } -pub fn __scanSync(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn __scanSync(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const alloc = getAllocator(globalThis); const arguments_ = callframe.arguments(1); @@ -438,7 +438,7 @@ pub fn __scanSync(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.Call return matchedPaths; } -pub fn match(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn match(this: *Glob, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const alloc = getAllocator(globalThis); var arena = Arena.init(alloc); defer arena.deinit(); diff --git a/src/bun.js/api/html_rewriter.zig b/src/bun.js/api/html_rewriter.zig index 767662975c..de7f11da1a 100644 --- a/src/bun.js/api/html_rewriter.zig +++ b/src/bun.js/api/html_rewriter.zig @@ -64,12 +64,12 @@ pub const HTMLRewriter = struct { selector_name: ZigString, callFrame: *JSC.CallFrame, listener: JSValue, - ) JSValue { + ) bun.JSError!JSValue { const selector_slice = std.fmt.allocPrint(bun.default_allocator, "{}", .{selector_name}) catch bun.outOfMemory(); var selector = LOLHTML.HTMLSelector.parse(selector_slice) catch return throwLOLHTMLError(global); - const handler_ = ElementHandler.init(global, listener) catch return .zero; + const handler_ = try ElementHandler.init(global, listener); const handler = getAllocator(global).create(ElementHandler) catch bun.outOfMemory(); handler.* = handler_; @@ -111,8 +111,8 @@ pub const HTMLRewriter = struct { global: *JSGlobalObject, listener: JSValue, callFrame: *JSC.CallFrame, - ) JSValue { - const handler_ = DocumentHandler.init(global, listener) catch return .zero; + ) bun.JSError!JSValue { + const handler_ = try DocumentHandler.init(global, listener); const handler = getAllocator(global).create(DocumentHandler) catch bun.outOfMemory(); handler.* = handler_; @@ -776,11 +776,8 @@ const DocumentHandler = struct { }; if (!thisObject.isObject()) { - global.throwInvalidArguments( - "Expected object", - .{}, - ); - return error.InvalidArguments; + global.throwInvalidArguments("Expected object", .{}); + return error.JSError; } errdefer { @@ -804,7 +801,7 @@ const DocumentHandler = struct { if (thisObject.get(global, "doctype")) |val| { if (val.isUndefinedOrNull() or !val.isCell() or !val.isCallable(global.vm())) { global.throwInvalidArguments("doctype must be a function", .{}); - return error.InvalidArguments; + return error.JSError; } val.protect(); handler.onDocTypeCallback = val; @@ -813,7 +810,7 @@ const DocumentHandler = struct { if (thisObject.get(global, "comments")) |val| { if (val.isUndefinedOrNull() or !val.isCell() or !val.isCallable(global.vm())) { global.throwInvalidArguments("comments must be a function", .{}); - return error.InvalidArguments; + return error.JSError; } val.protect(); handler.onCommentCallback = val; @@ -822,7 +819,7 @@ const DocumentHandler = struct { if (thisObject.get(global, "text")) |val| { if (val.isUndefinedOrNull() or !val.isCell() or !val.isCallable(global.vm())) { global.throwInvalidArguments("text must be a function", .{}); - return error.InvalidArguments; + return error.JSError; } val.protect(); handler.onTextCallback = val; @@ -831,7 +828,7 @@ const DocumentHandler = struct { if (thisObject.get(global, "end")) |val| { if (val.isUndefinedOrNull() or !val.isCell() or !val.isCallable(global.vm())) { global.throwInvalidArguments("end must be a function", .{}); - return error.InvalidArguments; + return error.JSError; } val.protect(); handler.onEndCallback = val; @@ -935,17 +932,14 @@ const ElementHandler = struct { } if (!thisObject.isObject()) { - global.throwInvalidArguments( - "Expected object", - .{}, - ); - return error.InvalidArguments; + global.throwInvalidArguments("Expected object", .{}); + return error.JSError; } if (thisObject.get(global, "element")) |val| { if (val.isUndefinedOrNull() or !val.isCell() or !val.isCallable(global.vm())) { global.throwInvalidArguments("element must be a function", .{}); - return error.InvalidArguments; + return error.JSError; } val.protect(); handler.onElementCallback = val; @@ -954,7 +948,7 @@ const ElementHandler = struct { if (thisObject.get(global, "comments")) |val| { if (val.isUndefinedOrNull() or !val.isCell() or !val.isCallable(global.vm())) { global.throwInvalidArguments("comments must be a function", .{}); - return error.InvalidArguments; + return error.JSError; } val.protect(); handler.onCommentCallback = val; @@ -963,7 +957,7 @@ const ElementHandler = struct { if (thisObject.get(global, "text")) |val| { if (val.isUndefinedOrNull() or !val.isCell() or !val.isCallable(global.vm())) { global.throwInvalidArguments("text must be a function", .{}); - return error.InvalidArguments; + return error.JSError; } val.protect(); handler.onTextCallback = val; @@ -1096,7 +1090,7 @@ pub const TextChunk = struct { this: *TextChunk, _: *JSGlobalObject, callFrame: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.text_chunk == null) return JSValue.jsUndefined(); this.text_chunk.?.remove(); @@ -1278,7 +1272,7 @@ pub const Comment = struct { this: *Comment, _: *JSGlobalObject, callFrame: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.comment == null) return JSValue.jsNull(); this.comment.?.remove(); @@ -1400,7 +1394,7 @@ pub const EndTag = struct { this: *EndTag, _: *JSGlobalObject, callFrame: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.end_tag == null) return JSValue.jsUndefined(); @@ -1449,7 +1443,7 @@ pub const AttributeIterator = struct { pub usingnamespace JSC.Codegen.JSAttributeIterator; - pub fn next(this: *AttributeIterator, globalObject: *JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn next(this: *AttributeIterator, globalObject: *JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { const done_label = JSC.ZigString.static("done"); const value_label = JSC.ZigString.static("value"); @@ -1475,7 +1469,7 @@ pub const AttributeIterator = struct { )); } - pub fn getThis(_: *AttributeIterator, _: *JSGlobalObject, callFrame: *JSC.CallFrame) JSValue { + pub fn getThis(_: *AttributeIterator, _: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSValue { return callFrame.this(); } }; @@ -1494,7 +1488,7 @@ pub const Element = struct { globalObject: *JSGlobalObject, function: JSValue, callFrame: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.element == null) return JSValue.jsNull(); if (function.isUndefinedOrNull() or !function.isCallable(globalObject.vm())) { @@ -1674,7 +1668,7 @@ pub const Element = struct { this: *Element, _: *JSGlobalObject, callFrame: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.element == null) return JSValue.jsUndefined(); @@ -1687,7 +1681,7 @@ pub const Element = struct { this: *Element, _: *JSGlobalObject, callFrame: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { if (this.element == null) return JSValue.jsUndefined(); diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 09791ad56d..9c483f0445 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -1968,7 +1968,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp } } - pub fn onResolve(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn onResolve(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { ctxLog("onResolve", .{}); const arguments = callframe.arguments(2); @@ -2113,7 +2113,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp this.ref_count += 1; } - pub fn onReject(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn onReject(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { ctxLog("onReject", .{}); const arguments = callframe.arguments(2); @@ -3331,7 +3331,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp req.endStream(req.shouldCloseConnection()); } - pub fn onResolveStream(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn onResolveStream(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { streamLog("onResolveStream", .{}); var args = callframe.arguments(2); var req: *@This() = args.ptr[args.len - 1].asPromisePtr(@This()); @@ -3339,7 +3339,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp req.handleResolveStream(); return JSValue.jsUndefined(); } - pub fn onRejectStream(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn onRejectStream(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { streamLog("onRejectStream", .{}); const args = callframe.arguments(2); var req = args.ptr[args.len - 1].asPromisePtr(@This()); @@ -4122,18 +4122,14 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp }); comptime { - @export(onResolve, .{ - .name = Export[0].symbol_name, - }); - @export(onReject, .{ - .name = Export[1].symbol_name, - }); - @export(onResolveStream, .{ - .name = Export[2].symbol_name, - }); - @export(onRejectStream, .{ - .name = Export[3].symbol_name, - }); + const jsonResolve = JSC.toJSHostFunction(onResolve); + @export(jsonResolve, .{ .name = Export[0].symbol_name }); + const jsonReject = JSC.toJSHostFunction(onReject); + @export(jsonReject, .{ .name = Export[1].symbol_name }); + const jsonResolveStream = JSC.toJSHostFunction(onResolveStream); + @export(jsonResolveStream, .{ .name = Export[2].symbol_name }); + const jsonRejectStream = JSC.toJSHostFunction(onRejectStream); + @export(jsonRejectStream, .{ .name = Export[3].symbol_name }); } }; } @@ -4835,7 +4831,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(4); if (args.len < 1) { log("publish()", .{}); @@ -4920,7 +4916,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(4); if (args.len < 1) { @@ -4983,7 +4979,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(4); if (args.len < 1) { @@ -5136,7 +5132,7 @@ pub const ServerWebSocket = struct { // Since we're passing the `this` value to the cork function, we need to // make sure the `this` value is up to date. this_value: JSC.JSValue, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(1); this.this_value = this_value; @@ -5175,7 +5171,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(2); if (args.len < 1) { @@ -5249,7 +5245,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(2); if (args.len < 1) { @@ -5333,7 +5329,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(2); if (args.len < 1) { @@ -5411,7 +5407,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return sendPing(this, globalThis, callframe, "ping", .ping); } @@ -5419,7 +5415,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return sendPing(this, globalThis, callframe, "pong", .pong); } @@ -5533,7 +5529,7 @@ pub const ServerWebSocket = struct { callframe: *JSC.CallFrame, // Since close() can lead to the close() callback being called, let's always ensure the `this` value is up to date. this_value: JSC.JSValue, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(2); log("close()", .{}); this.this_value = this_value; @@ -5580,7 +5576,7 @@ pub const ServerWebSocket = struct { callframe: *JSC.CallFrame, // Since terminate() can lead to close() being called, let's always ensure the `this` value is up to date. this_value: JSC.JSValue, - ) JSValue { + ) bun.JSError!JSValue { _ = globalThis; const args = callframe.arguments(2); _ = args; @@ -5638,7 +5634,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { log("getBufferedAmount()", .{}); if (this.isClosed()) { @@ -5651,7 +5647,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(1); if (args.len < 1) { globalThis.throw("subscribe requires at least 1 argument", .{}); @@ -5688,7 +5684,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(1); if (args.len < 1) { globalThis.throw("unsubscribe requires at least 1 argument", .{}); @@ -5725,7 +5721,7 @@ pub const ServerWebSocket = struct { this: *ServerWebSocket, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args = callframe.arguments(1); if (args.len < 1) { globalThis.throw("isSubscribed requires at least 1 argument", .{}); @@ -5828,7 +5824,7 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp pub const doRequestIP = JSC.wrapInstanceMethod(ThisServer, "requestIP", false); pub const doTimeout = JSC.wrapInstanceMethod(ThisServer, "timeout", false); - pub fn doSubscriberCount(this: *ThisServer, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn doSubscriberCount(this: *ThisServer, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("subscriberCount", 1, 0); @@ -6152,7 +6148,7 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp this.setRoutes(); } - pub fn onReload(this: *ThisServer, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) !JSC.JSValue { + pub fn onReload(this: *ThisServer, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("reload", 1, 0); @@ -6178,7 +6174,7 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp this: *ThisServer, ctx: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const arguments = callframe.arguments(2).slice(); if (arguments.len == 0) { @@ -6790,14 +6786,14 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp this.poll_ref.unref(this.vm); } - pub fn doRef(this: *ThisServer, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn doRef(this: *ThisServer, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const this_value = callframe.this(); this.ref(); return this_value; } - pub fn doUnref(this: *ThisServer, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn doUnref(this: *ThisServer, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const this_value = callframe.this(); this.unref(); diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig index 49a1f40b03..9a8774a2ff 100644 --- a/src/bun.js/base.zig +++ b/src/bun.js/base.zig @@ -980,16 +980,13 @@ pub fn DOMCall( if (!JSC.is_bindgen) { @export(slowpath, .{ .name = shim.symbolName("slowpath") }); @export(fastpath, .{ .name = shim.symbolName("fastpath") }); - } else { - _ = slowpath; - _ = fastpath; } } }; } pub fn InstanceMethodType(comptime Container: type) type { - return fn (instance: *Container, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue; + return fn (instance: *Container, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue; } pub fn wrapInstanceMethod( @@ -1007,7 +1004,7 @@ pub fn wrapInstanceMethod( this: *Container, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(FunctionTypeInfo.params.len); var iter = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments.slice()); var args: Args = undefined; @@ -1161,10 +1158,7 @@ pub fn wrapInstanceMethod( } } - const result = @call(.always_inline, @field(Container, name), args); - if (@TypeOf(result) == JSValue) return result; - if (@TypeOf(result) == bun.JSError!JSValue) return result catch .zero; - comptime unreachable; + return @call(.always_inline, @field(Container, name), args); } }.method; } @@ -1173,7 +1167,7 @@ pub fn wrapStaticMethod( comptime Container: type, comptime name: string, comptime auto_protect: bool, -) JSC.Codegen.StaticCallbackType { +) JSC.JSHostZigFunction { return struct { const FunctionType = @TypeOf(@field(Container, name)); const FunctionTypeInfo: std.builtin.Type.Fn = @typeInfo(FunctionType).Fn; @@ -1183,7 +1177,7 @@ pub fn wrapStaticMethod( pub fn method( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(FunctionTypeInfo.params.len); var iter = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments.slice()); var args: Args = undefined; diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index bba5538812..e3687ec989 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -2911,7 +2911,7 @@ pub const JSFunction = extern struct { pub fn create( global: *JSGlobalObject, fn_name: anytype, - comptime implementation: JSHostFunctionType, + comptime implementation: JSHostZigFunction, function_length: u32, options: CreateJSFunctionOptions, ) JSValue { @@ -2921,7 +2921,7 @@ pub const JSFunction = extern struct { bun.String => fn_name, else => bun.String.init(fn_name), }, - implementation, + toJSHostFunction(implementation), function_length, options.implementation_visibility, options.intrinsic, @@ -3012,19 +3012,6 @@ pub const JSGlobalObject = opaque { return .zero; } - /// This is a wrapper around just returning JSError - /// - /// The intent is for writing C++ bindings where null or some other value is - /// returned in the exception case. In a debug build, this asserts that such - /// exception is actually present, or else this will leak a .zero into - /// JS-land. - pub fn jsErrorFromCPP(global: *JSGlobalObject) JSError { - if (bun.Environment.isDebug) { - bun.assert(global.hasException()); - } - return JSError.JSError; - } - /// Pass a JSOrMemoryError!JSValue and variants through the C ABI boundary /// /// In C++, WebKit represents a thrown JavaScript expression as @@ -3456,6 +3443,7 @@ pub const JSGlobalObject = opaque { pub fn takeException(this: *JSGlobalObject, proof: bun.JSError) JSValue { switch (proof) { error.JSError => {}, + error.OutOfMemory => this.throwOutOfMemory(), } return this.tryTakeException() orelse { @@ -3685,7 +3673,7 @@ pub const JSGlobalObject = opaque { extern fn JSGlobalObject__throwTerminationException(this: *JSGlobalObject) void; }; -pub const JSNativeFn = JSHostFunctionPtr; +pub const JSNativeFn = JSHostZigFunction; pub const JSArrayIterator = struct { i: u32 = 0, @@ -5404,7 +5392,10 @@ pub const JSValue = enum(i64) { fn unwrap(value: GetResult, global: *JSGlobalObject) JSError!?JSValue { return switch (value) { - .thrown_exception => global.jsErrorFromCPP(), + .thrown_exception => { + bun.assert(global.hasException()); + return error.JSError; + }, .does_not_exist => null, else => @enumFromInt(@intFromEnum(value)), }; @@ -5429,6 +5420,10 @@ pub const JSValue = enum(i64) { } pub fn _then(this: JSValue, global: *JSGlobalObject, ctx: JSValue, resolve: JSNativeFn, reject: JSNativeFn) void { + return cppFn("_then", .{ this, global, ctx, toJSHostFunction(resolve), toJSHostFunction(reject) }); + } + + pub fn _then2(this: JSValue, global: *JSGlobalObject, ctx: JSValue, resolve: JSHostFunctionType, reject: JSHostFunctionType) void { return cppFn("_then", .{ this, global, ctx, resolve, reject }); } @@ -6849,28 +6844,28 @@ pub const EncodedJSValue = extern union { pub const JSHostFunctionType = fn (*JSGlobalObject, *CallFrame) callconv(JSC.conv) JSValue; pub const JSHostFunctionTypeWithCCallConvForAssertions = fn (*JSGlobalObject, *CallFrame) callconv(.C) JSValue; pub const JSHostFunctionPtr = *const JSHostFunctionType; +pub const JSHostZigFunction = fn (*JSGlobalObject, *CallFrame) bun.JSError!JSValue; -/// Wraps a Zig `fn (*JSGlobalObject, *CallFrame) !JSValue` into the proper JSC -/// host function type, adding handling for Zig error types and setting the -/// correct calling convention on Windows. -pub fn toJSHostFunction(comptime function_ptr: anytype) JSC.JSHostFunctionType { - const function = if (@typeInfo(@TypeOf(function_ptr)) == .Pointer) function_ptr.* else function_ptr; - - // Do not wrap twice - if (@TypeOf(function) == JSHostFunctionType) { - return function; - } - - // only operate on unspecified calling conventions. the code is going to be - // inlined anyways - const fn_type = @typeInfo(@TypeOf(function)).Fn; - bun.assert(fn_type.calling_convention == .Unspecified); - +pub fn toJSHostFunction(comptime Function: JSHostZigFunction) JSC.JSHostFunctionType { return struct { - pub fn wrapper(global: *JSGlobalObject, callframe: *CallFrame) callconv(JSC.conv) JSValue { - return global.errorUnionToCPP(@call(.always_inline, function_ptr, .{ global, callframe })); + pub fn function( + globalThis: *JSC.JSGlobalObject, + callframe: *JSC.CallFrame, + ) callconv(JSC.conv) JSC.JSValue { + return @call(.always_inline, Function, .{ globalThis, callframe }) catch |err| switch (err) { + error.JSError => .zero, + error.OutOfMemory => globalThis.throwOutOfMemoryValue(), + }; } - }.wrapper; + }.function; +} + +// XXX: temporary +pub fn toJSHostValue(globalThis: *JSGlobalObject, value: error{ OutOfMemory, JSError }!JSValue) JSValue { + return value catch |err| switch (err) { + error.JSError => .zero, + error.OutOfMemory => globalThis.throwOutOfMemoryValue(), + }; } const ParsedHostFunctionErrorSet = struct { @@ -6954,6 +6949,9 @@ pub fn NewFunction( comptime functionPointer: anytype, strong: bool, ) JSValue { + if (@TypeOf(functionPointer) == JSC.JSHostFunctionType) { + return NewRuntimeFunction(globalObject, symbolName, argCount, functionPointer, strong, false); + } return NewRuntimeFunction(globalObject, symbolName, argCount, toJSHostFunction(functionPointer), strong, false); } @@ -6963,6 +6961,9 @@ pub fn createCallback( argCount: u32, comptime functionPointer: anytype, ) JSValue { + if (@TypeOf(functionPointer) == JSC.JSHostFunctionType) { + return NewRuntimeFunction(globalObject, symbolName, argCount, functionPointer, false, false); + } return NewRuntimeFunction(globalObject, symbolName, argCount, toJSHostFunction(functionPointer), false, false); } @@ -6992,7 +6993,7 @@ pub fn NewFunctionWithData( globalObject: *JSGlobalObject, symbolName: ?*const ZigString, argCount: u32, - comptime functionPointer: anytype, + comptime functionPointer: JSC.JSHostZigFunction, strong: bool, data: *anyopaque, ) JSValue { diff --git a/src/bun.js/bindings/shimmer.zig b/src/bun.js/bindings/shimmer.zig index 307294918e..79c3dec3d4 100644 --- a/src/bun.js/bindings/shimmer.zig +++ b/src/bun.js/bindings/shimmer.zig @@ -122,7 +122,11 @@ pub fn Shimmer(comptime _namespace: []const u8, comptime _name: []const u8, comp const Fn: std.builtin.Type.Fn = @typeInfo(Function).Fn; if (Function == bun.JSC.JSHostFunctionTypeWithCCallConvForAssertions and bun.JSC.conv != .C) { @compileError("Expected " ++ bun.meta.typeName(Function) ++ " to have a JSC.conv Calling Convention."); - } else if (Function == bun.JSC.JSHostFunctionType) {} else if (Fn.calling_convention != .C) { + } else if (Function == bun.JSC.JSHostFunctionType) { + // + } else if (Function == bun.JSC.JSHostZigFunction) { + // + } else if (Fn.calling_convention != .C) { @compileError("Expected " ++ @typeName(Parent) ++ "." ++ @typeName(Function) ++ " to have a C Calling Convention."); } diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 3796aa3f61..cd4724135b 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -429,10 +429,14 @@ pub export fn Bun__GlobalObject__hasIPC(global: *JSC.JSGlobalObject) bool { extern fn Bun__Process__queueNextTick1(*JSC.ZigGlobalObject, JSC.JSValue, JSC.JSValue) void; -pub export fn Bun__Process__send( +comptime { + const Bun__Process__send = JSC.toJSHostFunction(Bun__Process__send_); + @export(Bun__Process__send, .{ .name = "Bun__Process__send" }); +} +pub fn Bun__Process__send_( globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame, -) callconv(JSC.conv) JSValue { +) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var message, var handle, var options_, var callback = callFrame.arguments(4).ptr; @@ -453,7 +457,7 @@ pub export fn Bun__Process__send( } const S = struct { - fn impl(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + fn impl(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(1).slice(); const ex = arguments_[0]; VirtualMachine.Process__emitErrorEvent(globalThis, ex); @@ -4543,7 +4547,7 @@ comptime { @export(string_allocation_limit, .{ .name = "Bun__stringSyntheticAllocationLimit" }); } -pub export fn Bun__setSyntheticAllocationLimitForTesting(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { +pub fn Bun__setSyntheticAllocationLimitForTesting(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const args = callframe.arguments(1).slice(); if (args.len < 1) { globalObject.throwNotEnoughArguments("setSyntheticAllocationLimitForTesting", 1, args.len); diff --git a/src/bun.js/node/node_cluster_binding.zig b/src/bun.js/node/node_cluster_binding.zig index ef7596b422..08e3e50dab 100644 --- a/src/bun.js/node/node_cluster_binding.zig +++ b/src/bun.js/node/node_cluster_binding.zig @@ -12,7 +12,7 @@ extern fn Process__emitErrorEvent(global: *JSC.JSGlobalObject, value: JSC.JSValu pub var child_singleton: InternalMsgHolder = .{}; -pub fn sendHelperChild(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn sendHelperChild(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { log("sendHelperChild", .{}); const arguments = callframe.arguments(3).ptr; @@ -50,7 +50,7 @@ pub fn sendHelperChild(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFram const ipc_instance = vm.getIPCInstance().?; const S = struct { - fn impl(globalThis_: *JSC.JSGlobalObject, callframe_: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + fn impl(globalThis_: *JSC.JSGlobalObject, callframe_: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe_.arguments(1).slice(); const ex = arguments_[0]; Process__emitErrorEvent(globalThis_, ex); @@ -71,7 +71,7 @@ pub fn sendHelperChild(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFram return .true; } -pub fn onInternalMessageChild(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn onInternalMessageChild(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { log("onInternalMessageChild", .{}); const arguments = callframe.arguments(2).ptr; child_singleton.worker = JSC.Strong.create(arguments[0], globalThis); @@ -172,7 +172,7 @@ pub const InternalMsgHolder = struct { } }; -pub fn sendHelperPrimary(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn sendHelperPrimary(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { log("sendHelperPrimary", .{}); const arguments = callframe.arguments(4).ptr; @@ -208,7 +208,7 @@ pub fn sendHelperPrimary(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFr return .true; } -pub fn onInternalMessagePrimary(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn onInternalMessagePrimary(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(3).ptr; const subprocess = arguments[0].as(bun.JSC.Subprocess).?; const ipc_data = subprocess.ipc() orelse return .undefined; @@ -252,7 +252,7 @@ pub fn handleInternalMessagePrimary(globalThis: *JSC.JSGlobalObject, subprocess: extern fn Bun__setChannelRef(*JSC.JSGlobalObject, bool) void; -pub fn setRef(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn setRef(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).ptr; if (arguments.len == 0) { diff --git a/src/bun.js/node/node_crypto_binding.zig b/src/bun.js/node/node_crypto_binding.zig index 430f87f951..b162be1cbd 100644 --- a/src/bun.js/node/node_crypto_binding.zig +++ b/src/bun.js/node/node_crypto_binding.zig @@ -12,7 +12,7 @@ const EVP = Crypto.EVP; const PBKDF2 = EVP.PBKDF2; const JSValue = JSC.JSValue; const validators = @import("./util/validators.zig"); -fn randomInt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +fn randomInt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).slice(); //min, max @@ -41,7 +41,7 @@ fn randomInt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSV fn pbkdf2( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(5); const data = PBKDF2.fromJS(globalThis, arguments.slice(), true) orelse { @@ -56,7 +56,7 @@ fn pbkdf2( fn pbkdf2Sync( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue { +) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(5); var data = PBKDF2.fromJS(globalThis, arguments.slice(), false) orelse { @@ -86,16 +86,12 @@ fn pbkdf2Sync( return out_arraybuffer; } -const jsPbkdf2 = JSC.toJSHostFunction(pbkdf2); -const jsPbkdf2Sync = JSC.toJSHostFunction(pbkdf2Sync); -const jsRandomInt = JSC.toJSHostFunction(randomInt); - pub fn createNodeCryptoBindingZig(global: *JSC.JSGlobalObject) JSC.JSValue { const crypto = JSC.JSValue.createEmptyObject(global, 3); - crypto.put(global, bun.String.init("pbkdf2"), JSC.JSFunction.create(global, "pbkdf2", jsPbkdf2, 5, .{})); - crypto.put(global, bun.String.init("pbkdf2Sync"), JSC.JSFunction.create(global, "pbkdf2Sync", jsPbkdf2Sync, 5, .{})); - crypto.put(global, bun.String.init("randomInt"), JSC.JSFunction.create(global, "randomInt", jsRandomInt, 2, .{})); + crypto.put(global, bun.String.init("pbkdf2"), JSC.JSFunction.create(global, "pbkdf2", pbkdf2, 5, .{})); + crypto.put(global, bun.String.init("pbkdf2Sync"), JSC.JSFunction.create(global, "pbkdf2Sync", pbkdf2Sync, 5, .{})); + crypto.put(global, bun.String.init("randomInt"), JSC.JSFunction.create(global, "randomInt", randomInt, 2, .{})); return crypto; } diff --git a/src/bun.js/node/node_error_binding.zig b/src/bun.js/node/node_error_binding.zig index 8c6de402a1..818725a0dd 100644 --- a/src/bun.js/node/node_error_binding.zig +++ b/src/bun.js/node/node_error_binding.zig @@ -14,9 +14,9 @@ pub const ERR_CHILD_CLOSED_BEFORE_REPLY = createSimpleError(createError, .ERR_CH fn createSimpleError(comptime createFn: anytype, comptime code: JSC.Node.ErrorCode, comptime message: string) JSC.JS2NativeFunctionType { const R = struct { - pub fn cbb(global: *JSC.JSGlobalObject) callconv(JSC.conv) JSC.JSValue { + pub fn cbb(global: *JSC.JSGlobalObject) bun.JSError!JSC.JSValue { const S = struct { - fn cb(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + fn cb(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = callframe; return createFn(globalThis, code, message, .{}); } diff --git a/src/bun.js/node/node_fs_binding.zig b/src/bun.js/node/node_fs_binding.zig index 202ab5bf1f..6a35883383 100644 --- a/src/bun.js/node/node_fs_binding.zig +++ b/src/bun.js/node/node_fs_binding.zig @@ -14,7 +14,7 @@ const NodeFSFunction = fn ( this: *JSC.Node.NodeJSFS, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) JSC.JSValue; +) bun.JSError!JSC.JSValue; const NodeFSFunctionEnum = std.meta.DeclEnum(JSC.Node.NodeFS); @@ -34,14 +34,14 @@ fn callSync(comptime FunctionEnum: NodeFSFunctionEnum) NodeFSFunction { this: *JSC.Node.NodeJSFS, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var arguments = callframe.arguments(8); var slice = ArgumentsSlice.init(globalObject.bunVM(), arguments.slice()); defer slice.deinit(); const args = if (comptime Arguments != void) - (Arguments.fromJS(globalObject, &slice) catch return .zero) + (try Arguments.fromJS(globalObject, &slice)) else Arguments{}; defer { @@ -79,7 +79,7 @@ fn call(comptime FunctionEnum: NodeFSFunctionEnum) NodeFSFunction { comptime if (function.params.len != 3) @compileError("Expected 3 arguments"); const Arguments = comptime function.params[1].type.?; const NodeBindingClosure = struct { - pub fn bind(this: *JSC.Node.NodeJSFS, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn bind(this: *JSC.Node.NodeJSFS, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var arguments = callframe.arguments(8); var slice = ArgumentsSlice.init(globalObject.bunVM(), arguments.slice()); @@ -244,7 +244,7 @@ pub fn createBinding(globalObject: *JSC.JSGlobalObject) JSC.JSValue { return module.toJS(globalObject); } -pub fn createMemfdForTesting(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { +pub fn createMemfdForTesting(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callFrame.arguments(1); if (arguments.len < 1) { diff --git a/src/bun.js/node/node_fs_stat_watcher.zig b/src/bun.js/node/node_fs_stat_watcher.zig index 0d1eaf5c17..2b923a888f 100644 --- a/src/bun.js/node/node_fs_stat_watcher.zig +++ b/src/bun.js/node/node_fs_stat_watcher.zig @@ -295,7 +295,7 @@ pub const StatWatcher = struct { } }; - pub fn doRef(this: *StatWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn doRef(this: *StatWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (!this.closed and !this.persistent) { this.persistent = true; this.poll_ref.ref(this.ctx); @@ -303,7 +303,7 @@ pub const StatWatcher = struct { return .undefined; } - pub fn doUnref(this: *StatWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn doUnref(this: *StatWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (this.persistent) { this.persistent = false; this.poll_ref.unref(this.ctx); @@ -329,7 +329,7 @@ pub const StatWatcher = struct { this.last_jsvalue.clear(); } - pub fn doClose(this: *StatWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn doClose(this: *StatWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { this.close(); return .undefined; } diff --git a/src/bun.js/node/node_fs_watcher.zig b/src/bun.js/node/node_fs_watcher.zig index 3a6c92690c..df4025082e 100644 --- a/src/bun.js/node/node_fs_watcher.zig +++ b/src/bun.js/node/node_fs_watcher.zig @@ -563,7 +563,7 @@ pub const FSWatcher = struct { ) catch |err| globalObject.reportActiveExceptionAsUnhandled(err); } - pub fn doRef(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn doRef(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (!this.closed and !this.persistent) { this.persistent = true; this.poll_ref.ref(this.ctx); @@ -571,7 +571,7 @@ pub const FSWatcher = struct { return .undefined; } - pub fn doUnref(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn doUnref(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (this.persistent) { this.persistent = false; this.poll_ref.unref(this.ctx); @@ -579,7 +579,7 @@ pub const FSWatcher = struct { return .undefined; } - pub fn hasRef(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn hasRef(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.persistent); } @@ -649,7 +649,7 @@ pub const FSWatcher = struct { this.js_this = .zero; } - pub fn doClose(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn doClose(this: *FSWatcher, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { this.close(); return .undefined; } diff --git a/src/bun.js/node/node_http_binding.zig b/src/bun.js/node/node_http_binding.zig index a393736cd2..a6798320cd 100644 --- a/src/bun.js/node/node_http_binding.zig +++ b/src/bun.js/node/node_http_binding.zig @@ -7,7 +7,7 @@ const Output = bun.Output; const ZigString = JSC.ZigString; const uv = bun.windows.libuv; -pub fn getBunServerAllClosedPromise(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn getBunServerAllClosedPromise(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("getBunServerAllClosePromise", 1, arguments.len); @@ -30,13 +30,13 @@ pub fn getBunServerAllClosedPromise(globalThis: *JSC.JSGlobalObject, callframe: return globalThis.throwInvalidArgumentTypeValue("server", "bun.Server", value); } -pub fn getMaxHTTPHeaderSize(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn getMaxHTTPHeaderSize(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = globalThis; // autofix _ = callframe; // autofix return JSC.JSValue.jsNumber(bun.http.max_http_header_size); } -pub fn setMaxHTTPHeaderSize(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn setMaxHTTPHeaderSize(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("setMaxHTTPHeaderSize", 1, arguments.len); diff --git a/src/bun.js/node/node_net_binding.zig b/src/bun.js/node/node_net_binding.zig index 8a4854261f..c7819c4314 100644 --- a/src/bun.js/node/node_net_binding.zig +++ b/src/bun.js/node/node_net_binding.zig @@ -13,7 +13,7 @@ pub var autoSelectFamilyDefault: bool = true; pub fn getDefaultAutoSelectFamily(global: *JSC.JSGlobalObject) JSC.JSValue { return JSC.JSFunction.create(global, "getDefaultAutoSelectFamily", (struct { - fn getter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + fn getter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = globalThis; _ = callframe; return JSC.jsBoolean(autoSelectFamilyDefault); @@ -23,7 +23,7 @@ pub fn getDefaultAutoSelectFamily(global: *JSC.JSGlobalObject) JSC.JSValue { pub fn setDefaultAutoSelectFamily(global: *JSC.JSGlobalObject) JSC.JSValue { return JSC.JSFunction.create(global, "setDefaultAutoSelectFamily", (struct { - fn setter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + fn setter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); if (arguments.len < 1) { globalThis.throw("missing argument", .{}); @@ -48,7 +48,7 @@ pub var autoSelectFamilyAttemptTimeoutDefault: u32 = 250; pub fn getDefaultAutoSelectFamilyAttemptTimeout(global: *JSC.JSGlobalObject) JSC.JSValue { return JSC.JSFunction.create(global, "getDefaultAutoSelectFamilyAttemptTimeout", (struct { - fn getter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + fn getter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = globalThis; _ = callframe; return JSC.jsNumber(autoSelectFamilyAttemptTimeoutDefault); @@ -58,7 +58,7 @@ pub fn getDefaultAutoSelectFamilyAttemptTimeout(global: *JSC.JSGlobalObject) JSC pub fn setDefaultAutoSelectFamilyAttemptTimeout(global: *JSC.JSGlobalObject) JSC.JSValue { return JSC.JSFunction.create(global, "setDefaultAutoSelectFamilyAttemptTimeout", (struct { - fn setter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + fn setter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); if (arguments.len < 1) { globalThis.throw("missing argument", .{}); diff --git a/src/bun.js/node/node_os.zig b/src/bun.js/node/node_os.zig index c18a7b5860..ee0f8af2a6 100644 --- a/src/bun.js/node/node_os.zig +++ b/src/bun.js/node/node_os.zig @@ -51,7 +51,7 @@ pub const OS = struct { } }; - pub fn cpus(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn cpus(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); return switch (Environment.os) { @@ -294,19 +294,19 @@ pub const OS = struct { return values; } - pub fn endianness(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn endianness(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); return JSC.ZigString.init("LE").withEncoding().toJS(globalThis); } - pub fn freemem(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn freemem(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); return JSC.JSValue.jsNumberFromUint64(C.getFreeMemory()); } - pub fn getPriority(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn getPriority(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var args_ = callframe.arguments(1); @@ -345,7 +345,7 @@ pub const OS = struct { return JSC.JSValue.jsNumberFromInt32(priority); } - pub fn homedir(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn homedir(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const dir: []const u8 = brk: { @@ -363,7 +363,7 @@ pub const OS = struct { return JSC.ZigString.init(dir).withEncoding().toJS(globalThis); } - pub fn hostname(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn hostname(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); if (comptime Environment.isWindows) { @@ -391,7 +391,7 @@ pub const OS = struct { return JSC.ZigString.init(std.posix.gethostname(&name_buffer) catch "unknown").withEncoding().toJS(globalThis); } - pub fn loadavg(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn loadavg(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const result = C.getSystemLoadavg(); @@ -402,7 +402,7 @@ pub const OS = struct { }); } - pub fn networkInterfaces(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn networkInterfaces(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { return switch (Environment.os) { .windows => networkInterfacesWindows(globalThis), else => networkInterfacesPosix(globalThis), @@ -707,19 +707,19 @@ pub const OS = struct { return ret; } - pub fn platform(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn platform(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); return JSC.ZigString.init(Global.os_name).withEncoding().toJS(globalThis); } - pub fn release(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn release(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var name_buffer: [bun.HOST_NAME_MAX]u8 = undefined; return JSC.ZigString.init(C.getRelease(&name_buffer)).withEncoding().toJS(globalThis); } - pub fn setPriority(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn setPriority(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var args_ = callframe.arguments(2); @@ -782,13 +782,13 @@ pub const OS = struct { return .undefined; } - pub fn totalmem(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn totalmem(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); return JSC.JSValue.jsNumberFromUint64(C.getTotalMemory()); } - pub fn @"type"(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn @"type"(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); if (comptime Environment.isWindows) @@ -801,7 +801,7 @@ pub const OS = struct { return JSC.ZigString.init(Global.os_name).withEncoding().toJS(globalThis); } - pub fn uptime(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn uptime(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (Environment.isWindows) { var uptime_value: f64 = undefined; const err = libuv.uv_uptime(&uptime_value); @@ -821,10 +821,10 @@ pub const OS = struct { return JSC.JSValue.jsNumberFromUint64(C.getSystemUptime()); } - pub fn userInfo(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn userInfo(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const result = JSC.JSValue.createEmptyObject(globalThis, 5); - result.put(globalThis, JSC.ZigString.static("homedir"), homedir(globalThis, callframe)); + result.put(globalThis, JSC.ZigString.static("homedir"), try homedir(globalThis, callframe)); if (comptime Environment.isWindows) { result.put(globalThis, JSC.ZigString.static("username"), JSC.ZigString.init(bun.getenvZ("USERNAME") orelse "unknown").withEncoding().toJS(globalThis)); @@ -844,7 +844,7 @@ pub const OS = struct { return result; } - pub fn version(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn version(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var name_buffer: [bun.HOST_NAME_MAX]u8 = undefined; return JSC.ZigString.init(C.getVersion(&name_buffer)).withEncoding().toJS(globalThis); @@ -865,7 +865,7 @@ pub const OS = struct { }; } - pub fn machine(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn machine(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); return JSC.ZigString.static(comptime getMachineName()).toJS(globalThis); } diff --git a/src/bun.js/node/node_util_binding.zig b/src/bun.js/node/node_util_binding.zig index dfbbcca653..7aea3927d3 100644 --- a/src/bun.js/node/node_util_binding.zig +++ b/src/bun.js/node/node_util_binding.zig @@ -7,7 +7,7 @@ const Output = bun.Output; const ZigString = JSC.ZigString; const uv = bun.windows.libuv; -pub fn internalErrorName(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn internalErrorName(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("internalErrorName", 1, arguments.len); diff --git a/src/bun.js/node/node_zlib_binding.zig b/src/bun.js/node/node_zlib_binding.zig index c1dc952e5d..096c2a50e7 100644 --- a/src/bun.js/node/node_zlib_binding.zig +++ b/src/bun.js/node/node_zlib_binding.zig @@ -7,7 +7,7 @@ const Output = bun.Output; const ZigString = JSC.ZigString; const validators = @import("./util/validators.zig"); -pub fn crc32(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn crc32(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).ptr; const data: ZigString.Slice = blk: { @@ -59,7 +59,7 @@ pub fn crc32(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSV pub fn CompressionStream(comptime T: type) type { return struct { - pub fn write(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn write(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(7).slice(); if (arguments.len != 7) { @@ -151,7 +151,7 @@ pub fn CompressionStream(comptime T: type) type { if (this.pending_close) _ = this._close(); } - pub fn writeSync(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn writeSync(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(7).slice(); if (arguments.len != 7) { @@ -201,7 +201,7 @@ pub fn CompressionStream(comptime T: type) type { // this.stream.doWork(); - if (this.checkError(globalThis) catch return .zero) { + if (try this.checkError(globalThis)) { this.stream.updateWriteResult(&this.write_result.?[1], &this.write_result.?[0]); this.write_in_progress = false; } @@ -210,17 +210,17 @@ pub fn CompressionStream(comptime T: type) type { return .undefined; } - pub fn reset(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn reset(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = callframe; const err = this.stream.reset(); if (err.isError()) { - this.emitError(globalThis, err) catch return .zero; + try this.emitError(globalThis, err); } return .undefined; } - pub fn close(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn close(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = globalThis; _ = callframe; this._close(); @@ -355,7 +355,7 @@ pub const SNativeZlib = struct { // return @sizeOf(SNativeZlib) + internal_state_size; // } - pub fn init(this: *SNativeZlib, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn init(this: *SNativeZlib, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(7).slice(); if (arguments.len != 7) { @@ -363,13 +363,13 @@ pub const SNativeZlib = struct { return .zero; } - const windowBits = validators.validateInt32(globalThis, arguments[0], "windowBits", .{}, null, null) catch return .zero; - const level = validators.validateInt32(globalThis, arguments[1], "level", .{}, null, null) catch return .zero; - const memLevel = validators.validateInt32(globalThis, arguments[2], "memLevel", .{}, null, null) catch return .zero; - const strategy = validators.validateInt32(globalThis, arguments[3], "strategy", .{}, null, null) catch return .zero; + const windowBits = try validators.validateInt32(globalThis, arguments[0], "windowBits", .{}, null, null); + const level = try validators.validateInt32(globalThis, arguments[1], "level", .{}, null, null); + const memLevel = try validators.validateInt32(globalThis, arguments[2], "memLevel", .{}, null, null); + const strategy = try validators.validateInt32(globalThis, arguments[3], "strategy", .{}, null, null); // this does not get gc'd because it is stored in the JS object's `this._writeState`. and the JS object is tied to the native handle as `_handle[owner_symbol]`. const writeResult = arguments[4].asArrayBuffer(globalThis).?.asU32().ptr; - const writeCallback = validators.validateFunction(globalThis, arguments[5], "writeCallback", .{}) catch return .zero; + const writeCallback = try validators.validateFunction(globalThis, arguments[5], "writeCallback", .{}); const dictionary = if (arguments[6].isUndefined()) null else arguments[6].asArrayBuffer(globalThis).?.byteSlice(); this.write_result = writeResult; @@ -380,7 +380,7 @@ pub const SNativeZlib = struct { return .undefined; } - pub fn params(this: *SNativeZlib, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn params(this: *SNativeZlib, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(2).slice(); if (arguments.len != 2) { @@ -388,12 +388,12 @@ pub const SNativeZlib = struct { return .zero; } - const level = validators.validateInt32(globalThis, arguments[0], "level", .{}, null, null) catch return .zero; - const strategy = validators.validateInt32(globalThis, arguments[1], "strategy", .{}, null, null) catch return .zero; + const level = try validators.validateInt32(globalThis, arguments[0], "level", .{}, null, null); + const strategy = try validators.validateInt32(globalThis, arguments[1], "strategy", .{}, null, null); const err = this.stream.setParams(level, strategy); if (err.isError()) { - this.emitError(globalThis, err) catch return .zero; + try this.emitError(globalThis, err); } return .undefined; } @@ -719,7 +719,7 @@ pub const SNativeBrotli = struct { }; } - pub fn init(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn init(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(3).slice(); if (arguments.len != 3) { globalThis.ERR_MISSING_ARGS("init(params, writeResult, writeCallback)", .{}).throw(); @@ -728,13 +728,13 @@ pub const SNativeBrotli = struct { // this does not get gc'd because it is stored in the JS object's `this._writeState`. and the JS object is tied to the native handle as `_handle[owner_symbol]`. const writeResult = arguments[1].asArrayBuffer(globalThis).?.asU32().ptr; - const writeCallback = validators.validateFunction(globalThis, arguments[2], "writeCallback", .{}) catch return .zero; + const writeCallback = try validators.validateFunction(globalThis, arguments[2], "writeCallback", .{}); this.write_result = writeResult; this.write_callback.set(globalThis, writeCallback); var err = this.stream.init(); if (err.isError()) { - this.emitError(globalThis, err) catch return .zero; + try this.emitError(globalThis, err); return JSC.jsBoolean(false); } @@ -747,14 +747,14 @@ pub const SNativeBrotli = struct { } err = this.stream.setParams(@intCast(i), d); if (err.isError()) { - // this.emitError(globalThis, err) catch return .zero; //XXX: onerror isn't set yet + // try this.emitError(globalThis, err); //XXX: onerror isn't set yet return JSC.jsBoolean(false); } } return JSC.jsBoolean(true); } - pub fn params(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn params(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = this; _ = globalThis; _ = callframe; diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig index 4cc83f317a..4b78d05bce 100644 --- a/src/bun.js/node/types.zig +++ b/src/bun.js/node/types.zig @@ -1636,13 +1636,13 @@ pub fn StatType(comptime Big: bool) type { const DOMCallFn = fn ( *This, *JSC.JSGlobalObject, - ) callconv(JSC.conv) JSC.JSValue; + ) bun.JSError!JSC.JSValue; fn domCall(comptime decl: meta.DeclEnum(This)) DOMCallFn { return struct { pub fn run( this: *This, _: *JSC.JSGlobalObject, - ) callconv(JSC.conv) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return @field(This, @tagName(decl))(this); } }.run; @@ -1884,49 +1884,49 @@ pub const Dirent = struct { this: *Dirent, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.kind == std.fs.File.Kind.block_device); } pub fn isCharacterDevice( this: *Dirent, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.kind == std.fs.File.Kind.character_device); } pub fn isDirectory( this: *Dirent, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.kind == std.fs.File.Kind.directory); } pub fn isFIFO( this: *Dirent, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.kind == std.fs.File.Kind.named_pipe or this.kind == std.fs.File.Kind.event_port); } pub fn isFile( this: *Dirent, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.kind == std.fs.File.Kind.file); } pub fn isSocket( this: *Dirent, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.kind == std.fs.File.Kind.unix_domain_socket); } pub fn isSymbolicLink( this: *Dirent, _: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return JSC.JSValue.jsBoolean(this.kind == std.fs.File.Kind.sym_link); } diff --git a/src/bun.js/node/util/parse_args.zig b/src/bun.js/node/util/parse_args.zig index 95f0fc6337..a7d2c1895d 100644 --- a/src/bun.js/node/util/parse_args.zig +++ b/src/bun.js/node/util/parse_args.zig @@ -650,7 +650,11 @@ const ParseArgsState = struct { } }; -pub fn parseArgs(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) !JSValue { +pub fn parseArgs( + globalThis: *JSGlobalObject, + callframe: *JSC.CallFrame, +) bun.JSError!JSValue { + JSC.markBinding(@src()); const arguments = callframe.argumentsAsArray(1); return parseArgsImpl(globalThis, arguments[0]); } diff --git a/src/bun.js/test/expect.zig b/src/bun.js/test/expect.zig index 2725ec4db5..b5c13afbb9 100644 --- a/src/bun.js/test/expect.zig +++ b/src/bun.js/test/expect.zig @@ -373,7 +373,7 @@ pub const Expect = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(2).slice(); const value = if (arguments.len < 1) JSValue.jsUndefined() else arguments[0]; @@ -435,7 +435,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const arguments_ = callFrame.arguments(1); @@ -482,7 +482,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const arguments_ = callFrame.arguments(1); @@ -525,7 +525,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callframe.this(); const arguments_ = callframe.arguments(2); @@ -592,7 +592,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callframe.this(); const arguments_ = callframe.arguments(1); @@ -667,7 +667,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -753,7 +753,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -851,7 +851,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -907,7 +907,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -980,7 +980,7 @@ pub const Expect = struct { this: *Expect, globalObject: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalObject); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -1048,7 +1048,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -1116,7 +1116,7 @@ pub const Expect = struct { this: *Expect, globalObject: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalObject); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -1173,7 +1173,7 @@ pub const Expect = struct { this: *Expect, globalObject: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalObject); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -1240,7 +1240,7 @@ pub const Expect = struct { this: *Expect, globalObject: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalObject); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -1313,7 +1313,7 @@ pub const Expect = struct { this: *Expect, globalObject: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalObject); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -1380,7 +1380,7 @@ pub const Expect = struct { this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); @@ -1482,7 +1482,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeTruthy(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeTruthy(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const value: JSValue = this.getValue(globalThis, thisValue, "toBeTruthy", "") orelse return .zero; @@ -1514,7 +1514,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeUndefined(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeUndefined(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const value: JSValue = this.getValue(globalThis, thisValue, "toBeUndefined", "") orelse return .zero; @@ -1544,7 +1544,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeNaN(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeNaN(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1578,7 +1578,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeNull(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeNull(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1607,7 +1607,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeDefined(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeDefined(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1636,7 +1636,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeFalsy(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeFalsy(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1670,7 +1670,7 @@ pub const Expect = struct { return .zero; } - pub fn toEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1712,7 +1712,7 @@ pub const Expect = struct { return .zero; } - pub fn toStrictEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toStrictEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1749,7 +1749,7 @@ pub const Expect = struct { return .zero; } - pub fn toHaveProperty(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toHaveProperty(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1844,7 +1844,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeEven(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeEven(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1898,7 +1898,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeGreaterThan(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeGreaterThan(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -1961,7 +1961,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeGreaterThanOrEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeGreaterThanOrEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2024,7 +2024,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeLessThan(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeLessThan(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2087,7 +2087,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeLessThanOrEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeLessThanOrEqual(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2150,7 +2150,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeCloseTo(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeCloseTo(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2233,7 +2233,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeOdd(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeOdd(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2285,7 +2285,7 @@ pub const Expect = struct { return .zero; } - pub fn toThrow(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toThrow(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2663,7 +2663,7 @@ pub const Expect = struct { this.throw(globalThis, signature, expected_fmt, .{ expected_class, result.toFmt(&formatter) }); return .zero; } - pub fn toMatchSnapshot(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toMatchSnapshot(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(2); @@ -2778,7 +2778,7 @@ pub const Expect = struct { return .undefined; } - pub fn toBeEmpty(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeEmpty(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2856,7 +2856,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeEmptyObject(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeEmptyObject(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2884,7 +2884,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeNil(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeNil(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2911,7 +2911,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeArray(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeArray(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2938,7 +2938,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeArrayOfSize(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeArrayOfSize(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -2982,7 +2982,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeBoolean(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeBoolean(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3009,7 +3009,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeTypeOf(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeTypeOf(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3086,7 +3086,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeTrue(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeTrue(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3113,7 +3113,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeFalse(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeFalse(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3140,7 +3140,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeNumber(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeNumber(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3167,7 +3167,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeInteger(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeInteger(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3194,7 +3194,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeObject(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeObject(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3221,7 +3221,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeFinite(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeFinite(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3254,7 +3254,7 @@ pub const Expect = struct { return .zero; } - pub fn toBePositive(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBePositive(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3287,7 +3287,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeNegative(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeNegative(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3320,7 +3320,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeWithin(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeWithin(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3383,7 +3383,7 @@ pub const Expect = struct { return .zero; } - pub fn toEqualIgnoringWhitespace(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toEqualIgnoringWhitespace(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3465,7 +3465,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeSymbol(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeSymbol(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3492,7 +3492,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeFunction(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeFunction(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3519,7 +3519,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeDate(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeDate(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3546,7 +3546,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeValidDate(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeValidDate(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3574,7 +3574,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeString(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeString(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3601,7 +3601,7 @@ pub const Expect = struct { return .zero; } - pub fn toInclude(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toInclude(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3658,7 +3658,7 @@ pub const Expect = struct { return .zero; } - pub fn toIncludeRepeated(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toIncludeRepeated(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3769,7 +3769,7 @@ pub const Expect = struct { return .zero; } - pub fn toSatisfy(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toSatisfy(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3827,7 +3827,7 @@ pub const Expect = struct { return .zero; } - pub fn toStartWith(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toStartWith(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3884,7 +3884,7 @@ pub const Expect = struct { return .zero; } - pub fn toEndWith(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toEndWith(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3941,7 +3941,7 @@ pub const Expect = struct { return .zero; } - pub fn toBeInstanceOf(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toBeInstanceOf(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer this.postMatch(globalThis); const thisValue = callFrame.this(); @@ -3988,7 +3988,7 @@ pub const Expect = struct { return .zero; } - pub fn toMatch(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toMatch(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); defer this.postMatch(globalThis); @@ -4052,7 +4052,7 @@ pub const Expect = struct { return .zero; } - pub fn toHaveBeenCalled(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toHaveBeenCalled(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); defer this.postMatch(globalThis); @@ -4085,7 +4085,7 @@ pub const Expect = struct { return .zero; } - pub fn toHaveBeenCalledTimes(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toHaveBeenCalledTimes(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); @@ -4128,7 +4128,7 @@ pub const Expect = struct { return .zero; } - pub fn toMatchObject(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn toMatchObject(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); defer this.postMatch(globalThis); @@ -4192,7 +4192,7 @@ pub const Expect = struct { return .zero; } - pub fn toHaveBeenCalledWith(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toHaveBeenCalledWith(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); @@ -4255,7 +4255,7 @@ pub const Expect = struct { return .zero; } - pub fn toHaveBeenLastCalledWith(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toHaveBeenLastCalledWith(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); @@ -4317,7 +4317,7 @@ pub const Expect = struct { return .zero; } - pub fn toHaveBeenNthCalledWith(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toHaveBeenNthCalledWith(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); @@ -4480,11 +4480,11 @@ pub const Expect = struct { } } - pub fn toHaveReturned(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toHaveReturned(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return toHaveReturnedTimesFn(this, globalThis, callframe, 1); } - pub fn toHaveReturnedTimes(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toHaveReturnedTimes(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return toHaveReturnedTimesFn(this, globalThis, callframe, null); } @@ -4507,36 +4507,36 @@ pub const Expect = struct { return ExpectStatic.create(globalThis, .{ .promise = .rejects }); } - pub fn any(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn any(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return ExpectAny.call(globalThis, callFrame); } - pub fn anything(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn anything(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return ExpectAnything.call(globalThis, callFrame); } - pub fn closeTo(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn closeTo(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return ExpectCloseTo.call(globalThis, callFrame); } - pub fn objectContaining(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn objectContaining(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return ExpectObjectContaining.call(globalThis, callFrame); } - pub fn stringContaining(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn stringContaining(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return ExpectStringContaining.call(globalThis, callFrame); } - pub fn stringMatching(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn stringMatching(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return ExpectStringMatching.call(globalThis, callFrame); } - pub fn arrayContaining(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn arrayContaining(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return ExpectArrayContaining.call(globalThis, callFrame); } /// Implements `expect.extend({ ... })` - pub fn extend(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn extend(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].isObject()) { @@ -4569,7 +4569,7 @@ pub const Expect = struct { // Even though they point to the same native functions for all matchers, // multiple instances are created because each instance will hold the matcher_fn as a property - const wrapper_fn = Bun__JSWrappingFunction__create(globalThis, matcher_name, Expect.applyCustomMatcher, matcher_fn, true); + const wrapper_fn = Bun__JSWrappingFunction__create(globalThis, matcher_name, JSC.toJSHostFunction(Expect.applyCustomMatcher), matcher_fn, true); expect_proto.put(globalThis, matcher_name, wrapper_fn); expect_constructor.put(globalThis, matcher_name, wrapper_fn); @@ -4757,7 +4757,7 @@ pub const Expect = struct { /// Function that is run for either `expect.myMatcher()` call or `expect().myMatcher` call, /// and we can known which case it is based on if the `callFrame.this()` value is an instance of Expect - pub fn applyCustomMatcher(globalThis: *JSGlobalObject, callFrame: *CallFrame) callconv(JSC.conv) JSValue { + pub fn applyCustomMatcher(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSC.JSValue { defer globalThis.bunVM().autoGarbageCollect(); // retrieve the user-provided matcher function (matcher_fn) @@ -4819,7 +4819,7 @@ pub const Expect = struct { pub const addSnapshotSerializer = notImplementedStaticFn; - pub fn hasAssertions(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn hasAssertions(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { _ = callFrame; defer globalThis.bunVM().autoGarbageCollect(); @@ -4828,7 +4828,7 @@ pub const Expect = struct { return .undefined; } - pub fn assertions(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn assertions(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { defer globalThis.bunVM().autoGarbageCollect(); const arguments_ = callFrame.arguments(1); @@ -4862,12 +4862,12 @@ pub const Expect = struct { return .undefined; } - pub fn notImplementedJSCFn(_: *Expect, globalThis: *JSGlobalObject, _: *CallFrame) JSValue { + pub fn notImplementedJSCFn(_: *Expect, globalThis: *JSGlobalObject, _: *CallFrame) bun.JSError!JSValue { globalThis.throw("Not implemented", .{}); return .zero; } - pub fn notImplementedStaticFn(globalThis: *JSGlobalObject, _: *CallFrame) JSValue { + pub fn notImplementedStaticFn(globalThis: *JSGlobalObject, _: *CallFrame) bun.JSError!JSValue { globalThis.throw("Not implemented", .{}); return .zero; } @@ -4887,7 +4887,7 @@ pub const Expect = struct { vm.autoGarbageCollect(); } - pub fn doUnreachable(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn doUnreachable(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arg = callframe.arguments(1).ptr[0]; if (arg.isEmptyOrUndefinedOrNull()) { @@ -4965,9 +4965,9 @@ pub const ExpectStatic = struct { return .zero; } - fn createAsymmetricMatcherWithFlags(T: anytype, this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + fn createAsymmetricMatcherWithFlags(T: type, this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { //const this: *ExpectStatic = ExpectStatic.fromJS(callFrame.this()); - const instance_jsvalue = T.call(globalThis, callFrame); + const instance_jsvalue = try T.call(globalThis, callFrame); if (instance_jsvalue != .zero and !instance_jsvalue.isAnyError()) { var instance = T.fromJS(instance_jsvalue) orelse { globalThis.throwOutOfMemory(); @@ -4978,31 +4978,31 @@ pub const ExpectStatic = struct { return instance_jsvalue; } - pub fn anything(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn anything(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return createAsymmetricMatcherWithFlags(ExpectAnything, this, globalThis, callFrame); } - pub fn any(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn any(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return createAsymmetricMatcherWithFlags(ExpectAny, this, globalThis, callFrame); } - pub fn arrayContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn arrayContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return createAsymmetricMatcherWithFlags(ExpectArrayContaining, this, globalThis, callFrame); } - pub fn closeTo(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn closeTo(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return createAsymmetricMatcherWithFlags(ExpectCloseTo, this, globalThis, callFrame); } - pub fn objectContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn objectContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return createAsymmetricMatcherWithFlags(ExpectObjectContaining, this, globalThis, callFrame); } - pub fn stringContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn stringContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return createAsymmetricMatcherWithFlags(ExpectStringContaining, this, globalThis, callFrame); } - pub fn stringMatching(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn stringMatching(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { return createAsymmetricMatcherWithFlags(ExpectStringMatching, this, globalThis, callFrame); } }; @@ -5018,7 +5018,7 @@ pub const ExpectAnything = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, _: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, _: *CallFrame) bun.JSError!JSValue { const anything = globalThis.bunVM().allocator.create(ExpectAnything) catch { globalThis.throwOutOfMemory(); return .zero; @@ -5046,7 +5046,7 @@ pub const ExpectStringMatching = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or (!args[0].isString() and !args[0].isRegExp())) { @@ -5083,7 +5083,7 @@ pub const ExpectCloseTo = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(2).slice(); if (args.len == 0 or !args[0].isNumber()) { @@ -5130,7 +5130,7 @@ pub const ExpectObjectContaining = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].isObject()) { @@ -5167,7 +5167,7 @@ pub const ExpectStringContaining = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].isString()) { @@ -5204,7 +5204,7 @@ pub const ExpectAny = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; @@ -5261,7 +5261,7 @@ pub const ExpectArrayContaining = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].jsType().isArray()) { @@ -5385,7 +5385,7 @@ pub const ExpectCustomAsymmetricMatcher = struct { return Expect.executeCustomMatcher(globalThis, matcher_name, matcher_fn, matcher_args.items, this.flags, true); } - pub fn asymmetricMatch(this: *ExpectCustomAsymmetricMatcher, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn asymmetricMatch(this: *ExpectCustomAsymmetricMatcher, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(1).slice(); const received_value = if (arguments.len < 1) JSValue.jsUndefined() else arguments[0]; const matched = execute(this, callframe.this(), globalThis, received_value); @@ -5419,7 +5419,7 @@ pub const ExpectCustomAsymmetricMatcher = struct { return false; } - pub fn toAsymmetricMatcher(this: *ExpectCustomAsymmetricMatcher, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn toAsymmetricMatcher(this: *ExpectCustomAsymmetricMatcher, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { var stack_fallback = std.heap.stackFallback(512, globalThis.allocator()); var mutable_string = bun.MutableString.init2048(stack_fallback.get()) catch { globalThis.throwOutOfMemory(); @@ -5472,7 +5472,7 @@ pub const ExpectMatcherContext = struct { return JSValue.false; } - pub fn equals(_: *ExpectMatcherContext, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn equals(_: *ExpectMatcherContext, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { var arguments = callframe.arguments(3); if (arguments.len < 2) { globalThis.throw("expect.extends matcher: this.util.equals expects at least 2 arguments", .{}); @@ -5541,25 +5541,25 @@ pub const ExpectMatcherUtils = struct { }; } - pub fn stringify(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn stringify(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(1).slice(); const value = if (arguments.len < 1) JSValue.jsUndefined() else arguments[0]; return printValueCatched(globalThis, value, null); } - pub fn printExpected(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn printExpected(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(1).slice(); const value = if (arguments.len < 1) JSValue.jsUndefined() else arguments[0]; return printValueCatched(globalThis, value, ""); } - pub fn printReceived(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn printReceived(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(1).slice(); const value = if (arguments.len < 1) JSValue.jsUndefined() else arguments[0]; return printValueCatched(globalThis, value, ""); } - pub fn matcherHint(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn matcherHint(_: *ExpectMatcherUtils, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(4).slice(); if (arguments.len == 0 or !arguments[0].isString()) { diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig index 813189a7b0..300355ede7 100644 --- a/src/bun.js/test/jest.zig +++ b/src/bun.js/test/jest.zig @@ -276,12 +276,12 @@ pub const TestRunner = struct { pub const Jest = struct { pub var runner: ?*TestRunner = null; - fn globalHook(comptime name: string) JSC.JSHostFunctionType { + fn globalHook(comptime name: string) JSC.JSHostZigFunction { return struct { pub fn appendGlobalFunctionCallback( globalThis: *JSGlobalObject, callframe: *CallFrame, - ) callconv(JSC.conv) JSValue { + ) bun.JSError!JSValue { const the_runner = runner orelse { globalThis.throw("Cannot use " ++ name ++ "() outside of the test runner. Run \"bun test\" to run tests.", .{}); return .zero; @@ -526,7 +526,7 @@ pub const Jest = struct { pub fn call( globalObject: *JSGlobalObject, callframe: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const vm = globalObject.bunVM(); if (vm.is_in_preload or runner == null) { return Bun__Jest__testPreloadObject(globalObject); @@ -554,7 +554,7 @@ pub const Jest = struct { return Bun__Jest__testModuleObject(globalObject); } - fn jsSetDefaultTimeout(globalObject: *JSGlobalObject, callframe: *CallFrame) JSValue { + fn jsSetDefaultTimeout(globalObject: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len < 1 or !arguments[0].isNumber()) { globalObject.throw("setTimeout() expects a number (milliseconds)", .{}); @@ -604,39 +604,39 @@ pub const TestScope = struct { actual: u32 = 0, }; - pub fn call(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "test()", true, .pass); } - pub fn only(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn only(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "test.only()", true, .only); } - pub fn skip(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn skip(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "test.skip()", true, .skip); } - pub fn todo(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn todo(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "test.todo()", true, .todo); } - pub fn each(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn each(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createEach(globalThis, callframe, "test.each()", "each", true); } - pub fn callIf(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn callIf(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createIfScope(globalThis, callframe, "test.if()", "if", TestScope, .pass); } - pub fn skipIf(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn skipIf(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createIfScope(globalThis, callframe, "test.skipIf()", "skipIf", TestScope, .skip); } - pub fn todoIf(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn todoIf(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createIfScope(globalThis, callframe, "test.todoIf()", "todoIf", TestScope, .todo); } - pub fn onReject(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn onReject(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { debug("onReject", .{}); const arguments = callframe.arguments(2); const err = arguments.ptr[0]; @@ -648,7 +648,7 @@ pub const TestScope = struct { } const jsOnReject = JSC.toJSHostFunction(onReject); - pub fn onResolve(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn onResolve(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { debug("onResolve", .{}); const arguments = callframe.arguments(2); var task: *TestRunnerTask = arguments.ptr[1].asPromisePtr(TestRunnerTask); @@ -661,7 +661,7 @@ pub const TestScope = struct { pub fn onDone( globalThis: *JSGlobalObject, callframe: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const function = callframe.callee(); const args = callframe.arguments(1); defer globalThis.bunVM().autoGarbageCollect(); @@ -778,7 +778,7 @@ pub const TestScope = struct { task.promise_state = .pending; switch (promise) { .normal => |p| { - _ = p.asValue(vm.global).then(vm.global, task, jsOnResolve, jsOnReject); + _ = p.asValue(vm.global).then(vm.global, task, onResolve, onReject); return .{ .pending = {} }; }, else => unreachable, @@ -890,14 +890,14 @@ pub const DescribeScope = struct { pub threadlocal var active: ?*DescribeScope = null; - const CallbackFn = JSC.JSHostFunctionType; + const CallbackFn = JSC.JSHostZigFunction; fn createCallback(comptime hook: LifecycleHook) CallbackFn { return struct { pub fn run( globalThis: *JSGlobalObject, callframe: *CallFrame, - ) callconv(JSC.conv) JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2); if (arguments.len < 1) { globalThis.throwNotEnoughArguments("callback", 1, arguments.len); @@ -920,7 +920,7 @@ pub const DescribeScope = struct { pub fn onDone( ctx: js.JSContextRef, callframe: *CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const function = callframe.callee(); const args = callframe.arguments(1); defer ctx.bunVM().autoGarbageCollect(); @@ -1074,35 +1074,35 @@ pub const DescribeScope = struct { return null; } - pub fn call(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn call(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "describe()", false, .pass); } - pub fn only(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn only(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "describe.only()", false, .only); } - pub fn skip(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn skip(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "describe.skip()", false, .skip); } - pub fn todo(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn todo(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createScope(globalThis, callframe, "describe.todo()", false, .todo); } - pub fn each(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn each(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createEach(globalThis, callframe, "describe.each()", "each", false); } - pub fn callIf(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn callIf(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createIfScope(globalThis, callframe, "describe.if()", "if", DescribeScope, .pass); } - pub fn skipIf(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn skipIf(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createIfScope(globalThis, callframe, "describe.skipIf()", "skipIf", DescribeScope, .skip); } - pub fn todoIf(globalThis: *JSGlobalObject, callframe: *CallFrame) JSValue { + pub fn todoIf(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { return createIfScope(globalThis, callframe, "describe.todoIf()", "todoIf", DescribeScope, .todo); } @@ -1230,9 +1230,9 @@ pub const DescribeScope = struct { const ScopeStack = ObjectPool(std.ArrayListUnmanaged(*DescribeScope), null, true, 16); }; -pub fn wrapTestFunction(comptime name: []const u8, comptime func: anytype) DescribeScope.CallbackFn { +pub fn wrapTestFunction(comptime name: []const u8, comptime func: JSC.JSHostZigFunction) DescribeScope.CallbackFn { return struct { - pub fn wrapped(globalThis: *JSGlobalObject, callframe: *CallFrame) callconv(JSC.conv) JSValue { + pub fn wrapped(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue { if (Jest.runner == null) { globalThis.throw("Cannot use " ++ name ++ "() outside of the test runner. Run \"bun test\" to run tests.", .{}); return .zero; @@ -1862,7 +1862,10 @@ fn consumeArg( ) !void { const allocator = getAllocator(globalThis); if (should_write) { - const owned_slice = arg.toSliceOrNull(globalThis) orelse return error.Failed; + const owned_slice = arg.toSliceOrNull(globalThis) orelse { + bun.assert(globalThis.hasException()); + return error.JSError; + }; defer owned_slice.deinit(); array_list.appendSlice(allocator, owned_slice.slice()) catch bun.outOfMemory(); } else { @@ -1945,7 +1948,7 @@ pub const EachData = struct { strong: JSC.Strong, is_test: bool }; fn eachBind( globalThis: *JSGlobalObject, callframe: *CallFrame, -) JSValue { +) bun.JSError!JSValue { const signature = "eachBind"; const callee = callframe.callee(); const arguments = callframe.arguments(3); @@ -2053,7 +2056,7 @@ fn eachBind( "" else (description.toSlice(globalThis, allocator).cloneIfNeeded(allocator) catch unreachable).slice(); - const formattedLabel = formatLabel(globalThis, label, function_args, test_idx) catch return .zero; + const formattedLabel = try formatLabel(globalThis, label, function_args, test_idx); const tag = parent.tag; diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig index 9f8aa0bf24..cdc099333f 100644 --- a/src/bun.js/webcore.zig +++ b/src/bun.js/webcore.zig @@ -23,7 +23,7 @@ pub const Lifetime = enum { }; /// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-alert -fn alert(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +fn alert(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); var output = bun.Output.writer(); const has_message = arguments.len != 0; @@ -73,7 +73,7 @@ fn alert(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv( return .undefined; } -fn confirm(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +fn confirm(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); var output = bun.Output.writer(); const has_message = arguments.len != 0; @@ -211,7 +211,7 @@ pub const Prompt = struct { pub fn call( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(3).slice(); var state = std.heap.stackFallback(2048, bun.default_allocator); const allocator = state.get(); @@ -554,7 +554,7 @@ pub const Crypto = struct { _: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).slice(); if (arguments.len < 2) { @@ -604,7 +604,7 @@ pub const Crypto = struct { _: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len == 0) { globalThis.throwInvalidArguments("Expected typed array but got nothing", .{}); @@ -655,7 +655,7 @@ pub const Crypto = struct { _: *@This(), globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const str, var bytes = bun.String.createUninitialized(.latin1, 36); defer str.deref(); @@ -665,10 +665,11 @@ pub const Crypto = struct { return str.toJS(globalThis); } - pub export fn Bun__randomUUIDv7( - globalThis: *JSC.JSGlobalObject, - callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + comptime { + const Bun__randomUUIDv7 = JSC.toJSHostFunction(Bun__randomUUIDv7_); + @export(Bun__randomUUIDv7, .{ .name = "Bun__randomUUIDv7" }); + } + pub fn Bun__randomUUIDv7_(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(2).slice(); var encoding_value: JSC.JSValue = .undefined; @@ -770,8 +771,11 @@ pub const Crypto = struct { comptime { if (!JSC.is_bindgen) { - @export(alert, .{ .name = "WebCore__alert" }); - @export(Prompt.call, .{ .name = "WebCore__prompt" }); - @export(confirm, .{ .name = "WebCore__confirm" }); + const js_alert = JSC.toJSHostFunction(alert); + @export(js_alert, .{ .name = "WebCore__alert" }); + const js_prompt = JSC.toJSHostFunction(Prompt.call); + @export(js_prompt, .{ .name = "WebCore__prompt" }); + const js_confirm = JSC.toJSHostFunction(confirm); + @export(js_confirm, .{ .name = "WebCore__confirm" }); } } diff --git a/src/bun.js/webcore/ObjectURLRegistry.zig b/src/bun.js/webcore/ObjectURLRegistry.zig index 10a30fdab8..3c99ca3973 100644 --- a/src/bun.js/webcore/ObjectURLRegistry.zig +++ b/src/bun.js/webcore/ObjectURLRegistry.zig @@ -89,7 +89,11 @@ pub fn has(this: *ObjectURLRegistry, pathname: []const u8) bool { return this.map.contains(uuid); } -export fn Bun__createObjectURL(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +comptime { + const Bun__createObjectURL = JSC.toJSHostFunction(Bun__createObjectURL_); + @export(Bun__createObjectURL, .{ .name = "Bun__createObjectURL" }); +} +fn Bun__createObjectURL_(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); if (arguments.len < 1) { globalObject.throwNotEnoughArguments("createObjectURL", 1, arguments.len); @@ -105,7 +109,11 @@ export fn Bun__createObjectURL(globalObject: *JSC.JSGlobalObject, callframe: *JS return str.transferToJS(globalObject); } -export fn Bun__revokeObjectURL(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +comptime { + const Bun__revokeObjectURL = JSC.toJSHostFunction(Bun__revokeObjectURL_); + @export(Bun__revokeObjectURL, .{ .name = "Bun__revokeObjectURL" }); +} +fn Bun__revokeObjectURL_(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); if (arguments.len < 1) { globalObject.throwNotEnoughArguments("revokeObjectURL", 1, arguments.len); @@ -132,7 +140,11 @@ export fn Bun__revokeObjectURL(globalObject: *JSC.JSGlobalObject, callframe: *JS return JSC.JSValue.undefined; } -export fn jsFunctionResolveObjectURL(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +comptime { + const jsFunctionResolveObjectURL = JSC.toJSHostFunction(jsFunctionResolveObjectURL_); + @export(jsFunctionResolveObjectURL, .{ .name = "jsFunctionResolveObjectURL" }); +} +fn jsFunctionResolveObjectURL_(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1); // Errors are ignored. @@ -161,12 +173,6 @@ export fn jsFunctionResolveObjectURL(globalObject: *JSC.JSGlobalObject, callfram return blob orelse JSC.JSValue.undefined; } -comptime { - _ = &Bun__createObjectURL; - _ = &Bun__revokeObjectURL; - _ = &jsFunctionResolveObjectURL; -} - pub const specifier_len = "blob:".len + UUID.stringLength; pub fn isBlobURL(url: []const u8) bool { diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index 14f14f0a5f..72cb780498 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -364,7 +364,7 @@ pub const Blob = struct { }, }; - _onStructuredCloneSerialize(this, Writer, writer) catch return .zero; + try _onStructuredCloneSerialize(this, Writer, writer); } pub fn onStructuredCloneTransfer( @@ -520,7 +520,16 @@ pub const Blob = struct { var buffer_stream = std.io.fixedBufferStream(ptr[0..total_length]); const reader = buffer_stream.reader(); - return _onStructuredCloneDeserialize(globalThis, @TypeOf(reader), reader) catch return .zero; + return _onStructuredCloneDeserialize(globalThis, @TypeOf(reader), reader) catch |err| switch (err) { + error.EndOfStream, error.TooSmall, error.InvalidValue => { + globalThis.throw("Blob.onStructuredCloneDeserialize failed", .{}); + return .zero; + }, + error.OutOfMemory => { + globalThis.throwOutOfMemory(); + return .zero; + }, + }; } const URLSearchParamsConverter = struct { @@ -946,8 +955,7 @@ pub const Blob = struct { } else if (destination_type == .bytes and source_type == .file) { var fake_call_frame: [8]JSC.JSValue = undefined; @memset(@as([*]u8, @ptrCast(&fake_call_frame))[0..@sizeOf(@TypeOf(fake_call_frame))], 0); - const blob_value = - source_blob.getSlice(ctx, @as(*JSC.CallFrame, @ptrCast(&fake_call_frame))); + const blob_value = source_blob.getSlice(ctx, @as(*JSC.CallFrame, @ptrCast(&fake_call_frame))) catch .zero; // TODO: return JSPromise.resolvedPromiseValue( ctx.ptr(), @@ -961,13 +969,13 @@ pub const Blob = struct { pub fn writeFile( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(3).slice(); var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments); defer args.deinit(); // accept a path or a blob - var path_or_blob = (PathOrBlob.fromJSNoCopy(globalThis, &args) catch return .zero) orelse { + var path_or_blob = (try PathOrBlob.fromJSNoCopy(globalThis, &args)) orelse { globalThis.throwInvalidArguments("Bun.write expects a path, file descriptor or a blob", .{}); return .zero; }; @@ -1552,13 +1560,13 @@ pub const Blob = struct { pub fn constructBunFile( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var vm = globalObject.bunVM(); const arguments = callframe.arguments(2).slice(); var args = JSC.Node.ArgumentsSlice.init(vm, arguments); defer args.deinit(); - var path = (JSC.Node.PathOrFileDescriptor.fromJS(globalObject, &args, bun.default_allocator) catch return .zero) orelse { + var path = (try JSC.Node.PathOrFileDescriptor.fromJS(globalObject, &args, bun.default_allocator)) orelse { globalObject.throwInvalidArguments("Expected file path string or file descriptor", .{}); return .zero; }; @@ -3246,7 +3254,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const thisValue = callframe.this(); if (Blob.streamGetCached(thisValue)) |cached| { return cached; @@ -3289,7 +3297,7 @@ pub const Blob = struct { pub fn toStreamWithOffset( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const this = callframe.this().as(Blob) orelse @panic("this is not a Blob"); const args = callframe.arguments(1).slice(); @@ -3313,7 +3321,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return this.getTextClone(globalThis); } @@ -3341,7 +3349,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return this.getJSONShare(globalThis); } @@ -3379,7 +3387,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return this.getArrayBufferClone(globalThis); } @@ -3397,7 +3405,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return this.getBytesClone(globalThis); } @@ -3415,7 +3423,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const store = this.store; if (store) |st| st.ref(); defer if (store) |st| st.deref(); @@ -3447,7 +3455,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { return JSC.JSPromise.resolvedPromiseValue(globalThis, this.getExistsSync()); } @@ -3455,7 +3463,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var arguments_ = callframe.arguments(1); var arguments = arguments_.ptr[0..arguments_.len]; @@ -3594,7 +3602,7 @@ pub const Blob = struct { this: *Blob, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const allocator = bun.default_allocator; var arguments_ = callframe.arguments(3); var args = arguments_.ptr[0..arguments_.len]; diff --git a/src/bun.js/webcore/body.zig b/src/bun.js/webcore/body.zig index 3948775827..9ce2b8ac64 100644 --- a/src/bun.js/webcore/body.zig +++ b/src/bun.js/webcore/body.zig @@ -1108,7 +1108,7 @@ pub fn BodyMixin(comptime Type: type) type { this: *Type, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var value: *Body.Value = this.getBodyValue(); if (value.* == .Used) { return handleBodyAlreadyUsed(globalObject); @@ -1174,7 +1174,7 @@ pub fn BodyMixin(comptime Type: type) type { this: *Type, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var value: *Body.Value = this.getBodyValue(); if (value.* == .Used) { return handleBodyAlreadyUsed(globalObject); @@ -1204,7 +1204,7 @@ pub fn BodyMixin(comptime Type: type) type { this: *Type, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var value: *Body.Value = this.getBodyValue(); if (value.* == .Used) { @@ -1232,7 +1232,7 @@ pub fn BodyMixin(comptime Type: type) type { this: *Type, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var value: *Body.Value = this.getBodyValue(); if (value.* == .Used) { @@ -1258,7 +1258,7 @@ pub fn BodyMixin(comptime Type: type) type { this: *Type, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var value: *Body.Value = this.getBodyValue(); if (value.* == .Used) { @@ -1308,7 +1308,7 @@ pub fn BodyMixin(comptime Type: type) type { this: *Type, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { return getBlobWithThisValue(this, globalObject, callframe.this()); } @@ -1510,14 +1510,14 @@ pub const BodyValueBufferer = struct { } } - pub fn onResolveStream(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn onResolveStream(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var args = callframe.arguments(2); var sink: *@This() = args.ptr[args.len - 1].asPromisePtr(@This()); sink.handleResolveStream(true); return JSValue.jsUndefined(); } - pub fn onRejectStream(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn onRejectStream(_: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args = callframe.arguments(2); var sink = args.ptr[args.len - 1].asPromisePtr(@This()); const err = args.ptr[0]; @@ -1706,12 +1706,10 @@ pub const BodyValueBufferer = struct { comptime { if (!JSC.is_bindgen) { - @export(onResolveStream, .{ - .name = Export[0].symbol_name, - }); - @export(onRejectStream, .{ - .name = Export[1].symbol_name, - }); + const jsonResolveStream = JSC.toJSHostFunction(onResolveStream); + @export(jsonResolveStream, .{ .name = Export[0].symbol_name }); + const jsonRejectStream = JSC.toJSHostFunction(onRejectStream); + @export(jsonRejectStream, .{ .name = Export[1].symbol_name }); } } }; diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig index 4940fa6caf..161e46275c 100644 --- a/src/bun.js/webcore/encoding.zig +++ b/src/bun.js/webcore/encoding.zig @@ -420,7 +420,7 @@ pub const TextEncoderStreamEncoder = struct { return TextEncoderStreamEncoder.new(.{}); } - pub fn encode(this: *TextEncoderStreamEncoder, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) JSValue { + pub fn encode(this: *TextEncoderStreamEncoder, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSValue { const arguments = callFrame.arguments(1).slice(); if (arguments.len == 0) { globalObject.throwNotEnoughArguments("TextEncoderStreamEncoder.encode", 1, arguments.len); @@ -590,7 +590,7 @@ pub const TextEncoderStreamEncoder = struct { } } - pub fn flush(this: *TextEncoderStreamEncoder, globalObject: *JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn flush(this: *TextEncoderStreamEncoder, globalObject: *JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { return flushBody(this, globalObject); } @@ -760,7 +760,7 @@ pub const TextDecoder = struct { return .{ output, saw_error }; } - pub fn decode(this: *TextDecoder, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn decode(this: *TextDecoder, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const arguments = callframe.arguments(2).slice(); const input_slice = input_slice: { diff --git a/src/bun.js/webcore/request.zig b/src/bun.js/webcore/request.zig index bbd04d239b..1bf5c3c7c2 100644 --- a/src/bun.js/webcore/request.zig +++ b/src/bun.js/webcore/request.zig @@ -802,7 +802,7 @@ pub const Request = struct { this: *Request, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const this_value = callframe.this(); var cloned = this.clone(getAllocator(globalThis), globalThis); diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index 371ed5ebdd..0e0f6ddf27 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -260,7 +260,7 @@ pub const Response = struct { this: *Response, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const this_value = callframe.this(); const cloned = this.clone(globalThis); if (globalThis.hasException()) { @@ -363,7 +363,7 @@ pub const Response = struct { pub fn constructJSON( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const args_list = callframe.arguments(2); // https://github.com/remix-run/remix/blob/db2c31f64affb2095e4286b91306b96435967969/packages/remix-server-runtime/responses.ts#L4 var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), args_list.ptr[0..args_list.len]); @@ -431,7 +431,7 @@ pub const Response = struct { pub fn constructRedirect( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { var args_list = callframe.arguments(4); // https://github.com/remix-run/remix/blob/db2c31f64affb2095e4286b91306b96435967969/packages/remix-server-runtime/responses.ts#L4 var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), args_list.ptr[0..args_list.len]); @@ -493,7 +493,7 @@ pub const Response = struct { pub fn constructError( globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const response = bun.new( Response, Response{ @@ -798,13 +798,6 @@ pub const Fetch = struct { break :brk errors; }; - comptime { - if (!JSC.is_bindgen) { - _ = Bun__fetch; - _ = Bun__fetchPreconnect; - } - } - pub const FetchTasklet = struct { const log = Output.scoped(.FetchTasklet, false); @@ -1926,10 +1919,14 @@ pub const Fetch = struct { return JSPromise.resolvedPromiseValue(globalThis, response.toJS(globalThis)); } - pub export fn Bun__fetchPreconnect( + comptime { + const Bun__fetchPreconnect = JSC.toJSHostFunction(Bun__fetchPreconnect_); + @export(Bun__fetchPreconnect, .{ .name = "Bun__fetchPreconnect" }); + } + pub fn Bun__fetchPreconnect_( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len < 1) { @@ -1991,10 +1988,14 @@ pub const Fetch = struct { } }; - pub export fn Bun__fetch( + comptime { + const Bun__fetch = JSC.toJSHostFunction(Bun__fetch_); + @export(Bun__fetch, .{ .name = "Bun__fetch" }); + } + pub fn Bun__fetch_( ctx: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + ) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const globalThis = ctx.ptr(); const arguments = callframe.arguments(2); diff --git a/src/bun.js/webcore/streams.zig b/src/bun.js/webcore/streams.zig index abd5fa5348..9f14fd3ed1 100644 --- a/src/bun.js/webcore/streams.zig +++ b/src/bun.js/webcore/streams.zig @@ -1673,7 +1673,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { return shim.cppFn("setDestroyCallback", .{ value, callback }); } - pub fn construct(globalThis: *JSGlobalObject, _: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn construct(globalThis: *JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); if (comptime !@hasDecl(SinkType, "construct")) { @@ -1745,7 +1745,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { } - pub fn write(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn write(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var this = getThis(globalThis, callframe) orelse return invalidThis(globalThis); @@ -1814,7 +1814,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { return this.sink.writeLatin1(.{ .temporary = bun.ByteList.initConst(str.slice()) }).toJS(globalThis); } - pub fn writeUTF8(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn writeUTF8(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var this = getThis(globalThis, callframe) orelse return invalidThis(globalThis); @@ -1867,7 +1867,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { return this.sink.end(null).toJS(globalThis); } - pub fn flush(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn flush(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var this = getThis(globalThis, callframe) orelse return invalidThis(globalThis); @@ -1902,7 +1902,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { return this.sink.flush().toJS(globalThis); } - pub fn start(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn start(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var this = getThis(globalThis, callframe) orelse return invalidThis(globalThis); @@ -1935,7 +1935,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { ).toJS(globalThis); } - pub fn end(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn end(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); var this = getThis(globalThis, callframe) orelse return invalidThis(globalThis); @@ -1984,14 +1984,20 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { this.sink.updateRef(value); } + const jsWrite = JSC.toJSHostFunction(write); + const jsFlush = JSC.toJSHostFunction(flush); + const jsStart = JSC.toJSHostFunction(start); + const jsEnd = JSC.toJSHostFunction(end); + const jsConstruct = JSC.toJSHostFunction(construct); + comptime { @export(finalize, .{ .name = shim.symbolName("finalize") }); - @export(write, .{ .name = shim.symbolName("write") }); + @export(jsWrite, .{ .name = shim.symbolName("write") }); @export(close, .{ .name = shim.symbolName("close") }); - @export(flush, .{ .name = shim.symbolName("flush") }); - @export(start, .{ .name = shim.symbolName("start") }); - @export(end, .{ .name = shim.symbolName("end") }); - @export(construct, .{ .name = shim.symbolName("construct") }); + @export(jsFlush, .{ .name = shim.symbolName("flush") }); + @export(jsStart, .{ .name = shim.symbolName("start") }); + @export(jsEnd, .{ .name = shim.symbolName("end") }); + @export(jsConstruct, .{ .name = shim.symbolName("construct") }); @export(endWithSink, .{ .name = shim.symbolName("endWithSink") }); @export(updateRef, .{ .name = shim.symbolName("updateRef") }); @@ -2015,7 +2021,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { // socket: Socket, -// pub fn connect(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { +// pub fn connect(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { // JSC.markBinding(@src()); // var this = @ptrCast(*ThisSocket, @alignCast( fromJS(globalThis, callframe.this()) orelse { @@ -2783,7 +2789,7 @@ pub fn ReadableStreamSource( return ReadableStream.fromNative(globalThis, out_value); } - pub fn setRawModeFromJS(this: *ReadableStreamSourceType, global: *JSC.JSGlobalObject, call_frame: *JSC.CallFrame) JSValue { + pub fn setRawModeFromJS(this: *ReadableStreamSourceType, global: *JSC.JSGlobalObject, call_frame: *JSC.CallFrame) bun.JSError!JSValue { if (@hasDecl(Context, "setRawMode")) { const flag = call_frame.argument(0); if (Environment.allow_assert) { @@ -2825,7 +2831,7 @@ pub fn ReadableStreamSource( return null; } - pub fn pull(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn pull(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const this_jsvalue = callFrame.this(); const arguments = callFrame.arguments(2); @@ -2841,7 +2847,7 @@ pub fn ReadableStreamSource( ); } - pub fn start(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn start(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); this.globalThis = globalThis; this.this_jsvalue = callFrame.this(); @@ -2890,7 +2896,7 @@ pub fn ReadableStreamSource( } } - pub fn cancel(this: *ReadableStreamSourceType, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn cancel(this: *ReadableStreamSourceType, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = globalObject; // autofix JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); @@ -2955,7 +2961,7 @@ pub fn ReadableStreamSource( return .undefined; } - pub fn updateRef(this: *ReadableStreamSourceType, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn updateRef(this: *ReadableStreamSourceType, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { _ = globalObject; // autofix JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); @@ -2981,7 +2987,7 @@ pub fn ReadableStreamSource( _ = this.decrementCount(); } - pub fn drain(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn drain(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); var list = this.drain(); @@ -2991,7 +2997,7 @@ pub fn ReadableStreamSource( return JSValue.jsUndefined(); } - pub fn text(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn text(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); @@ -3003,7 +3009,7 @@ pub fn ReadableStreamSource( return .zero; } - pub fn arrayBuffer(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn arrayBuffer(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); @@ -3015,7 +3021,7 @@ pub fn ReadableStreamSource( return .zero; } - pub fn blob(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn blob(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); @@ -3027,7 +3033,7 @@ pub fn ReadableStreamSource( return .zero; } - pub fn bytes(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn bytes(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); @@ -3039,7 +3045,7 @@ pub fn ReadableStreamSource( return .zero; } - pub fn json(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSC.JSValue { + pub fn json(this: *ReadableStreamSourceType, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); this.this_jsvalue = callFrame.this(); diff --git a/src/bun.zig b/src/bun.zig index bc3dea7612..0b19824aec 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -91,20 +91,11 @@ pub inline fn namedAllocator(comptime name: [:0]const u8) std.mem.Allocator { pub const OOM = std.mem.Allocator.Error; pub const JSError = error{ - /// There is an active exception on the global object. Options: - /// - /// - Bubble up `error.JSError` to the caller. - /// - Call `global.takeException(err)` to branch on the JSValue of the exception. - /// - Call `global.reportActiveExceptionAsUnhandled(err)` to mark it unhandled. - /// - /// Calling into JS bindings while an active error exists is will crash. - /// Ignoring the exception will also cause a crash. - /// - /// Prefer `global.jsErrorFromCPP()` over constructing this error manually, - /// that way the exception is asserted to actually exist. - /// - /// To pass this to C++ code as JSC::JSValue, use `global.errorUnionToCPP(...)` + /// There is an active exception on the global object. + /// You should almost never have to construct this manually. JSError, + // XXX: This is temporary! meghan will remove this soon + OutOfMemory, }; pub const JSOOM = OOM || JSError; diff --git a/src/bun_js.zig b/src/bun_js.zig index fb97c269cb..ef0cebd4f5 100644 --- a/src/bun_js.zig +++ b/src/bun_js.zig @@ -415,7 +415,7 @@ pub const Run = struct { if (result.asAnyPromise()) |promise| { switch (promise.status(vm.jsc)) { .pending => { - result._then(vm.global, .undefined, Bun__onResolveEntryPointResult, Bun__onRejectEntryPointResult); + result._then2(vm.global, .undefined, Bun__onResolveEntryPointResult, Bun__onRejectEntryPointResult); vm.tick(); vm.eventLoop().autoTickActive(); diff --git a/src/cli/pack_command.zig b/src/cli/pack_command.zig index 7645912fef..02d43702a1 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -2297,7 +2297,7 @@ pub const bindings = struct { // return obj; // } - pub fn jsReadTarball(global: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn jsReadTarball(global: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(1).slice(); if (args.len < 1 or !args[0].isString()) { global.throw("expected tarball path string argument", .{}); diff --git a/src/cli/upgrade_command.zig b/src/cli/upgrade_command.zig index 8b337f15f9..f399e12607 100644 --- a/src/cli/upgrade_command.zig +++ b/src/cli/upgrade_command.zig @@ -988,7 +988,7 @@ pub const upgrade_js_bindings = struct { /// For testing upgrades when the temp directory has an open handle without FILE_SHARE_DELETE. /// Windows only - pub fn jsOpenTempDirWithoutSharingDelete(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSC.JSValue { + pub fn jsOpenTempDirWithoutSharingDelete(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!bun.JSC.JSValue { if (comptime !Environment.isWindows) return .undefined; const w = std.os.windows; @@ -1042,7 +1042,7 @@ pub const upgrade_js_bindings = struct { return .undefined; } - pub fn jsCloseTempDirHandle(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn jsCloseTempDirHandle(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { if (comptime !Environment.isWindows) return .undefined; if (tempdir_fd) |fd| { diff --git a/src/codegen/generate-classes.ts b/src/codegen/generate-classes.ts index 44e008e8de..50281b1440 100644 --- a/src/codegen/generate-classes.ts +++ b/src/codegen/generate-classes.ts @@ -294,7 +294,7 @@ function propRow( return `{ "${name}"_s, static_cast(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute${extraPropertyAttributes}), NoIntrinsic, { HashTableValue::GetterSetterType, ${getter}, 0 } } `.trim(); } else if (getter && !supportsObjectCreate && writable) { - return `{ "${name}"_s, static_cast(JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute${extraPropertyAttributes}), NoIntrinsic, { HashTableValue::GetterSetterType, ${getter}, ${setter} } } + return `{ "${name}"_s, static_cast(JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute${extraPropertyAttributes}), NoIntrinsic, { HashTableValue::GetterSetterType, ${getter}, ${setter} } } `.trim(); } else if (getter && supportsObjectCreate) { setter = getter.replace("Get", "Set"); @@ -356,14 +356,14 @@ function generatePrototype(typeName, obj) { if (obj.construct) { externs += ` -extern JSC_CALLCONV void* JSC_HOST_CALL_ATTRIBUTES ${classSymbolName(typeName, "construct")}(JSC::JSGlobalObject*, JSC::CallFrame*); +extern JSC_CALLCONV void* JSC_HOST_CALL_ATTRIBUTES ${classSymbolName(typeName, "construct")}(JSC::JSGlobalObject*, JSC::CallFrame*); JSC_DECLARE_CUSTOM_GETTER(js${typeName}Constructor); `; } if (obj.wantsThis) { externs += ` -extern JSC_CALLCONV void* JSC_HOST_CALL_ATTRIBUTES ${classSymbolName(typeName, "_setThis")}(JSC::JSGlobalObject*, void*, JSC::EncodedJSValue); +extern JSC_CALLCONV void* JSC_HOST_CALL_ATTRIBUTES ${classSymbolName(typeName, "_setThis")}(JSC::JSGlobalObject*, void*, JSC::EncodedJSValue); `; } @@ -809,11 +809,11 @@ function renderCallbacksZig(typeName, callbacks: Record) { out += "\n};\n"; out += ` - + pub fn callbacks(_: *const ${typeName}, instance: JSC.JSValue) Callbacks { return .{.instance = instance }; } - + `; return "\n" + out; @@ -1027,10 +1027,10 @@ JSC_DEFINE_CUSTOM_GETTER(${symbolName(typeName, name)}GetterWrap, (JSGlobalObjec } JSC::EnsureStillAliveScope thisArg = JSC::EnsureStillAliveScope(thisObject); - + if (JSValue cachedValue = thisObject->${cacheName}.get()) return JSValue::encode(cachedValue); - + JSC::JSValue result = JSC::JSValue::decode( ${symbolName(typeName, proto[name].getter)}(thisObject->wrapped(),${ proto[name].this!! ? " thisValue, " : "" @@ -1523,7 +1523,7 @@ extern JSC_CALLCONV void* JSC_HOST_CALL_ATTRIBUTES ${typeName}__fromJSDirect(JSC Zig::GlobalObject* globalObject = jsDynamicCast(object->globalObject()); - if (UNLIKELY(globalObject == nullptr || cell->structureID() != globalObject->${className(typeName)}Structure()->id())) { + if (UNLIKELY(globalObject == nullptr || cell->structureID() != globalObject->${className(typeName)}Structure()->id())) { return nullptr; } @@ -1691,8 +1691,8 @@ function generateZig( function renderMethods() { const exports = new Map(); var output = ` -const JavaScriptCoreBindings = struct { - +const JavaScriptCoreBindings = struct { + `; if (estimatedSize) { @@ -1748,7 +1748,10 @@ 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}({})", .{callFrame}); - return @call(.always_inline, wrapHostFunction(${typeName}.call), .{globalObject, callFrame}); + return @call(.always_inline, ${typeName}.call, .{globalObject, callFrame}) catch |err| switch (err) { + error.JSError => .zero, + error.OutOfMemory => globalObject.throwOutOfMemoryValue(), + }; } `; } @@ -1801,7 +1804,10 @@ 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("${typeName}.${name}({})", .{callFrame}); - return @call(.always_inline, ${proto[name].passThis ? 'wrapMethodWithThis' : 'wrapMethod'}(${typeName}, ${typeName}.${fn}), .{thisValue, globalObject, callFrame${proto[name].passThis ? ", js_this_value" : ""}}); + 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(), + }; } `; } @@ -1848,7 +1854,10 @@ 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}); + return @call(.always_inline, ${typeName}.${fn}, .{globalObject, callFrame}) catch |err| switch (err) { + error.JSError => .zero, + error.OutOfMemory => globalObject.throwOutOfMemoryValue(), + }; } `; } diff --git a/src/codegen/generate-js2native.ts b/src/codegen/generate-js2native.ts index f8b1a4c6ee..5db18419df 100644 --- a/src/codegen/generate-js2native.ts +++ b/src/codegen/generate-js2native.ts @@ -205,28 +205,19 @@ export function getJS2NativeZig(gs2NativeZigPath: string) { ...wrapperCalls .filter(x => x.type === "zig") .flatMap(x => [ - `const ${symbol({ + `export fn ${symbol({ type: "zig", symbol: x.symbol_target, filename: x.filename, - })} = JSC.toJSHostFunction(@import(${JSON.stringify(path.relative(path.dirname(gs2NativeZigPath), x.filename))}).${x.symbol_target});`, + })}(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(), + };`, + "}", ]), - "comptime {", - ...wrapperCalls - .filter(x => x.type === "zig") - .flatMap(x => { - const s = symbol({ - type: "zig", - symbol: x.symbol_target, - filename: x.filename, - }); - return ` @export(${s}, .{ .name = "${s}" });`; - }), - ...nativeCalls.filter(x => x.type === "zig").flatMap(call => ` _ = &${symbol(call)}_workaround;`), - ...wrapperCalls - .filter(x => x.type === "zig") - .flatMap(x => ` _ = &${symbol({ type: "zig", symbol: x.symbol_target, filename: x.filename })};`), - "}", ].join("\n"); } diff --git a/src/crash_handler.zig b/src/crash_handler.zig index f73ce4e32a..d9cb08f989 100644 --- a/src/crash_handler.zig +++ b/src/crash_handler.zig @@ -1679,7 +1679,7 @@ pub const js_bindings = struct { return obj; } - pub fn jsGetMachOImageZeroOffset(_: *bun.JSC.JSGlobalObject, _: *bun.JSC.CallFrame) JSValue { + pub fn jsGetMachOImageZeroOffset(_: *bun.JSC.JSGlobalObject, _: *bun.JSC.CallFrame) bun.JSError!JSValue { if (!bun.Environment.isMac) return .undefined; const header = std.c._dyld_get_image_header(0) orelse return .undefined; @@ -1689,7 +1689,7 @@ pub const js_bindings = struct { return JSValue.jsNumber(base_address - vmaddr_slide); } - pub fn jsSegfault(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn jsSegfault(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { @setRuntimeSafety(false); const ptr: [*]align(1) u64 = @ptrFromInt(0xDEADBEEF); ptr[0] = 0xDEADBEEF; @@ -1697,23 +1697,23 @@ pub const js_bindings = struct { return .undefined; } - pub fn jsPanic(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn jsPanic(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { bun.crash_handler.panicImpl("invoked crashByPanic() handler", null, null); } - pub fn jsRootError(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn jsRootError(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { bun.crash_handler.handleRootError(error.Test, null); } - pub fn jsOutOfMemory(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn jsOutOfMemory(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { bun.outOfMemory(); } - pub fn jsRaiseIgnoringPanicHandler(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn jsRaiseIgnoringPanicHandler(_: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { bun.Global.raiseIgnoringPanicHandler(.SIGSEGV); } - pub fn jsGetFeaturesAsVLQ(global: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn jsGetFeaturesAsVLQ(global: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { const bits = bun.Analytics.packedFeatures(); var buf = std.BoundedArray(u8, 16){}; writeU64AsTwoVLQs(buf.writer(), @bitCast(bits)) catch { @@ -1724,7 +1724,7 @@ pub const js_bindings = struct { return str.transferToJS(global); } - pub fn jsGetFeatureData(global: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn jsGetFeatureData(global: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { const obj = JSValue.createEmptyObject(global, 5); const list = bun.Analytics.packed_features_list; const array = JSValue.createEmptyArray(global, list.len); diff --git a/src/css/css_internals.zig b/src/css/css_internals.zig index be61eb545e..578a6dfa99 100644 --- a/src/css/css_internals.zig +++ b/src/css/css_internals.zig @@ -17,15 +17,15 @@ const TestKind = enum { prefix, }; -pub fn minifyTestWithOptions(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn minifyTestWithOptions(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { return testingImpl(globalThis, callframe, .minify); } -pub fn prefixTestWithOptions(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn prefixTestWithOptions(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { return testingImpl(globalThis, callframe, .prefix); } -pub fn testWithOptions(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn testWithOptions(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { return testingImpl(globalThis, callframe, .normal); } @@ -195,7 +195,7 @@ fn targetsFromJS(globalThis: *JSC.JSGlobalObject, jsobj: JSValue) bun.css.target return targets; } -pub fn attrTest(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn attrTest(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var arena = arena_ orelse brk: { break :brk Arena.init() catch @panic("oopsie arena no good"); }; diff --git a/src/css/values/color_js.zig b/src/css/values/color_js.zig index 557c157b89..c0b7ffd739 100644 --- a/src/css/values/color_js.zig +++ b/src/css/values/color_js.zig @@ -147,7 +147,7 @@ pub const Ansi256 = struct { } }; -pub fn jsFunctionColor(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { +pub fn jsFunctionColor(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args = callFrame.argumentsAsArray(2); if (args[0].isUndefined()) { globalThis.throwInvalidArgumentType("color", "input", "string, number, or object"); @@ -169,7 +169,7 @@ pub fn jsFunctionColor(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFram return JSC.JSValue.jsUndefined(); } - break :brk args[1].toEnum(globalThis, "format", OutputColorFormat) catch return .zero; + break :brk try args[1].toEnum(globalThis, "format", OutputColorFormat); } break :brk OutputColorFormat.css; diff --git a/src/deps/c_ares.zig b/src/deps/c_ares.zig index b51dbbd6ac..1ada5d45e6 100644 --- a/src/deps/c_ares.zig +++ b/src/deps/c_ares.zig @@ -1554,10 +1554,14 @@ pub const ares_uri_reply = struct_ares_uri_reply; pub const ares_addr_node = struct_ares_addr_node; pub const ares_addr_port_node = struct_ares_addr_port_node; -pub export fn Bun__canonicalizeIP( +comptime { + const Bun__canonicalizeIP = JSC.toJSHostFunction(Bun__canonicalizeIP_); + @export(Bun__canonicalizeIP, .{ .name = "Bun__canonicalizeIP" }); +} +pub fn Bun__canonicalizeIP_( ctx: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, -) callconv(JSC.conv) JSC.JSValue { +) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const globalThis = ctx.ptr(); @@ -1655,9 +1659,4 @@ pub fn getSockaddr(addr: []const u8, port: u16, sa: *std.posix.sockaddr) c_int { return -1; } -comptime { - if (!JSC.is_bindgen) { - _ = Bun__canonicalizeIP; - } -} const GetAddrInfo = bun.dns.GetAddrInfo; diff --git a/src/deps/uws.zig b/src/deps/uws.zig index 3953cae719..5e3256130f 100644 --- a/src/deps/uws.zig +++ b/src/deps/uws.zig @@ -219,7 +219,7 @@ pub const UpgradedDuplex = struct { fn onReceivedData( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { log("onReceivedData", .{}); const function = callframe.callee(); @@ -269,7 +269,7 @@ pub const UpgradedDuplex = struct { fn onWritable( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { log("onWritable", .{}); _ = globalObject; @@ -291,7 +291,7 @@ pub const UpgradedDuplex = struct { fn onCloseJS( globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { log("onCloseJS", .{}); _ = globalObject; diff --git a/src/fmt.zig b/src/fmt.zig index 309a278c8a..2e25ab767b 100644 --- a/src/fmt.zig +++ b/src/fmt.zig @@ -1714,7 +1714,7 @@ pub const fmt_js_test_bindings = struct { }; /// Internal function for testing in highlighter.test.ts - pub fn jsFunctionStringFormatter(globalThis: *bun.JSC.JSGlobalObject, callframe: *bun.JSC.CallFrame) callconv(bun.JSC.conv) bun.JSC.JSValue { + pub fn jsFunctionStringFormatter(globalThis: *bun.JSC.JSGlobalObject, callframe: *bun.JSC.CallFrame) bun.JSError!bun.JSC.JSValue { const args = callframe.arguments(2); if (args.len < 2) { globalThis.throwNotEnoughArguments("code", 1, 0); diff --git a/src/ini.zig b/src/ini.zig index d4b014204b..1046aa6cf1 100644 --- a/src/ini.zig +++ b/src/ini.zig @@ -512,7 +512,7 @@ pub const IniTestingAPIs = struct { pub fn loadNpmrcFromJS( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) !JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arg = callframe.argument(0); const npmrc_contents = arg.toBunString(globalThis); defer npmrc_contents.deref(); @@ -599,7 +599,7 @@ pub const IniTestingAPIs = struct { }).toJS(); } - pub fn parse(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) !JSC.JSValue { + pub fn parse(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(1); const arguments = arguments_.slice(); diff --git a/src/install/dependency.zig b/src/install/dependency.zig index 6a1bd3a961..0c33bdad0a 100644 --- a/src/install/dependency.zig +++ b/src/install/dependency.zig @@ -763,7 +763,7 @@ pub const Version = struct { return .npm; } - pub fn inferFromJS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn inferFromJS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(1).slice(); if (arguments.len == 0 or !arguments[0].isString()) { return .undefined; @@ -1234,7 +1234,7 @@ pub fn parseWithTag( } } -pub fn fromJS(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { +pub fn fromJS(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(2).slice(); if (arguments.len == 1) { return bun.install.PackageManager.UpdateRequest.fromJS(globalThis, arguments[0]); diff --git a/src/install/install.zig b/src/install/install.zig index 846db8281b..3730522fb4 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -9704,7 +9704,7 @@ pub const PackageManager = struct { const outro_text = \\Examples: \\ Display files that would be published, without publishing to the registry. - \\ bun publish --dry-run + \\ bun publish --dry-run \\ \\ Publish the current package with public access. \\ bun publish --access public @@ -14910,7 +14910,7 @@ pub const bun_install_js_bindings = struct { return obj; } - pub fn jsParseLockfile(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) JSValue { + pub fn jsParseLockfile(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSValue { const allocator = bun.default_allocator; var log = logger.Log.init(allocator); defer log.deinit(); diff --git a/src/install/npm.zig b/src/install/npm.zig index 69f5bf6f02..51ff0e86ea 100644 --- a/src/install/npm.zig +++ b/src/install/npm.zig @@ -652,7 +652,7 @@ pub const OperatingSystem = enum(u16) { } const JSC = bun.JSC; - pub fn jsFunctionOperatingSystemIsMatch(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn jsFunctionOperatingSystemIsMatch(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args = callframe.arguments(1); var operating_system = negatable(.none); var iter = args.ptr[0].arrayIterator(globalObject); @@ -694,7 +694,7 @@ pub const Libc = enum(u8) { pub const current: Libc = @intFromEnum(glibc); const JSC = bun.JSC; - pub fn jsFunctionLibcIsMatch(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn jsFunctionLibcIsMatch(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args = callframe.arguments(1); var libc = negatable(.none); var iter = args.ptr[0].arrayIterator(globalObject); @@ -769,7 +769,7 @@ pub const Architecture = enum(u16) { } const JSC = bun.JSC; - pub fn jsFunctionArchitectureIsMatch(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn jsFunctionArchitectureIsMatch(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args = callframe.arguments(1); var architecture = negatable(.none); var iter = args.ptr[0].arrayIterator(globalObject); @@ -1283,7 +1283,7 @@ pub const PackageManifest = struct { return obj; } - pub fn jsParseManifest(global: *JSGlobalObject, callFrame: *CallFrame) JSValue { + pub fn jsParseManifest(global: *JSGlobalObject, callFrame: *CallFrame) bun.JSError!JSValue { const args = callFrame.arguments(2).slice(); if (args.len < 2 or !args[0].isString() or !args[1].isString()) { global.throw("expected manifest filename and registry string arguments", .{}); diff --git a/src/install/semver.zig b/src/install/semver.zig index ef93a68be5..bac7bde18d 100644 --- a/src/install/semver.zig +++ b/src/install/semver.zig @@ -2651,7 +2651,7 @@ pub const SemverObject = struct { pub fn order( globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var arena = std.heap.ArenaAllocator.init(bun.default_allocator); defer arena.deinit(); var stack_fallback = std.heap.stackFallback(512, arena.allocator()); @@ -2703,7 +2703,7 @@ pub const SemverObject = struct { pub fn satisfies( globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { var arena = std.heap.ArenaAllocator.init(bun.default_allocator); defer arena.deinit(); var stack_fallback = std.heap.stackFallback(512, arena.allocator()); diff --git a/src/patch.zig b/src/patch.zig index be170ab084..718629f8e5 100644 --- a/src/patch.zig +++ b/src/patch.zig @@ -1094,7 +1094,7 @@ const PatchLinesParser = struct { }; pub const TestingAPIs = struct { - pub fn makeDiff(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn makeDiff(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); @@ -1146,7 +1146,7 @@ pub const TestingAPIs = struct { } } }; - pub fn apply(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn apply(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var args = switch (parseApplyArgs(globalThis, callframe)) { .err => |e| return e, .result => |a| a, @@ -1161,7 +1161,7 @@ pub const TestingAPIs = struct { return .true; } /// Used in JS tests, see `internal-for-testing.ts` and patch tests. - pub fn parse(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn parse(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 59d20a9649..e9f13be668 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -3270,7 +3270,11 @@ pub const Resolver = struct { }; } - pub export fn Resolver__nodeModulePathsForJS(globalThis: *bun.JSC.JSGlobalObject, callframe: *bun.JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + comptime { + const Resolver__nodeModulePathsForJS = JSC.toJSHostFunction(Resolver__nodeModulePathsForJS_); + @export(Resolver__nodeModulePathsForJS, .{ .name = "Resolver__nodeModulePathsForJS" }); + } + pub fn Resolver__nodeModulePathsForJS_(globalThis: *bun.JSC.JSGlobalObject, callframe: *bun.JSC.CallFrame) bun.JSError!JSC.JSValue { bun.JSC.markBinding(@src()); const argument: bun.JSC.JSValue = callframe.argument(0); @@ -4194,7 +4198,6 @@ pub const GlobalCache = enum { comptime { if (!bun.JSC.is_bindgen) { - _ = Resolver.Resolver__nodeModulePathsForJS; _ = Resolver.Resolver__propForRequireMainPaths; } } diff --git a/src/shell/interpreter.zig b/src/shell/interpreter.zig index d3b7c64366..4ff5534010 100644 --- a/src/shell/interpreter.zig +++ b/src/shell/interpreter.zig @@ -703,7 +703,7 @@ pub const ParsedShellScript = struct { bun.destroy(this); } - pub fn setCwd(this: *ParsedShellScript, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn setCwd(this: *ParsedShellScript, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); const str_js = arguments.nextEat() orelse { @@ -715,13 +715,13 @@ pub const ParsedShellScript = struct { return .undefined; } - pub fn setQuiet(this: *ParsedShellScript, _: *JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn setQuiet(this: *ParsedShellScript, _: *JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { log("Interpreter(0x{x}) setQuiet()", .{@intFromPtr(this)}); this.quiet = true; return .undefined; } - pub fn setEnv(this: *ParsedShellScript, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn setEnv(this: *ParsedShellScript, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var env = if (this.export_env) |*env| brk: { @@ -768,7 +768,7 @@ pub const ParsedShellScript = struct { pub fn createParsedShellScript( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { var shargs = ShellArgs.init(); const arguments_ = callframe.arguments(2); @@ -1188,7 +1188,7 @@ pub const Interpreter = struct { pub fn createShellInterpreter( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSValue { + ) bun.JSError!JSValue { const allocator = bun.default_allocator; const arguments_ = callframe.arguments(3); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); @@ -1642,7 +1642,7 @@ pub const Interpreter = struct { return Maybe(void).success; } - pub fn runFromJS(this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn runFromJS(this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { _ = callframe; // autofix if (this.setupIOBeforeRun().asErr()) |e| { @@ -1786,13 +1786,13 @@ pub const Interpreter = struct { this.allocator.destroy(this); } - pub fn setQuiet(this: *ThisInterpreter, _: *JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue { + pub fn setQuiet(this: *ThisInterpreter, _: *JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSC.JSValue { log("Interpreter(0x{x}) setQuiet()", .{@intFromPtr(this)}); this.flags.quiet = true; return .undefined; } - pub fn setCwd(this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn setCwd(this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const value = callframe.argument(0); const str = bun.String.fromJS(value, globalThis); @@ -1808,7 +1808,7 @@ pub const Interpreter = struct { return .undefined; } - pub fn setEnv(this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn setEnv(this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const value1 = callframe.argument(0); if (!value1.isObject()) { globalThis.throwInvalidArguments("env must be an object", .{}); @@ -1849,7 +1849,7 @@ pub const Interpreter = struct { this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { _ = globalThis; // autofix _ = callframe; // autofix @@ -1860,7 +1860,7 @@ pub const Interpreter = struct { this: *ThisInterpreter, globalThis: *JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { _ = globalThis; // autofix _ = callframe; // autofix diff --git a/src/shell/shell.zig b/src/shell/shell.zig index f6b5049ba5..49d28db840 100644 --- a/src/shell/shell.zig +++ b/src/shell/shell.zig @@ -4313,7 +4313,7 @@ pub fn SmolList(comptime T: type, comptime INLINED_MAX: comptime_int) type { /// Used in JS tests, see `internal-for-testing.ts` and shell tests. pub const TestingAPIs = struct { - pub fn disabledOnThisPlatform(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue { + pub fn disabledOnThisPlatform(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (comptime bun.Environment.isWindows) return JSValue.false; const arguments_ = callframe.arguments(1); @@ -4339,7 +4339,7 @@ pub const TestingAPIs = struct { pub fn shellLex( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); const string_args = arguments.nextEat() orelse { @@ -4429,7 +4429,7 @@ pub const TestingAPIs = struct { pub fn shellParse( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) JSC.JSValue { + ) bun.JSError!JSC.JSValue { const arguments_ = callframe.arguments(2); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); const string_args = arguments.nextEat() orelse { diff --git a/src/sql/postgres.zig b/src/sql/postgres.zig index 330f4528d0..f79476c3e3 100644 --- a/src/sql/postgres.zig +++ b/src/sql/postgres.zig @@ -147,7 +147,7 @@ pub const PostgresSQLContext = struct { onQueryResolveFn: JSC.Strong = .{}, onQueryRejectFn: JSC.Strong = .{}, - pub fn init(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn init(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var ctx = &globalObject.bunVM().rareData().postgresql_context; ctx.onQueryResolveFn.set(globalObject, callframe.argument(0)); ctx.onQueryRejectFn.set(globalObject, callframe.argument(1)); @@ -157,9 +157,8 @@ pub const PostgresSQLContext = struct { comptime { if (!JSC.is_bindgen) { - @export(init, .{ - .name = "PostgresSQLContext__init", - }); + const js_init = JSC.toJSHostFunction(init); + @export(js_init, .{ .name = "PostgresSQLContext__init" }); } } }; @@ -416,7 +415,7 @@ pub const PostgresSQLQuery = struct { return @sizeOf(PostgresSQLQuery); } - pub fn call(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn call(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.arguments(4).slice(); const query = arguments[0]; const values = arguments[1]; @@ -467,13 +466,13 @@ pub const PostgresSQLQuery = struct { pending_value.push(globalThis, value); } - pub fn doDone(this: *@This(), globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn doDone(this: *@This(), globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { _ = globalObject; this.is_done = true; return .undefined; } - pub fn doRun(this: *PostgresSQLQuery, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn doRun(this: *PostgresSQLQuery, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { var arguments_ = callframe.arguments(2); const arguments = arguments_.slice(); var connection = arguments[0].as(PostgresSQLConnection) orelse { @@ -579,7 +578,7 @@ pub const PostgresSQLQuery = struct { return .undefined; } - pub fn doCancel(this: *PostgresSQLQuery, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn doCancel(this: *PostgresSQLQuery, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { _ = callframe; _ = globalObject; _ = this; @@ -589,7 +588,8 @@ pub const PostgresSQLQuery = struct { comptime { if (!JSC.is_bindgen) { - @export(call, .{ .name = "PostgresSQLQuery__createInstance" }); + const jscall = JSC.toJSHostFunction(call); + @export(jscall, .{ .name = "PostgresSQLQuery__createInstance" }); } } }; @@ -1221,11 +1221,12 @@ pub const PostgresSQLConnection = struct { comptime { if (!JSC.is_bindgen) { - @export(call, .{ .name = "PostgresSQLConnection__createInstance" }); + const jscall = JSC.toJSHostFunction(call); + @export(jscall, .{ .name = "PostgresSQLConnection__createInstance" }); } } - pub fn call(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(JSC.conv) JSValue { + pub fn call(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { var vm = globalObject.bunVM(); const arguments = callframe.arguments(9).slice(); const hostname_str = arguments[0].toBunString(globalObject); @@ -1379,13 +1380,13 @@ pub const PostgresSQLConnection = struct { this.ref_count += 1; } - pub fn doRef(this: *@This(), _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn doRef(this: *@This(), _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { this.poll_ref.ref(this.globalObject.bunVM()); this.updateHasPendingActivity(); return .undefined; } - pub fn doUnref(this: *@This(), _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn doUnref(this: *@This(), _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { this.poll_ref.unref(this.globalObject.bunVM()); this.updateHasPendingActivity(); return .undefined; @@ -1401,7 +1402,7 @@ pub const PostgresSQLConnection = struct { } } - pub fn doClose(this: *@This(), globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue { + pub fn doClose(this: *@This(), globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { _ = globalObject; this.disconnect(); this.write_buffer.deinit(bun.default_allocator); @@ -2263,7 +2264,7 @@ pub const PostgresSQLConnection = struct { } } - pub fn doFlush(this: *PostgresSQLConnection, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn doFlush(this: *PostgresSQLConnection, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { _ = callframe; _ = globalObject; _ = this; @@ -2271,7 +2272,7 @@ pub const PostgresSQLConnection = struct { return .undefined; } - pub fn createQuery(this: *PostgresSQLConnection, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue { + pub fn createQuery(this: *PostgresSQLConnection, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { _ = callframe; _ = globalObject; _ = this; diff --git a/src/string.zig b/src/string.zig index ae0c42100c..3be422ccfc 100644 --- a/src/string.zig +++ b/src/string.zig @@ -709,7 +709,8 @@ pub const String = extern struct { bun.assert(out.tag != .Dead); return out; } else { - return globalObject.jsErrorFromCPP(); + bun.assert(globalObject.hasException()); + return error.JSError; } } @@ -1325,7 +1326,7 @@ pub const String = extern struct { return try concat(strings.len, allocator, strings); } - pub export fn jsGetStringWidth(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue { + pub fn jsGetStringWidth(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args.ptr[0].isString()) { diff --git a/src/url.zig b/src/url.zig index 38e3b6aab1..9cf7aee9f8 100644 --- a/src/url.zig +++ b/src/url.zig @@ -977,10 +977,10 @@ pub const FormData = struct { } } - pub fn jsFunctionFromMultipartData( + pub fn fromMultipartData( globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, - ) callconv(JSC.conv) JSC.JSValue { + ) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); const args_ = callframe.arguments(2); @@ -1038,6 +1038,7 @@ pub const FormData = struct { } comptime { + const jsFunctionFromMultipartData = JSC.toJSHostFunction(fromMultipartData); if (!JSC.is_bindgen) @export(jsFunctionFromMultipartData, .{ .name = "FormData__jsFunctionFromMultipartData" }); }