From 215fdb4697da0228ee6654d18ab04d82855e9701 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Tue, 26 Nov 2024 17:17:12 -0800 Subject: [PATCH] zig: make throwInvalidArgumentTypeValue use JSError (#15302) Co-authored-by: Ciro Spaciari --- src/bun.js/api/BunObject.zig | 14 ++++------ src/bun.js/api/bun/h2_frame_parser.zig | 3 +-- src/bun.js/api/bun/socket.zig | 14 ++++------ src/bun.js/api/ffi.zig | 9 +++---- src/bun.js/bindings/bindings.zig | 36 ++++++++++---------------- src/bun.js/javascript.zig | 14 +++------- src/bun.js/node/node_fs.zig | 15 ++++------- src/bun.js/node/node_zlib_binding.zig | 12 +++------ src/bun.js/node/types.zig | 3 +-- src/bun.js/webcore.zig | 9 +------ src/bun.js/webcore/blob.zig | 7 ++--- 11 files changed, 44 insertions(+), 92 deletions(-) diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index b650b4693fce5d..668e58788c633b 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -1389,8 +1389,7 @@ pub const Crypto = struct { } if (!arguments[3].isAnyInt()) { - _ = globalThis.throwInvalidArgumentTypeValue("keylen", "integer", arguments[3]); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("keylen", "integer", arguments[3]); } const length = arguments[3].coerce(i64, globalThis); @@ -1404,8 +1403,7 @@ pub const Crypto = struct { } if (!arguments[2].isAnyInt()) { - _ = globalThis.throwInvalidArgumentTypeValue("iteration count", "integer", arguments[2]); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("iteration count", "integer", arguments[2]); } const iteration_count = arguments[2].coerce(i64, globalThis); @@ -1420,8 +1418,7 @@ pub const Crypto = struct { const algorithm = brk: { if (!arguments[4].isString()) { - _ = globalThis.throwInvalidArgumentTypeValue("algorithm", "string", arguments[4]); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("algorithm", "string", arguments[4]); } break :brk EVP.Algorithm.map.fromJSCaseInsensitive(globalThis, arguments[4]) orelse { @@ -1450,8 +1447,7 @@ pub const Crypto = struct { } out.salt = JSC.Node.StringOrBuffer.fromJSMaybeAsync(globalThis, bun.default_allocator, arguments[1], is_async) orelse { - _ = globalThis.throwInvalidArgumentTypeValue("salt", "string or buffer", arguments[1]); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("salt", "string or buffer", arguments[1]); }; if (out.salt.slice().len > std.math.maxInt(i32)) { @@ -1460,7 +1456,7 @@ pub const Crypto = struct { out.password = JSC.Node.StringOrBuffer.fromJSMaybeAsync(globalThis, bun.default_allocator, arguments[0], is_async) orelse { if (!globalThis.hasException()) { - _ = globalThis.throwInvalidArgumentTypeValue("password", "string or buffer", arguments[0]); + return globalThis.throwInvalidArgumentTypeValue("password", "string or buffer", arguments[0]); } return error.JSError; }; diff --git a/src/bun.js/api/bun/h2_frame_parser.zig b/src/bun.js/api/bun/h2_frame_parser.zig index ab7c421e48bbfa..343d425d5d059a 100644 --- a/src/bun.js/api/bun/h2_frame_parser.zig +++ b/src/bun.js/api/bun/h2_frame_parser.zig @@ -3105,8 +3105,7 @@ pub const H2FrameParser = struct { } if (!encoding_arg.isString()) { - _ = globalObject.throwInvalidArgumentTypeValue("write", "encoding", encoding_arg); - return error.JSError; + return globalObject.throwInvalidArgumentTypeValue("write", "encoding", encoding_arg); } break :brk JSC.Node.Encoding.fromJS(encoding_arg, globalObject) orelse { diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 861d5d002bfe3e..e87d82dd1e19e2 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -1474,13 +1474,7 @@ fn NewSocket(comptime ssl: bool) type { const initialDelay: u32 = brk: { if (args.len > 1) { - if (globalThis.validateIntegerRange(args.ptr[1], i32, 0, .{ - .min = 0, - .field_name = "initialDelay", - })) |signedDelay| { - break :brk @intCast(signedDelay); - } - return .zero; + break :brk @intCast(try globalThis.validateIntegerRange(args.ptr[1], i32, 0, .{ .min = 0, .field_name = "initialDelay" })); } break :brk 0; }; @@ -2209,7 +2203,8 @@ fn NewSocket(comptime ssl: bool) type { return .fail; } orelse { if (!globalObject.hasException()) { - _ = globalObject.throwInvalidArgumentTypeValue("data", "string, buffer, or blob", data_value); + globalObject.throwInvalidArgumentTypeValue("data", "string, buffer, or blob", data_value) catch {}; + return .fail; } return .fail; }; @@ -2320,7 +2315,8 @@ fn NewSocket(comptime ssl: bool) type { return .fail; } orelse { if (!globalObject.hasException()) { - _ = globalObject.throwInvalidArgumentTypeValue("data", "string, buffer, or blob", args[0]); + globalObject.throwInvalidArgumentTypeValue("data", "string, buffer, or blob", args[0]) catch {}; + return .fail; } return .fail; }; diff --git a/src/bun.js/api/ffi.zig b/src/bun.js/api/ffi.zig index 262ff8357e7b6d..7ebc432d4108f7 100644 --- a/src/bun.js/api/ffi.zig +++ b/src/bun.js/api/ffi.zig @@ -597,8 +597,7 @@ pub const FFI = struct { bun.default_allocator.free(@constCast(item)); } items.deinit(); - _ = globalThis.throwInvalidArgumentTypeValue(property, "array of strings", val); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue(property, "array of strings", val); } const str = val.getZigString(globalThis); if (str.isEmpty()) continue; @@ -611,8 +610,7 @@ pub const FFI = struct { pub fn fromJSString(globalThis: *JSC.JSGlobalObject, value: JSC.JSValue, comptime property: []const u8) bun.JSError!StringArray { if (value == .undefined) return .{}; if (!value.isString()) { - _ = globalThis.throwInvalidArgumentTypeValue(property, "array of strings", value); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue(property, "array of strings", value); } const str = value.getZigString(globalThis); if (str.isEmpty()) return .{}; @@ -648,8 +646,7 @@ pub const FFI = struct { const symbols_object = object.getOwn(globalThis, "symbols") orelse .undefined; if (!globalThis.hasException() and (symbols_object == .zero or !symbols_object.isObject())) { - _ = globalThis.throwInvalidArgumentTypeValue("symbols", "object", symbols_object); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("symbols", "object", symbols_object); } if (globalThis.hasException()) { diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 13a498e658ea8e..138cb1f59e7f80 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -3023,10 +3023,10 @@ pub const JSGlobalObject = opaque { argname: []const u8, typename: []const u8, value: JSValue, - ) JSValue { + ) bun.JSError { var formatter = JSC.ConsoleObject.Formatter{ .globalThis = this }; this.ERR_INVALID_ARG_TYPE("The \"{s}\" argument must be of type {s}. Received {}", .{ argname, typename, value.toFmt(&formatter) }).throw(); - return .zero; + return error.JSError; } pub fn throwInvalidArgumentRangeValue( @@ -3034,9 +3034,9 @@ pub const JSGlobalObject = opaque { argname: []const u8, typename: []const u8, value: i64, - ) JSValue { + ) bun.JSError { this.ERR_OUT_OF_RANGE("The \"{s}\" is out of range. {s}. Received {}", .{ argname, typename, value }).throw(); - return .zero; + return error.JSError; } pub fn throwInvalidPropertyTypeValue( @@ -3044,11 +3044,11 @@ pub const JSGlobalObject = opaque { field: []const u8, typename: []const u8, value: JSValue, - ) JSValue { + ) bun.JSError { const ty_str = value.jsTypeString(this).toSlice(this, bun.default_allocator); defer ty_str.deinit(); this.ERR_INVALID_ARG_TYPE("The \"{s}\" property must be of type {s}. Received {s}", .{ field, typename, ty_str.slice() }).throw(); - return .zero; + return error.JSError; } pub fn createNotEnoughArguments( @@ -3423,15 +3423,13 @@ pub const JSGlobalObject = opaque { allowFunction: bool = false, nullable: bool = false, }, - ) bool { + ) bun.JSError!void { if ((!opts.nullable and value.isNull()) or (!opts.allowArray and value.isArray()) or (!value.isObject() and (!opts.allowFunction or !value.isFunction()))) { - _ = this.throwInvalidArgumentTypeValue(arg_name, "object", value); - return false; + return this.throwInvalidArgumentTypeValue(arg_name, "object", value); } - return true; } pub fn throwRangeError(this: *JSGlobalObject, value: anytype, options: bun.fmt.OutOfRangeOptions) void { @@ -3447,7 +3445,7 @@ pub const JSGlobalObject = opaque { always_allow_zero: bool = false, }; - pub fn validateIntegerRange(this: *JSGlobalObject, value: JSValue, comptime T: type, default: T, comptime range: IntegerRange) ?T { + pub fn validateIntegerRange(this: *JSGlobalObject, value: JSValue, comptime T: type, default: T, comptime range: IntegerRange) bun.JSError!T { if (value == .undefined or value == .zero) { return default; } @@ -3476,14 +3474,13 @@ pub const JSGlobalObject = opaque { } if (int < min_t or int > max_t) { this.throwRangeError(int, .{ .field_name = field_name, .min = min, .max = max }); - return null; + return error.JSError; } return @intCast(int); } if (!value.isNumber()) { - _ = this.throwInvalidPropertyTypeValue(field_name, "number", value); - return null; + return this.throwInvalidPropertyTypeValue(field_name, "number", value); } const f64_val = value.asNumber(); if (always_allow_zero and f64_val == 0) { @@ -3495,12 +3492,11 @@ pub const JSGlobalObject = opaque { return default; } if (@floor(f64_val) != f64_val) { - _ = this.throwInvalidPropertyTypeValue(field_name, "integer", value); - return null; + return this.throwInvalidPropertyTypeValue(field_name, "integer", value); } if (f64_val < min_t or f64_val > max_t) { this.throwRangeError(f64_val, .{ .field_name = comptime field_name, .min = min, .max = max }); - return null; + return error.JSError; } return @intFromFloat(f64_val); @@ -5406,10 +5402,8 @@ pub const JSValue = enum(i64) { if (prop.isNull() or prop == .false) { return null; } - if (prop.isSymbol()) { - _ = global.throwInvalidPropertyTypeValue(property, "string", prop); - return error.JSError; + return global.throwInvalidPropertyTypeValue(property, "string", prop); } const str = prop.toBunString(global); @@ -5417,11 +5411,9 @@ pub const JSValue = enum(i64) { str.deref(); return error.JSError; } - if (str.isEmpty()) { return null; } - return str; } diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 8b8e8b6c1520bc..2dd1346042d6d9 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -433,17 +433,9 @@ 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, -) bun.JSError!JSC.JSValue { +pub fn Bun__Process__send_(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) bun.JSError!JSC.JSValue { JSC.markBinding(@src()); - var message, var handle, var options_, var callback = callFrame.arguments_old(4).ptr; - - if (message == .zero) message = .undefined; - if (handle == .zero) handle = .undefined; - if (options_ == .zero) options_ = .undefined; - if (callback == .zero) callback = .undefined; + var message, var handle, var options_, var callback = callFrame.argumentsAsArray(4); if (handle.isFunction()) { callback = handle; @@ -453,7 +445,7 @@ pub fn Bun__Process__send_( callback = options_; options_ = .undefined; } else if (!options_.isUndefined()) { - if (!globalObject.validateObject("options", options_, .{})) return .zero; + try globalObject.validateObject("options", options_, .{}); } const S = struct { diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index ec043e44ce1d6e..e58bce0e7c857d 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -1513,8 +1513,7 @@ pub const Arguments = struct { arguments.eat(); if (!uid_value.isNumber()) { - _ = ctx.throwInvalidArgumentTypeValue("uid", "number", uid_value); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("uid", "number", uid_value); } break :brk @as(uid_t, @intCast(uid_value.toInt32())); }; @@ -1526,8 +1525,7 @@ pub const Arguments = struct { arguments.eat(); if (!gid_value.isNumber()) { - _ = ctx.throwInvalidArgumentTypeValue("gid", "number", gid_value); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("gid", "number", gid_value); } break :brk @as(gid_t, @intCast(gid_value.toInt32())); }; @@ -2411,12 +2409,10 @@ pub const Arguments = struct { const buffer = StringOrBuffer.fromJS(ctx, bun.default_allocator, buffer_value orelse { return ctx.throwInvalidArguments("data is required", .{}); }) orelse { - _ = ctx.throwInvalidArgumentTypeValue("buffer", "string or TypedArray", buffer_value.?); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("buffer", "string or TypedArray", buffer_value.?); }; if (buffer_value.?.isString() and !buffer_value.?.isStringLiteral()) { - _ = ctx.throwInvalidArgumentTypeValue("buffer", "string or TypedArray", buffer_value.?); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("buffer", "string or TypedArray", buffer_value.?); } var args = Write{ @@ -2506,8 +2502,7 @@ pub const Arguments = struct { const buffer = Buffer.fromJS(ctx, buffer_value orelse { return ctx.throwInvalidArguments("buffer is required", .{}); }) orelse { - _ = ctx.throwInvalidArgumentTypeValue("buffer", "TypedArray", buffer_value.?); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("buffer", "TypedArray", buffer_value.?); }; arguments.eat(); diff --git a/src/bun.js/node/node_zlib_binding.zig b/src/bun.js/node/node_zlib_binding.zig index daa52c0f57e0a7..a094d75bda3856 100644 --- a/src/bun.js/node/node_zlib_binding.zig +++ b/src/bun.js/node/node_zlib_binding.zig @@ -326,13 +326,11 @@ pub const SNativeZlib = struct { var mode = arguments[0]; if (!mode.isNumber()) { - _ = globalThis.throwInvalidArgumentTypeValue("mode", "number", mode); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("mode", "number", mode); } const mode_double = mode.asNumber(); if (@mod(mode_double, 1.0) != 0.0) { - _ = globalThis.throwInvalidArgumentTypeValue("mode", "integer", mode); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("mode", "integer", mode); } const mode_int: i64 = @intFromFloat(mode_double); if (mode_int < 1 or mode_int > 7) { @@ -686,13 +684,11 @@ pub const SNativeBrotli = struct { var mode = arguments[0]; if (!mode.isNumber()) { - _ = globalThis.throwInvalidArgumentTypeValue("mode", "number", mode); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("mode", "number", mode); } const mode_double = mode.asNumber(); if (@mod(mode_double, 1.0) != 0.0) { - _ = globalThis.throwInvalidArgumentTypeValue("mode", "integer", mode); - return error.JSError; + return globalThis.throwInvalidArgumentTypeValue("mode", "integer", mode); } const mode_int: i64 = @intFromFloat(mode_double); if (mode_int < 8 or mode_int > 9) { diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig index 52ff09cd61a9b6..c1529a53452cf6 100644 --- a/src/bun.js/node/types.zig +++ b/src/bun.js/node/types.zig @@ -1267,8 +1267,7 @@ pub fn modeFromJS(ctx: JSC.C.JSContextRef, value: JSC.JSValue) bun.JSError!?Mode if (value.isUndefinedOrNull()) return null; if (!value.isString()) { - _ = ctx.throwInvalidArgumentTypeValue("mode", "number", value); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("mode", "number", value); } // An easier method of constructing the mode is to use a sequence of diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig index 1ed17a0127ad6c..9f62705280f169 100644 --- a/src/bun.js/webcore.zig +++ b/src/bun.js/webcore.zig @@ -689,14 +689,7 @@ pub const Crypto = struct { const date = timestamp_value.getUnixTimestamp(); break :brk @intFromFloat(@max(0, date)); } - - if (globalThis.validateIntegerRange(timestamp_value, i64, 0, .{ - .min = 0, - .field_name = "timestamp", - })) |timestamp_int| { - break :brk @intCast(timestamp_int); - } - return .zero; + break :brk @intCast(try globalThis.validateIntegerRange(timestamp_value, i64, 0, .{ .min = 0, .field_name = "timestamp" })); } break :brk @intCast(@max(0, std.time.milliTimestamp())); diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index 1c027b6132878a..5ab0caa5965abc 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -56,17 +56,14 @@ const PathOrBlob = union(enum) { } const arg = args.nextEat() orelse { - _ = ctx.throwInvalidArgumentTypeValue("destination", "path, file descriptor, or Blob", .undefined); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("destination", "path, file descriptor, or Blob", .undefined); }; if (arg.as(Blob)) |blob| { return PathOrBlob{ .blob = blob.*, }; } - - _ = ctx.throwInvalidArgumentTypeValue("destination", "path, file descriptor, or Blob", arg); - return error.JSError; + return ctx.throwInvalidArgumentTypeValue("destination", "path, file descriptor, or Blob", arg); } };