1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
//! This module implements the global `Math` object. //! //! `Math` is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object. //! //! `Math` works with the `Number` type. It doesn't work with `BigInt`. //! //! More information: //! - [ECMAScript reference][spec] //! - [MDN documentation][mdn] //! //! [spec]: https://tc39.es/ecma262/#sec-math-object //! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math use crate::{builtins::function::make_builtin_fn, BoaProfiler, Context, Result, Value}; use std::f64; #[cfg(test)] mod tests; /// Javascript `Math` object. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub(crate) struct Math; impl Math { /// The name of the object. pub(crate) const NAME: &'static str = "Math"; /// Get the absolute value of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.abs /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs pub(crate) fn abs(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::abs) .into()) } /// Get the arccos of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.acos /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acos pub(crate) fn acos(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::acos) .into()) } /// Get the hyperbolic arccos of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.acosh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acosh pub(crate) fn acosh(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::acosh) .into()) } /// Get the arcsine of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.asin /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asin pub(crate) fn asin(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::asin) .into()) } /// Get the hyperbolic arcsine of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.asinh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asinh pub(crate) fn asinh(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::asinh) .into()) } /// Get the arctangent of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.atan /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan pub(crate) fn atan(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::atan) .into()) } /// Get the hyperbolic arctangent of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.atanh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atanh pub(crate) fn atanh(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::atanh) .into()) } /// Get the arctangent of a numbers. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.atan2 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2 pub(crate) fn atan2(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(match ( args.get(0).map(|x| x.to_number(ctx)).transpose()?, args.get(1).map(|x| x.to_number(ctx)).transpose()?, ) { (Some(x), Some(y)) => x.atan2(y), (_, _) => f64::NAN, } .into()) } /// Get the cubic root of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.cbrt /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cbrt pub(crate) fn cbrt(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::cbrt) .into()) } /// Get lowest integer above a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.ceil /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil pub(crate) fn ceil(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::ceil) .into()) } /// Get the number of leading zeros in the 32 bit representation of a number /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.clz32 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/clz32 pub(crate) fn clz32(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_u32(ctx)) .transpose()? .map(u32::leading_zeros) .unwrap_or(32) .into()) } /// Get the cosine of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.cos /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cos pub(crate) fn cos(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::cos) .into()) } /// Get the hyperbolic cosine of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.cosh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh pub(crate) fn cosh(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::cosh) .into()) } /// Get the power to raise the natural logarithm to get the number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.exp /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/exp pub(crate) fn exp(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::exp) .into()) } /// The Math.expm1() function returns e^x - 1, where x is the argument, and e the base of /// the natural logarithms. The result is computed in a way that is accurate even when the /// value of x is close 0 /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.expm1 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1 pub(crate) fn expm1(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::exp_m1) .into()) } /// Get the highest integer below a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.floor /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor pub(crate) fn floor(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::floor) .into()) } /// Get the nearest 32-bit single precision float representation of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.fround /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround pub(crate) fn fround(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, |x| (x as f32) as f64) .into()) } /// Get an approximation of the square root of the sum of squares of all arguments. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.hypot /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot pub(crate) fn hypot(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { let mut result = 0f64; for arg in args { let x = arg.to_number(ctx)?; result = result.hypot(x); } Ok(result.into()) } /// Get the result of the C-like 32-bit multiplication of the two parameters. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.imul /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul pub(crate) fn imul(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(match ( args.get(0).map(|x| x.to_u32(ctx)).transpose()?, args.get(1).map(|x| x.to_u32(ctx)).transpose()?, ) { (Some(x), Some(y)) => x.wrapping_mul(y) as i32, (_, _) => 0, } .into()) } /// Get the natural logarithm of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.log /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log pub(crate) fn log(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, |x| if x <= 0.0 { f64::NAN } else { x.ln() }) .into()) } /// Get approximation to the natural logarithm of 1 + x. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.log1p /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p pub(crate) fn log1p(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::ln_1p) .into()) } /// Get the base 10 logarithm of the number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.log10 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10 pub(crate) fn log10(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, |x| if x <= 0.0 { f64::NAN } else { x.log10() }) .into()) } /// Get the base 2 logarithm of the number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.log2 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2 pub(crate) fn log2(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, |x| if x <= 0.0 { f64::NAN } else { x.log2() }) .into()) } /// Get the maximum of several numbers. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.max /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max pub(crate) fn max(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { let mut max = f64::NEG_INFINITY; for arg in args { let num = arg.to_number(ctx)?; max = max.max(num); } Ok(max.into()) } /// Get the minimum of several numbers. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.min /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min pub(crate) fn min(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { let mut min = f64::INFINITY; for arg in args { let num = arg.to_number(ctx)?; min = min.min(num); } Ok(min.into()) } /// Raise a number to a power. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.pow /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow pub(crate) fn pow(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(match ( args.get(0).map(|x| x.to_number(ctx)).transpose()?, args.get(1).map(|x| x.to_number(ctx)).transpose()?, ) { (Some(x), Some(y)) => x.powf(y), (_, _) => f64::NAN, } .into()) } /// Generate a random floating-point number between `0` and `1`. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.random /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random pub(crate) fn random(_: &Value, _: &[Value], _: &mut Context) -> Result<Value> { Ok(rand::random::<f64>().into()) } /// Round a number to the nearest integer. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.round /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round pub(crate) fn round(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::round) .into()) } /// Get the sign of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.sign /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign pub(crate) fn sign(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or( f64::NAN, |x| { if x == 0.0 || x == -0.0 { x } else { x.signum() } }, ) .into()) } /// Get the sine of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.sin /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin pub(crate) fn sin(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::sin) .into()) } /// Get the hyperbolic sine of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.sinh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sinh pub(crate) fn sinh(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::sinh) .into()) } /// Get the square root of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.sqrt /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt pub(crate) fn sqrt(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::sqrt) .into()) } /// Get the tangent of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.tan /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tan pub(crate) fn tan(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::tan) .into()) } /// Get the hyperbolic tangent of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.tanh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tanh pub(crate) fn tanh(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::tanh) .into()) } /// Get the integer part of a number. /// /// More information: /// - [ECMAScript reference][spec] /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-math.trunc /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc pub(crate) fn trunc(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { Ok(args .get(0) .map(|x| x.to_number(ctx)) .transpose()? .map_or(f64::NAN, f64::trunc) .into()) } /// Create a new `Math` object pub(crate) fn create(interpreter: &mut Context) -> Value { let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event("math:create", "init"); let math = Value::new_object(Some(global)); { let mut properties = math.as_object_mut().unwrap(); properties.insert_field("E", Value::from(f64::consts::E)); properties.insert_field("LN2", Value::from(f64::consts::LN_2)); properties.insert_field("LN10", Value::from(f64::consts::LN_10)); properties.insert_field("LOG2E", Value::from(f64::consts::LOG2_E)); properties.insert_field("LOG10E", Value::from(f64::consts::LOG10_E)); properties.insert_field("SQRT1_2", Value::from(0.5_f64.sqrt())); properties.insert_field("SQRT2", Value::from(f64::consts::SQRT_2)); properties.insert_field("PI", Value::from(f64::consts::PI)); } make_builtin_fn(Self::abs, "abs", &math, 1, interpreter); make_builtin_fn(Self::acos, "acos", &math, 1, interpreter); make_builtin_fn(Self::acosh, "acosh", &math, 1, interpreter); make_builtin_fn(Self::asin, "asin", &math, 1, interpreter); make_builtin_fn(Self::asinh, "asinh", &math, 1, interpreter); make_builtin_fn(Self::atan, "atan", &math, 1, interpreter); make_builtin_fn(Self::atanh, "atanh", &math, 1, interpreter); make_builtin_fn(Self::atan2, "atan2", &math, 2, interpreter); make_builtin_fn(Self::cbrt, "cbrt", &math, 1, interpreter); make_builtin_fn(Self::ceil, "ceil", &math, 1, interpreter); make_builtin_fn(Self::clz32, "clz32", &math, 1, interpreter); make_builtin_fn(Self::cos, "cos", &math, 1, interpreter); make_builtin_fn(Self::cosh, "cosh", &math, 1, interpreter); make_builtin_fn(Self::exp, "exp", &math, 1, interpreter); make_builtin_fn(Self::expm1, "expm1", &math, 1, interpreter); make_builtin_fn(Self::floor, "floor", &math, 1, interpreter); make_builtin_fn(Self::fround, "fround", &math, 1, interpreter); make_builtin_fn(Self::hypot, "hypot", &math, 1, interpreter); make_builtin_fn(Self::imul, "imul", &math, 1, interpreter); make_builtin_fn(Self::log, "log", &math, 1, interpreter); make_builtin_fn(Self::log1p, "log1p", &math, 1, interpreter); make_builtin_fn(Self::log10, "log10", &math, 1, interpreter); make_builtin_fn(Self::log2, "log2", &math, 1, interpreter); make_builtin_fn(Self::max, "max", &math, 2, interpreter); make_builtin_fn(Self::min, "min", &math, 2, interpreter); make_builtin_fn(Self::pow, "pow", &math, 2, interpreter); make_builtin_fn(Self::random, "random", &math, 0, interpreter); make_builtin_fn(Self::round, "round", &math, 1, interpreter); make_builtin_fn(Self::sign, "sign", &math, 1, interpreter); make_builtin_fn(Self::sin, "sin", &math, 1, interpreter); make_builtin_fn(Self::sinh, "sinh", &math, 1, interpreter); make_builtin_fn(Self::sqrt, "sqrt", &math, 1, interpreter); make_builtin_fn(Self::tan, "tan", &math, 1, interpreter); make_builtin_fn(Self::tanh, "tanh", &math, 1, interpreter); make_builtin_fn(Self::trunc, "trunc", &math, 1, interpreter); math } /// Initialise the `Math` object on the global object. #[inline] pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); (Self::NAME, Self::create(interpreter)) } }