Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function c(r, n) {
let e;
return {
e: new Uint8Array(4 * r * r),
s: Math.sqrt(1.25),
s: 1.118033988749895,
c: (r - n) / 2
};
}
24 changes: 24 additions & 0 deletions crates/swc_ecma_minifier/src/compress/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,30 @@ pub(crate) fn eval_as_number(expr_ctx: ExprCtx, e: &Expr) -> Option<f64> {
return Some(base.pow(exponent).into());
}

"ceil" => {
let v = eval_as_number(expr_ctx, &args.first()?.expr)?;

return Some(v.ceil());
}

"floor" => {
let v = eval_as_number(expr_ctx, &args.first()?.expr)?;

return Some(v.floor());
}

"round" => {
let v = eval_as_number(expr_ctx, &args.first()?.expr)?;

return Some(v.round());
}

"sqrt" => {
let v = eval_as_number(expr_ctx, &args.first()?.expr)?;

return Some(v.sqrt());
}
Comment on lines 521 to 555
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new Math function implementations do not validate the number of arguments. Unlike the 'pow' function above (lines 510-519) which explicitly checks args.len() != 2, these functions should verify that exactly one argument is provided. While args.first()? handles the zero-argument case, multiple arguments should be rejected to match JavaScript's Math method behavior of ignoring extra arguments. Consider adding argument count validation for consistency and clarity.

Copilot uses AI. Check for mistakes.

_ => {}
},
_ => {}
Expand Down
22 changes: 10 additions & 12 deletions crates/swc_ecma_minifier/tests/benches-full/d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ function(global, factory) {
return x;
};
}
var e10 = Math.sqrt(50), e5 = Math.sqrt(10), e2 = Math.sqrt(2);
function ticks(start, stop, count) {
var reverse, n, ticks, step, i = -1;
if (count *= 1, (start *= 1) == (stop *= 1) && count > 0) return [
Expand All @@ -142,11 +141,11 @@ function(global, factory) {
}
function tickIncrement(start, stop, count) {
var step = (stop - start) / Math.max(0, count), power = Math.floor(Math.log(step) / Math.LN10), error = step / Math.pow(10, power);
return power >= 0 ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power) : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
return power >= 0 ? (error >= 7.0710678118654755 ? 10 : error >= 3.1622776601683795 ? 5 : error >= 1.4142135623730951 ? 2 : 1) * Math.pow(10, power) : -Math.pow(10, -power) / (error >= 7.0710678118654755 ? 10 : error >= 3.1622776601683795 ? 5 : error >= 1.4142135623730951 ? 2 : 1);
}
function tickStep(start, stop, count) {
var step0 = Math.abs(stop - start) / Math.max(0, count), step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)), error = step0 / step1;
return error >= e10 ? step1 *= 10 : error >= e5 ? step1 *= 5 : error >= e2 && (step1 *= 2), stop < start ? -step1 : step1;
return error >= 7.0710678118654755 ? step1 *= 10 : error >= 3.1622776601683795 ? step1 *= 5 : error >= 1.4142135623730951 && (step1 *= 2), stop < start ? -step1 : step1;
}
function nice(start, stop, count) {
let prestep;
Expand Down Expand Up @@ -4320,7 +4319,7 @@ function(global, factory) {
}, treeProto.y = function(_) {
return arguments.length ? (this._y = _, this) : this._y;
};
var initialAngle = Math.PI * (3 - Math.sqrt(5));
var initialAngle = Math.PI * (3 - 2.23606797749979);
// Computes the decimal coefficient and exponent of the specified number x with
// significant digits p, where x is positive and p is in [1, 21] or undefined.
// For example, formatDecimalParts(1.23) returns ["123", 0].
Expand Down Expand Up @@ -6508,8 +6507,6 @@ function(global, factory) {
function treemapSlice(parent, x0, y0, x1, y1) {
for(var node, nodes = parent.children, i = -1, n = nodes.length, k = parent.value && (y1 - y0) / parent.value; ++i < n;)(node = nodes[i]).x0 = x0, node.x1 = x1, node.y0 = y0, node.y1 = y0 += node.value * k;
}
TreeNode.prototype = Object.create(Node.prototype);
var phi = (1 + Math.sqrt(5)) / 2;
function squarifyRatio(ratio, parent, x0, y0, x1, y1) {
for(var row, nodeValue, dx, dy, sumValue, minValue, maxValue, newRatio, minRatio, alpha, beta, rows = [], nodes = parent.children, i0 = 0, i1 = 0, n = nodes.length, value = parent.value; i0 < n;){
dx = x1 - x0, dy = y1 - y0;
Expand All @@ -6533,14 +6530,15 @@ function(global, factory) {
}
return rows;
}
TreeNode.prototype = Object.create(Node.prototype);
var squarify = function custom(ratio) {
function squarify(parent, x0, y0, x1, y1) {
squarifyRatio(ratio, parent, x0, y0, x1, y1);
}
return squarify.ratio = function(x) {
return custom((x *= 1) > 1 ? x : 1);
}, squarify;
}(phi), resquarify = function custom(ratio) {
}((1 + 2.23606797749979) / 2), resquarify = function custom(ratio) {
function resquarify(parent, x0, y0, x1, y1) {
if ((rows = parent._squarify) && rows.ratio === ratio) for(var rows, row, nodes, i, n, j = -1, m = rows.length, value = parent.value; ++j < m;){
for(nodes = (row = rows[j]).children, i = row.value = 0, n = nodes.length; i < n; ++i)row.value += nodes[i].value;
Expand All @@ -6551,7 +6549,7 @@ function(global, factory) {
return resquarify.ratio = function(x) {
return custom((x *= 1) > 1 ? x : 1);
}, resquarify;
}(phi);
}((1 + 2.23606797749979) / 2);
function lexicographicOrder(a, b) {
return a[0] - b[0] || a[1] - b[1];
}
Expand Down Expand Up @@ -8357,12 +8355,12 @@ function(global, factory) {
var w = Math.sqrt(size), x = -w / 2;
context.rect(x, x, w, w);
}
}, sqrt3 = Math.sqrt(3), triangle = {
}, triangle = {
draw: function(context, size) {
var y = -Math.sqrt(size / (3 * sqrt3));
context.moveTo(0, 2 * y), context.lineTo(-sqrt3 * y, -y), context.lineTo(sqrt3 * y, -y), context.closePath();
var y = -Math.sqrt(size / 5.196152422706632);
context.moveTo(0, 2 * y), context.lineTo(-1.7320508075688772 * y, -y), context.lineTo(1.7320508075688772 * y, -y), context.closePath();
}
}, s = Math.sqrt(3) / 2, k = 1 / Math.sqrt(12), a$1 = (k / 2 + 1) * 3, wye = {
}, s = 1.7320508075688772 / 2, k = 1 / 3.4641016151377544, a$1 = (1 / 3.4641016151377544 / 2 + 1) * 3, wye = {
draw: function(context, size) {
var r = Math.sqrt(size / a$1), x0 = r / 2, y0 = r * k, y1 = r * k + r, x2 = -x0;
context.moveTo(x0, y0), context.lineTo(x0, y1), context.lineTo(x2, y1), context.lineTo(-0.5 * x0 - s * y0, s * x0 + -0.5 * y0), context.lineTo(-0.5 * x0 - s * y1, s * x0 + -0.5 * y1), context.lineTo(-0.5 * x2 - s * y1, s * x2 + -0.5 * y1), context.lineTo(-0.5 * x0 + s * y0, -0.5 * y0 - s * x0), context.lineTo(-0.5 * x0 + s * y1, -0.5 * y1 - s * x0), context.lineTo(-0.5 * x2 + s * y1, -0.5 * y1 - s * x2), context.closePath();
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/echarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -27040,7 +27040,7 @@
height: '80%',
sort: !0,
clipWindow: 'origin',
squareRatio: 0.5 * (1 + Math.sqrt(5)),
squareRatio: 0.5 * (1 + 2.23606797749979),
leafDepth: null,
drillDownIcon: '▶',
// to align specialized icon. ▷▶❒❐▼✚
Expand Down
12 changes: 6 additions & 6 deletions crates/swc_ecma_minifier/tests/benches-full/three.js
Original file line number Diff line number Diff line change
Expand Up @@ -8900,7 +8900,7 @@ function(global, factory) {
}(BufferGeometry), DodecahedronBufferGeometry = /*#__PURE__*/ function(_PolyhedronBufferGeom) {
function DodecahedronBufferGeometry(radius, detail) {
void 0 === radius && (radius = 1), void 0 === detail && (detail = 0);
var _this, t = (1 + Math.sqrt(5)) / 2, r = 1 / t;
var _this, t = (1 + 2.23606797749979) / 2, r = 1 / ((1 + 2.23606797749979) / 2);
return (_this = _PolyhedronBufferGeom.call(this, [
-1,
-1,
Expand Down Expand Up @@ -9596,7 +9596,7 @@ function(global, factory) {
}(Geometry), IcosahedronBufferGeometry = /*#__PURE__*/ function(_PolyhedronBufferGeom) {
function IcosahedronBufferGeometry(radius, detail) {
void 0 === radius && (radius = 1), void 0 === detail && (detail = 0);
var _this, t = (1 + Math.sqrt(5)) / 2;
var _this, t = (1 + 2.23606797749979) / 2;
return (_this = _PolyhedronBufferGeom.call(this, [
-1,
t,
Expand Down Expand Up @@ -13210,13 +13210,13 @@ function(global, factory) {
}
function HemisphereLightProbe(skyColor, groundColor, intensity) {
LightProbe.call(this, void 0, intensity);
var color1 = new Color().set(skyColor), color2 = new Color().set(groundColor), sky = new Vector3(color1.r, color1.g, color1.b), ground = new Vector3(color2.r, color2.g, color2.b), c0 = Math.sqrt(Math.PI), c1 = c0 * Math.sqrt(0.75);
this.sh.coefficients[0].copy(sky).add(ground).multiplyScalar(c0), this.sh.coefficients[1].copy(sky).sub(ground).multiplyScalar(c1);
var color1 = new Color().set(skyColor), color2 = new Color().set(groundColor), sky = new Vector3(color1.r, color1.g, color1.b), ground = new Vector3(color2.r, color2.g, color2.b);
this.sh.coefficients[0].copy(sky).add(ground).multiplyScalar(1.7724538509055159), this.sh.coefficients[1].copy(sky).sub(ground).multiplyScalar(1.5349900619197325);
}
function AmbientLightProbe(color, intensity) {
LightProbe.call(this, void 0, intensity);
var color1 = new Color().set(color); // without extra factor of PI in the shader, would be 2 / Math.sqrt( Math.PI );
this.sh.coefficients[0].set(color1.r, color1.g, color1.b).multiplyScalar(2 * Math.sqrt(Math.PI));
this.sh.coefficients[0].set(color1.r, color1.g, color1.b).multiplyScalar(3.5449077018110318);
}
AudioLoader.prototype = Object.assign(Object.create(Loader.prototype), {
constructor: AudioLoader,
Expand Down Expand Up @@ -15176,7 +15176,7 @@ function(global, factory) {
_sizeLods: _sizeLods,
_sigmas: _sigmas
};
}(), _lodPlanes = _createPlanes2._lodPlanes, _sizeLods = _createPlanes2._sizeLods, _sigmas = _createPlanes2._sigmas, _clearColor = /*@__PURE__*/ new Color(), _oldTarget = null, PHI = (1 + Math.sqrt(5)) / 2, INV_PHI = 1 / PHI, _axisDirections = [
}(), _lodPlanes = _createPlanes2._lodPlanes, _sizeLods = _createPlanes2._sizeLods, _sigmas = _createPlanes2._sigmas, _clearColor = /*@__PURE__*/ new Color(), _oldTarget = null, PHI = (1 + 2.23606797749979) / 2, INV_PHI = 1 / ((1 + 2.23606797749979) / 2), _axisDirections = [
/*@__PURE__*/ new Vector3(1, 1, 1),
/*@__PURE__*/ new Vector3(-1, 1, 1),
/*@__PURE__*/ new Vector3(1, 1, -1),
Expand Down
50 changes: 22 additions & 28 deletions crates/swc_ecma_minifier/tests/benches-full/victory.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,19 @@
!*** /Users/boygirl/formidable/v/victory/node_modules/d3-array/src/ticks.js ***!
\******************************************************************************/ /*! exports provided: default, tickIncrement, tickStep */ /***/ function(module1, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickIncrement", function() {
return tickIncrement;
}), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickStep", function() {
return tickStep;
});
var e10 = Math.sqrt(50), e5 = Math.sqrt(10), e2 = Math.sqrt(2);
function tickIncrement(start, stop, count) {
var step = (stop - start) / Math.max(0, count), power = Math.floor(Math.log(step) / Math.LN10), error = step / Math.pow(10, power);
return power >= 0 ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power) : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
return power >= 0 ? (error >= 7.0710678118654755 ? 10 : error >= 3.1622776601683795 ? 5 : error >= 1.4142135623730951 ? 2 : 1) * Math.pow(10, power) : -Math.pow(10, -power) / (error >= 7.0710678118654755 ? 10 : error >= 3.1622776601683795 ? 5 : error >= 1.4142135623730951 ? 2 : 1);
}
function tickStep(start, stop, count) {
var step0 = Math.abs(stop - start) / Math.max(0, count), step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)), error = step0 / step1;
return error >= e10 ? step1 *= 10 : error >= e5 ? step1 *= 5 : error >= e2 && (step1 *= 2), stop < start ? -step1 : step1;
return error >= 7.0710678118654755 ? step1 *= 10 : error >= 3.1622776601683795 ? step1 *= 5 : error >= 1.4142135623730951 && (step1 *= 2), stop < start ? -step1 : step1;
}
/* harmony default export */ __webpack_exports__.default = function(start, stop, count) {
__webpack_require__.r(__webpack_exports__), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickIncrement", function() {
return tickIncrement;
}), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickStep", function() {
return tickStep;
}), /* harmony default export */ __webpack_exports__.default = function(start, stop, count) {
var reverse, n, ticks, step, i = -1;
if (count *= 1, (start *= 1) == (stop *= 1) && count > 0) return [
start
Expand Down Expand Up @@ -5212,12 +5210,10 @@
!*** /Users/boygirl/formidable/v/victory/node_modules/d3-shape/src/symbol/triangle.js ***!
\****************************************************************************************/ /*! exports provided: default */ /***/ function(module1, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
var sqrt3 = Math.sqrt(3);
/* harmony default export */ __webpack_exports__.default = {
__webpack_require__.r(__webpack_exports__), /* harmony default export */ __webpack_exports__.default = {
draw: function(context, size) {
var y = -Math.sqrt(size / (3 * sqrt3));
context.moveTo(0, 2 * y), context.lineTo(-sqrt3 * y, -y), context.lineTo(sqrt3 * y, -y), context.closePath();
var y = -Math.sqrt(size / 5.196152422706632);
context.moveTo(0, 2 * y), context.lineTo(-1.7320508075688772 * y, -y), context.lineTo(1.7320508075688772 * y, -y), context.closePath();
}
};
/***/ },
Expand All @@ -5226,7 +5222,7 @@
\***********************************************************************************/ /*! exports provided: default */ /***/ function(module1, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
var s = Math.sqrt(3) / 2, k = 1 / Math.sqrt(12), a = (k / 2 + 1) * 3;
var s = 1.7320508075688772 / 2, k = 1 / 3.4641016151377544, a = (1 / 3.4641016151377544 / 2 + 1) * 3;
/* harmony default export */ __webpack_exports__.default = {
draw: function(context, size) {
var r = Math.sqrt(size / a), x0 = r / 2, y0 = r * k, y1 = r * k + r, x2 = -x0;
Expand Down Expand Up @@ -18781,12 +18777,12 @@
return "M ".concat(x, ", ").concat(y + length, "\n l ").concat(length, ", -").concat(length, "\n l -").concat(length, ", -").concat(length, "\n l -").concat(length, ", ").concat(length, "\n l ").concat(length, ", ").concat(length, "\n z");
},
triangleDown: function(x, y, size) {
var height = size / 2 * Math.sqrt(3), x0 = x - size, y0 = y - size;
return "M ".concat(x0, ", ").concat(y0, "\n L ").concat(x + size, ", ").concat(y0, "\n L ").concat(x, ", ").concat(y + height, "\n z");
var x0 = x - size, y0 = y - size;
return "M ".concat(x0, ", ").concat(y0, "\n L ").concat(x + size, ", ").concat(y0, "\n L ").concat(x, ", ").concat(y + size / 2 * 1.7320508075688772, "\n z");
},
triangleUp: function(x, y, size) {
var height = size / 2 * Math.sqrt(3), x0 = x - size, y1 = y + size;
return "M ".concat(x0, ", ").concat(y1, "\n L ").concat(x + size, ", ").concat(y1, "\n L ").concat(x, ", ").concat(y - height, "\n z");
var x0 = x - size, y1 = y + size;
return "M ".concat(x0, ", ").concat(y1, "\n L ").concat(x + size, ", ").concat(y1, "\n L ").concat(x, ", ").concat(y - size / 2 * 1.7320508075688772, "\n z");
},
plus: function(x, y, size) {
var baseSize = 1.1 * size, distance = baseSize / 1.5; // eslint-disable-line no-magic-numbers
Expand Down Expand Up @@ -28968,21 +28964,19 @@
!*** /Users/boygirl/formidable/v/victory/packages/victory-histogram/node_modules/d3-array/src/ticks.js ***!
\*********************************************************************************************************/ /*! exports provided: default, tickIncrement, tickStep */ /***/ function(module1, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickIncrement", function() {
return tickIncrement;
}), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickStep", function() {
return tickStep;
});
var e10 = Math.sqrt(50), e5 = Math.sqrt(10), e2 = Math.sqrt(2);
function tickIncrement(start, stop, count) {
var step = (stop - start) / Math.max(0, count), power = Math.floor(Math.log(step) / Math.LN10), error = step / Math.pow(10, power);
return power >= 0 ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power) : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
return power >= 0 ? (error >= 7.0710678118654755 ? 10 : error >= 3.1622776601683795 ? 5 : error >= 1.4142135623730951 ? 2 : 1) * Math.pow(10, power) : -Math.pow(10, -power) / (error >= 7.0710678118654755 ? 10 : error >= 3.1622776601683795 ? 5 : error >= 1.4142135623730951 ? 2 : 1);
}
function tickStep(start, stop, count) {
var step0 = Math.abs(stop - start) / Math.max(0, count), step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)), error = step0 / step1;
return error >= e10 ? step1 *= 10 : error >= e5 ? step1 *= 5 : error >= e2 && (step1 *= 2), stop < start ? -step1 : step1;
return error >= 7.0710678118654755 ? step1 *= 10 : error >= 3.1622776601683795 ? step1 *= 5 : error >= 1.4142135623730951 && (step1 *= 2), stop < start ? -step1 : step1;
}
/* harmony default export */ __webpack_exports__.default = function(start, stop, count) {
__webpack_require__.r(__webpack_exports__), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickIncrement", function() {
return tickIncrement;
}), /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "tickStep", function() {
return tickStep;
}), /* harmony default export */ __webpack_exports__.default = function(start, stop, count) {
var reverse, n, ticks, step, i = -1;
if (count *= 1, (start *= 1) == (stop *= 1) && count > 0) return [
start
Expand Down
Loading
Loading