diff --git a/src/bun.js/ConsoleObject.zig b/src/bun.js/ConsoleObject.zig index 69548626234a8..55f4bbd33445f 100644 --- a/src/bun.js/ConsoleObject.zig +++ b/src/bun.js/ConsoleObject.zig @@ -453,7 +453,7 @@ pub const TablePrinter = struct { value = row_value.getOwn(this.globalObject, col.name) orelse JSValue.zero; } - if (value.isEmpty()) { + if (value == .zero) { try writer.writeByteNTimes(' ', col.width + (PADDING * 2)); } else { const len: u32 = this.getWidthForValue(value); @@ -2259,7 +2259,7 @@ pub const Formatter = struct { this.addForNewLine(2); } - if (element.isEmpty()) { + if (element == .zero) { empty_start = 0; break :first; } @@ -2278,7 +2278,7 @@ pub const Formatter = struct { while (i < len) : (i += 1) { const element = value.getDirectIndex(this.globalThis, i); - if (element.isEmpty()) { + if (element == .zero) { if (empty_start == null) { empty_start = i; } diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 381bac382778a..ef493e1790c01 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -4284,7 +4284,7 @@ pub const FFIObject = struct { value: JSValue, byteOffset: ?JSValue, ) JSValue { - if (value.isEmpty()) { + if (value == .zero) { return JSC.JSValue.jsNull(); } diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig index 67b3dd63af209..c67632b15b129 100644 --- a/src/bun.js/api/JSTranspiler.zig +++ b/src/bun.js/api/JSTranspiler.zig @@ -659,7 +659,7 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std while (iter.next()) |key_| { const value = iter.value; - if (value.isEmpty()) continue; + if (value == .zero) continue; const key = try key_.toOwnedSlice(bun.default_allocator); @@ -1056,12 +1056,12 @@ pub fn transformSync( return .zero; } } - if (!js_ctx_value.isEmpty()) { + if (js_ctx_value != .zero) { js_ctx_value.ensureStillAlive(); } defer { - if (!js_ctx_value.isEmpty()) { + if (js_ctx_value != .zero) { js_ctx_value.ensureStillAlive(); } } diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index e6c33aa52acc0..fb798a561ae84 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -1971,7 +1971,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp if (class_name.eqlComptime("Response")) { Output.errGeneric("Expected a native Response object, but received a polyfilled Response object. Bun.serve() only supports native Response objects.", .{}); - } else if (!value.isEmpty() and !globalThis.hasException()) { + } else if (value != .zero and !globalThis.hasException()) { var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true, @@ -2471,7 +2471,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp this.flags.has_finalized = true; } - if (!this.response_jsvalue.isEmpty()) { + if (this.response_jsvalue != .zero) { ctxLog("finalizeWithoutDeinit: response_jsvalue != .zero", .{}); if (this.flags.response_protected) { this.response_jsvalue.unprotect(); @@ -3669,7 +3669,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp ) void { JSC.markBinding(@src()); if (this.server) |server| { - if (!server.config.onError.isEmpty() and !this.flags.has_called_error_handler) { + if (server.config.onError != .zero and !this.flags.has_called_error_handler) { this.flags.has_called_error_handler = true; const result = server.config.onError.call( server.globalThis, @@ -4849,7 +4849,7 @@ pub const ServerWebSocket = struct { return .zero; } - if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) { + if (!compress_value.isBoolean() and !compress_value.isUndefined() and compress_value != .zero) { globalThis.throw("publish expects compress to be a boolean", .{}); return .zero; } @@ -4931,7 +4931,7 @@ pub const ServerWebSocket = struct { var topic_slice = topic_value.toSlice(globalThis, bun.default_allocator); defer topic_slice.deinit(); - if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) { + if (!compress_value.isBoolean() and !compress_value.isUndefined() and compress_value != .zero) { globalThis.throw("publishText expects compress to be a boolean", .{}); return .zero; } @@ -4997,7 +4997,7 @@ pub const ServerWebSocket = struct { return .zero; } - if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) { + if (!compress_value.isBoolean() and !compress_value.isUndefined() and compress_value != .zero) { globalThis.throw("publishBinary expects compress to be a boolean", .{}); return .zero; } @@ -5173,7 +5173,7 @@ pub const ServerWebSocket = struct { const message_value = args.ptr[0]; const compress_value = args.ptr[1]; - if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) { + if (!compress_value.isBoolean() and !compress_value.isUndefined() and compress_value != .zero) { globalThis.throw("send expects compress to be a boolean", .{}); return .zero; } @@ -5247,7 +5247,7 @@ pub const ServerWebSocket = struct { const message_value = args.ptr[0]; const compress_value = args.ptr[1]; - if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) { + if (!compress_value.isBoolean() and !compress_value.isUndefined() and compress_value != .zero) { globalThis.throw("sendText expects compress to be a boolean", .{}); return .zero; } @@ -5331,7 +5331,7 @@ pub const ServerWebSocket = struct { const message_value = args.ptr[0]; const compress_value = args.ptr[1]; - if (!compress_value.isBoolean() and !compress_value.isUndefined() and !compress_value.isEmpty()) { + if (!compress_value.isBoolean() and !compress_value.isUndefined() and compress_value != .zero) { globalThis.throw("sendBinary expects compress to be a boolean", .{}); return .zero; } @@ -5524,7 +5524,7 @@ pub const ServerWebSocket = struct { } const code = brk: { - if (args.ptr[0].isEmpty() or args.ptr[0].isUndefined()) { + if (args.ptr[0] == .zero or args.ptr[0].isUndefined()) { // default exception code break :brk 1000; } @@ -5538,7 +5538,7 @@ pub const ServerWebSocket = struct { }; var message_value: ZigString.Slice = brk: { - if (args.ptr[1].isEmpty() or args.ptr[1].isUndefined()) break :brk ZigString.Slice.empty; + if (args.ptr[1] == .zero or args.ptr[1].isUndefined()) break :brk ZigString.Slice.empty; if (args.ptr[1].toSliceOrNull(globalThis)) |slice| { break :brk slice; diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig index acf7aecd21ae9..49a1f40b03763 100644 --- a/src/bun.js/base.zig +++ b/src/bun.js/base.zig @@ -480,7 +480,7 @@ pub const ArrayBuffer = extern struct { const log = Output.scoped(.ArrayBuffer, false); pub fn toJS(this: ArrayBuffer, ctx: JSC.C.JSContextRef, exception: JSC.C.ExceptionRef) JSC.JSValue { - if (!this.value.isEmpty()) { + if (this.value != .zero) { return this.value; } @@ -520,7 +520,7 @@ pub const ArrayBuffer = extern struct { callback: JSC.C.JSTypedArrayBytesDeallocator, exception: JSC.C.ExceptionRef, ) JSC.JSValue { - if (!this.value.isEmpty()) { + if (this.value != .zero) { return this.value; } diff --git a/src/bun.js/bindings/JSPropertyIterator.zig b/src/bun.js/bindings/JSPropertyIterator.zig index 53c1ee6af4ae9..f5361cc3ae0fe 100644 --- a/src/bun.js/bindings/JSPropertyIterator.zig +++ b/src/bun.js/bindings/JSPropertyIterator.zig @@ -58,7 +58,7 @@ pub fn JSPropertyIterator(comptime options: JSPropertyIteratorOptions) type { var name = bun.String.dead; if (comptime options.include_value) { const current = Bun__JSPropertyIterator__getNameAndValue(this.impl, this.globalObject, this.object, &name, i); - if (current.isEmpty()) { + if (current == .zero) { return this.next(); } current.ensureStillAlive(); diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 8eea345d73bd2..a7fede1d3ced9 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -2434,7 +2434,7 @@ pub const JSPromise = extern struct { } pub fn wrapValue(globalObject: *JSGlobalObject, value: JSValue) JSValue { - if (value.isEmpty()) { + if (value == .zero) { return resolvedPromiseValue(globalObject, JSValue.jsUndefined()); } else if (value.isEmptyOrUndefinedOrNull() or !value.isCell()) { return resolvedPromiseValue(globalObject, value); @@ -4169,9 +4169,7 @@ pub const JSValue = enum(JSValueReprInt) { pub fn jsType( this: JSValue, ) JSType { - if (comptime bun.Environment.allow_assert) { - bun.assert(!this.isEmpty()); - } + bun.assert(this != .zero); return cppFn("jsType", .{this}); } @@ -4793,13 +4791,6 @@ pub const JSValue = enum(JSValueReprInt) { else => false, }; } - /// Empty as in "JSValue {}" rather than an empty string - pub inline fn isEmpty(this: JSValue) bool { - return switch (@intFromEnum(this)) { - 0 => true, - else => false, - }; - } pub fn isBoolean(this: JSValue) bool { return cppFn("isBoolean", .{this}); } @@ -5274,6 +5265,7 @@ pub const JSValue = enum(JSValueReprInt) { /// Equivalent to `obj.property` in JavaScript. /// Reminder: `undefined` is a value! + // TODO: change the return of this from `?JSValue` to `bun.JSError!JSValue` pub fn get(this: JSValue, global: *JSGlobalObject, property: []const u8) ?JSValue { if (comptime bun.Environment.isDebug) { if (BuiltinName.has(property)) { @@ -5282,7 +5274,7 @@ pub const JSValue = enum(JSValueReprInt) { } const value = getIfPropertyExistsImpl(this, global, property.ptr, @as(u32, @intCast(property.len))); - return if (value.isEmpty()) null else value; + return if (value == .zero) null else value; } extern fn JSC__JSValue__getOwn(value: JSValue, globalObject: *JSGlobalObject, propertyName: [*c]const bun.String) JSValue; diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index 94003ef968bd2..fde3e30901c3b 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -2334,7 +2334,7 @@ pub const Arguments = struct { if (val.getTruthy(ctx, "mode")) |mode_| { mode = try JSC.Node.modeFromJS(ctx, mode_) orelse mode; } - } else if (!val.isEmpty()) { + } else if (val != .zero) { if (!val.isUndefinedOrNull()) { // error is handled below flags = try FileSystemFlags.fromJS(ctx, val) orelse flags; diff --git a/src/bun.js/test/expect.zig b/src/bun.js/test/expect.zig index d91c9a19286d4..2725ec4db51c6 100644 --- a/src/bun.js/test/expect.zig +++ b/src/bun.js/test/expect.zig @@ -1784,7 +1784,7 @@ pub const Expect = struct { if (pass) { received_property = value.getIfPropertyExistsFromPath(globalThis, expected_property_path); - pass = !received_property.isEmpty(); + pass = received_property != .zero; } if (pass and expected_property != null) { @@ -1799,7 +1799,7 @@ pub const Expect = struct { if (not) { if (expected_property != null) { const signature = comptime getSignature("toHaveProperty", "path, value", true); - if (!received_property.isEmpty()) { + if (received_property != .zero) { this.throw(globalThis, signature, "\n\nExpected path: {any}\n\nExpected value: not {any}\n", .{ expected_property_path.toFmt(&formatter), expected_property.?.toFmt(&formatter), @@ -1818,7 +1818,7 @@ pub const Expect = struct { if (expected_property != null) { const signature = comptime getSignature("toHaveProperty", "path, value", false); - if (!received_property.isEmpty()) { + if (received_property != .zero) { // deep equal case const diff_format = DiffFormatter{ .received = received_property, @@ -2381,7 +2381,7 @@ pub const Expect = struct { const result: JSValue = result_.?; var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; - if (expected_value.isEmpty() or expected_value.isUndefined()) { + if (expected_value == .zero or expected_value.isUndefined()) { const signature_no_args = comptime getSignature("toThrow", "", true); if (result.toError()) |err| { const name = err.getTruthyComptime(globalThis, "name") orelse JSValue.undefined; @@ -2474,7 +2474,7 @@ pub const Expect = struct { } if (did_throw) { - if (expected_value.isEmpty() or expected_value.isUndefined()) return .undefined; + if (expected_value == .zero or expected_value.isUndefined()) return .undefined; const result: JSValue = if (result_.?.toError()) |r| r @@ -2631,7 +2631,7 @@ pub const Expect = struct { var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; const received_line = "Received function did not throw\nReceived value: {any}\n"; - if (expected_value.isEmpty() or expected_value.isUndefined()) { + if (expected_value == .zero or expected_value.isUndefined()) { const signature = comptime getSignature("toThrow", "", false); this.throw(globalThis, signature, "\n\n" ++ received_line, .{result.toFmt(&formatter)}); return .zero; @@ -4663,12 +4663,7 @@ pub const Expect = struct { matcher_context_jsvalue.ensureStillAlive(); // call the custom matcher implementation - var result = matcher_fn.call(globalThis, matcher_context_jsvalue, args) catch |err| globalThis.takeException(err); - assert(!result.isEmpty()); - if (result.toError()) |err| { - globalThis.throwValue(err); - return false; - } + var result = matcher_fn.call(globalThis, matcher_context_jsvalue, args) catch return false; // support for async matcher results if (result.asAnyPromise()) |promise| { const vm = globalThis.vm(); @@ -4678,7 +4673,7 @@ pub const Expect = struct { result = promise.result(vm); result.ensureStillAlive(); - assert(!result.isEmpty()); + assert(result != .zero); switch (promise.status(vm)) { .pending => unreachable, .fulfilled => {}, @@ -4734,13 +4729,7 @@ pub const Expect = struct { if (comptime Environment.allow_assert) assert(message.isCallable(globalThis.vm())); // checked above - const message_result = message.callWithGlobalThis(globalThis, &.{}) catch |err| - globalThis.takeException(err); - assert(!message_result.isEmpty()); - if (message_result.toError()) |err| { - globalThis.throwValue(err); - return false; - } + const message_result = message.callWithGlobalThis(globalThis, &.{}) catch return false; if (bun.String.tryFromJS(message_result, globalThis)) |str| { message_text = str; } else { @@ -4979,7 +4968,7 @@ pub const ExpectStatic = struct { fn createAsymmetricMatcherWithFlags(T: anytype, this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *CallFrame) JSValue { //const this: *ExpectStatic = ExpectStatic.fromJS(callFrame.this()); const instance_jsvalue = T.call(globalThis, callFrame); - if (!instance_jsvalue.isEmpty() and !instance_jsvalue.isAnyError()) { + if (instance_jsvalue != .zero and !instance_jsvalue.isAnyError()) { var instance = T.fromJS(instance_jsvalue) orelse { globalThis.throwOutOfMemory(); return .zero; @@ -5639,8 +5628,8 @@ pub const ExpectMatcherUtils = struct { // Extract the matcher_fn from a JSCustomExpectMatcherFunction instance inline fn getCustomMatcherFn(thisValue: JSValue, globalThis: *JSGlobalObject) ?JSValue { - var matcher_fn = Bun__JSWrappingFunction__getWrappedFunction(thisValue, globalThis); - return if (matcher_fn.isEmpty()) null else matcher_fn; + const matcher_fn = Bun__JSWrappingFunction__getWrappedFunction(thisValue, globalThis); + return if (matcher_fn == .zero) null else matcher_fn; } /// JSValue.zero is used to indicate it was not a JSMockFunction diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig index 5dff0ed4e39e5..813189a7b06f1 100644 --- a/src/bun.js/test/jest.zig +++ b/src/bun.js/test/jest.zig @@ -1886,7 +1886,7 @@ fn formatLabel(globalThis: *JSGlobalObject, label: string, function_args: []JSVa switch (label[idx + 1]) { 's' => { - try consumeArg(globalThis, !current_arg.isEmpty() and current_arg.jsType().isString(), &idx, &args_idx, &list, ¤t_arg, "%s"); + try consumeArg(globalThis, current_arg != .zero and current_arg.jsType().isString(), &idx, &args_idx, &list, ¤t_arg, "%s"); }, 'i' => { try consumeArg(globalThis, current_arg.isAnyInt(), &idx, &args_idx, &list, ¤t_arg, "%i"); @@ -2128,7 +2128,7 @@ inline fn createEach( } var array = args[0]; - if (array.isEmpty() or !array.jsType().isArray()) { + if (array == .zero or !array.jsType().isArray()) { globalThis.throwPretty("{s} expects an array", .{signature}); return .zero; } diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig index d4b51840c985f..4940fa6cafb8f 100644 --- a/src/bun.js/webcore/encoding.zig +++ b/src/bun.js/webcore/encoding.zig @@ -195,7 +195,7 @@ pub const TextEncoder = struct { return .undefined; } - if (array.isEmpty()) { + if (array == .zero) { array = JSC.JSValue.createUninitializedUint8Array(globalThis, length); array.ensureStillAlive(); @memcpy(array.asArrayBuffer(globalThis).?.ptr[0..length], buf_to_use[0..length]); diff --git a/src/js_ast.zig b/src/js_ast.zig index df0b6d95dd4e0..2d752179ad7e8 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -8424,7 +8424,7 @@ pub const Macro = struct { var js_args: []JSC.JSValue = &.{}; var js_processed_args_len: usize = 0; defer { - for (js_args[0..js_processed_args_len -| @as(usize, @intFromBool(!javascript_object.isEmpty()))]) |arg| { + for (js_args[0..js_processed_args_len -| @as(usize, @intFromBool(javascript_object != .zero))]) |arg| { arg.unprotect(); } @@ -8436,7 +8436,7 @@ pub const Macro = struct { switch (caller.data) { .e_call => |call| { const call_args: []Expr = call.args.slice(); - js_args = try allocator.alloc(JSC.JSValue, call_args.len + @as(usize, @intFromBool(!javascript_object.isEmpty()))); + js_args = try allocator.alloc(JSC.JSValue, call_args.len + @as(usize, @intFromBool(javascript_object != .zero))); js_processed_args_len = js_args.len; for (0.., call_args, js_args[0..call_args.len]) |i, in, *out| { @@ -8461,7 +8461,7 @@ pub const Macro = struct { }, } - if (!javascript_object.isEmpty()) { + if (javascript_object != .zero) { if (js_args.len == 0) { js_args = try allocator.alloc(JSC.JSValue, 1); } diff --git a/src/napi/napi.zig b/src/napi/napi.zig index 8f25ae5159905..4a9412025ad14 100644 --- a/src/napi/napi.zig +++ b/src/napi/napi.zig @@ -617,7 +617,7 @@ pub export fn napi_set_element(env: napi_env, object_: napi_value, index: c_uint if (!object.jsType().isIndexable()) { return .array_expected; } - if (value.isEmpty()) + if (value == .zero) return invalidArg(); JSC.C.JSObjectSetPropertyAtIndex(env.ref(), object.asObjectRef(), index, value.asObjectRef(), TODO_EXCEPTION); return .ok; @@ -1004,7 +1004,7 @@ pub export fn napi_is_promise(_: napi_env, value_: napi_value, is_promise_: ?*bo return invalidArg(); }; - if (value.isEmpty()) { + if (value == .zero) { return invalidArg(); } diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 438d16b9b69f3..61a0252ffc804 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -3284,7 +3284,7 @@ pub const Resolver = struct { bun.JSC.markBinding(@src()); const argument: bun.JSC.JSValue = callframe.argument(0); - if (argument.isEmpty() or !argument.isString()) { + if (argument == .zero or !argument.isString()) { globalThis.throwInvalidArgumentType("nodeModulePaths", "path", "string"); return .zero; } diff --git a/src/shell/shell.zig b/src/shell/shell.zig index afd9782900b4f..f6b5049ba5894 100644 --- a/src/shell/shell.zig +++ b/src/shell/shell.zig @@ -3782,7 +3782,7 @@ pub fn handleTemplateValue( jsobjref_buf: []u8, ) !bool { var builder = ShellSrcBuilder.init(globalThis, out_script, jsstrings); - if (!template_value.isEmpty()) { + if (template_value != .zero) { if (template_value.asArrayBuffer(globalThis)) |array_buffer| { _ = array_buffer; const idx = out_jsobjs.items.len;