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 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
|
package goja
import (
"fmt"
"log"
"math"
"strconv"
"sync"
"sync/atomic"
)
const (
maxInt = 1 << 53
)
type valueStack []Value
type stash struct {
values valueStack
extraArgs valueStack
names map[string]uint32
obj objectImpl
outer *stash
}
type context struct {
prg *Program
funcName string
stash *stash
pc, sb int
args int
}
type iterStackItem struct {
val Value
f iterNextFunc
}
type ref interface {
get() Value
set(Value)
refname() string
}
type stashRef struct {
v *Value
n string
}
func (r stashRef) get() Value {
return *r.v
}
func (r *stashRef) set(v Value) {
*r.v = v
}
func (r *stashRef) refname() string {
return r.n
}
type objRef struct {
base objectImpl
name string
strict bool
}
func (r *objRef) get() Value {
return r.base.getStr(r.name)
}
func (r *objRef) set(v Value) {
r.base.putStr(r.name, v, r.strict)
}
func (r *objRef) refname() string {
return r.name
}
type unresolvedRef struct {
runtime *Runtime
name string
}
func (r *unresolvedRef) get() Value {
r.runtime.throwReferenceError(r.name)
panic("Unreachable")
}
func (r *unresolvedRef) set(v Value) {
r.get()
}
func (r *unresolvedRef) refname() string {
return r.name
}
type vm struct {
r *Runtime
prg *Program
funcName string
pc int
stack valueStack
sp, sb, args int
stash *stash
callStack []context
iterStack []iterStackItem
refStack []ref
stashAllocs int
halt bool
interrupted uint32
interruptVal interface{}
interruptLock sync.Mutex
}
type instruction interface {
exec(*vm)
}
func intToValue(i int64) Value {
if i >= -maxInt && i <= maxInt {
if i >= -128 && i <= 127 {
return intCache[i+128]
}
return valueInt(i)
}
return valueFloat(float64(i))
}
func floatToInt(f float64) (result int64, ok bool) {
if (f != 0 || !math.Signbit(f)) && !math.IsInf(f, 0) && f == math.Trunc(f) && f >= -maxInt && f <= maxInt {
return int64(f), true
}
return 0, false
}
func floatToValue(f float64) (result Value) {
if i, ok := floatToInt(f); ok {
return intToValue(i)
}
switch {
case f == 0:
return _negativeZero
case math.IsNaN(f):
return _NaN
case math.IsInf(f, 1):
return _positiveInf
case math.IsInf(f, -1):
return _negativeInf
}
return valueFloat(f)
}
func toInt(v Value) (int64, bool) {
num := v.ToNumber()
if i, ok := num.assertInt(); ok {
return i, true
}
if f, ok := num.assertFloat(); ok {
if i, ok := floatToInt(f); ok {
return i, true
}
}
return 0, false
}
func toIntIgnoreNegZero(v Value) (int64, bool) {
num := v.ToNumber()
if i, ok := num.assertInt(); ok {
return i, true
}
if f, ok := num.assertFloat(); ok {
if v == _negativeZero {
return 0, true
}
if i, ok := floatToInt(f); ok {
return i, true
}
}
return 0, false
}
func (s *valueStack) expand(idx int) {
if idx < len(*s) {
return
}
if idx < cap(*s) {
*s = (*s)[:idx+1]
} else {
n := make([]Value, idx+1, (idx+1)<<1)
copy(n, *s)
*s = n
}
}
func (s *stash) put(name string, v Value) bool {
if s.obj != nil {
if found := s.obj.getStr(name); found != nil {
s.obj.putStr(name, v, false)
return true
}
return false
} else {
if idx, found := s.names[name]; found {
s.values.expand(int(idx))
s.values[idx] = v
return true
}
return false
}
}
func (s *stash) putByIdx(idx uint32, v Value) {
if s.obj != nil {
panic("Attempt to put by idx into an object scope")
}
s.values.expand(int(idx))
s.values[idx] = v
}
func (s *stash) getByIdx(idx uint32) Value {
if int(idx) < len(s.values) {
return s.values[idx]
}
return _undefined
}
func (s *stash) getByName(name string, vm *vm) (v Value, exists bool) {
if s.obj != nil {
v = s.obj.getStr(name)
if v == nil {
return nil, false
//return valueUnresolved{r: vm.r, ref: name}, false
}
return v, true
}
if idx, exists := s.names[name]; exists {
return s.values[idx], true
}
return nil, false
//return valueUnresolved{r: vm.r, ref: name}, false
}
func (s *stash) createBinding(name string) {
if s.names == nil {
s.names = make(map[string]uint32)
}
if _, exists := s.names[name]; !exists {
s.names[name] = uint32(len(s.names))
s.values = append(s.values, _undefined)
}
}
func (s *stash) deleteBinding(name string) bool {
if s.obj != nil {
return s.obj.deleteStr(name, false)
}
if idx, found := s.names[name]; found {
s.values[idx] = nil
delete(s.names, name)
return true
}
return false
}
func (vm *vm) newStash() {
vm.stash = &stash{
outer: vm.stash,
}
vm.stashAllocs++
}
func (vm *vm) init() {
}
func (vm *vm) run() {
vm.halt = false
interrupted := false
for !vm.halt {
if interrupted = atomic.LoadUint32(&vm.interrupted) != 0; interrupted {
break
}
vm.prg.code[vm.pc].exec(vm)
}
if interrupted {
vm.interruptLock.Lock()
v := &InterruptedError{
iface: vm.interruptVal,
}
atomic.StoreUint32(&vm.interrupted, 0)
vm.interruptVal = nil
vm.interruptLock.Unlock()
panic(v)
}
}
func (vm *vm) Interrupt(v interface{}) {
vm.interruptLock.Lock()
vm.interruptVal = v
atomic.StoreUint32(&vm.interrupted, 1)
vm.interruptLock.Unlock()
}
func (vm *vm) captureStack(stack []stackFrame, ctxOffset int) []stackFrame {
// Unroll the context stack
stack = append(stack, stackFrame{prg: vm.prg, pc: vm.pc, funcName: vm.funcName})
for i := len(vm.callStack) - 1; i > ctxOffset-1; i-- {
if vm.callStack[i].pc != -1 {
stack = append(stack, stackFrame{prg: vm.callStack[i].prg, pc: vm.callStack[i].pc - 1, funcName: vm.callStack[i].funcName})
}
}
return stack
}
func (vm *vm) try(f func()) (ex *Exception) {
var ctx context
vm.saveCtx(&ctx)
ctxOffset := len(vm.callStack)
sp := vm.sp
iterLen := len(vm.iterStack)
refLen := len(vm.refStack)
defer func() {
if x := recover(); x != nil {
defer func() {
vm.callStack = vm.callStack[:ctxOffset]
vm.restoreCtx(&ctx)
vm.sp = sp
// Restore other stacks
iterTail := vm.iterStack[iterLen:]
for i, _ := range iterTail {
iterTail[i] = iterStackItem{}
}
vm.iterStack = vm.iterStack[:iterLen]
refTail := vm.refStack[refLen:]
for i, _ := range refTail {
refTail[i] = nil
}
vm.refStack = vm.refStack[:refLen]
}()
switch x1 := x.(type) {
case Value:
ex = &Exception{
val: x1,
}
case *InterruptedError:
x1.stack = vm.captureStack(x1.stack, ctxOffset)
panic(x1)
case *Exception:
ex = x1
default:
if vm.prg != nil {
vm.prg.dumpCode(log.Printf)
}
//log.Print("Stack: ", string(debug.Stack()))
panic(fmt.Errorf("Panic at %d: %v", vm.pc, x))
}
ex.stack = vm.captureStack(ex.stack, ctxOffset)
}
}()
f()
return
}
func (vm *vm) runTry() (ex *Exception) {
return vm.try(vm.run)
}
func (vm *vm) push(v Value) {
vm.stack.expand(vm.sp)
vm.stack[vm.sp] = v
vm.sp++
}
func (vm *vm) pop() Value {
vm.sp--
return vm.stack[vm.sp]
}
func (vm *vm) peek() Value {
return vm.stack[vm.sp-1]
}
func (vm *vm) saveCtx(ctx *context) {
ctx.prg = vm.prg
ctx.funcName = vm.funcName
ctx.stash = vm.stash
ctx.pc = vm.pc
ctx.sb = vm.sb
ctx.args = vm.args
}
func (vm *vm) pushCtx() {
/*
vm.ctxStack = append(vm.ctxStack, context{
prg: vm.prg,
stash: vm.stash,
pc: vm.pc,
sb: vm.sb,
args: vm.args,
})*/
vm.callStack = append(vm.callStack, context{})
vm.saveCtx(&vm.callStack[len(vm.callStack)-1])
}
func (vm *vm) restoreCtx(ctx *context) {
vm.prg = ctx.prg
vm.funcName = ctx.funcName
vm.pc = ctx.pc
vm.stash = ctx.stash
vm.sb = ctx.sb
vm.args = ctx.args
}
func (vm *vm) popCtx() {
l := len(vm.callStack) - 1
vm.prg = vm.callStack[l].prg
vm.callStack[l].prg = nil
vm.funcName = vm.callStack[l].funcName
vm.pc = vm.callStack[l].pc
vm.stash = vm.callStack[l].stash
vm.callStack[l].stash = nil
vm.sb = vm.callStack[l].sb
vm.args = vm.callStack[l].args
vm.callStack = vm.callStack[:l]
}
func (r *Runtime) toObject(v Value, args ...interface{}) *Object {
//r.checkResolveable(v)
if obj, ok := v.(*Object); ok {
return obj
}
if len(args) > 0 {
r.typeErrorResult(true, args)
} else {
r.typeErrorResult(true, "Value is not an object: %s", v.ToString())
}
panic("Unreachable")
}
func (r *Runtime) toCallee(v Value) *Object {
if obj, ok := v.(*Object); ok {
return obj
}
switch unresolved := v.(type) {
case valueUnresolved:
unresolved.throw()
panic("Unreachable")
case memberUnresolved:
r.typeErrorResult(true, "Object has no member '%s'", unresolved.ref)
panic("Unreachable")
}
r.typeErrorResult(true, "Value is not an object: %s", v.ToString())
panic("Unreachable")
}
type _newStash struct{}
var newStash _newStash
func (_newStash) exec(vm *vm) {
vm.newStash()
vm.pc++
}
type _noop struct{}
var noop _noop
func (_noop) exec(vm *vm) {
vm.pc++
}
type loadVal uint32
func (l loadVal) exec(vm *vm) {
vm.push(vm.prg.values[l])
vm.pc++
}
type loadVal1 uint32
func (l *loadVal1) exec(vm *vm) {
vm.push(vm.prg.values[*l])
vm.pc++
}
type _loadUndef struct{}
var loadUndef _loadUndef
func (_loadUndef) exec(vm *vm) {
vm.push(_undefined)
vm.pc++
}
type _loadNil struct{}
var loadNil _loadNil
func (_loadNil) exec(vm *vm) {
vm.push(nil)
vm.pc++
}
type _loadGlobalObject struct{}
var loadGlobalObject _loadGlobalObject
func (_loadGlobalObject) exec(vm *vm) {
vm.push(vm.r.globalObject)
vm.pc++
}
type loadStack int
func (l loadStack) exec(vm *vm) {
// l < 0 -- arg<-l-1>
// l > 0 -- var<l-1>
// l == 0 -- this
if l < 0 {
arg := int(-l)
if arg > vm.args {
vm.push(_undefined)
} else {
vm.push(vm.stack[vm.sb+arg])
}
} else if l > 0 {
vm.push(vm.stack[vm.sb+vm.args+int(l)])
} else {
vm.push(vm.stack[vm.sb])
}
vm.pc++
}
type _loadCallee struct{}
var loadCallee _loadCallee
func (_loadCallee) exec(vm *vm) {
vm.push(vm.stack[vm.sb-1])
vm.pc++
}
func (vm *vm) storeStack(s int) {
// l < 0 -- arg<-l-1>
// l > 0 -- var<l-1>
// l == 0 -- this
if s < 0 {
vm.stack[vm.sb-s] = vm.stack[vm.sp-1]
} else if s > 0 {
vm.stack[vm.sb+vm.args+s] = vm.stack[vm.sp-1]
} else {
panic("Attempt to modify this")
}
vm.pc++
}
type storeStack int
func (s storeStack) exec(vm *vm) {
vm.storeStack(int(s))
}
type storeStackP int
func (s storeStackP) exec(vm *vm) {
vm.storeStack(int(s))
vm.sp--
}
type _toNumber struct{}
var toNumber _toNumber
func (_toNumber) exec(vm *vm) {
vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
vm.pc++
}
type _add struct{}
var add _add
func (_add) exec(vm *vm) {
right := vm.stack[vm.sp-1]
left := vm.stack[vm.sp-2]
if o, ok := left.(*Object); ok {
left = o.self.toPrimitive()
}
if o, ok := right.(*Object); ok {
right = o.self.toPrimitive()
}
var ret Value
leftString, isLeftString := left.assertString()
rightString, isRightString := right.assertString()
if isLeftString || isRightString {
if !isLeftString {
leftString = left.ToString()
}
if !isRightString {
rightString = right.ToString()
}
ret = leftString.concat(rightString)
} else {
if leftInt, ok := left.assertInt(); ok {
if rightInt, ok := right.assertInt(); ok {
ret = intToValue(int64(leftInt) + int64(rightInt))
} else {
ret = floatToValue(float64(leftInt) + right.ToFloat())
}
} else {
ret = floatToValue(left.ToFloat() + right.ToFloat())
}
}
vm.stack[vm.sp-2] = ret
vm.sp--
vm.pc++
}
type _sub struct{}
var sub _sub
func (_sub) exec(vm *vm) {
right := vm.stack[vm.sp-1]
left := vm.stack[vm.sp-2]
var result Value
if left, ok := left.assertInt(); ok {
if right, ok := right.assertInt(); ok {
result = intToValue(left - right)
goto end
}
}
result = floatToValue(left.ToFloat() - right.ToFloat())
end:
vm.sp--
vm.stack[vm.sp-1] = result
vm.pc++
}
type _mul struct{}
var mul _mul
func (_mul) exec(vm *vm) {
left := vm.stack[vm.sp-2]
right := vm.stack[vm.sp-1]
var result Value
if left, ok := toInt(left); ok {
if right, ok := toInt(right); ok {
if left == 0 && right == -1 || left == -1 && right == 0 {
result = _negativeZero
goto end
}
res := left * right
// check for overflow
if left == 0 || right == 0 || res/left == right {
result = intToValue(res)
goto end
}
}
}
result = floatToValue(left.ToFloat() * right.ToFloat())
end:
vm.sp--
vm.stack[vm.sp-1] = result
vm.pc++
}
type _div struct{}
var div _div
func (_div) exec(vm *vm) {
left := vm.stack[vm.sp-2].ToFloat()
right := vm.stack[vm.sp-1].ToFloat()
var result Value
if math.IsNaN(left) || math.IsNaN(right) {
result = _NaN
goto end
}
if math.IsInf(left, 0) && math.IsInf(right, 0) {
result = _NaN
goto end
}
if left == 0 && right == 0 {
result = _NaN
goto end
}
if math.IsInf(left, 0) {
if math.Signbit(left) == math.Signbit(right) {
result = _positiveInf
goto end
} else {
result = _negativeInf
goto end
}
}
if math.IsInf(right, 0) {
if math.Signbit(left) == math.Signbit(right) {
result = _positiveZero
goto end
} else {
result = _negativeZero
goto end
}
}
if right == 0 {
if math.Signbit(left) == math.Signbit(right) {
result = _positiveInf
goto end
} else {
result = _negativeInf
goto end
}
}
result = floatToValue(left / right)
end:
vm.sp--
vm.stack[vm.sp-1] = result
vm.pc++
}
type _mod struct{}
var mod _mod
func (_mod) exec(vm *vm) {
left := vm.stack[vm.sp-2]
right := vm.stack[vm.sp-1]
var result Value
if leftInt, ok := toInt(left); ok {
if rightInt, ok := toInt(right); ok {
if rightInt == 0 {
result = _NaN
goto end
}
r := leftInt % rightInt
if r == 0 && leftInt < 0 {
result = _negativeZero
} else {
result = intToValue(leftInt % rightInt)
}
goto end
}
}
result = floatToValue(math.Mod(left.ToFloat(), right.ToFloat()))
end:
vm.sp--
vm.stack[vm.sp-1] = result
vm.pc++
}
type _neg struct{}
var neg _neg
func (_neg) exec(vm *vm) {
operand := vm.stack[vm.sp-1]
var result Value
if i, ok := toInt(operand); ok {
if i == 0 {
result = _negativeZero
} else {
result = valueInt(-i)
}
} else {
f := operand.ToFloat()
if !math.IsNaN(f) {
f = -f
}
result = valueFloat(f)
}
vm.stack[vm.sp-1] = result
vm.pc++
}
type _plus struct{}
var plus _plus
func (_plus) exec(vm *vm) {
vm.stack[vm.sp-1] = vm.stack[vm.sp-1].ToNumber()
vm.pc++
}
type _inc struct{}
var inc _inc
func (_inc) exec(vm *vm) {
v := vm.stack[vm.sp-1]
if i, ok := toInt(v); ok {
v = intToValue(i + 1)
goto end
}
v = valueFloat(v.ToFloat() + 1)
end:
vm.stack[vm.sp-1] = v
vm.pc++
}
type _dec struct{}
var dec _dec
func (_dec) exec(vm *vm) {
v := vm.stack[vm.sp-1]
if i, ok := toInt(v); ok {
v = intToValue(i - 1)
goto end
}
v = valueFloat(v.ToFloat() - 1)
end:
vm.stack[vm.sp-1] = v
vm.pc++
}
type _and struct{}
var and _and
func (_and) exec(vm *vm) {
left := toInt32(vm.stack[vm.sp-2])
right := toInt32(vm.stack[vm.sp-1])
vm.stack[vm.sp-2] = intToValue(int64(left & right))
vm.sp--
vm.pc++
}
type _or struct{}
var or _or
func (_or) exec(vm *vm) {
left := toInt32(vm.stack[vm.sp-2])
right := toInt32(vm.stack[vm.sp-1])
vm.stack[vm.sp-2] = intToValue(int64(left | right))
vm.sp--
vm.pc++
}
type _xor struct{}
var xor _xor
func (_xor) exec(vm *vm) {
left := toInt32(vm.stack[vm.sp-2])
right := toInt32(vm.stack[vm.sp-1])
vm.stack[vm.sp-2] = intToValue(int64(left ^ right))
vm.sp--
vm.pc++
}
type _bnot struct{}
var bnot _bnot
func (_bnot) exec(vm *vm) {
op := toInt32(vm.stack[vm.sp-1])
vm.stack[vm.sp-1] = intToValue(int64(^op))
vm.pc++
}
type _sal struct{}
var sal _sal
func (_sal) exec(vm *vm) {
left := toInt32(vm.stack[vm.sp-2])
right := toUInt32(vm.stack[vm.sp-1])
vm.stack[vm.sp-2] = intToValue(int64(left << (right & 0x1F)))
vm.sp--
vm.pc++
}
type _sar struct{}
var sar _sar
func (_sar) exec(vm *vm) {
left := toInt32(vm.stack[vm.sp-2])
right := toUInt32(vm.stack[vm.sp-1])
vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
vm.sp--
vm.pc++
}
type _shr struct{}
var shr _shr
func (_shr) exec(vm *vm) {
left := toUInt32(vm.stack[vm.sp-2])
right := toUInt32(vm.stack[vm.sp-1])
vm.stack[vm.sp-2] = intToValue(int64(left >> (right & 0x1F)))
vm.sp--
vm.pc++
}
type _halt struct{}
var halt _halt
func (_halt) exec(vm *vm) {
vm.halt = true
vm.pc++
}
type jump int32
func (j jump) exec(vm *vm) {
vm.pc += int(j)
}
type _setElem struct{}
var setElem _setElem
func (_setElem) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-3])
propName := vm.stack[vm.sp-2]
val := vm.stack[vm.sp-1]
obj.self.put(propName, val, false)
vm.sp -= 2
vm.stack[vm.sp-1] = val
vm.pc++
}
type _setElemStrict struct{}
var setElemStrict _setElemStrict
func (_setElemStrict) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-3])
propName := vm.stack[vm.sp-2]
val := vm.stack[vm.sp-1]
obj.self.put(propName, val, true)
vm.sp -= 2
vm.stack[vm.sp-1] = val
vm.pc++
}
type _deleteElem struct{}
var deleteElem _deleteElem
func (_deleteElem) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-2])
propName := vm.stack[vm.sp-1]
if !obj.self.hasProperty(propName) || obj.self.delete(propName, false) {
vm.stack[vm.sp-2] = valueTrue
} else {
vm.stack[vm.sp-2] = valueFalse
}
vm.sp--
vm.pc++
}
type _deleteElemStrict struct{}
var deleteElemStrict _deleteElemStrict
func (_deleteElemStrict) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-2])
propName := vm.stack[vm.sp-1]
obj.self.delete(propName, true)
vm.stack[vm.sp-2] = valueTrue
vm.sp--
vm.pc++
}
type deleteProp string
func (d deleteProp) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-1])
if !obj.self.hasPropertyStr(string(d)) || obj.self.deleteStr(string(d), false) {
vm.stack[vm.sp-1] = valueTrue
} else {
vm.stack[vm.sp-1] = valueFalse
}
vm.pc++
}
type deletePropStrict string
func (d deletePropStrict) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-1])
obj.self.deleteStr(string(d), true)
vm.stack[vm.sp-1] = valueTrue
vm.pc++
}
type setProp string
func (p setProp) exec(vm *vm) {
val := vm.stack[vm.sp-1]
vm.r.toObject(vm.stack[vm.sp-2]).self.putStr(string(p), val, false)
vm.stack[vm.sp-2] = val
vm.sp--
vm.pc++
}
type setPropStrict string
func (p setPropStrict) exec(vm *vm) {
obj := vm.stack[vm.sp-2]
val := vm.stack[vm.sp-1]
obj1 := vm.r.toObject(obj)
obj1.self.putStr(string(p), val, true)
vm.stack[vm.sp-2] = val
vm.sp--
vm.pc++
}
type setProp1 string
func (p setProp1) exec(vm *vm) {
vm.r.toObject(vm.stack[vm.sp-2]).self._putProp(string(p), vm.stack[vm.sp-1], true, true, true)
vm.sp--
vm.pc++
}
type _setProto struct{}
var setProto _setProto
func (_setProto) exec(vm *vm) {
vm.r.toObject(vm.stack[vm.sp-2]).self.putStr("__proto__", vm.stack[vm.sp-1], true)
vm.sp--
vm.pc++
}
type setPropGetter string
func (s setPropGetter) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-2])
val := vm.stack[vm.sp-1]
descr := vm.r.NewObject().self
descr.putStr("get", val, false)
descr.putStr("configurable", valueTrue, false)
descr.putStr("enumerable", valueTrue, false)
obj.self.defineOwnProperty(newStringValue(string(s)), descr, false)
vm.sp--
vm.pc++
}
type setPropSetter string
func (s setPropSetter) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-2])
val := vm.stack[vm.sp-1]
descr := vm.r.NewObject().self
descr.putStr("set", val, false)
descr.putStr("configurable", valueTrue, false)
descr.putStr("enumerable", valueTrue, false)
obj.self.defineOwnProperty(newStringValue(string(s)), descr, false)
vm.sp--
vm.pc++
}
type getProp string
func (g getProp) exec(vm *vm) {
v := vm.stack[vm.sp-1]
obj := v.baseObject(vm.r)
if obj == nil {
vm.r.typeErrorResult(true, "Cannot read property '%s' of undefined", g)
}
prop := obj.self.getPropStr(string(g))
if prop1, ok := prop.(*valueProperty); ok {
vm.stack[vm.sp-1] = prop1.get(v)
} else {
if prop == nil {
prop = _undefined
}
vm.stack[vm.sp-1] = prop
}
vm.pc++
}
type getPropCallee string
func (g getPropCallee) exec(vm *vm) {
v := vm.stack[vm.sp-1]
obj := v.baseObject(vm.r)
if obj == nil {
vm.r.typeErrorResult(true, "Cannot read property '%s' of undefined", g)
}
prop := obj.self.getPropStr(string(g))
if prop1, ok := prop.(*valueProperty); ok {
vm.stack[vm.sp-1] = prop1.get(v)
} else {
if prop == nil {
prop = memberUnresolved{valueUnresolved{r: vm.r, ref: string(g)}}
}
vm.stack[vm.sp-1] = prop
}
vm.pc++
}
type _getElem struct{}
var getElem _getElem
func (_getElem) exec(vm *vm) {
v := vm.stack[vm.sp-2]
obj := v.baseObject(vm.r)
propName := vm.stack[vm.sp-1]
if obj == nil {
vm.r.typeErrorResult(true, "Cannot read property '%s' of undefined", propName.String())
}
prop := obj.self.getProp(propName)
if prop1, ok := prop.(*valueProperty); ok {
vm.stack[vm.sp-2] = prop1.get(v)
} else {
if prop == nil {
prop = _undefined
}
vm.stack[vm.sp-2] = prop
}
vm.sp--
vm.pc++
}
type _getElemCallee struct{}
var getElemCallee _getElemCallee
func (_getElemCallee) exec(vm *vm) {
v := vm.stack[vm.sp-2]
obj := v.baseObject(vm.r)
propName := vm.stack[vm.sp-1]
if obj == nil {
vm.r.typeErrorResult(true, "Cannot read property '%s' of undefined", propName.String())
panic("Unreachable")
}
prop := obj.self.getProp(propName)
if prop1, ok := prop.(*valueProperty); ok {
vm.stack[vm.sp-2] = prop1.get(v)
} else {
if prop == nil {
prop = memberUnresolved{valueUnresolved{r: vm.r, ref: propName.String()}}
}
vm.stack[vm.sp-2] = prop
}
vm.sp--
vm.pc++
}
type _dup struct{}
var dup _dup
func (_dup) exec(vm *vm) {
vm.push(vm.stack[vm.sp-1])
vm.pc++
}
type dupN uint32
func (d dupN) exec(vm *vm) {
vm.push(vm.stack[vm.sp-1-int(d)])
vm.pc++
}
type rdupN uint32
func (d rdupN) exec(vm *vm) {
vm.stack[vm.sp-1-int(d)] = vm.stack[vm.sp-1]
vm.pc++
}
type _newObject struct{}
var newObject _newObject
func (_newObject) exec(vm *vm) {
vm.push(vm.r.NewObject())
vm.pc++
}
type newArray uint32
func (l newArray) exec(vm *vm) {
values := make([]Value, l)
if l > 0 {
copy(values, vm.stack[vm.sp-int(l):vm.sp])
}
obj := vm.r.newArrayValues(values)
if l > 0 {
vm.sp -= int(l) - 1
vm.stack[vm.sp-1] = obj
} else {
vm.push(obj)
}
vm.pc++
}
type newRegexp struct {
pattern regexpPattern
src valueString
global, ignoreCase, multiline bool
}
func (n *newRegexp) exec(vm *vm) {
vm.push(vm.r.newRegExpp(n.pattern, n.src, n.global, n.ignoreCase, n.multiline, vm.r.global.RegExpPrototype))
vm.pc++
}
func (vm *vm) setLocal(s int) {
v := vm.stack[vm.sp-1]
level := s >> 24
idx := uint32(s & 0x00FFFFFF)
stash := vm.stash
for i := 0; i < level; i++ {
stash = stash.outer
}
stash.putByIdx(idx, v)
vm.pc++
}
type setLocal uint32
func (s setLocal) exec(vm *vm) {
vm.setLocal(int(s))
}
type setLocalP uint32
func (s setLocalP) exec(vm *vm) {
vm.setLocal(int(s))
vm.sp--
}
type setVar struct {
name string
idx uint32
}
func (s setVar) exec(vm *vm) {
v := vm.peek()
level := int(s.idx >> 24)
idx := uint32(s.idx & 0x00FFFFFF)
stash := vm.stash
name := s.name
for i := 0; i < level; i++ {
if stash.put(name, v) {
goto end
}
stash = stash.outer
}
if stash != nil {
stash.putByIdx(idx, v)
} else {
vm.r.globalObject.self.putStr(name, v, false)
}
end:
vm.pc++
}
type resolveVar1 string
func (s resolveVar1) exec(vm *vm) {
name := string(s)
var ref ref
for stash := vm.stash; stash != nil; stash = stash.outer {
if stash.obj != nil {
if stash.obj.hasPropertyStr(name) {
ref = &objRef{
base: stash.obj,
name: name,
}
goto end
}
} else {
if idx, exists := stash.names[name]; exists {
ref = &stashRef{
v: &stash.values[idx],
}
goto end
}
}
}
ref = &objRef{
base: vm.r.globalObject.self,
name: name,
}
end:
vm.refStack = append(vm.refStack, ref)
vm.pc++
}
type deleteVar string
func (d deleteVar) exec(vm *vm) {
name := string(d)
ret := true
for stash := vm.stash; stash != nil; stash = stash.outer {
if stash.obj != nil {
if stash.obj.hasPropertyStr(name) {
ret = stash.obj.deleteStr(name, false)
goto end
}
} else {
if _, exists := stash.names[name]; exists {
ret = false
goto end
}
}
}
if vm.r.globalObject.self.hasPropertyStr(name) {
ret = vm.r.globalObject.self.deleteStr(name, false)
}
end:
if ret {
vm.push(valueTrue)
} else {
vm.push(valueFalse)
}
vm.pc++
}
type deleteGlobal string
func (d deleteGlobal) exec(vm *vm) {
name := string(d)
var ret bool
if vm.r.globalObject.self.hasPropertyStr(name) {
ret = vm.r.globalObject.self.deleteStr(name, false)
} else {
ret = true
}
if ret {
vm.push(valueTrue)
} else {
vm.push(valueFalse)
}
vm.pc++
}
type resolveVar1Strict string
func (s resolveVar1Strict) exec(vm *vm) {
name := string(s)
var ref ref
for stash := vm.stash; stash != nil; stash = stash.outer {
if stash.obj != nil {
if stash.obj.hasPropertyStr(name) {
ref = &objRef{
base: stash.obj,
name: name,
strict: true,
}
goto end
}
} else {
if idx, exists := stash.names[name]; exists {
ref = &stashRef{
v: &stash.values[idx],
}
goto end
}
}
}
if vm.r.globalObject.self.hasPropertyStr(name) {
ref = &objRef{
base: vm.r.globalObject.self,
name: name,
strict: true,
}
goto end
}
ref = &unresolvedRef{
runtime: vm.r,
name: string(s),
}
end:
vm.refStack = append(vm.refStack, ref)
vm.pc++
}
type setGlobal string
func (s setGlobal) exec(vm *vm) {
v := vm.peek()
vm.r.globalObject.self.putStr(string(s), v, false)
vm.pc++
}
type setVarStrict struct {
name string
idx uint32
}
func (s setVarStrict) exec(vm *vm) {
v := vm.peek()
level := int(s.idx >> 24)
idx := uint32(s.idx & 0x00FFFFFF)
stash := vm.stash
name := s.name
for i := 0; i < level; i++ {
if stash.put(name, v) {
goto end
}
stash = stash.outer
}
if stash != nil {
stash.putByIdx(idx, v)
} else {
o := vm.r.globalObject.self
if o.hasOwnPropertyStr(name) {
o.putStr(name, v, true)
} else {
vm.r.throwReferenceError(name)
}
}
end:
vm.pc++
}
type setVar1Strict string
func (s setVar1Strict) exec(vm *vm) {
v := vm.peek()
var o objectImpl
name := string(s)
for stash := vm.stash; stash != nil; stash = stash.outer {
if stash.put(name, v) {
goto end
}
}
o = vm.r.globalObject.self
if o.hasOwnPropertyStr(name) {
o.putStr(name, v, true)
} else {
vm.r.throwReferenceError(name)
}
end:
vm.pc++
}
type setGlobalStrict string
func (s setGlobalStrict) exec(vm *vm) {
v := vm.peek()
name := string(s)
o := vm.r.globalObject.self
if o.hasOwnPropertyStr(name) {
o.putStr(name, v, true)
} else {
vm.r.throwReferenceError(name)
}
vm.pc++
}
type getLocal uint32
func (g getLocal) exec(vm *vm) {
level := int(g >> 24)
idx := uint32(g & 0x00FFFFFF)
stash := vm.stash
for i := 0; i < level; i++ {
stash = stash.outer
}
vm.push(stash.getByIdx(idx))
vm.pc++
}
type getVar struct {
name string
idx uint32
ref bool
}
func (g getVar) exec(vm *vm) {
level := int(g.idx >> 24)
idx := uint32(g.idx & 0x00FFFFFF)
stash := vm.stash
name := g.name
for i := 0; i < level; i++ {
if v, found := stash.getByName(name, vm); found {
vm.push(v)
goto end
}
stash = stash.outer
}
if stash != nil {
vm.push(stash.getByIdx(idx))
} else {
v := vm.r.globalObject.self.getStr(name)
if v == nil {
if g.ref {
v = valueUnresolved{r: vm.r, ref: name}
} else {
vm.r.throwReferenceError(name)
}
}
vm.push(v)
}
end:
vm.pc++
}
type resolveVar struct {
name string
idx uint32
strict bool
}
func (r resolveVar) exec(vm *vm) {
level := int(r.idx >> 24)
idx := uint32(r.idx & 0x00FFFFFF)
stash := vm.stash
var ref ref
for i := 0; i < level; i++ {
if stash.obj != nil {
if stash.obj.hasPropertyStr(r.name) {
ref = &objRef{
base: stash.obj,
name: r.name,
strict: r.strict,
}
goto end
}
} else {
if idx, exists := stash.names[r.name]; exists {
ref = &stashRef{
v: &stash.values[idx],
}
goto end
}
}
stash = stash.outer
}
if stash != nil {
ref = &stashRef{
v: &stash.values[idx],
}
goto end
} /*else {
if vm.r.globalObject.self.hasProperty(nameVal) {
ref = &objRef{
base: vm.r.globalObject.self,
name: r.name,
}
goto end
}
} */
ref = &unresolvedRef{
runtime: vm.r,
name: r.name,
}
end:
vm.refStack = append(vm.refStack, ref)
vm.pc++
}
type _getValue struct{}
var getValue _getValue
func (_getValue) exec(vm *vm) {
ref := vm.refStack[len(vm.refStack)-1]
if v := ref.get(); v != nil {
vm.push(v)
} else {
vm.r.throwReferenceError(ref.refname())
panic("Unreachable")
}
vm.pc++
}
type _putValue struct{}
var putValue _putValue
func (_putValue) exec(vm *vm) {
l := len(vm.refStack) - 1
ref := vm.refStack[l]
vm.refStack[l] = nil
vm.refStack = vm.refStack[:l]
ref.set(vm.stack[vm.sp-1])
vm.pc++
}
type getVar1 string
func (n getVar1) exec(vm *vm) {
name := string(n)
var val Value
for stash := vm.stash; stash != nil; stash = stash.outer {
if v, exists := stash.getByName(name, vm); exists {
val = v
break
}
}
if val == nil {
val = vm.r.globalObject.self.getStr(name)
if val == nil {
vm.r.throwReferenceError(name)
}
}
vm.push(val)
vm.pc++
}
type getVar1Callee string
func (n getVar1Callee) exec(vm *vm) {
name := string(n)
var val Value
for stash := vm.stash; stash != nil; stash = stash.outer {
if v, exists := stash.getByName(name, vm); exists {
val = v
break
}
}
if val == nil {
val = vm.r.globalObject.self.getStr(name)
if val == nil {
val = valueUnresolved{r: vm.r, ref: name}
}
}
vm.push(val)
vm.pc++
}
type _pop struct{}
var pop _pop
func (_pop) exec(vm *vm) {
vm.sp--
vm.pc++
}
type _swap struct{}
var swap _swap
func (_swap) exec(vm *vm) {
vm.stack[vm.sp-1], vm.stack[vm.sp-2] = vm.stack[vm.sp-2], vm.stack[vm.sp-1]
vm.pc++
}
func (vm *vm) callEval(n int, strict bool) {
if vm.r.toObject(vm.stack[vm.sp-n-1]) == vm.r.global.Eval {
if n > 0 {
srcVal := vm.stack[vm.sp-n]
if src, ok := srcVal.assertString(); ok {
var this Value
if vm.sb != 0 {
this = vm.stack[vm.sb]
} else {
this = vm.r.globalObject
}
ret := vm.r.eval(src.String(), true, strict, this)
vm.stack[vm.sp-n-2] = ret
} else {
vm.stack[vm.sp-n-2] = srcVal
}
} else {
vm.stack[vm.sp-n-2] = _undefined
}
vm.sp -= n + 1
vm.pc++
} else {
call(n).exec(vm)
}
}
type callEval uint32
func (numargs callEval) exec(vm *vm) {
vm.callEval(int(numargs), false)
}
type callEvalStrict uint32
func (numargs callEvalStrict) exec(vm *vm) {
vm.callEval(int(numargs), true)
}
type _boxThis struct{}
var boxThis _boxThis
func (_boxThis) exec(vm *vm) {
v := vm.stack[vm.sb]
if v == _undefined || v == _null {
vm.stack[vm.sb] = vm.r.globalObject
} else {
vm.stack[vm.sb] = v.ToObject(vm.r)
}
vm.pc++
}
type call uint32
func (numargs call) exec(vm *vm) {
// this
// callee
// arg0
// ...
// arg<numargs-1>
n := int(numargs)
v := vm.stack[vm.sp-n-1] // callee
obj := vm.r.toCallee(v)
repeat:
switch f := obj.self.(type) {
case *funcObject:
vm.pc++
vm.pushCtx()
vm.args = n
vm.prg = f.prg
vm.stash = f.stash
vm.pc = 0
vm.stack[vm.sp-n-1], vm.stack[vm.sp-n-2] = vm.stack[vm.sp-n-2], vm.stack[vm.sp-n-1]
return
case *nativeFuncObject:
vm._nativeCall(f, n)
case *boundFuncObject:
vm._nativeCall(&f.nativeFuncObject, n)
case *lazyObject:
obj.self = f.create(obj)
goto repeat
default:
vm.r.typeErrorResult(true, "Not a function: %s", obj.ToString())
}
}
func (vm *vm) _nativeCall(f *nativeFuncObject, n int) {
if f.f != nil {
vm.pushCtx()
vm.prg = nil
vm.funcName = f.nameProp.get(nil).String()
ret := f.f(FunctionCall{
Arguments: vm.stack[vm.sp-n : vm.sp],
This: vm.stack[vm.sp-n-2],
})
if ret == nil {
ret = _undefined
}
vm.stack[vm.sp-n-2] = ret
vm.popCtx()
} else {
vm.stack[vm.sp-n-2] = _undefined
}
vm.sp -= n + 1
vm.pc++
}
type enterFunc uint32
func (e enterFunc) exec(vm *vm) {
// Input stack:
//
// callee
// this
// arg0
// ...
// argN
// <- sp
// Output stack:
//
// this <- sb
// <- sp
vm.newStash()
offset := vm.args - int(e)
vm.stash.values = make([]Value, e)
if offset > 0 {
copy(vm.stash.values, vm.stack[vm.sp-vm.args:])
vm.stash.extraArgs = make([]Value, offset)
copy(vm.stash.extraArgs, vm.stack[vm.sp-offset:])
} else {
copy(vm.stash.values, vm.stack[vm.sp-vm.args:])
vv := vm.stash.values[vm.args:]
for i, _ := range vv {
vv[i] = _undefined
}
}
vm.sp -= vm.args
vm.sb = vm.sp - 1
vm.pc++
}
type _ret struct{}
var ret _ret
func (_ret) exec(vm *vm) {
// callee -3
// this -2
// retval -1
vm.stack[vm.sp-3] = vm.stack[vm.sp-1]
vm.sp -= 2
vm.popCtx()
if vm.pc < 0 {
vm.halt = true
}
}
type enterFuncStashless struct {
stackSize uint32
args uint32
}
func (e enterFuncStashless) exec(vm *vm) {
vm.sb = vm.sp - vm.args - 1
var ss int
d := int(e.args) - vm.args
if d > 0 {
ss = int(e.stackSize) + d
vm.args = int(e.args)
} else {
ss = int(e.stackSize)
}
sp := vm.sp
if ss > 0 {
vm.sp += int(ss)
vm.stack.expand(vm.sp)
s := vm.stack[sp:vm.sp]
for i, _ := range s {
s[i] = _undefined
}
}
vm.pc++
}
type _retStashless struct{}
var retStashless _retStashless
func (_retStashless) exec(vm *vm) {
retval := vm.stack[vm.sp-1]
vm.sp = vm.sb
vm.stack[vm.sp-1] = retval
vm.popCtx()
if vm.pc < 0 {
vm.halt = true
}
}
type newFunc struct {
prg *Program
name string
length uint32
strict bool
srcStart, srcEnd uint32
}
func (n *newFunc) exec(vm *vm) {
obj := vm.r.newFunc(n.name, int(n.length), n.strict)
obj.prg = n.prg
obj.stash = vm.stash
obj.src = n.prg.src.src[n.srcStart:n.srcEnd]
vm.push(obj.val)
vm.pc++
}
type bindName string
func (d bindName) exec(vm *vm) {
if vm.stash != nil {
vm.stash.createBinding(string(d))
} else {
vm.r.globalObject.self._putProp(string(d), _undefined, true, true, false)
}
vm.pc++
}
type jne int32
func (j jne) exec(vm *vm) {
vm.sp--
if !vm.stack[vm.sp].ToBoolean() {
vm.pc += int(j)
} else {
vm.pc++
}
}
type jeq int32
func (j jeq) exec(vm *vm) {
vm.sp--
if vm.stack[vm.sp].ToBoolean() {
vm.pc += int(j)
} else {
vm.pc++
}
}
type jeq1 int32
func (j jeq1) exec(vm *vm) {
if vm.stack[vm.sp-1].ToBoolean() {
vm.pc += int(j)
} else {
vm.pc++
}
}
type jneq1 int32
func (j jneq1) exec(vm *vm) {
if !vm.stack[vm.sp-1].ToBoolean() {
vm.pc += int(j)
} else {
vm.pc++
}
}
type _not struct{}
var not _not
func (_not) exec(vm *vm) {
if vm.stack[vm.sp-1].ToBoolean() {
vm.stack[vm.sp-1] = valueFalse
} else {
vm.stack[vm.sp-1] = valueTrue
}
vm.pc++
}
func toPrimitiveNumber(v Value) Value {
if o, ok := v.(*Object); ok {
return o.self.toPrimitiveNumber()
}
return v
}
func cmp(px, py Value) Value {
var ret bool
var nx, ny float64
if xs, ok := px.assertString(); ok {
if ys, ok := py.assertString(); ok {
ret = xs.compareTo(ys) < 0
goto end
}
}
if xi, ok := px.assertInt(); ok {
if yi, ok := py.assertInt(); ok {
ret = xi < yi
goto end
}
}
nx = px.ToFloat()
ny = py.ToFloat()
if math.IsNaN(nx) || math.IsNaN(ny) {
return _undefined
}
ret = nx < ny
end:
if ret {
return valueTrue
}
return valueFalse
}
type _op_lt struct{}
var op_lt _op_lt
func (_op_lt) exec(vm *vm) {
left := toPrimitiveNumber(vm.stack[vm.sp-2])
right := toPrimitiveNumber(vm.stack[vm.sp-1])
r := cmp(left, right)
if r == _undefined {
vm.stack[vm.sp-2] = valueFalse
} else {
vm.stack[vm.sp-2] = r
}
vm.sp--
vm.pc++
}
type _op_lte struct{}
var op_lte _op_lte
func (_op_lte) exec(vm *vm) {
left := toPrimitiveNumber(vm.stack[vm.sp-2])
right := toPrimitiveNumber(vm.stack[vm.sp-1])
r := cmp(right, left)
if r == _undefined || r == valueTrue {
vm.stack[vm.sp-2] = valueFalse
} else {
vm.stack[vm.sp-2] = valueTrue
}
vm.sp--
vm.pc++
}
type _op_gt struct{}
var op_gt _op_gt
func (_op_gt) exec(vm *vm) {
left := toPrimitiveNumber(vm.stack[vm.sp-2])
right := toPrimitiveNumber(vm.stack[vm.sp-1])
r := cmp(right, left)
if r == _undefined {
vm.stack[vm.sp-2] = valueFalse
} else {
vm.stack[vm.sp-2] = r
}
vm.sp--
vm.pc++
}
type _op_gte struct{}
var op_gte _op_gte
func (_op_gte) exec(vm *vm) {
left := toPrimitiveNumber(vm.stack[vm.sp-2])
right := toPrimitiveNumber(vm.stack[vm.sp-1])
r := cmp(left, right)
if r == _undefined || r == valueTrue {
vm.stack[vm.sp-2] = valueFalse
} else {
vm.stack[vm.sp-2] = valueTrue
}
vm.sp--
vm.pc++
}
type _op_eq struct{}
var op_eq _op_eq
func (_op_eq) exec(vm *vm) {
if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
vm.stack[vm.sp-2] = valueTrue
} else {
vm.stack[vm.sp-2] = valueFalse
}
vm.sp--
vm.pc++
}
type _op_neq struct{}
var op_neq _op_neq
func (_op_neq) exec(vm *vm) {
if vm.stack[vm.sp-2].Equals(vm.stack[vm.sp-1]) {
vm.stack[vm.sp-2] = valueFalse
} else {
vm.stack[vm.sp-2] = valueTrue
}
vm.sp--
vm.pc++
}
type _op_strict_eq struct{}
var op_strict_eq _op_strict_eq
func (_op_strict_eq) exec(vm *vm) {
if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
vm.stack[vm.sp-2] = valueTrue
} else {
vm.stack[vm.sp-2] = valueFalse
}
vm.sp--
vm.pc++
}
type _op_strict_neq struct{}
var op_strict_neq _op_strict_neq
func (_op_strict_neq) exec(vm *vm) {
if vm.stack[vm.sp-2].StrictEquals(vm.stack[vm.sp-1]) {
vm.stack[vm.sp-2] = valueFalse
} else {
vm.stack[vm.sp-2] = valueTrue
}
vm.sp--
vm.pc++
}
type _op_instanceof struct{}
var op_instanceof _op_instanceof
func (_op_instanceof) exec(vm *vm) {
left := vm.stack[vm.sp-2]
right := vm.r.toObject(vm.stack[vm.sp-1])
if right.self.hasInstance(left) {
vm.stack[vm.sp-2] = valueTrue
} else {
vm.stack[vm.sp-2] = valueFalse
}
vm.sp--
vm.pc++
}
type _op_in struct{}
var op_in _op_in
func (_op_in) exec(vm *vm) {
left := vm.stack[vm.sp-2]
right := vm.r.toObject(vm.stack[vm.sp-1])
if right.self.hasProperty(left) {
vm.stack[vm.sp-2] = valueTrue
} else {
vm.stack[vm.sp-2] = valueFalse
}
vm.sp--
vm.pc++
}
type try struct {
catchOffset int32
finallyOffset int32
dynamic bool
}
func (t try) exec(vm *vm) {
o := vm.pc
vm.pc++
ex := vm.runTry()
if ex != nil && t.catchOffset > 0 {
// run the catch block (in try)
vm.pc = o + int(t.catchOffset)
// TODO: if ex.val is an Error, set the stack property
if t.dynamic {
vm.newStash()
vm.stash.putByIdx(0, ex.val)
} else {
vm.push(ex.val)
}
ex = vm.runTry()
if t.dynamic {
vm.stash = vm.stash.outer
}
}
if t.finallyOffset > 0 {
pc := vm.pc
// Run finally
vm.pc = o + int(t.finallyOffset)
vm.run()
if vm.prg.code[vm.pc] == retFinally {
vm.pc = pc
} else {
// break or continue out of finally, dropping exception
ex = nil
}
}
vm.halt = false
if ex != nil {
panic(ex)
}
}
type _retFinally struct{}
var retFinally _retFinally
func (_retFinally) exec(vm *vm) {
vm.pc++
}
type enterCatch string
func (varName enterCatch) exec(vm *vm) {
vm.stash.names = map[string]uint32{
string(varName): 0,
}
vm.pc++
}
type _throw struct{}
var throw _throw
func (_throw) exec(vm *vm) {
panic(vm.stack[vm.sp-1])
}
type _new uint32
func (n _new) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-1-int(n)])
repeat:
switch f := obj.self.(type) {
case *funcObject:
args := make([]Value, n)
copy(args, vm.stack[vm.sp-int(n):])
vm.sp -= int(n)
vm.stack[vm.sp-1] = f.construct(args)
case *nativeFuncObject:
vm._nativeNew(f, int(n))
case *boundFuncObject:
vm._nativeNew(&f.nativeFuncObject, int(n))
case *lazyObject:
obj.self = f.create(obj)
goto repeat
default:
vm.r.typeErrorResult(true, "Not a constructor")
}
vm.pc++
}
func (vm *vm) _nativeNew(f *nativeFuncObject, n int) {
if f.construct != nil {
args := make([]Value, n)
copy(args, vm.stack[vm.sp-n:])
vm.sp -= n
vm.stack[vm.sp-1] = f.construct(args)
} else {
vm.r.typeErrorResult(true, "Not a constructor")
}
}
type _typeof struct{}
var typeof _typeof
func (_typeof) exec(vm *vm) {
var r Value
switch v := vm.stack[vm.sp-1].(type) {
case valueUndefined, valueUnresolved:
r = stringUndefined
case valueNull:
r = stringObjectC
case *Object:
repeat:
switch s := v.self.(type) {
case *funcObject, *nativeFuncObject, *boundFuncObject:
r = stringFunction
case *lazyObject:
v.self = s.create(v)
goto repeat
default:
r = stringObjectC
}
case valueBool:
r = stringBoolean
case valueString:
r = stringString
case valueInt, valueFloat:
r = stringNumber
default:
panic(fmt.Errorf("Unknown type: %T", v))
}
vm.stack[vm.sp-1] = r
vm.pc++
}
type createArgs uint32
func (formalArgs createArgs) exec(vm *vm) {
v := &Object{runtime: vm.r}
args := &argumentsObject{}
args.extensible = true
args.prototype = vm.r.global.ObjectPrototype
args.class = "Arguments"
v.self = args
args.val = v
args.length = vm.args
args.init()
i := 0
c := int(formalArgs)
if vm.args < c {
c = vm.args
}
for ; i < c; i++ {
args._put(strconv.Itoa(i), &mappedProperty{
valueProperty: valueProperty{
writable: true,
configurable: true,
enumerable: true,
},
v: &vm.stash.values[i],
})
}
for _, v := range vm.stash.extraArgs {
args._put(strconv.Itoa(i), v)
i++
}
args._putProp("callee", vm.stack[vm.sb-1], true, false, true)
vm.push(v)
vm.pc++
}
type createArgsStrict uint32
func (formalArgs createArgsStrict) exec(vm *vm) {
args := vm.r.newBaseObject(vm.r.global.ObjectPrototype, "Arguments")
i := 0
c := int(formalArgs)
if vm.args < c {
c = vm.args
}
for _, v := range vm.stash.values[:c] {
args._put(strconv.Itoa(i), v)
i++
}
for _, v := range vm.stash.extraArgs {
args._put(strconv.Itoa(i), v)
i++
}
args._putProp("length", intToValue(int64(vm.args)), true, false, true)
args._put("callee", vm.r.global.throwerProperty)
args._put("caller", vm.r.global.throwerProperty)
vm.push(args.val)
vm.pc++
}
type _enterWith struct{}
var enterWith _enterWith
func (_enterWith) exec(vm *vm) {
vm.newStash()
vm.stash.obj = vm.stack[vm.sp-1].ToObject(vm.r).self
vm.sp--
vm.pc++
}
type _leaveWith struct{}
var leaveWith _leaveWith
func (_leaveWith) exec(vm *vm) {
vm.stash = vm.stash.outer
vm.pc++
}
func emptyIter() (propIterItem, iterNextFunc) {
return propIterItem{}, nil
}
type _enumerate struct{}
var enumerate _enumerate
func (_enumerate) exec(vm *vm) {
v := vm.stack[vm.sp-1]
if v == _undefined || v == _null {
vm.iterStack = append(vm.iterStack, iterStackItem{f: emptyIter})
} else {
vm.iterStack = append(vm.iterStack, iterStackItem{f: v.ToObject(vm.r).self.enumerate(false, true)})
}
vm.sp--
vm.pc++
}
type enumNext int32
func (jmp enumNext) exec(vm *vm) {
l := len(vm.iterStack) - 1
item, n := vm.iterStack[l].f()
if n != nil {
vm.iterStack[l].val = newStringValue(item.name)
vm.iterStack[l].f = n
vm.pc++
} else {
vm.pc += int(jmp)
}
}
type _enumGet struct{}
var enumGet _enumGet
func (_enumGet) exec(vm *vm) {
l := len(vm.iterStack) - 1
vm.push(vm.iterStack[l].val)
vm.pc++
}
type _enumPop struct{}
var enumPop _enumPop
func (_enumPop) exec(vm *vm) {
l := len(vm.iterStack) - 1
vm.iterStack[l] = iterStackItem{}
vm.iterStack = vm.iterStack[:l]
vm.pc++
}
|