Skip to content

Commit

Permalink
CSS bundling & general fixes (#16486)
Browse files Browse the repository at this point in the history
  • Loading branch information
zackradisic authored Jan 18, 2025
1 parent 6a2fd6d commit 87dedd1
Show file tree
Hide file tree
Showing 37 changed files with 8,753 additions and 3,127 deletions.
16 changes: 12 additions & 4 deletions src/baby_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ pub fn BabyList(comptime Type: type) type {
}

fn assertValidDeepClone(comptime T: type) void {
if (@hasDecl(T, "deepClone")) return;
return switch (T) {
bun.JSAst.Expr, bun.JSAst.G.Property, bun.css.ImportConditions => {},
bun.JSAst.Expr, bun.JSAst.G.Property, bun.css.ImportConditions, bun.css.LayerName => {},
else => {
@compileError("Unsupported type for BabyList.deepClone(): " ++ @typeName(Type));
},
Expand All @@ -130,11 +131,12 @@ pub fn BabyList(comptime Type: type) type {
}

/// Same as `deepClone` but doesn't return an error
pub fn deepClone2(this: @This(), allocator: std.mem.Allocator) @This() {
pub fn deepClone2(this: *const @This(), allocator: std.mem.Allocator) @This() {
assertValidDeepClone(Type);
var list_ = initCapacity(allocator, this.len) catch bun.outOfMemory();
for (this.slice()) |item| {
list_.appendAssumeCapacity(item.deepClone(allocator));
list_.len = this.len;
for (this.sliceConst(), list_.slice()) |*old, *new| {
new.* = old.deepClone(allocator);
}

return list_;
Expand Down Expand Up @@ -308,6 +310,12 @@ pub fn BabyList(comptime Type: type) type {
this.update(list__);
}

pub fn insert(this: *@This(), allocator: std.mem.Allocator, index: usize, val: Type) !void {
var list__ = this.listManaged(allocator);
try list__.insert(index, val);
this.update(list__);
}

pub fn append(this: *@This(), allocator: std.mem.Allocator, value: []const Type) !void {
var list__ = this.listManaged(allocator);
try list__.appendSlice(value);
Expand Down
4 changes: 4 additions & 0 deletions src/bitflags.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub fn Bitflags(comptime T: type) type {
return @bitCast(@as(IntType, asBits(lhs) & asBits(rhs)));
}

pub inline fn intersect(lhs: T, rhs: T) T {
return bitwiseAnd(lhs, rhs);
}

pub inline fn insert(this: *T, other: T) void {
this.* = bitwiseOr(this.*, other);
}
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ pub const TimeLike = if (Environment.isWindows) f64 else std.posix.timespec;
/// - "path"
/// - "errno"
pub fn Maybe(comptime ReturnTypeT: type, comptime ErrorTypeT: type) type {
const hasRetry = ErrorTypeT != void and @hasDecl(ErrorTypeT, "retry");
const hasTodo = ErrorTypeT != void and @hasDecl(ErrorTypeT, "todo");
// can't call @hasDecl on void, anyerror, etc
const has_any_decls = ErrorTypeT != void and ErrorTypeT != anyerror;
const hasRetry = has_any_decls and @hasDecl(ErrorTypeT, "retry");
const hasTodo = has_any_decls and @hasDecl(ErrorTypeT, "todo");

return union(Tag) {
pub const ErrorType = ErrorTypeT;
Expand Down
86 changes: 86 additions & 0 deletions src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4147,6 +4147,16 @@ pub inline fn take(val: anytype) ?bun.meta.OptionalChild(@TypeOf(val)) {
return null;
}

/// `val` must be a pointer to an optional type (e.g. `*?T`)
///
/// This function deinitializes the value and sets the optional to null.
pub inline fn clear(val: anytype, allocator: std.mem.Allocator) void {
if (val.*) |*v| {
v.deinit(allocator);
val.* = null;
}
}

pub inline fn wrappingNegation(val: anytype) @TypeOf(val) {
return 0 -% val;
}
Expand Down Expand Up @@ -4175,6 +4185,82 @@ pub inline fn itemOrNull(comptime T: type, slice: []const T, index: usize) ?T {
return if (index < slice.len) slice[index] else null;
}

pub const Maybe = bun.JSC.Node.Maybe;

/// Type which could be borrowed or owned
/// The name is from the Rust std's `Cow` type
/// Can't think of a better name
pub fn Cow(comptime T: type, comptime VTable: type) type {
const Handler = struct {
fn copy(this: *const T, allocator: std.mem.Allocator) T {
if (!@hasDecl(VTable, "copy")) @compileError(@typeName(VTable) ++ " needs `copy()` function");
return VTable.copy(this, allocator);
}

fn deinit(this: *T, allocator: std.mem.Allocator) void {
if (!@hasDecl(VTable, "deinit")) @compileError(@typeName(VTable) ++ " needs `deinit()` function");
return VTable.deinit(this, allocator);
}
};

return union(enum) {
borrowed: *const T,
owned: T,

pub fn borrow(val: *const T) @This() {
return .{
.borrowed = val,
};
}

pub fn own(val: T) @This() {
return .{
.owned = val,
};
}

pub fn replace(this: *@This(), allocator: std.mem.Allocator, newval: T) void {
if (this.* == .owned) {
this.deinit(allocator);
}
this.* = .{ .owned = newval };
}

/// Get the underlying value.
pub inline fn inner(this: *const @This()) *const T {
return switch (this.*) {
.borrowed => this.borrowed,
.owned => &this.owned,
};
}

pub inline fn innerMut(this: *@This()) ?*T {
return switch (this.*) {
.borrowed => null,
.owned => &this.owned,
};
}

pub fn toOwned(this: *@This(), allocator: std.mem.Allocator) *T {
switch (this.*) {
.borrowed => {
this.* = .{
.owned = Handler.copy(this.borrowed, allocator),
};
},
.owned => {},
}
return &this.owned;
}

pub fn deinit(this: *@This(), allocator: std.mem.Allocator) void {
if (this.* == .owned) {
Handler.deinit(&this.owned, allocator);
}
}
};
}

/// To handle stack overflows:
/// 1. StackCheck.init()
/// 2. .isSafeToRecurse()
Expand Down
Loading

0 comments on commit 87dedd1

Please sign in to comment.