boa_engine 0.17.0

Boa is a Javascript lexer, parser and compiler written in Rust. Currently, it has support for some of the language.
Documentation
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
//! Boa's implementation of ECMAScript's 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::BuiltInObject, context::intrinsics::Intrinsics, object::JsObject,
    property::Attribute, realm::Realm, string::utf16, symbol::JsSymbol, Context, JsArgs, JsResult,
    JsValue,
};
use boa_profiler::Profiler;

use super::{BuiltInBuilder, IntrinsicObject};

#[cfg(test)]
mod tests;

/// Javascript `Math` object.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Math;

impl IntrinsicObject for Math {
    fn init(realm: &Realm) {
        let _timer = Profiler::global().start_event(Self::NAME, "init");

        let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT;
        BuiltInBuilder::with_intrinsic::<Self>(realm)
            .static_property(utf16!("E"), std::f64::consts::E, attribute)
            .static_property(utf16!("LN10"), std::f64::consts::LN_10, attribute)
            .static_property(utf16!("LN2"), std::f64::consts::LN_2, attribute)
            .static_property(utf16!("LOG10E"), std::f64::consts::LOG10_E, attribute)
            .static_property(utf16!("LOG2E"), std::f64::consts::LOG2_E, attribute)
            .static_property(utf16!("PI"), std::f64::consts::PI, attribute)
            .static_property(
                utf16!("SQRT1_2"),
                std::f64::consts::FRAC_1_SQRT_2,
                attribute,
            )
            .static_property(utf16!("SQRT2"), std::f64::consts::SQRT_2, attribute)
            .static_method(Self::abs, "abs", 1)
            .static_method(Self::acos, "acos", 1)
            .static_method(Self::acosh, "acosh", 1)
            .static_method(Self::asin, "asin", 1)
            .static_method(Self::asinh, "asinh", 1)
            .static_method(Self::atan, "atan", 1)
            .static_method(Self::atanh, "atanh", 1)
            .static_method(Self::atan2, "atan2", 2)
            .static_method(Self::cbrt, "cbrt", 1)
            .static_method(Self::ceil, "ceil", 1)
            .static_method(Self::clz32, "clz32", 1)
            .static_method(Self::cos, "cos", 1)
            .static_method(Self::cosh, "cosh", 1)
            .static_method(Self::exp, "exp", 1)
            .static_method(Self::expm1, "expm1", 1)
            .static_method(Self::floor, "floor", 1)
            .static_method(Self::fround, "fround", 1)
            .static_method(Self::hypot, "hypot", 2)
            .static_method(Self::imul, "imul", 2)
            .static_method(Self::log, "log", 1)
            .static_method(Self::log1p, "log1p", 1)
            .static_method(Self::log10, "log10", 1)
            .static_method(Self::log2, "log2", 1)
            .static_method(Self::max, "max", 2)
            .static_method(Self::min, "min", 2)
            .static_method(Self::pow, "pow", 2)
            .static_method(Self::random, "random", 0)
            .static_method(Self::round, "round", 1)
            .static_method(Self::sign, "sign", 1)
            .static_method(Self::sin, "sin", 1)
            .static_method(Self::sinh, "sinh", 1)
            .static_method(Self::sqrt, "sqrt", 1)
            .static_method(Self::tan, "tan", 1)
            .static_method(Self::tanh, "tanh", 1)
            .static_method(Self::trunc, "trunc", 1)
            .static_property(
                JsSymbol::to_string_tag(),
                Self::NAME,
                Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
            )
            .build();
    }

    fn get(intrinsics: &Intrinsics) -> JsObject {
        intrinsics.objects().math()
    }
}

impl BuiltInObject for Math {
    const NAME: &'static str = "Math";
}

impl 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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 3. If n is -0𝔽, return +0𝔽.
            // 2. If n is NaN, return NaN.
            // 4. If n is -βˆžπ”½, return +βˆžπ”½.
            // 5. If n < +0𝔽, return -n.
            // 6. Return n.
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n > 1𝔽, or n < -1𝔽, return NaN.
            // 3. If n is 1𝔽, return +0𝔽.
            // 4. Return an implementation-approximated value representing the result of the inverse cosine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 4. If n < 1𝔽, return NaN.
            // 2. If n is NaN or n is +βˆžπ”½, return n.
            // 3. If n is 1𝔽, return +0𝔽.
            // 5. Return an implementation-approximated value representing the result of the inverse hyperbolic cosine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
            // 3. If n > 1𝔽 or n < -1𝔽, return NaN.
            // 4. Return an implementation-approximated value representing the result of the inverse sine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +βˆžπ”½, or n is -βˆžπ”½, return n.
            // 3. Return an implementation-approximated value representing the result of the inverse hyperbolic sine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
            // 3. If n is +βˆžπ”½, return an implementation-approximated value representing Ο€ / 2.
            // 4. If n is -βˆžπ”½, return an implementation-approximated value representing -Ο€ / 2.
            // 5. Return an implementation-approximated value representing the result of the inverse tangent of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
            // 3. If n > 1𝔽 or n < -1𝔽, return NaN.
            // 4. If n is 1𝔽, return +βˆžπ”½.
            // 5. If n is -1𝔽, return -βˆžπ”½.
            // 6. Return an implementation-approximated value representing the result of the inverse hyperbolic tangent of ℝ(n).
            .atanh()
            .into())
    }

    /// Get the four quadrant arctangent of the quotient y / x.
    ///
    /// 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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let ny be ? ToNumber(y).
        let y = args.get_or_undefined(0).to_number(context)?;

        // 2. Let nx be ? ToNumber(x).
        let x = args.get_or_undefined(1).to_number(context)?;

        // 4. If ny is +βˆžπ”½, then
        // a. If nx is +βˆžπ”½, return an implementation-approximated value representing Ο€ / 4.
        // b. If nx is -βˆžπ”½, return an implementation-approximated value representing 3Ο€ / 4.
        // c. Return an implementation-approximated value representing Ο€ / 2.
        // 5. If ny is -βˆžπ”½, then
        // a. If nx is +βˆžπ”½, return an implementation-approximated value representing -Ο€ / 4.
        // b. If nx is -βˆžπ”½, return an implementation-approximated value representing -3Ο€ / 4.
        // c. Return an implementation-approximated value representing -Ο€ / 2.
        // 6. If ny is +0𝔽, then
        // a. If nx > +0𝔽 or nx is +0𝔽, return +0𝔽.
        // b. Return an implementation-approximated value representing Ο€.
        // 7. If ny is -0𝔽, then
        // a. If nx > +0𝔽 or nx is +0𝔽, return -0𝔽.
        // b. Return an implementation-approximated value representing -Ο€.
        // 8. Assert: ny is finite and is neither +0𝔽 nor -0𝔽.
        // 9. If ny > +0𝔽, then
        // a. If nx is +βˆžπ”½, return +0𝔽.
        // b. If nx is -βˆžπ”½, return an implementation-approximated value representing Ο€.
        // c. If nx is +0𝔽 or nx is -0𝔽, return an implementation-approximated value representing Ο€ / 2.
        // 10. If ny < +0𝔽, then
        // a. If nx is +βˆžπ”½, return -0𝔽.
        // b. If nx is -βˆžπ”½, return an implementation-approximated value representing -Ο€.
        // c. If nx is +0𝔽 or nx is -0𝔽, return an implementation-approximated value representing -Ο€ / 2.
        // 11. Assert: nx is finite and is neither +0𝔽 nor -0𝔽.
        // 12. Return an implementation-approximated value representing the result of the inverse tangent of the quotient ℝ(ny) / ℝ(nx).
        Ok(y.atan2(x).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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +βˆžπ”½, or n is -βˆžπ”½, return n.
            // 3. Return an implementation-approximated value representing the result of the cube root of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +βˆžπ”½, or n is -βˆžπ”½, return n.
            // 3. If n < +0𝔽 and n > -1𝔽, return -0𝔽.
            // 4. If n is an integral Number, return n.
            // 5. Return the smallest (closest to -∞) integral Number value that is not less than n.
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToUint32(x).
            .to_u32(context)?
            // 2. Let p be the number of leading zero bits in the unsigned 32-bit binary representation of n.
            // 3. Return 𝔽(p).
            .leading_zeros()
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +βˆžπ”½, or n is -βˆžπ”½, return NaN.
            // 3. If n is +0𝔽 or n is -0𝔽, return 1𝔽.
            // 4. Return an implementation-approximated value representing the result of the cosine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, return NaN.
            // 3. If n is +βˆžπ”½ or n is -βˆžπ”½, return +βˆžπ”½.
            // 4. If n is +0𝔽 or n is -0𝔽, return 1𝔽.
            // 5. Return an implementation-approximated value representing the result of the hyperbolic cosine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN or n is +βˆžπ”½, return n.
            // 3. If n is +0𝔽 or n is -0𝔽, return 1𝔽.
            // 4. If n is -βˆžπ”½, return +0𝔽.
            // 5. Return an implementation-approximated value representing the result of the exponential function of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, or n is +βˆžπ”½, return n.
            // 3. If n is -βˆžπ”½, return -1𝔽.
            // 4. Return an implementation-approximated value representing the result of subtracting 1 from the exponential function of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +βˆžπ”½, or n is -βˆžπ”½, return n.
            // 3. If n < 1𝔽 and n > +0𝔽, return +0𝔽.
            // 4. If n is an integral Number, return n.
            // 5. Return the greatest (closest to +∞) integral Number value that is not greater than n.
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let n be ? ToNumber(x).
        let x = args.get_or_undefined(0).to_number(context)?;

        // 2. If n is NaN, return NaN.
        // 3. If n is one of +0𝔽, -0𝔽, +βˆžπ”½, or -βˆžπ”½, return n.
        // 4. Let n32 be the result of converting n to a value in IEEE 754-2019 binary32 format using roundTiesToEven mode.
        // 5. Let n64 be the result of converting n32 to a value in IEEE 754-2019 binary64 format.
        // 6. Return the ECMAScript Number value corresponding to n64.
        Ok(f64::from(x as f32).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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let coerced be a new empty List.
        // 2. For each element arg of args, do
        // a. Let n be ? ToNumber(arg).
        // b. Append n to coerced.
        // 3. For each element number of coerced, do
        // 5. For each element number of coerced, do
        let mut result = 0f64;
        for arg in args {
            let num = arg.to_number(context)?;
            // a. If number is +βˆžπ”½ or number is -βˆžπ”½, return +βˆžπ”½.
            // 4. Let onlyZero be true.
            // a. If number is NaN, return NaN.
            // b. If number is neither +0𝔽 nor -0𝔽, set onlyZero to false.
            // 6. If onlyZero is true, return +0𝔽.
            // 7. Return an implementation-approximated value representing the square root of the sum of squares of the mathematical values of the elements of coerced.
            result = result.hypot(num);
        }

        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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let a be ℝ(? ToUint32(x)).
        let x = args.get_or_undefined(0).to_u32(context)?;

        // 2. Let b be ℝ(? ToUint32(y)).
        let y = args.get_or_undefined(1).to_u32(context)?;

        // 3. Let product be (a Γ— b) modulo 2^32.
        // 4. If product β‰₯ 2^31, return 𝔽(product - 2^32); otherwise return 𝔽(product).
        Ok((x.wrapping_mul(y) as i32).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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN or n is +βˆžπ”½, return n.
            // 3. If n is 1𝔽, return +0𝔽.
            // 4. If n is +0𝔽 or n is -0𝔽, return -βˆžπ”½.
            // 5. If n < +0𝔽, return NaN.
            // 6. Return an implementation-approximated value representing the result of the natural logarithm of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, or n is +βˆžπ”½, return n.
            // 3. If n is -1𝔽, return -βˆžπ”½.
            // 4. If n < -1𝔽, return NaN.
            // 5. Return an implementation-approximated value representing the result of the natural logarithm of 1 + ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN or n is +βˆžπ”½, return n.
            // 3. If n is 1𝔽, return +0𝔽.
            // 4. If n is +0𝔽 or n is -0𝔽, return -βˆžπ”½.
            // 5. If n < +0𝔽, return NaN.
            // 6. Return an implementation-approximated value representing the result of the base 10 logarithm of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN or n is +βˆžπ”½, return n.
            // 3. If n is 1𝔽, return +0𝔽.
            // 4. If n is +0𝔽 or n is -0𝔽, return -βˆžπ”½.
            // 5. If n < +0𝔽, return NaN.
            // 6. Return an implementation-approximated value representing the result of the base 2 logarithm of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let coerced be a new empty List.
        // 2. For each element arg of args, do
        // b. Append n to coerced.
        // 3. Let highest be -βˆžπ”½.
        let mut highest = f64::NEG_INFINITY;

        // 4. For each element number of coerced, do
        for arg in args {
            // a. Let n be ? ToNumber(arg).
            let num = arg.to_number(context)?;

            highest = if highest.is_nan() {
                continue;
            } else if num.is_nan() {
                // a. If number is NaN, return NaN.
                f64::NAN
            } else {
                match (highest, num) {
                    // b. When x and y are +0𝔽 -0𝔽, return +0𝔽.
                    (x, y) if x == 0f64 && y == 0f64 && x.signum() != y.signum() => 0f64,
                    // c. Otherwise, return the maximum value.
                    (x, y) => x.max(y),
                }
            };
        }
        // 5. Return highest.
        Ok(highest.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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let coerced be a new empty List.
        // 2. For each element arg of args, do
        // b. Append n to coerced.
        // 3. Let lowest be +βˆžπ”½.
        let mut lowest = f64::INFINITY;

        // 4. For each element number of coerced, do
        for arg in args {
            // a. Let n be ? ToNumber(arg).
            let num = arg.to_number(context)?;

            lowest = if lowest.is_nan() {
                continue;
            } else if num.is_nan() {
                // a. If number is NaN, return NaN.
                f64::NAN
            } else {
                match (lowest, num) {
                    // b. When x and y are +0𝔽 -0𝔽, return -0𝔽.
                    (x, y) if x == 0f64 && y == 0f64 && x.signum() != y.signum() => -0f64,
                    // c. Otherwise, return the minimum value.
                    (x, y) => x.min(y),
                }
            };
        }
        // 5. Return lowest.
        Ok(lowest.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
    #[allow(clippy::float_cmp)]
    pub(crate) fn pow(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Set base to ? ToNumber(base).
        let x = args.get_or_undefined(0).to_number(context)?;

        // 2. Set exponent to ? ToNumber(exponent).
        let y = args.get_or_undefined(1).to_number(context)?;

        // 3. If |x| = 1 and the exponent is infinite, return NaN.
        if f64::abs(x) == 1f64 && y.is_infinite() {
            return Ok(f64::NAN.into());
        }

        // 4. Return ! Number::exponentiate(base, exponent).
        Ok(x.powf(y).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
    #[allow(clippy::unnecessary_wraps)]
    pub(crate) fn random(_: &JsValue, _: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
        // NOTE: Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls.
        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
    #[allow(clippy::float_cmp)]
    pub(crate) fn round(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        let num = args
            .get_or_undefined(0)
            //1. Let n be ? ToNumber(x).
            .to_number(context)?;

        //2. If n is NaN, +βˆžπ”½, -βˆžπ”½, or an integral Number, return n.
        //3. If n < 0.5𝔽 and n > +0𝔽, return +0𝔽.
        //4. If n < +0𝔽 and n β‰₯ -0.5𝔽, return -0𝔽.
        //5. Return the integral Number closest to n, preferring the Number closer to +∞ in the case of a tie.

        if num.fract() == -0.5 {
            Ok(num.ceil().into())
        } else {
            Ok(num.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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        // 1. Let n be ? ToNumber(x).
        let n = args.get_or_undefined(0).to_number(context)?;

        // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
        if n == 0f64 {
            return Ok(n.into());
        }
        // 3. If n < +0𝔽, return -1𝔽.
        // 4. Return 1𝔽.
        Ok(n.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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
            // 3. If n is +βˆžπ”½ or n is -βˆžπ”½, return NaN.
            // 4. Return an implementation-approximated value representing the result of the sine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +βˆžπ”½, or n is -βˆžπ”½, return n.
            // 3. Return an implementation-approximated value representing the result of the hyperbolic sine of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, or n is +βˆžπ”½, return n.
            // 3. If n < +0𝔽, return NaN.
            // 4. Return an implementation-approximated value representing the result of the square root of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
            // 3. If n is +βˆžπ”½, or n is -βˆžπ”½, return NaN.
            // 4. Return an implementation-approximated value representing the result of the tangent of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
            // 3. If n is +βˆžπ”½, return 1𝔽.
            // 4. If n is -βˆžπ”½, return -1𝔽.
            // 5. Return an implementation-approximated value representing the result of the hyperbolic tangent of ℝ(n).
            .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(
        _: &JsValue,
        args: &[JsValue],
        context: &mut Context<'_>,
    ) -> JsResult<JsValue> {
        Ok(args
            .get_or_undefined(0)
            // 1. Let n be ? ToNumber(x).
            .to_number(context)?
            // 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +βˆžπ”½, or n is -βˆžπ”½, return n.
            // 3. If n < 1𝔽 and n > +0𝔽, return +0𝔽.
            // 4. If n < +0𝔽 and n > -1𝔽, return -0𝔽.
            // 5. Return the integral Number nearest n in the direction of +0𝔽.
            .trunc()
            .into())
    }
}