Skip to content

Commit

Permalink
Make 'em all pass
Browse files Browse the repository at this point in the history
  • Loading branch information
zackradisic committed Sep 26, 2024
1 parent 7318fd8 commit 74b740f
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 99 deletions.
30 changes: 30 additions & 0 deletions src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ pub const huge_allocator_threshold: comptime_int = @import("./memory_allocator.z
pub const callmod_inline: std.builtin.CallModifier = if (builtin.mode == .Debug) .auto else .always_inline;
pub const callconv_inline: std.builtin.CallingConvention = if (builtin.mode == .Debug) .Unspecified else .Inline;

/// Restrict a value to a certain interval unless it is a float and NaN.
pub inline fn clamp(self: anytype, min: @TypeOf(self), max: @TypeOf(self)) @TypeOf(self) {
bun.debugAssert(min <= max);
if (comptime (@TypeOf(self) == f32 or @TypeOf(self) == f64)) {
return clampFloat(self, min, max);
}
return std.math.clamp(self, min, max);
}

/// Restrict a value to a certain interval unless it is NaN.
///
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
/// less than `min`. Otherwise this returns `self`.
///
/// Note that this function returns NaN if the initial value was NaN as
/// well.
pub inline fn clampFloat(_self: anytype, min: @TypeOf(_self), max: @TypeOf(_self)) @TypeOf(_self) {
if (comptime !(@TypeOf(_self) == f32 or @TypeOf(_self) == f64)) {
@compileError("Only call this on floats.");
}
var self = _self;
if (self < min) {
self = min;
}
if (self > max) {
self = max;
}
return self;
}

/// We cannot use a threadlocal memory allocator for FileSystem-related things
/// FileSystem is a singleton.
pub const fs_allocator = default_allocator;
Expand Down
7 changes: 4 additions & 3 deletions src/css/css_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6192,6 +6192,7 @@ fn restrict_prec(buf: []u8, comptime prec: u8) struct { []u8, Notation } {
const coeff_end = pos_exp orelse len;
// Decimal dot is effectively at the end of coefficient part if no
// dot presents before that.
const had_pos_dot = _pos_dot != null;
const pos_dot = _pos_dot orelse coeff_end;
// Find the end position of the number within the given precision.
const prec_end: u8 = brk: {
Expand Down Expand Up @@ -6226,7 +6227,7 @@ fn restrict_prec(buf: []u8, comptime prec: u8) struct { []u8, Notation } {
buf[i] = '0';
}
new_coeff_end = pos_dot;
} else {
} else if (had_pos_dot) {
// Strip any trailing zeros.
var i = new_coeff_end;
while (i != 0) {
Expand All @@ -6245,7 +6246,7 @@ fn restrict_prec(buf: []u8, comptime prec: u8) struct { []u8, Notation } {
const exp_len = len - posexp;
if (new_coeff_end != posexp) {
for (0..exp_len) |i| {
buf[new_coeff_end + i] = buf[posexp + 1];
buf[new_coeff_end + i] = buf[posexp + i];
}
}
break :brk new_coeff_end + exp_len;
Expand Down Expand Up @@ -6274,5 +6275,5 @@ fn restrict_prec(buf: []u8, comptime prec: u8) struct { []u8, Notation } {
}

pub inline fn fract(val: f32) f32 {
return 1.0 - @trunc(val);
return val - @trunc(val);
}
2 changes: 1 addition & 1 deletion src/css/properties/custom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ pub const UnresolvedColor = union(enum) {
) PrintErr!void {
const Helper = struct {
pub fn conv(c: f32) i32 {
return @intFromFloat(std.math.clamp(@round(c * 255.0), 0.0, 255.0));
return @intFromFloat(bun.clamp(@round(c * 255.0), 0.0, 255.0));
}
};

Expand Down
Loading

0 comments on commit 74b740f

Please sign in to comment.