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
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
|
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/AllOf.h>
#include <AK/Assertions.h>
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/FloatingPointStringConversions.h>
#include <AK/StringBuilder.h>
#include <AK/StringFloatingPointConversions.h>
#include <AK/Utf8View.h>
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibCrypto/NumberTheory/ModularFunctions.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/BigIntObject.h>
#include <LibJS/Runtime/BooleanObject.h>
#include <LibJS/Runtime/BoundFunction.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/NumberObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/ProxyObject.h>
#include <LibJS/Runtime/RegExpObject.h>
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/Runtime/SymbolObject.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
#include <math.h>
namespace JS {
static inline bool same_type_for_equality(Value const& lhs, Value const& rhs)
{
// If the top two bytes are identical then either:
// both are NaN boxed Values with the same type
// or they are doubles which happen to have the same top bytes.
if ((lhs.encoded() & TAG_EXTRACTION) == (rhs.encoded() & TAG_EXTRACTION))
return true;
if (lhs.is_number() && rhs.is_number())
return true;
// One of the Values is not a number and they do not have the same tag
return false;
}
static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
ALWAYS_INLINE bool both_number(Value const& lhs, Value const& rhs)
{
return lhs.is_number() && rhs.is_number();
}
ALWAYS_INLINE bool both_bigint(Value const& lhs, Value const& rhs)
{
return lhs.is_bigint() && rhs.is_bigint();
}
// 6.1.6.1.20 Number::toString ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-tostring
// Implementation for radix = 10
DeprecatedString number_to_string(double d, NumberToStringMode mode)
{
auto convert_to_decimal_digits_array = [](auto x, auto& digits, auto& length) {
for (; x; x /= 10)
digits[length++] = x % 10 | '0';
for (i32 i = 0; 2 * i + 1 < length; ++i)
swap(digits[i], digits[length - i - 1]);
};
// 1. If x is NaN, return "NaN".
if (isnan(d))
return "NaN";
// 2. If x is +0๐ฝ or -0๐ฝ, return "0".
if (d == +0.0 || d == -0.0)
return "0";
// 4. If x is +โ๐ฝ, return "Infinity".
if (isinf(d)) {
if (d > 0)
return "Infinity";
else
return "-Infinity";
}
StringBuilder builder;
// 5. Let n, k, and s be integers such that k โฅ 1, radix ^ (k - 1) โค s < radix ^ k,
// ๐ฝ(s ร radix ^ (n - k)) is x, and k is as small as possible. Note that k is the number of
// digits in the representation of s using radix radix, that s is not divisible by radix, and
// that the least significant digit of s is not necessarily uniquely determined by these criteria.
//
// Note: guarantees provided by convert_floating_point_to_decimal_exponential_form satisfy
// requirements of NOTE 2.
auto [sign, mantissa, exponent] = convert_floating_point_to_decimal_exponential_form(d);
i32 k = 0;
AK::Array<char, 20> mantissa_digits;
convert_to_decimal_digits_array(mantissa, mantissa_digits, k);
i32 n = exponent + k; // s = mantissa
// 3. If x < -0๐ฝ, return the string-concatenation of "-" and Number::toString(-x, radix).
if (sign)
builder.append('-');
// Non-standard: Intl needs number-to-string conversions for extremely large numbers without any
// exponential formatting, as it will handle such formatting itself in a locale-aware way.
bool force_no_exponent = mode == NumberToStringMode::WithoutExponent;
// 6. If radix โ 10 or n is in the inclusive interval from -5 to 21, then
if ((n >= -5 && n <= 21) || force_no_exponent) {
// a. If n โฅ k, then
if (n >= k) {
// i. Return the string-concatenation of:
// the code units of the k digits of the representation of s using radix radix
builder.append(mantissa_digits.data(), k);
// n - k occurrences of the code unit 0x0030 (DIGIT ZERO)
builder.append_repeated('0', n - k);
// b. Else if n > 0, then
} else if (n > 0) {
// i. Return the string-concatenation of:
// the code units of the most significant n digits of the representation of s using radix radix
builder.append(mantissa_digits.data(), n);
// the code unit 0x002E (FULL STOP)
builder.append('.');
// the code units of the remaining k - n digits of the representation of s using radix radix
builder.append(mantissa_digits.data() + n, k - n);
// c. Else,
} else {
// i. Assert: n โค 0.
VERIFY(n <= 0);
// ii. Return the string-concatenation of:
// the code unit 0x0030 (DIGIT ZERO)
builder.append('0');
// the code unit 0x002E (FULL STOP)
builder.append('.');
// -n occurrences of the code unit 0x0030 (DIGIT ZERO)
builder.append_repeated('0', -n);
// the code units of the k digits of the representation of s using radix radix
builder.append(mantissa_digits.data(), k);
}
return builder.to_deprecated_string();
}
// 7. NOTE: In this case, the input will be represented using scientific E notation, such as 1.2e+3.
// 9. If n < 0, then
// a. Let exponentSign be the code unit 0x002D (HYPHEN-MINUS).
// 10. Else,
// a. Let exponentSign be the code unit 0x002B (PLUS SIGN).
char exponent_sign = n < 0 ? '-' : '+';
AK::Array<char, 5> exponent_digits;
i32 exponent_length = 0;
convert_to_decimal_digits_array(abs(n - 1), exponent_digits, exponent_length);
// 11. If k is 1, then
if (k == 1) {
// a. Return the string-concatenation of:
// the code unit of the single digit of s
builder.append(mantissa_digits[0]);
// the code unit 0x0065 (LATIN SMALL LETTER E)
builder.append('e');
// exponentSign
builder.append(exponent_sign);
// the code units of the decimal representation of abs(n - 1)
builder.append(exponent_digits.data(), exponent_length);
return builder.to_deprecated_string();
}
// 12. Return the string-concatenation of:
// the code unit of the most significant digit of the decimal representation of s
builder.append(mantissa_digits[0]);
// the code unit 0x002E (FULL STOP)
builder.append('.');
// the code units of the remaining k - 1 digits of the decimal representation of s
builder.append(mantissa_digits.data() + 1, k - 1);
// the code unit 0x0065 (LATIN SMALL LETTER E)
builder.append('e');
// exponentSign
builder.append(exponent_sign);
// the code units of the decimal representation of abs(n - 1)
builder.append(exponent_digits.data(), exponent_length);
return builder.to_deprecated_string();
}
// 7.2.2 IsArray ( argument ), https://tc39.es/ecma262/#sec-isarray
ThrowCompletionOr<bool> Value::is_array(VM& vm) const
{
// 1. If argument is not an Object, return false.
if (!is_object())
return false;
auto const& object = as_object();
// 2. If argument is an Array exotic object, return true.
if (is<Array>(object))
return true;
// 3. If argument is a Proxy exotic object, then
if (is<ProxyObject>(object)) {
auto const& proxy = static_cast<ProxyObject const&>(object);
// a. If argument.[[ProxyHandler]] is null, throw a TypeError exception.
if (proxy.is_revoked())
return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
// b. Let target be argument.[[ProxyTarget]].
auto const& target = proxy.target();
// c. Return ? IsArray(target).
return Value(&target).is_array(vm);
}
// 4. Return false.
return false;
}
Array& Value::as_array()
{
VERIFY(is_object() && is<Array>(as_object()));
return static_cast<Array&>(as_object());
}
// 7.2.3 IsCallable ( argument ), https://tc39.es/ecma262/#sec-iscallable
bool Value::is_function() const
{
// 1. If argument is not an Object, return false.
// 2. If argument has a [[Call]] internal method, return true.
// 3. Return false.
return is_object() && as_object().is_function();
}
FunctionObject& Value::as_function()
{
VERIFY(is_function());
return static_cast<FunctionObject&>(as_object());
}
FunctionObject const& Value::as_function() const
{
VERIFY(is_function());
return static_cast<FunctionObject const&>(as_object());
}
// 7.2.4 IsConstructor ( argument ), https://tc39.es/ecma262/#sec-isconstructor
bool Value::is_constructor() const
{
// 1. If Type(argument) is not Object, return false.
if (!is_function())
return false;
// 2. If argument has a [[Construct]] internal method, return true.
if (as_function().has_constructor())
return true;
// 3. Return false.
return false;
}
// 7.2.8 IsRegExp ( argument ), https://tc39.es/ecma262/#sec-isregexp
ThrowCompletionOr<bool> Value::is_regexp(VM& vm) const
{
// 1. If argument is not an Object, return false.
if (!is_object())
return false;
// 2. Let matcher be ? Get(argument, @@match).
auto matcher = TRY(as_object().get(*vm.well_known_symbol_match()));
// 3. If matcher is not undefined, return ToBoolean(matcher).
if (!matcher.is_undefined())
return matcher.to_boolean();
// 4. If argument has a [[RegExpMatcher]] internal slot, return true.
// 5. Return false.
return is<RegExpObject>(as_object());
}
// 13.5.3 The typeof Operator, https://tc39.es/ecma262/#sec-typeof-operator
DeprecatedString Value::typeof() const
{
// 9. If val is a Number, return "number".
if (is_number())
return "number";
switch (m_value.tag) {
// 4. If val is undefined, return "undefined".
case UNDEFINED_TAG:
return "undefined";
// 5. If val is null, return "object".
case NULL_TAG:
return "object";
// 6. If val is a String, return "string".
case STRING_TAG:
return "string";
// 7. If val is a Symbol, return "symbol".
case SYMBOL_TAG:
return "symbol";
// 8. If val is a Boolean, return "boolean".
case BOOLEAN_TAG:
return "boolean";
// 10. If val is a BigInt, return "bigint".
case BIGINT_TAG:
return "bigint";
// 11. Assert: val is an Object.
case OBJECT_TAG:
// B.3.6.3 Changes to the typeof Operator, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-typeof
// 12. If val has an [[IsHTMLDDA]] internal slot, return "undefined".
if (as_object().is_htmldda())
return "undefined";
// 13. If val has a [[Call]] internal slot, return "function".
if (is_function())
return "function";
// 14. Return "object".
return "object";
default:
VERIFY_NOT_REACHED();
}
}
DeprecatedString Value::to_string_without_side_effects() const
{
if (is_double())
return number_to_string(m_value.as_double);
switch (m_value.tag) {
case UNDEFINED_TAG:
return "undefined";
case NULL_TAG:
return "null";
case BOOLEAN_TAG:
return as_bool() ? "true" : "false";
case INT32_TAG:
return DeprecatedString::number(as_i32());
case STRING_TAG:
return as_string().deprecated_string();
case SYMBOL_TAG:
return as_symbol().to_deprecated_string();
case BIGINT_TAG:
return as_bigint().to_deprecated_string();
case OBJECT_TAG:
return DeprecatedString::formatted("[object {}]", as_object().class_name());
case ACCESSOR_TAG:
return "<accessor>";
default:
VERIFY_NOT_REACHED();
}
}
ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(VM& vm)
{
if (is_string())
return &as_string();
auto string = TRY(to_string(vm));
return PrimitiveString::create(vm, string).ptr();
}
// 7.1.17 ToString ( argument ), https://tc39.es/ecma262/#sec-tostring
ThrowCompletionOr<DeprecatedString> Value::to_string(VM& vm) const
{
if (is_double())
return number_to_string(m_value.as_double);
switch (m_value.tag) {
// 1. If argument is a String, return argument.
case STRING_TAG:
return as_string().deprecated_string();
// 2. If argument is a Symbol, throw a TypeError exception.
case SYMBOL_TAG:
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "string");
// 3. If argument is undefined, return "undefined".
case UNDEFINED_TAG:
return "undefined"sv;
// 4. If argument is null, return "null".
case NULL_TAG:
return "null"sv;
// 5. If argument is true, return "true".
// 6. If argument is false, return "false".
case BOOLEAN_TAG:
return as_bool() ? "true"sv : "false"sv;
// 7. If argument is a Number, return Number::toString(argument, 10).
case INT32_TAG:
return DeprecatedString::number(as_i32());
// 8. If argument is a BigInt, return BigInt::toString(argument, 10).
case BIGINT_TAG:
return as_bigint().big_integer().to_base(10);
// 9. Assert: argument is an Object.
case OBJECT_TAG: {
// 10. Let primValue be ? ToPrimitive(argument, string).
auto primitive_value = TRY(to_primitive(vm, PreferredType::String));
// 11. Assert: primValue is not an Object.
VERIFY(!primitive_value.is_object());
// 12. Return ? ToString(primValue).
return primitive_value.to_string(vm);
}
default:
VERIFY_NOT_REACHED();
}
}
ThrowCompletionOr<Utf16String> Value::to_utf16_string(VM& vm) const
{
if (is_string())
return as_string().utf16_string();
auto utf8_string = TRY(to_string(vm));
return Utf16String(utf8_string);
}
// 7.1.2 ToBoolean ( argument ), https://tc39.es/ecma262/#sec-toboolean
bool Value::to_boolean() const
{
if (is_double()) {
if (is_nan())
return false;
return m_value.as_double != 0;
}
switch (m_value.tag) {
// 1. If argument is a Boolean, return argument.
case BOOLEAN_TAG:
return as_bool();
// 2. If argument is any of undefined, null, +0๐ฝ, -0๐ฝ, NaN, 0โค, or the empty String, return false.
case UNDEFINED_TAG:
case NULL_TAG:
return false;
case INT32_TAG:
return as_i32() != 0;
case STRING_TAG:
return !as_string().is_empty();
case BIGINT_TAG:
return as_bigint().big_integer() != BIGINT_ZERO;
case OBJECT_TAG:
// B.3.6.1 Changes to ToBoolean, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-to-boolean
// 3. If argument is an Object and argument has an [[IsHTMLDDA]] internal slot, return false.
if (as_object().is_htmldda())
return false;
// 4. Return true.
return true;
case SYMBOL_TAG:
return true;
default:
VERIFY_NOT_REACHED();
}
}
// 7.1.1 ToPrimitive ( input [ , preferredType ] ), https://tc39.es/ecma262/#sec-toprimitive
ThrowCompletionOr<Value> Value::to_primitive(VM& vm, PreferredType preferred_type) const
{
// 1. If input is an Object, then
if (is_object()) {
// a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
auto* exotic_to_primitive = TRY(get_method(vm, *vm.well_known_symbol_to_primitive()));
// b. If exoticToPrim is not undefined, then
if (exotic_to_primitive) {
auto hint = [&]() -> DeprecatedString {
switch (preferred_type) {
// i. If preferredType is not present, let hint be "default".
case PreferredType::Default:
return "default";
// ii. Else if preferredType is string, let hint be "string".
case PreferredType::String:
return "string";
// iii. Else,
// 1. Assert: preferredType is number.
// 2. Let hint be "number".
case PreferredType::Number:
return "number";
default:
VERIFY_NOT_REACHED();
}
}();
// iv. Let result be ? Call(exoticToPrim, input, ยซ hint ยป).
auto result = TRY(call(vm, *exotic_to_primitive, *this, PrimitiveString::create(vm, hint)));
// v. If result is not an Object, return result.
if (!result.is_object())
return result;
// vi. Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::ToPrimitiveReturnedObject, to_string_without_side_effects(), hint);
}
// c. If preferredType is not present, let preferredType be number.
if (preferred_type == PreferredType::Default)
preferred_type = PreferredType::Number;
// d. Return ? OrdinaryToPrimitive(input, preferredType).
return as_object().ordinary_to_primitive(preferred_type);
}
// 2. Return input.
return *this;
}
// 7.1.18 ToObject ( argument ), https://tc39.es/ecma262/#sec-toobject
ThrowCompletionOr<Object*> Value::to_object(VM& vm) const
{
auto& realm = *vm.current_realm();
VERIFY(!is_empty());
// Number
if (is_number()) {
// Return a new Number object whose [[NumberData]] internal slot is set to argument. See 21.1 for a description of Number objects.
return NumberObject::create(realm, as_double());
}
switch (m_value.tag) {
// Undefined
// Null
case UNDEFINED_TAG:
case NULL_TAG:
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefined);
// Boolean
case BOOLEAN_TAG:
// Return a new Boolean object whose [[BooleanData]] internal slot is set to argument. See 20.3 for a description of Boolean objects.
return BooleanObject::create(realm, as_bool());
// String
case STRING_TAG:
// Return a new String object whose [[StringData]] internal slot is set to argument. See 22.1 for a description of String objects.
return StringObject::create(realm, const_cast<JS::PrimitiveString&>(as_string()), *realm.intrinsics().string_prototype());
// Symbol
case SYMBOL_TAG:
// Return a new Symbol object whose [[SymbolData]] internal slot is set to argument. See 20.4 for a description of Symbol objects.
return SymbolObject::create(realm, const_cast<JS::Symbol&>(as_symbol()));
// BigInt
case BIGINT_TAG:
// Return a new BigInt object whose [[BigIntData]] internal slot is set to argument. See 21.2 for a description of BigInt objects.
return BigIntObject::create(realm, const_cast<JS::BigInt&>(as_bigint()));
// Object
case OBJECT_TAG:
// Return argument.
return &const_cast<Object&>(as_object());
default:
VERIFY_NOT_REACHED();
}
}
// 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric
FLATTEN ThrowCompletionOr<Value> Value::to_numeric(VM& vm) const
{
// 1. Let primValue be ? ToPrimitive(value, number).
auto primitive_value = TRY(to_primitive(vm, Value::PreferredType::Number));
// 2. If primValue is a BigInt, return primValue.
if (primitive_value.is_bigint())
return primitive_value;
// 3. Return ? ToNumber(primValue).
return primitive_value.to_number(vm);
}
constexpr bool is_ascii_number(u32 code_point)
{
return is_ascii_digit(code_point) || code_point == '.' || (code_point == 'e' || code_point == 'E') || code_point == '+' || code_point == '-';
}
struct NumberParseResult {
StringView literal;
u8 base;
};
static Optional<NumberParseResult> parse_number_text(StringView text)
{
NumberParseResult result {};
auto check_prefix = [&](auto lower_prefix, auto upper_prefix) {
if (text.length() <= 2)
return false;
if (!text.starts_with(lower_prefix) && !text.starts_with(upper_prefix))
return false;
return true;
};
// https://tc39.es/ecma262/#sec-tonumber-applied-to-the-string-type
if (check_prefix("0b"sv, "0B"sv)) {
if (!all_of(text.substring_view(2), is_ascii_binary_digit))
return {};
result.literal = text.substring_view(2);
result.base = 2;
} else if (check_prefix("0o"sv, "0O"sv)) {
if (!all_of(text.substring_view(2), is_ascii_octal_digit))
return {};
result.literal = text.substring_view(2);
result.base = 8;
} else if (check_prefix("0x"sv, "0X"sv)) {
if (!all_of(text.substring_view(2), is_ascii_hex_digit))
return {};
result.literal = text.substring_view(2);
result.base = 16;
} else {
if (!all_of(text, is_ascii_number))
return {};
result.literal = text;
result.base = 10;
}
return result;
}
// 7.1.4.1.1 StringToNumber ( str ), https://tc39.es/ecma262/#sec-stringtonumber
Optional<Value> string_to_number(StringView string)
{
// 1. Let text be StringToCodePoints(str).
DeprecatedString text = Utf8View(string).trim(whitespace_characters, AK::TrimMode::Both).as_string();
// 2. Let literal be ParseText(text, StringNumericLiteral).
if (text.is_empty())
return Value(0);
if (text == "Infinity" || text == "+Infinity")
return js_infinity();
if (text == "-Infinity")
return js_negative_infinity();
auto result = parse_number_text(text);
// 3. If literal is a List of errors, return NaN.
if (!result.has_value())
return js_nan();
// 4. Return StringNumericValue of literal.
if (result->base != 10) {
auto bigint = Crypto::UnsignedBigInteger::from_base(result->base, result->literal);
return Value(bigint.to_double());
}
auto maybe_double = text.to_double(AK::TrimWhitespace::No);
if (!maybe_double.has_value())
return js_nan();
return Value(*maybe_double);
}
// 7.1.4 ToNumber ( argument ), https://tc39.es/ecma262/#sec-tonumber
ThrowCompletionOr<Value> Value::to_number(VM& vm) const
{
VERIFY(!is_empty());
// 1. If argument is a Number, return argument.
if (is_number())
return *this;
switch (m_value.tag) {
// 2. If argument is either a Symbol or a BigInt, throw a TypeError exception.
case SYMBOL_TAG:
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "number");
case BIGINT_TAG:
return vm.throw_completion<TypeError>(ErrorType::Convert, "BigInt", "number");
// 3. If argument is undefined, return NaN.
case UNDEFINED_TAG:
return js_nan();
// 4. If argument is either null or false, return +0๐ฝ.
case NULL_TAG:
return Value(0);
// 5. If argument is true, return 1๐ฝ.
case BOOLEAN_TAG:
return Value(as_bool() ? 1 : 0);
// 6. If argument is a String, return StringToNumber(argument).
case STRING_TAG:
return string_to_number(as_string().deprecated_string().view());
// 7. Assert: argument is an Object.
case OBJECT_TAG: {
// 8. Let primValue be ? ToPrimitive(argument, number).
auto primitive_value = TRY(to_primitive(vm, PreferredType::Number));
// 9. Assert: primValue is not an Object.
VERIFY(!primitive_value.is_object());
// 10. Return ? ToNumber(primValue).
return primitive_value.to_number(vm);
}
default:
VERIFY_NOT_REACHED();
}
}
static Optional<BigInt*> string_to_bigint(VM& vm, StringView string);
// 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint
ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
{
// 1. Let prim be ? ToPrimitive(argument, number).
auto primitive = TRY(to_primitive(vm, PreferredType::Number));
// 2. Return the value that prim corresponds to in Table 12.
// Number
if (primitive.is_number()) {
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "number", "BigInt");
}
switch (primitive.m_value.tag) {
// Undefined
case UNDEFINED_TAG:
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "undefined", "BigInt");
// Null
case NULL_TAG:
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "null", "BigInt");
// Boolean
case BOOLEAN_TAG: {
// Return 1n if prim is true and 0n if prim is false.
auto value = primitive.as_bool() ? 1 : 0;
return BigInt::create(vm, Crypto::SignedBigInteger { value }).ptr();
}
// BigInt
case BIGINT_TAG:
// Return prim.
return &primitive.as_bigint();
case STRING_TAG: {
// 1. Let n be ! StringToBigInt(prim).
auto bigint = string_to_bigint(vm, primitive.as_string().deprecated_string());
// 2. If n is undefined, throw a SyntaxError exception.
if (!bigint.has_value())
return vm.throw_completion<SyntaxError>(ErrorType::BigIntInvalidValue, primitive);
// 3. Return n.
return bigint.release_value();
}
// Symbol
case SYMBOL_TAG:
// Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "BigInt");
default:
VERIFY_NOT_REACHED();
}
}
struct BigIntParseResult {
StringView literal;
u8 base { 10 };
bool is_negative { false };
};
static Optional<BigIntParseResult> parse_bigint_text(StringView text)
{
BigIntParseResult result {};
auto parse_for_prefixed_base = [&](auto lower_prefix, auto upper_prefix, auto validator) {
if (text.length() <= 2)
return false;
if (!text.starts_with(lower_prefix) && !text.starts_with(upper_prefix))
return false;
return all_of(text.substring_view(2), validator);
};
if (parse_for_prefixed_base("0b"sv, "0B"sv, is_ascii_binary_digit)) {
result.literal = text.substring_view(2);
result.base = 2;
} else if (parse_for_prefixed_base("0o"sv, "0O"sv, is_ascii_octal_digit)) {
result.literal = text.substring_view(2);
result.base = 8;
} else if (parse_for_prefixed_base("0x"sv, "0X"sv, is_ascii_hex_digit)) {
result.literal = text.substring_view(2);
result.base = 16;
} else {
if (text.starts_with('-')) {
text = text.substring_view(1);
result.is_negative = true;
} else if (text.starts_with('+')) {
text = text.substring_view(1);
}
if (!all_of(text, is_ascii_digit))
return {};
result.literal = text;
result.base = 10;
}
return result;
}
// 7.1.14 StringToBigInt ( str ), https://tc39.es/ecma262/#sec-stringtobigint
static Optional<BigInt*> string_to_bigint(VM& vm, StringView string)
{
// 1. Let text be StringToCodePoints(str).
auto text = Utf8View(string).trim(whitespace_characters, AK::TrimMode::Both).as_string();
// 2. Let literal be ParseText(text, StringIntegerLiteral).
auto result = parse_bigint_text(text);
// 3. If literal is a List of errors, return undefined.
if (!result.has_value())
return {};
// 4. Let mv be the MV of literal.
// 5. Assert: mv is an integer.
auto bigint = Crypto::SignedBigInteger::from_base(result->base, result->literal);
if (result->is_negative && (bigint != BIGINT_ZERO))
bigint.negate();
// 6. Return โค(mv).
return BigInt::create(vm, move(bigint));
}
// 7.1.15 ToBigInt64 ( argument ), https://tc39.es/ecma262/#sec-tobigint64
ThrowCompletionOr<i64> Value::to_bigint_int64(VM& vm) const
{
// 1. Let n be ? ToBigInt(argument).
auto* bigint = TRY(to_bigint(vm));
// 2. Let int64bit be โ(n) modulo 2^64.
// 3. If int64bit โฅ 2^63, return โค(int64bit - 2^64); otherwise return โค(int64bit).
return static_cast<i64>(bigint->big_integer().to_u64());
}
// 7.1.16 ToBigUint64 ( argument ), https://tc39.es/ecma262/#sec-tobiguint64
ThrowCompletionOr<u64> Value::to_bigint_uint64(VM& vm) const
{
// 1. Let n be ? ToBigInt(argument).
auto* bigint = TRY(to_bigint(vm));
// 2. Let int64bit be โ(n) modulo 2^64.
// 3. Return โค(int64bit).
return bigint->big_integer().to_u64();
}
ThrowCompletionOr<double> Value::to_double(VM& vm) const
{
return TRY(to_number(vm)).as_double();
}
// 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey
ThrowCompletionOr<PropertyKey> Value::to_property_key(VM& vm) const
{
// OPTIMIZATION: Return the value as a numeric PropertyKey, if possible.
if (is_int32() && as_i32() >= 0)
return PropertyKey { as_i32() };
// 1. Let key be ? ToPrimitive(argument, string).
auto key = TRY(to_primitive(vm, PreferredType::String));
// 2. If key is a Symbol, then
if (key.is_symbol()) {
// a. Return key.
return &key.as_symbol();
}
// 3. Return ! ToString(key).
return MUST(key.to_string(vm));
}
// 7.1.6 ToInt32 ( argument ), https://tc39.es/ecma262/#sec-toint32
ThrowCompletionOr<i32> Value::to_i32_slow_case(VM& vm) const
{
VERIFY(!is_int32());
// 1. Let number be ? ToNumber(argument).
double number = TRY(to_number(vm)).as_double();
// 2. If number is not finite or number is either +0๐ฝ or -0๐ฝ, return +0๐ฝ.
if (!isfinite(number) || number == 0)
return 0;
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(โ(number))).
auto abs = fabs(number);
auto int_val = floor(abs);
if (signbit(number))
int_val = -int_val;
// 4. Let int32bit be int modulo 2^32.
auto remainder = fmod(int_val, 4294967296.0);
auto int32bit = remainder >= 0.0 ? remainder : remainder + 4294967296.0; // The notation โx modulo yโ computes a value k of the same sign as y
// 5. If int32bit โฅ 2^31, return ๐ฝ(int32bit - 2^32); otherwise return ๐ฝ(int32bit).
if (int32bit >= 2147483648.0)
int32bit -= 4294967296.0;
return static_cast<i32>(int32bit);
}
// 7.1.6 ToInt32 ( argument ), https://tc39.es/ecma262/#sec-toint32
ThrowCompletionOr<i32> Value::to_i32(VM& vm) const
{
if (is_int32())
return as_i32();
return to_i32_slow_case(vm);
}
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
ThrowCompletionOr<u32> Value::to_u32(VM& vm) const
{
// 1. Let number be ? ToNumber(argument).
double number = TRY(to_number(vm)).as_double();
// 2. If number is not finite or number is either +0๐ฝ or -0๐ฝ, return +0๐ฝ.
if (!isfinite(number) || number == 0)
return 0;
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(โ(number))).
auto int_val = floor(fabs(number));
if (signbit(number))
int_val = -int_val;
// 4. Let int32bit be int modulo 2^32.
auto int32bit = fmod(int_val, NumericLimits<u32>::max() + 1.0);
// 5. Return ๐ฝ(int32bit).
// Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior
// Otherwise, negative numbers cause a UBSAN warning.
return static_cast<u32>(static_cast<i64>(int32bit));
}
// 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
ThrowCompletionOr<i16> Value::to_i16(VM& vm) const
{
double value = TRY(to_number(vm)).as_double();
if (!isfinite(value) || value == 0)
return 0;
auto abs = fabs(value);
auto int_val = floor(abs);
if (signbit(value))
int_val = -int_val;
auto remainder = fmod(int_val, 65536.0);
auto int16bit = remainder >= 0.0 ? remainder : remainder + 65536.0; // The notation โx modulo yโ computes a value k of the same sign as y
if (int16bit >= 32768.0)
int16bit -= 65536.0;
return static_cast<i16>(int16bit);
}
// 7.1.9 ToUint16 ( argument ), https://tc39.es/ecma262/#sec-touint16
ThrowCompletionOr<u16> Value::to_u16(VM& vm) const
{
double value = TRY(to_number(vm)).as_double();
if (!isfinite(value) || value == 0)
return 0;
auto int_val = floor(fabs(value));
if (signbit(value))
int_val = -int_val;
auto int16bit = fmod(int_val, NumericLimits<u16>::max() + 1.0);
if (int16bit < 0)
int16bit += NumericLimits<u16>::max() + 1.0;
return static_cast<u16>(int16bit);
}
// 7.1.10 ToInt8 ( argument ), https://tc39.es/ecma262/#sec-toint8
ThrowCompletionOr<i8> Value::to_i8(VM& vm) const
{
double value = TRY(to_number(vm)).as_double();
if (!isfinite(value) || value == 0)
return 0;
auto abs = fabs(value);
auto int_val = floor(abs);
if (signbit(value))
int_val = -int_val;
auto remainder = fmod(int_val, 256.0);
auto int8bit = remainder >= 0.0 ? remainder : remainder + 256.0; // The notation โx modulo yโ computes a value k of the same sign as y
if (int8bit >= 128.0)
int8bit -= 256.0;
return static_cast<i8>(int8bit);
}
// 7.1.11 ToUint8 ( argument ), https://tc39.es/ecma262/#sec-touint8
ThrowCompletionOr<u8> Value::to_u8(VM& vm) const
{
double value = TRY(to_number(vm)).as_double();
if (!isfinite(value) || value == 0)
return 0;
auto int_val = floor(fabs(value));
if (signbit(value))
int_val = -int_val;
auto int8bit = fmod(int_val, NumericLimits<u8>::max() + 1.0);
if (int8bit < 0)
int8bit += NumericLimits<u8>::max() + 1.0;
return static_cast<u8>(int8bit);
}
// 7.1.12 ToUint8Clamp ( argument ), https://tc39.es/ecma262/#sec-touint8clamp
ThrowCompletionOr<u8> Value::to_u8_clamp(VM& vm) const
{
auto number = TRY(to_number(vm));
if (number.is_nan())
return 0;
double value = number.as_double();
if (value <= 0.0)
return 0;
if (value >= 255.0)
return 255;
auto int_val = floor(value);
if (int_val + 0.5 < value)
return static_cast<u8>(int_val + 1.0);
if (value < int_val + 0.5)
return static_cast<u8>(int_val);
if (fmod(int_val, 2.0) == 1.0)
return static_cast<u8>(int_val + 1.0);
return static_cast<u8>(int_val);
}
// 7.1.20 ToLength ( argument ), https://tc39.es/ecma262/#sec-tolength
ThrowCompletionOr<size_t> Value::to_length(VM& vm) const
{
auto len = TRY(to_integer_or_infinity(vm));
if (len <= 0)
return 0;
// FIXME: The spec says that this function's output range is 0 - 2^53-1. But we don't want to overflow the size_t.
constexpr double length_limit = sizeof(void*) == 4 ? NumericLimits<size_t>::max() : MAX_ARRAY_LIKE_INDEX;
return min(len, length_limit);
}
// 7.1.22 ToIndex ( argument ), https://tc39.es/ecma262/#sec-toindex
ThrowCompletionOr<size_t> Value::to_index(VM& vm) const
{
if (is_undefined())
return 0;
auto integer_index = TRY(to_integer_or_infinity(vm));
if (integer_index < 0)
return vm.throw_completion<RangeError>(ErrorType::InvalidIndex);
auto index = MUST(Value(integer_index).to_length(vm));
if (integer_index != index)
return vm.throw_completion<RangeError>(ErrorType::InvalidIndex);
return index;
}
// 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity
ThrowCompletionOr<double> Value::to_integer_or_infinity(VM& vm) const
{
auto number = TRY(to_number(vm));
if (number.is_nan() || number.as_double() == 0)
return 0;
if (number.is_infinity())
return number.as_double();
auto integer = floor(fabs(number.as_double()));
if (number.as_double() < 0 && integer != 0)
integer = -integer;
return integer;
}
// Standalone variant using plain doubles for cases where we already got numbers and know the AO won't throw.
double to_integer_or_infinity(double number)
{
// 1. Let number be ? ToNumber(argument).
// 2. If number is NaN, +0๐ฝ, or -0๐ฝ, return 0.
if (isnan(number) || number == 0)
return 0;
// 3. If number is +โ๐ฝ, return +โ.
if (__builtin_isinf_sign(number) > 0)
return static_cast<double>(INFINITY);
// 4. If number is -โ๐ฝ, return -โ.
if (__builtin_isinf_sign(number) < 0)
return static_cast<double>(-INFINITY);
// 5. Let integer be floor(abs(โ(number))).
auto integer = floor(fabs(number));
// 6. If number < -0๐ฝ, set integer to -integer.
if (number < 0 && integer != 0)
integer = -integer;
// 7. Return integer.
return integer;
}
// 7.3.3 GetV ( V, P ), https://tc39.es/ecma262/#sec-getv
ThrowCompletionOr<Value> Value::get(VM& vm, PropertyKey const& property_key) const
{
// 1. Assert: IsPropertyKey(P) is true.
VERIFY(property_key.is_valid());
// 2. Let O be ? ToObject(V).
auto* object = TRY(to_object(vm));
// 3. Return ? O.[[Get]](P, V).
return TRY(object->internal_get(property_key, *this));
}
// 7.3.11 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
ThrowCompletionOr<FunctionObject*> Value::get_method(VM& vm, PropertyKey const& property_key) const
{
// 1. Assert: IsPropertyKey(P) is true.
VERIFY(property_key.is_valid());
// 2. Let func be ? GetV(V, P).
auto function = TRY(get(vm, property_key));
// 3. If func is either undefined or null, return undefined.
if (function.is_nullish())
return nullptr;
// 4. If IsCallable(func) is false, throw a TypeError exception.
if (!function.is_function())
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, function.to_string_without_side_effects());
// 5. Return func.
return &function.as_function();
}
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
ThrowCompletionOr<Value> greater_than(VM& vm, Value lhs, Value rhs)
{
if (lhs.is_int32() && rhs.is_int32())
return lhs.as_i32() > rhs.as_i32();
TriState relation = TRY(is_less_than(vm, lhs, rhs, false));
if (relation == TriState::Unknown)
return Value(false);
return Value(relation == TriState::True);
}
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
ThrowCompletionOr<Value> greater_than_equals(VM& vm, Value lhs, Value rhs)
{
if (lhs.is_int32() && rhs.is_int32())
return lhs.as_i32() >= rhs.as_i32();
TriState relation = TRY(is_less_than(vm, lhs, rhs, true));
if (relation == TriState::Unknown || relation == TriState::True)
return Value(false);
return Value(true);
}
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
ThrowCompletionOr<Value> less_than(VM& vm, Value lhs, Value rhs)
{
if (lhs.is_int32() && rhs.is_int32())
return lhs.as_i32() < rhs.as_i32();
TriState relation = TRY(is_less_than(vm, lhs, rhs, true));
if (relation == TriState::Unknown)
return Value(false);
return Value(relation == TriState::True);
}
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
ThrowCompletionOr<Value> less_than_equals(VM& vm, Value lhs, Value rhs)
{
if (lhs.is_int32() && rhs.is_int32())
return lhs.as_i32() <= rhs.as_i32();
TriState relation = TRY(is_less_than(vm, lhs, rhs, false));
if (relation == TriState::Unknown || relation == TriState::True)
return Value(false);
return Value(true);
}
// 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
ThrowCompletionOr<Value> bitwise_and(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
return Value(0);
return Value(TRY(lhs_numeric.to_i32(vm)) & TRY(rhs_numeric.to_i32(vm)));
}
if (both_bigint(lhs_numeric, rhs_numeric))
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
}
// 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
ThrowCompletionOr<Value> bitwise_or(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
return Value(0);
if (!lhs_numeric.is_finite_number())
return rhs_numeric;
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
return Value(TRY(lhs_numeric.to_i32(vm)) | TRY(rhs_numeric.to_i32(vm)));
}
if (both_bigint(lhs_numeric, rhs_numeric))
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()));
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
}
// 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
ThrowCompletionOr<Value> bitwise_xor(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
return Value(0);
if (!lhs_numeric.is_finite_number())
return rhs_numeric;
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
return Value(TRY(lhs_numeric.to_i32(vm)) ^ TRY(rhs_numeric.to_i32(vm)));
}
if (both_bigint(lhs_numeric, rhs_numeric))
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
}
// 13.5.6 Bitwise NOT Operator ( ~ ), https://tc39.es/ecma262/#sec-bitwise-not-operator
ThrowCompletionOr<Value> bitwise_not(VM& vm, Value lhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
if (lhs_numeric.is_number())
return Value(~TRY(lhs_numeric.to_i32(vm)));
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_not());
}
// 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator
ThrowCompletionOr<Value> unary_plus(VM& vm, Value lhs)
{
return TRY(lhs.to_number(vm));
}
// 13.5.5 Unary - Operator, https://tc39.es/ecma262/#sec-unary-minus-operator
ThrowCompletionOr<Value> unary_minus(VM& vm, Value lhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
if (lhs_numeric.is_number()) {
if (lhs_numeric.is_nan())
return js_nan();
return Value(-lhs_numeric.as_double());
}
if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
return BigInt::create(vm, BIGINT_ZERO);
auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
big_integer_negated.negate();
return BigInt::create(vm, big_integer_negated);
}
// 13.9.1 The Left Shift Operator ( << ), https://tc39.es/ecma262/#sec-left-shift-operator
ThrowCompletionOr<Value> left_shift(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number())
return Value(0);
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
// Ok, so this performs toNumber() again but that "can't" throw
auto lhs_i32 = MUST(lhs_numeric.to_i32(vm));
auto rhs_u32 = MUST(rhs_numeric.to_u32(vm)) % 32;
return Value(lhs_i32 << rhs_u32);
}
if (both_bigint(lhs_numeric, rhs_numeric)) {
// 6.1.6.2.9 BigInt::leftShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-leftShift
auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
// 1. If y < 0โค, then
if (rhs_numeric.as_bigint().big_integer().is_negative()) {
// a. Return the BigInt value that represents โ(x) / 2^-y, rounding down to the nearest integer, including for negative numbers.
// NOTE: Since y is negative we can just do โ(x) / 2^|y|
auto const& big_integer = lhs_numeric.as_bigint().big_integer();
auto division_result = big_integer.divided_by(multiplier_divisor);
// For positive initial values and no remainder just return quotient
if (division_result.remainder.is_zero() || !big_integer.is_negative())
return BigInt::create(vm, division_result.quotient);
// For negative round "down" to the next negative number
return BigInt::create(vm, division_result.quotient.minus(Crypto::SignedBigInteger { 1 }));
}
// 2. Return the BigInt value that represents โ(x) ร 2^y.
return Value(BigInt::create(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor)));
}
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "left-shift");
}
// 13.9.2 The Signed Right Shift Operator ( >> ), https://tc39.es/ecma262/#sec-signed-right-shift-operator
ThrowCompletionOr<Value> right_shift(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number())
return Value(0);
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
auto lhs_i32 = MUST(lhs_numeric.to_i32(vm));
auto rhs_u32 = MUST(rhs_numeric.to_u32(vm)) % 32;
return Value(lhs_i32 >> rhs_u32);
}
if (both_bigint(lhs_numeric, rhs_numeric)) {
auto rhs_negated = rhs_numeric.as_bigint().big_integer();
rhs_negated.negate();
return left_shift(vm, lhs, BigInt::create(vm, rhs_negated));
}
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "right-shift");
}
// 13.9.3 The Unsigned Right Shift Operator ( >>> ), https://tc39.es/ecma262/#sec-unsigned-right-shift-operator
ThrowCompletionOr<Value> unsigned_right_shift(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number())
return Value(0);
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
// Ok, so this performs toNumber() again but that "can't" throw
auto lhs_u32 = MUST(lhs_numeric.to_u32(vm));
auto rhs_u32 = MUST(rhs_numeric.to_u32(vm)) % 32;
return Value(lhs_u32 >> rhs_u32);
}
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperator, "unsigned right-shift");
}
// 13.8.1 The Addition Operator ( + ), https://tc39.es/ecma262/#sec-addition-operator-plus
ThrowCompletionOr<Value> add(VM& vm, Value lhs, Value rhs)
{
if (both_number(lhs, rhs)) {
if (lhs.is_int32() && rhs.is_int32()) {
Checked<i32> result;
result = MUST(lhs.to_i32(vm));
result += MUST(rhs.to_i32(vm));
if (!result.has_overflow())
return Value(result.value());
}
return Value(lhs.as_double() + rhs.as_double());
}
auto lhs_primitive = TRY(lhs.to_primitive(vm));
auto rhs_primitive = TRY(rhs.to_primitive(vm));
if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
auto lhs_string = TRY(lhs_primitive.to_primitive_string(vm));
auto rhs_string = TRY(rhs_primitive.to_primitive_string(vm));
return PrimitiveString::create(vm, *lhs_string, *rhs_string);
}
auto lhs_numeric = TRY(lhs_primitive.to_numeric(vm));
auto rhs_numeric = TRY(rhs_primitive.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer()));
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "addition");
}
// 13.8.2 The Subtraction Operator ( - ), https://tc39.es/ecma262/#sec-subtraction-operator-minus
ThrowCompletionOr<Value> sub(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
double lhsd = lhs_numeric.as_double();
double rhsd = rhs_numeric.as_double();
double interm = lhsd - rhsd;
return Value(interm);
}
if (both_bigint(lhs_numeric, rhs_numeric))
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()));
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "subtraction");
}
// 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
ThrowCompletionOr<Value> mul(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()));
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "multiplication");
}
// 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
ThrowCompletionOr<Value> div(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric)) {
if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
return vm.throw_completion<RangeError>(ErrorType::DivisionByZero);
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient);
}
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "division");
}
// 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
ThrowCompletionOr<Value> mod(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric)) {
// 6.1.6.1.6 Number::remainder ( n, d ), https://tc39.es/ecma262/#sec-numeric-types-number-remainder
// The ECMA specification is describing the mathematical definition of modulus
// implemented by fmod.
auto n = lhs_numeric.as_double();
auto d = rhs_numeric.as_double();
return Value(fmod(n, d));
}
if (both_bigint(lhs_numeric, rhs_numeric)) {
if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
return vm.throw_completion<RangeError>(ErrorType::DivisionByZero);
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder);
}
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "modulo");
}
static Value exp_double(Value base, Value exponent)
{
VERIFY(both_number(base, exponent));
if (exponent.is_nan())
return js_nan();
if (exponent.is_positive_zero() || exponent.is_negative_zero())
return Value(1);
if (base.is_nan())
return js_nan();
if (base.is_positive_infinity())
return exponent.as_double() > 0 ? js_infinity() : Value(0);
if (base.is_negative_infinity()) {
auto is_odd_integral_number = exponent.is_integral_number() && (static_cast<i32>(exponent.as_double()) % 2 != 0);
if (exponent.as_double() > 0)
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
else
return is_odd_integral_number ? Value(-0.0) : Value(0);
}
if (base.is_positive_zero())
return exponent.as_double() > 0 ? Value(0) : js_infinity();
if (base.is_negative_zero()) {
auto is_odd_integral_number = exponent.is_integral_number() && (static_cast<i32>(exponent.as_double()) % 2 != 0);
if (exponent.as_double() > 0)
return is_odd_integral_number ? Value(-0.0) : Value(0);
else
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
}
VERIFY(base.is_finite_number() && !base.is_positive_zero() && !base.is_negative_zero());
if (exponent.is_positive_infinity()) {
auto absolute_base = fabs(base.as_double());
if (absolute_base > 1)
return js_infinity();
else if (absolute_base == 1)
return js_nan();
else if (absolute_base < 1)
return Value(0);
}
if (exponent.is_negative_infinity()) {
auto absolute_base = fabs(base.as_double());
if (absolute_base > 1)
return Value(0);
else if (absolute_base == 1)
return js_nan();
else if (absolute_base < 1)
return js_infinity();
}
VERIFY(exponent.is_finite_number() && !exponent.is_positive_zero() && !exponent.is_negative_zero());
if (base.as_double() < 0 && !exponent.is_integral_number())
return js_nan();
return Value(::pow(base.as_double(), exponent.as_double()));
}
// 13.6 Exponentiation Operator, https://tc39.es/ecma262/#sec-exp-operator
ThrowCompletionOr<Value> exp(VM& vm, Value lhs, Value rhs)
{
auto lhs_numeric = TRY(lhs.to_numeric(vm));
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric))
return exp_double(lhs_numeric, rhs_numeric);
if (both_bigint(lhs_numeric, rhs_numeric)) {
if (rhs_numeric.as_bigint().big_integer().is_negative())
return vm.throw_completion<RangeError>(ErrorType::NegativeExponent);
return BigInt::create(vm, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer()));
}
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "exponentiation");
}
ThrowCompletionOr<Value> in(VM& vm, Value lhs, Value rhs)
{
if (!rhs.is_object())
return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
auto lhs_property_key = TRY(lhs.to_property_key(vm));
return Value(TRY(rhs.as_object().has_property(lhs_property_key)));
}
// 13.10.2 InstanceofOperator ( V, target ), https://tc39.es/ecma262/#sec-instanceofoperator
ThrowCompletionOr<Value> instance_of(VM& vm, Value lhs, Value rhs)
{
if (!rhs.is_object())
return vm.throw_completion<TypeError>(ErrorType::NotAnObject, rhs.to_string_without_side_effects());
auto has_instance_method = TRY(rhs.get_method(vm, *vm.well_known_symbol_has_instance()));
if (has_instance_method) {
auto has_instance_result = TRY(call(vm, *has_instance_method, rhs, lhs));
return Value(has_instance_result.to_boolean());
}
if (!rhs.is_function())
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, rhs.to_string_without_side_effects());
return TRY(ordinary_has_instance(vm, lhs, rhs));
}
// 7.3.22 OrdinaryHasInstance ( C, O ), https://tc39.es/ecma262/#sec-ordinaryhasinstance
ThrowCompletionOr<Value> ordinary_has_instance(VM& vm, Value lhs, Value rhs)
{
if (!rhs.is_function())
return Value(false);
auto& rhs_function = rhs.as_function();
if (is<BoundFunction>(rhs_function)) {
auto& bound_target = static_cast<BoundFunction const&>(rhs_function);
return instance_of(vm, lhs, Value(&bound_target.bound_target_function()));
}
if (!lhs.is_object())
return Value(false);
Object* lhs_object = &lhs.as_object();
auto rhs_prototype = TRY(rhs_function.get(vm.names.prototype));
if (!rhs_prototype.is_object())
return vm.throw_completion<TypeError>(ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
while (true) {
lhs_object = TRY(lhs_object->internal_get_prototype_of());
if (!lhs_object)
return Value(false);
if (same_value(rhs_prototype, lhs_object))
return Value(true);
}
}
// 7.2.10 SameValue ( x, y ), https://tc39.es/ecma262/#sec-samevalue
bool same_value(Value lhs, Value rhs)
{
if (!same_type_for_equality(lhs, rhs))
return false;
if (lhs.is_number()) {
if (lhs.is_nan() && rhs.is_nan())
return true;
if (lhs.is_positive_zero() && rhs.is_negative_zero())
return false;
if (lhs.is_negative_zero() && rhs.is_positive_zero())
return false;
return lhs.as_double() == rhs.as_double();
}
if (lhs.is_bigint()) {
auto lhs_big_integer = lhs.as_bigint().big_integer();
auto rhs_big_integer = rhs.as_bigint().big_integer();
if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
return false;
return lhs_big_integer == rhs_big_integer;
}
return same_value_non_numeric(lhs, rhs);
}
// 7.2.11 SameValueZero ( x, y ), https://tc39.es/ecma262/#sec-samevaluezero
bool same_value_zero(Value lhs, Value rhs)
{
if (!same_type_for_equality(lhs, rhs))
return false;
if (lhs.is_number()) {
if (lhs.is_nan() && rhs.is_nan())
return true;
return lhs.as_double() == rhs.as_double();
}
if (lhs.is_bigint())
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
return same_value_non_numeric(lhs, rhs);
}
// 7.2.12 SameValueNonNumeric ( x, y ), https://tc39.es/ecma262/#sec-samevaluenonnumeric
bool same_value_non_numeric(Value lhs, Value rhs)
{
VERIFY(!lhs.is_number() && !lhs.is_bigint());
VERIFY(same_type_for_equality(lhs, rhs));
if (lhs.is_string())
return lhs.as_string().deprecated_string() == rhs.as_string().deprecated_string();
return lhs.m_value.encoded == rhs.m_value.encoded;
}
// 7.2.15 IsStrictlyEqual ( x, y ), https://tc39.es/ecma262/#sec-isstrictlyequal
bool is_strictly_equal(Value lhs, Value rhs)
{
if (!same_type_for_equality(lhs, rhs))
return false;
if (lhs.is_number()) {
if (lhs.is_nan() || rhs.is_nan())
return false;
if (lhs.as_double() == rhs.as_double())
return true;
return false;
}
if (lhs.is_bigint())
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
return same_value_non_numeric(lhs, rhs);
}
// 7.2.14 IsLooselyEqual ( x, y ), https://tc39.es/ecma262/#sec-islooselyequal
ThrowCompletionOr<bool> is_loosely_equal(VM& vm, Value lhs, Value rhs)
{
// 1. If Type(x) is the same as Type(y), then
if (same_type_for_equality(lhs, rhs)) {
// a. Return IsStrictlyEqual(x, y).
return is_strictly_equal(lhs, rhs);
}
// 2. If x is null and y is undefined, return true.
// 3. If x is undefined and y is null, return true.
if (lhs.is_nullish() && rhs.is_nullish())
return true;
// 4. NOTE: This step is replaced in section B.3.6.2.
// B.3.6.2 Changes to IsLooselyEqual, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-aec
// 4. Perform the following steps:
// a. If Type(x) is Object and x has an [[IsHTMLDDA]] internal slot and y is either null or undefined, return true.
if (lhs.is_object() && lhs.as_object().is_htmldda() && rhs.is_nullish())
return true;
// b. If x is either null or undefined and Type(y) is Object and y has an [[IsHTMLDDA]] internal slot, return true.
if (lhs.is_nullish() && rhs.is_object() && rhs.as_object().is_htmldda())
return true;
// == End of B.3.6.2 ==
// 5. If Type(x) is Number and Type(y) is String, return ! IsLooselyEqual(x, ! ToNumber(y)).
if (lhs.is_number() && rhs.is_string())
return is_loosely_equal(vm, lhs, MUST(rhs.to_number(vm)));
// 6. If Type(x) is String and Type(y) is Number, return ! IsLooselyEqual(! ToNumber(x), y).
if (lhs.is_string() && rhs.is_number())
return is_loosely_equal(vm, MUST(lhs.to_number(vm)), rhs);
// 7. If Type(x) is BigInt and Type(y) is String, then
if (lhs.is_bigint() && rhs.is_string()) {
// a. Let n be StringToBigInt(y).
auto bigint = string_to_bigint(vm, rhs.as_string().deprecated_string());
// b. If n is undefined, return false.
if (!bigint.has_value())
return false;
// c. Return ! IsLooselyEqual(x, n).
return is_loosely_equal(vm, lhs, *bigint);
}
// 8. If Type(x) is String and Type(y) is BigInt, return ! IsLooselyEqual(y, x).
if (lhs.is_string() && rhs.is_bigint())
return is_loosely_equal(vm, rhs, lhs);
// 9. If Type(x) is Boolean, return ! IsLooselyEqual(! ToNumber(x), y).
if (lhs.is_boolean())
return is_loosely_equal(vm, MUST(lhs.to_number(vm)), rhs);
// 10. If Type(y) is Boolean, return ! IsLooselyEqual(x, ! ToNumber(y)).
if (rhs.is_boolean())
return is_loosely_equal(vm, lhs, MUST(rhs.to_number(vm)));
// 11. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return ! IsLooselyEqual(x, ? ToPrimitive(y)).
if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
auto rhs_primitive = TRY(rhs.to_primitive(vm));
return is_loosely_equal(vm, lhs, rhs_primitive);
}
// 12. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return ! IsLooselyEqual(? ToPrimitive(x), y).
if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_bigint() || rhs.is_symbol())) {
auto lhs_primitive = TRY(lhs.to_primitive(vm));
return is_loosely_equal(vm, lhs_primitive, rhs);
}
// 13. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
// a. If x or y are any of NaN, +โ๐ฝ, or -โ๐ฝ, return false.
if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
return false;
// b. If โ(x) = โ(y), return true; otherwise return false.
if ((lhs.is_number() && !lhs.is_integral_number()) || (rhs.is_number() && !rhs.is_integral_number()))
return false;
VERIFY(!lhs.is_nan() && !rhs.is_nan());
auto& number_side = lhs.is_number() ? lhs : rhs;
auto& bigint_side = lhs.is_number() ? rhs : lhs;
return bigint_side.as_bigint().big_integer().compare_to_double(number_side.as_double()) == Crypto::UnsignedBigInteger::CompareResult::DoubleEqualsBigInt;
}
// 14. Return false.
return false;
}
// 7.2.13 IsLessThan ( x, y, LeftFirst ), https://tc39.es/ecma262/#sec-islessthan
ThrowCompletionOr<TriState> is_less_than(VM& vm, Value lhs, Value rhs, bool left_first)
{
Value x_primitive;
Value y_primitive;
if (left_first) {
x_primitive = TRY(lhs.to_primitive(vm, Value::PreferredType::Number));
y_primitive = TRY(rhs.to_primitive(vm, Value::PreferredType::Number));
} else {
y_primitive = TRY(lhs.to_primitive(vm, Value::PreferredType::Number));
x_primitive = TRY(rhs.to_primitive(vm, Value::PreferredType::Number));
}
if (x_primitive.is_string() && y_primitive.is_string()) {
auto x_string = x_primitive.as_string().deprecated_string();
auto y_string = y_primitive.as_string().deprecated_string();
Utf8View x_code_points { x_string };
Utf8View y_code_points { y_string };
for (auto k = x_code_points.begin(), l = y_code_points.begin();
k != x_code_points.end() && l != y_code_points.end();
++k, ++l) {
if (*k != *l) {
if (*k < *l) {
return TriState::True;
} else {
return TriState::False;
}
}
}
return x_code_points.length() < y_code_points.length()
? TriState::True
: TriState::False;
}
if (x_primitive.is_bigint() && y_primitive.is_string()) {
auto y_bigint = string_to_bigint(vm, y_primitive.as_string().deprecated_string());
if (!y_bigint.has_value())
return TriState::Unknown;
if (x_primitive.as_bigint().big_integer() < (*y_bigint)->big_integer())
return TriState::True;
return TriState::False;
}
if (x_primitive.is_string() && y_primitive.is_bigint()) {
auto x_bigint = string_to_bigint(vm, x_primitive.as_string().deprecated_string());
if (!x_bigint.has_value())
return TriState::Unknown;
if ((*x_bigint)->big_integer() < y_primitive.as_bigint().big_integer())
return TriState::True;
return TriState::False;
}
auto x_numeric = TRY(x_primitive.to_numeric(vm));
auto y_numeric = TRY(y_primitive.to_numeric(vm));
if (x_numeric.is_nan() || y_numeric.is_nan())
return TriState::Unknown;
if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
return TriState::False;
if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
return TriState::True;
if (x_numeric.is_number() && y_numeric.is_number()) {
if (x_numeric.as_double() < y_numeric.as_double())
return TriState::True;
else
return TriState::False;
}
if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
return TriState::True;
else
return TriState::False;
}
VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
bool x_lower_than_y;
VERIFY(!x_numeric.is_nan() && !y_numeric.is_nan());
if (x_numeric.is_number()) {
x_lower_than_y = y_numeric.as_bigint().big_integer().compare_to_double(x_numeric.as_double())
== Crypto::UnsignedBigInteger::CompareResult::DoubleLessThanBigInt;
} else {
x_lower_than_y = x_numeric.as_bigint().big_integer().compare_to_double(y_numeric.as_double())
== Crypto::UnsignedBigInteger::CompareResult::DoubleGreaterThanBigInt;
}
if (x_lower_than_y)
return TriState::True;
else
return TriState::False;
}
// 7.3.21 Invoke ( V, P [ , argumentsList ] ), https://tc39.es/ecma262/#sec-invoke
ThrowCompletionOr<Value> Value::invoke_internal(VM& vm, PropertyKey const& property_key, Optional<MarkedVector<Value>> arguments)
{
auto property = TRY(get(vm, property_key));
if (!property.is_function())
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, property.to_string_without_side_effects());
return call(vm, property.as_function(), *this, move(arguments));
}
}
|