Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

zig: make throwInvalidArgumentTypeValue use JSError #15302

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
};
Expand Down
3 changes: 1 addition & 2 deletions src/bun.js/api/bun/h2_frame_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 5 additions & 9 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down
9 changes: 3 additions & 6 deletions src/bun.js/api/ffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 .{};
Expand Down Expand Up @@ -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()) {
Expand Down
36 changes: 14 additions & 22 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3023,32 +3023,32 @@ 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(
this: *JSGlobalObject,
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(
this: *JSGlobalObject,
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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -5406,22 +5402,18 @@ 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);
if (global.hasException()) {
str.deref();
return error.JSError;
}

if (str.isEmpty()) {
return null;
}

return str;
}

Expand Down
14 changes: 3 additions & 11 deletions src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
15 changes: 5 additions & 10 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
};
Expand All @@ -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()));
};
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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();

Expand Down
12 changes: 4 additions & 8 deletions src/bun.js/node/node_zlib_binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions src/bun.js/webcore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
7 changes: 2 additions & 5 deletions src/bun.js/webcore/blob.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down