Skip to content

Commit

Permalink
zig: remove JSValue.isEmpty (#15128)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Nov 14, 2024
1 parent f8979b0 commit 35513a9
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2259,7 +2259,7 @@ pub const Formatter = struct {
this.addForNewLine(2);
}

if (element.isEmpty()) {
if (element == .zero) {
empty_start = 0;
break :first;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4284,7 +4284,7 @@ pub const FFIObject = struct {
value: JSValue,
byteOffset: ?JSValue,
) JSValue {
if (value.isEmpty()) {
if (value == .zero) {
return JSC.JSValue.jsNull();
}

Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/api/JSTranspiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/JSPropertyIterator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 4 additions & 12 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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});
}

Expand Down Expand Up @@ -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});
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 12 additions & 23 deletions src/bun.js/test/expect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -1799,7 +1799,7 @@ pub const Expect = struct {
if (not) {
if (expected_property != null) {
const signature = comptime getSignature("toHaveProperty", "<green>path<r><d>, <r><green>value<r>", true);
if (!received_property.isEmpty()) {
if (received_property != .zero) {
this.throw(globalThis, signature, "\n\nExpected path: <green>{any}<r>\n\nExpected value: not <green>{any}<r>\n", .{
expected_property_path.toFmt(&formatter),
expected_property.?.toFmt(&formatter),
Expand All @@ -1818,7 +1818,7 @@ pub const Expect = struct {

if (expected_property != null) {
const signature = comptime getSignature("toHaveProperty", "<green>path<r><d>, <r><green>value<r>", false);
if (!received_property.isEmpty()) {
if (received_property != .zero) {
// deep equal case
const diff_format = DiffFormatter{
.received = received_property,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: <red>{any}<r>\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;
Expand Down Expand Up @@ -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();
Expand All @@ -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 => {},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/test/jest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, &current_arg, "%s");
try consumeArg(globalThis, current_arg != .zero and current_arg.jsType().isString(), &idx, &args_idx, &list, &current_arg, "%s");
},
'i' => {
try consumeArg(globalThis, current_arg.isAnyInt(), &idx, &args_idx, &list, &current_arg, "%i");
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/webcore/encoding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Loading

0 comments on commit 35513a9

Please sign in to comment.