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
|
#include "jsi.h"
#include "jscompile.h"
#include "jsvalue.h"
#include "jsrun.h"
#include "utf.h"
static void jsR_run(js_State *J, js_Function *F);
/* Push values on stack */
#define STACK (J->stack)
#define TOP (J->top)
#define BOT (J->bot)
static void js_stackoverflow(js_State *J)
{
STACK[TOP].type = JS_TLITSTR;
STACK[TOP].u.litstr = "stack overflow";
++TOP;
js_throw(J);
}
static void js_outofmemory(js_State *J)
{
STACK[TOP].type = JS_TLITSTR;
STACK[TOP].u.litstr = "out of memory";
++TOP;
js_throw(J);
}
void *js_malloc(js_State *J, int size)
{
void *ptr = J->alloc(J->actx, NULL, size);
if (!ptr)
js_outofmemory(J);
return ptr;
}
void *js_realloc(js_State *J, void *ptr, int size)
{
ptr = J->alloc(J->actx, ptr, size);
if (!ptr)
js_outofmemory(J);
return ptr;
}
char *js_strdup(js_State *J, const char *s)
{
int n = strlen(s) + 1;
char *p = js_malloc(J, n);
memcpy(p, s, n);
return p;
}
void js_free(js_State *J, void *ptr)
{
J->alloc(J->actx, ptr, 0);
}
js_String *jsV_newmemstring(js_State *J, const char *s, int n)
{
js_String *v = js_malloc(J, soffsetof(js_String, p) + n + 1);
memcpy(v->p, s, n);
v->p[n] = 0;
v->gcmark = 0;
v->gcnext = J->gcstr;
J->gcstr = v;
++J->gccounter;
return v;
}
#define CHECKSTACK(n) if (TOP + n >= JS_STACKSIZE) js_stackoverflow(J)
void js_pushvalue(js_State *J, js_Value v)
{
CHECKSTACK(1);
STACK[TOP] = v;
++TOP;
}
void js_pushundefined(js_State *J)
{
CHECKSTACK(1);
STACK[TOP].type = JS_TUNDEFINED;
++TOP;
}
void js_pushnull(js_State *J)
{
CHECKSTACK(1);
STACK[TOP].type = JS_TNULL;
++TOP;
}
void js_pushboolean(js_State *J, int v)
{
CHECKSTACK(1);
STACK[TOP].type = JS_TBOOLEAN;
STACK[TOP].u.boolean = !!v;
++TOP;
}
void js_pushnumber(js_State *J, double v)
{
CHECKSTACK(1);
STACK[TOP].type = JS_TNUMBER;
STACK[TOP].u.number = v;
++TOP;
}
void js_pushstring(js_State *J, const char *v)
{
int n = strlen(v);
CHECKSTACK(1);
if (n <= soffsetof(js_Value, type)) {
char *s = STACK[TOP].u.shrstr;
while (n--) *s++ = *v++;
*s = 0;
STACK[TOP].type = JS_TSHRSTR;
} else {
STACK[TOP].type = JS_TMEMSTR;
STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
}
++TOP;
}
void js_pushlstring(js_State *J, const char *v, int n)
{
CHECKSTACK(1);
if (n <= soffsetof(js_Value, type)) {
char *s = STACK[TOP].u.shrstr;
while (n--) *s++ = *v++;
*s = 0;
STACK[TOP].type = JS_TSHRSTR;
} else {
STACK[TOP].type = JS_TMEMSTR;
STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
}
++TOP;
}
void js_pushliteral(js_State *J, const char *v)
{
CHECKSTACK(1);
STACK[TOP].type = JS_TLITSTR;
STACK[TOP].u.litstr = v;
++TOP;
}
void js_pushobject(js_State *J, js_Object *v)
{
CHECKSTACK(1);
STACK[TOP].type = JS_TOBJECT;
STACK[TOP].u.object = v;
++TOP;
}
void js_pushglobal(js_State *J)
{
js_pushobject(J, J->G);
}
void js_currentfunction(js_State *J)
{
CHECKSTACK(1);
STACK[TOP] = STACK[BOT-1];
++TOP;
}
/* Read values from stack */
static js_Value *stackidx(js_State *J, int idx)
{
static js_Value undefined = { {0}, {0}, JS_TUNDEFINED };
idx = idx < 0 ? TOP + idx : BOT + idx;
if (idx < 0 || idx >= TOP)
return &undefined;
return STACK + idx;
}
js_Value *js_tovalue(js_State *J, int idx)
{
return stackidx(J, idx);
}
int js_isdefined(js_State *J, int idx) { return stackidx(J, idx)->type != JS_TUNDEFINED; }
int js_isundefined(js_State *J, int idx) { return stackidx(J, idx)->type == JS_TUNDEFINED; }
int js_isnull(js_State *J, int idx) { return stackidx(J, idx)->type == JS_TNULL; }
int js_isboolean(js_State *J, int idx) { return stackidx(J, idx)->type == JS_TBOOLEAN; }
int js_isnumber(js_State *J, int idx) { return stackidx(J, idx)->type == JS_TNUMBER; }
int js_isstring(js_State *J, int idx) { enum js_Type t = stackidx(J, idx)->type; return t == JS_TSHRSTR || t == JS_TLITSTR || t == JS_TMEMSTR; }
int js_isprimitive(js_State *J, int idx) { return stackidx(J, idx)->type != JS_TOBJECT; }
int js_isobject(js_State *J, int idx) { return stackidx(J, idx)->type == JS_TOBJECT; }
int js_iscoercible(js_State *J, int idx) { js_Value *v = stackidx(J, idx); return v->type != JS_TUNDEFINED && v->type != JS_TNULL; }
int js_iscallable(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
if (v->type == JS_TOBJECT)
return v->u.object->type == JS_CFUNCTION ||
v->u.object->type == JS_CSCRIPT ||
v->u.object->type == JS_CEVAL ||
v->u.object->type == JS_CCFUNCTION;
return 0;
}
int js_isarray(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
return v->type == JS_TOBJECT && v->u.object->type == JS_CARRAY;
}
int js_isregexp(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
return v->type == JS_TOBJECT && v->u.object->type == JS_CREGEXP;
}
int js_isuserdata(js_State *J, int idx, const char *tag)
{
js_Value *v = stackidx(J, idx);
if (v->type == JS_TOBJECT && v->u.object->type == JS_CUSERDATA)
return !strcmp(tag, v->u.object->u.user.tag);
return 0;
}
int js_iserror(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
return v->type == JS_TOBJECT && v->u.object->type == JS_CERROR;
}
const char *js_typeof(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
switch (v->type) {
default:
case JS_TSHRSTR: return "string";
case JS_TUNDEFINED: return "undefined";
case JS_TNULL: return "object";
case JS_TBOOLEAN: return "boolean";
case JS_TNUMBER: return "number";
case JS_TLITSTR: return "string";
case JS_TMEMSTR: return "string";
case JS_TOBJECT:
if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
return "function";
return "object";
}
}
int js_toboolean(js_State *J, int idx)
{
return jsV_toboolean(J, stackidx(J, idx));
}
double js_tonumber(js_State *J, int idx)
{
return jsV_tonumber(J, stackidx(J, idx));
}
int js_tointeger(js_State *J, int idx)
{
return jsV_numbertointeger(jsV_tonumber(J, stackidx(J, idx)));
}
int js_toint32(js_State *J, int idx)
{
return jsV_numbertoint32(jsV_tonumber(J, stackidx(J, idx)));
}
unsigned int js_touint32(js_State *J, int idx)
{
return jsV_numbertouint32(jsV_tonumber(J, stackidx(J, idx)));
}
short js_toint16(js_State *J, int idx)
{
return jsV_numbertoint16(jsV_tonumber(J, stackidx(J, idx)));
}
unsigned short js_touint16(js_State *J, int idx)
{
return jsV_numbertouint16(jsV_tonumber(J, stackidx(J, idx)));
}
const char *js_tostring(js_State *J, int idx)
{
return jsV_tostring(J, stackidx(J, idx));
}
js_Object *js_toobject(js_State *J, int idx)
{
return jsV_toobject(J, stackidx(J, idx));
}
void js_toprimitive(js_State *J, int idx, int hint)
{
jsV_toprimitive(J, stackidx(J, idx), hint);
}
js_Regexp *js_toregexp(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
if (v->type == JS_TOBJECT && v->u.object->type == JS_CREGEXP)
return &v->u.object->u.r;
js_typeerror(J, "not a regexp");
}
void *js_touserdata(js_State *J, int idx, const char *tag)
{
js_Value *v = stackidx(J, idx);
if (v->type == JS_TOBJECT && v->u.object->type == JS_CUSERDATA)
if (!strcmp(tag, v->u.object->u.user.tag))
return v->u.object->u.user.data;
js_typeerror(J, "not a %s", tag);
}
static js_Object *jsR_tofunction(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
if (v->type == JS_TUNDEFINED || v->type == JS_TNULL)
return NULL;
if (v->type == JS_TOBJECT)
if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
return v->u.object;
js_typeerror(J, "not a function");
}
/* Stack manipulation */
int js_gettop(js_State *J)
{
return TOP - BOT;
}
void js_pop(js_State *J, int n)
{
TOP -= n;
if (TOP < BOT) {
TOP = BOT;
js_error(J, "stack underflow!");
}
}
void js_remove(js_State *J, int idx)
{
idx = idx < 0 ? TOP + idx : BOT + idx;
if (idx < BOT || idx >= TOP)
js_error(J, "stack error!");
for (;idx < TOP - 1; ++idx)
STACK[idx] = STACK[idx+1];
--TOP;
}
void js_insert(js_State *J, int idx)
{
js_error(J, "not implemented yet");
}
void js_replace(js_State* J, int idx)
{
idx = idx < 0 ? TOP + idx : BOT + idx;
if (idx < BOT || idx >= TOP)
js_error(J, "stack error!");
STACK[idx] = STACK[--TOP];
}
void js_copy(js_State *J, int idx)
{
CHECKSTACK(1);
STACK[TOP] = *stackidx(J, idx);
++TOP;
}
void js_dup(js_State *J)
{
CHECKSTACK(1);
STACK[TOP] = STACK[TOP-1];
++TOP;
}
void js_dup2(js_State *J)
{
CHECKSTACK(2);
STACK[TOP] = STACK[TOP-2];
STACK[TOP+1] = STACK[TOP-1];
TOP += 2;
}
void js_rot2(js_State *J)
{
/* A B -> B A */
js_Value tmp = STACK[TOP-1]; /* A B (B) */
STACK[TOP-1] = STACK[TOP-2]; /* A A */
STACK[TOP-2] = tmp; /* B A */
}
void js_rot3(js_State *J)
{
/* A B C -> C A B */
js_Value tmp = STACK[TOP-1]; /* A B C (C) */
STACK[TOP-1] = STACK[TOP-2]; /* A B B */
STACK[TOP-2] = STACK[TOP-3]; /* A A B */
STACK[TOP-3] = tmp; /* C A B */
}
void js_rot4(js_State *J)
{
/* A B C D -> D A B C */
js_Value tmp = STACK[TOP-1]; /* A B C D (D) */
STACK[TOP-1] = STACK[TOP-2]; /* A B C C */
STACK[TOP-2] = STACK[TOP-3]; /* A B B C */
STACK[TOP-3] = STACK[TOP-4]; /* A A B C */
STACK[TOP-4] = tmp; /* D A B C */
}
void js_rot2pop1(js_State *J)
{
/* A B -> B */
STACK[TOP-2] = STACK[TOP-1];
--TOP;
}
void js_rot3pop2(js_State *J)
{
/* A B C -> C */
STACK[TOP-3] = STACK[TOP-1];
TOP -= 2;
}
void js_rot(js_State *J, int n)
{
int i;
js_Value tmp = STACK[TOP-1];
for (i = 1; i < n; ++i)
STACK[TOP-i] = STACK[TOP-i-1];
STACK[TOP-i] = tmp;
}
/* Property access that takes care of attributes and getters/setters */
int js_isarrayindex(js_State *J, const char *p, int *idx)
{
int n = 0;
/* check for empty string */
if (p[0] == 0)
return 0;
/* check for '0' and integers with leading zero */
if (p[0] == '0')
return (p[1] == 0) ? *idx = 0, 1 : 0;
while (*p) {
int c = *p++;
if (c >= '0' && c <= '9') {
if (n >= INT_MAX / 10)
return 0;
n = n * 10 + (c - '0');
} else {
return 0;
}
}
return *idx = n, 1;
}
static void js_pushrune(js_State *J, Rune rune)
{
char buf[UTFmax + 1];
if (rune >= 0) {
buf[runetochar(buf, &rune)] = 0;
js_pushstring(J, buf);
} else {
js_pushundefined(J);
}
}
static int jsR_hasproperty(js_State *J, js_Object *obj, const char *name)
{
js_Property *ref;
int k;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length")) {
js_pushnumber(J, obj->u.a.length);
return 1;
}
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length")) {
js_pushnumber(J, obj->u.s.length);
return 1;
}
if (js_isarrayindex(J, name, &k)) {
if (k >= 0 && k < obj->u.s.length) {
js_pushrune(J, js_runeat(J, obj->u.s.string, k));
return 1;
}
}
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) {
js_pushliteral(J, obj->u.r.source);
return 1;
}
if (!strcmp(name, "global")) {
js_pushboolean(J, obj->u.r.flags & JS_REGEXP_G);
return 1;
}
if (!strcmp(name, "ignoreCase")) {
js_pushboolean(J, obj->u.r.flags & JS_REGEXP_I);
return 1;
}
if (!strcmp(name, "multiline")) {
js_pushboolean(J, obj->u.r.flags & JS_REGEXP_M);
return 1;
}
if (!strcmp(name, "lastIndex")) {
js_pushnumber(J, obj->u.r.last);
return 1;
}
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.has && obj->u.user.has(J, obj->u.user.data, name))
return 1;
}
ref = jsV_getproperty(J, obj, name);
if (ref) {
if (ref->getter) {
js_pushobject(J, ref->getter);
js_pushobject(J, obj);
js_call(J, 0);
} else {
js_pushvalue(J, ref->value);
}
return 1;
}
return 0;
}
static void jsR_getproperty(js_State *J, js_Object *obj, const char *name)
{
if (!jsR_hasproperty(J, obj, name))
js_pushundefined(J);
}
static void jsR_setproperty(js_State *J, js_Object *obj, const char *name, int transient)
{
js_Value *value = stackidx(J, -1);
js_Property *ref;
int k;
int own;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length")) {
double rawlen = jsV_tonumber(J, value);
int newlen = jsV_numbertointeger(rawlen);
if (newlen != rawlen || newlen < 0)
js_rangeerror(J, "invalid array length");
jsV_resizearray(J, obj, newlen);
return;
}
if (js_isarrayindex(J, name, &k))
if (k >= obj->u.a.length)
obj->u.a.length = k + 1;
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length"))
goto readonly;
if (js_isarrayindex(J, name, &k))
if (k >= 0 && k < obj->u.s.length)
goto readonly;
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) goto readonly;
if (!strcmp(name, "global")) goto readonly;
if (!strcmp(name, "ignoreCase")) goto readonly;
if (!strcmp(name, "multiline")) goto readonly;
if (!strcmp(name, "lastIndex")) {
obj->u.r.last = jsV_tointeger(J, value);
return;
}
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.put && obj->u.user.put(J, obj->u.user.data, name))
return;
}
/* First try to find a setter in prototype chain */
ref = jsV_getpropertyx(J, obj, name, &own);
if (ref) {
if (ref->setter) {
js_pushobject(J, ref->setter);
js_pushobject(J, obj);
js_pushvalue(J, *value);
js_call(J, 1);
js_pop(J, 1);
return;
} else {
if (J->strict)
if (ref->getter)
js_typeerror(J, "setting property '%s' that only has a getter", name);
if (ref->atts & JS_READONLY)
goto readonly;
}
}
/* Property not found on this object, so create one */
if (!ref || !own) {
if (transient) {
if (J->strict)
js_typeerror(J, "cannot create property '%s' on transient object", name);
return;
}
ref = jsV_setproperty(J, obj, name);
}
if (ref) {
if (!(ref->atts & JS_READONLY))
ref->value = *value;
else
goto readonly;
}
return;
readonly:
if (J->strict)
js_typeerror(J, "'%s' is read-only", name);
}
static void jsR_defproperty(js_State *J, js_Object *obj, const char *name,
int atts, js_Value *value, js_Object *getter, js_Object *setter)
{
js_Property *ref;
int k;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length"))
goto readonly;
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length"))
goto readonly;
if (js_isarrayindex(J, name, &k))
if (k >= 0 && k < obj->u.s.length)
goto readonly;
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) goto readonly;
if (!strcmp(name, "global")) goto readonly;
if (!strcmp(name, "ignoreCase")) goto readonly;
if (!strcmp(name, "multiline")) goto readonly;
if (!strcmp(name, "lastIndex")) goto readonly;
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.put && obj->u.user.put(J, obj->u.user.data, name))
return;
}
ref = jsV_setproperty(J, obj, name);
if (ref) {
if (value) {
if (!(ref->atts & JS_READONLY))
ref->value = *value;
else if (J->strict)
js_typeerror(J, "'%s' is read-only", name);
}
if (getter) {
if (!(ref->atts & JS_DONTCONF))
ref->getter = getter;
else if (J->strict)
js_typeerror(J, "'%s' is non-configurable", name);
}
if (setter) {
if (!(ref->atts & JS_DONTCONF))
ref->setter = setter;
else if (J->strict)
js_typeerror(J, "'%s' is non-configurable", name);
}
ref->atts |= atts;
}
return;
readonly:
if (J->strict)
js_typeerror(J, "'%s' is read-only or non-configurable", name);
}
static int jsR_delproperty(js_State *J, js_Object *obj, const char *name)
{
js_Property *ref;
int k;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length"))
goto dontconf;
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length"))
goto dontconf;
if (js_isarrayindex(J, name, &k))
if (k >= 0 && k < obj->u.s.length)
goto dontconf;
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) goto dontconf;
if (!strcmp(name, "global")) goto dontconf;
if (!strcmp(name, "ignoreCase")) goto dontconf;
if (!strcmp(name, "multiline")) goto dontconf;
if (!strcmp(name, "lastIndex")) goto dontconf;
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.delete && obj->u.user.delete(J, obj->u.user.data, name))
return 1;
}
ref = jsV_getownproperty(J, obj, name);
if (ref) {
if (ref->atts & JS_DONTCONF)
goto dontconf;
jsV_delproperty(J, obj, name);
}
return 1;
dontconf:
if (J->strict)
js_typeerror(J, "'%s' is non-configurable", name);
return 0;
}
/* Registry, global and object property accessors */
const char *js_ref(js_State *J)
{
js_Value *v = stackidx(J, -1);
const char *s;
char buf[32];
switch (v->type) {
case JS_TUNDEFINED: s = "_Undefined"; break;
case JS_TNULL: s = "_Null"; break;
case JS_TBOOLEAN:
s = v->u.boolean ? "_True" : "_False";
break;
case JS_TOBJECT:
sprintf(buf, "%p", (void*)v->u.object);
s = js_intern(J, buf);
break;
default:
sprintf(buf, "%d", J->nextref++);
s = js_intern(J, buf);
break;
}
js_setregistry(J, s);
return s;
}
void js_unref(js_State *J, const char *ref)
{
js_delregistry(J, ref);
}
void js_getregistry(js_State *J, const char *name)
{
jsR_getproperty(J, J->R, name);
}
void js_setregistry(js_State *J, const char *name)
{
jsR_setproperty(J, J->R, name, 0);
js_pop(J, 1);
}
void js_delregistry(js_State *J, const char *name)
{
jsR_delproperty(J, J->R, name);
}
void js_getglobal(js_State *J, const char *name)
{
jsR_getproperty(J, J->G, name);
}
void js_setglobal(js_State *J, const char *name)
{
jsR_setproperty(J, J->G, name, 0);
js_pop(J, 1);
}
void js_defglobal(js_State *J, const char *name, int atts)
{
jsR_defproperty(J, J->G, name, atts, stackidx(J, -1), NULL, NULL);
js_pop(J, 1);
}
void js_delglobal(js_State *J, const char *name)
{
jsR_delproperty(J, J->G, name);
}
void js_getproperty(js_State *J, int idx, const char *name)
{
jsR_getproperty(J, js_toobject(J, idx), name);
}
void js_setproperty(js_State *J, int idx, const char *name)
{
jsR_setproperty(J, js_toobject(J, idx), name, !js_isobject(J, idx));
js_pop(J, 1);
}
void js_defproperty(js_State *J, int idx, const char *name, int atts)
{
jsR_defproperty(J, js_toobject(J, idx), name, atts, stackidx(J, -1), NULL, NULL);
js_pop(J, 1);
}
void js_delproperty(js_State *J, int idx, const char *name)
{
jsR_delproperty(J, js_toobject(J, idx), name);
}
void js_defaccessor(js_State *J, int idx, const char *name, int atts)
{
jsR_defproperty(J, js_toobject(J, idx), name, atts, NULL, jsR_tofunction(J, -2), jsR_tofunction(J, -1));
js_pop(J, 2);
}
int js_hasproperty(js_State *J, int idx, const char *name)
{
return jsR_hasproperty(J, js_toobject(J, idx), name);
}
/* Iterator */
void js_pushiterator(js_State *J, int idx, int own)
{
js_pushobject(J, jsV_newiterator(J, js_toobject(J, idx), own));
}
const char *js_nextiterator(js_State *J, int idx)
{
return jsV_nextiterator(J, js_toobject(J, idx));
}
/* Environment records */
js_Environment *jsR_newenvironment(js_State *J, js_Object *vars, js_Environment *outer)
{
js_Environment *E = js_malloc(J, sizeof *E);
E->gcmark = 0;
E->gcnext = J->gcenv;
J->gcenv = E;
++J->gccounter;
E->outer = outer;
E->variables = vars;
return E;
}
static void js_initvar(js_State *J, const char *name, int idx)
{
jsR_defproperty(J, J->E->variables, name, JS_DONTENUM | JS_DONTCONF, stackidx(J, idx), NULL, NULL);
}
static int js_hasvar(js_State *J, const char *name)
{
js_Environment *E = J->E;
do {
js_Property *ref = jsV_getproperty(J, E->variables, name);
if (ref) {
if (ref->getter) {
js_pushobject(J, ref->getter);
js_pushobject(J, E->variables);
js_call(J, 0);
} else {
js_pushvalue(J, ref->value);
}
return 1;
}
E = E->outer;
} while (E);
return 0;
}
static void js_setvar(js_State *J, const char *name)
{
js_Environment *E = J->E;
do {
js_Property *ref = jsV_getproperty(J, E->variables, name);
if (ref) {
if (ref->setter) {
js_pushobject(J, ref->setter);
js_pushobject(J, E->variables);
js_copy(J, -3);
js_call(J, 1);
js_pop(J, 1);
return;
}
if (!(ref->atts & JS_READONLY))
ref->value = *stackidx(J, -1);
else if (J->strict)
js_typeerror(J, "'%s' is read-only", name);
return;
}
E = E->outer;
} while (E);
if (J->strict)
js_referenceerror(J, "assignment to undeclared variable '%s'", name);
jsR_setproperty(J, J->G, name, 0);
}
static int js_delvar(js_State *J, const char *name)
{
js_Environment *E = J->E;
do {
js_Property *ref = jsV_getownproperty(J, E->variables, name);
if (ref) {
if (ref->atts & JS_DONTCONF) {
if (J->strict)
js_typeerror(J, "'%s' is non-configurable", name);
return 0;
}
jsV_delproperty(J, E->variables, name);
return 1;
}
E = E->outer;
} while (E);
return jsR_delproperty(J, J->G, name);
}
/* Function calls */
static void jsR_savescope(js_State *J, js_Environment *newE)
{
if (J->envtop + 1 >= JS_ENVLIMIT)
js_stackoverflow(J);
J->envstack[J->envtop++] = J->E;
J->E = newE;
}
static void jsR_restorescope(js_State *J)
{
J->E = J->envstack[--J->envtop];
}
static void jsR_calllwfunction(js_State *J, int n, js_Function *F, js_Environment *scope)
{
js_Value v;
int i;
jsR_savescope(J, scope);
if (n > F->numparams) {
js_pop(J, n - F->numparams);
n = F->numparams;
}
for (i = n; i < F->varlen; ++i)
js_pushundefined(J);
jsR_run(J, F);
v = *stackidx(J, -1);
TOP = --BOT; /* clear stack */
js_pushvalue(J, v);
jsR_restorescope(J);
}
static void jsR_callfunction(js_State *J, int n, js_Function *F, js_Environment *scope)
{
js_Value v;
int i;
scope = jsR_newenvironment(J, jsV_newobject(J, JS_COBJECT, NULL), scope);
jsR_savescope(J, scope);
if (F->arguments) {
js_newarguments(J);
if (!J->strict) {
js_currentfunction(J);
js_defproperty(J, -2, "callee", JS_DONTENUM);
}
js_pushnumber(J, n);
js_defproperty(J, -2, "length", JS_DONTENUM);
for (i = 0; i < n; ++i) {
js_copy(J, i + 1);
js_setindex(J, -2, i);
}
js_initvar(J, "arguments", -1);
js_pop(J, 1);
}
for (i = 0; i < n && i < F->numparams; ++i)
js_initvar(J, F->vartab[i], i + 1);
js_pop(J, n);
for (; i < F->varlen; ++i) {
js_pushundefined(J);
js_initvar(J, F->vartab[i], -1);
js_pop(J, 1);
}
jsR_run(J, F);
v = *stackidx(J, -1);
TOP = --BOT; /* clear stack */
js_pushvalue(J, v);
jsR_restorescope(J);
}
static void jsR_calleval(js_State *J, int n, js_Function *F, js_Environment *scope)
{
js_Value v;
int i;
scope = jsR_newenvironment(J, jsV_newobject(J, JS_COBJECT, NULL), scope);
jsR_savescope(J, scope);
/* scripts take no arguments */
js_pop(J, n);
for (i = 0; i < F->varlen; ++i) {
js_pushundefined(J);
js_initvar(J, F->vartab[i], -1);
js_pop(J, 1);
}
jsR_run(J, F);
v = *stackidx(J, -1);
TOP = --BOT; /* clear stack */
js_pushvalue(J, v);
jsR_restorescope(J);
}
static void jsR_callscript(js_State *J, int n, js_Function *F, js_Environment *scope)
{
js_Value v;
int i;
if (scope)
jsR_savescope(J, scope);
/* scripts take no arguments */
js_pop(J, n);
for (i = 0; i < F->varlen; ++i) {
js_pushundefined(J);
js_initvar(J, F->vartab[i], -1);
js_pop(J, 1);
}
jsR_run(J, F);
v = *stackidx(J, -1);
TOP = --BOT; /* clear stack */
js_pushvalue(J, v);
if (scope)
jsR_restorescope(J);
}
static void jsR_callcfunction(js_State *J, int n, int min, js_CFunction F)
{
int i;
js_Value v;
for (i = n; i < min; ++i)
js_pushundefined(J);
F(J);
v = *stackidx(J, -1);
TOP = --BOT; /* clear stack */
js_pushvalue(J, v);
}
static void jsR_pushtrace(js_State *J, const char *name, const char *file, int line)
{
if (J->tracetop + 1 == JS_ENVLIMIT)
js_error(J, "call stack overflow");
++J->tracetop;
J->trace[J->tracetop].name = name;
J->trace[J->tracetop].file = file;
J->trace[J->tracetop].line = line;
}
void js_call(js_State *J, int n)
{
js_Object *obj;
int savebot;
if (!js_iscallable(J, -n-2))
js_typeerror(J, "%s is not callable", js_typeof(J, -n-2));
obj = js_toobject(J, -n-2);
savebot = BOT;
BOT = TOP - n - 1;
if (obj->type == JS_CFUNCTION) {
jsR_pushtrace(J, obj->u.f.function->name, obj->u.f.function->filename, obj->u.f.function->line);
if (obj->u.f.function->lightweight)
jsR_calllwfunction(J, n, obj->u.f.function, obj->u.f.scope);
else
jsR_callfunction(J, n, obj->u.f.function, obj->u.f.scope);
--J->tracetop;
} else if (obj->type == JS_CSCRIPT) {
jsR_pushtrace(J, obj->u.f.function->name, obj->u.f.function->filename, obj->u.f.function->line);
jsR_callscript(J, n, obj->u.f.function, obj->u.f.scope);
--J->tracetop;
} else if (obj->type == JS_CEVAL) {
jsR_pushtrace(J, obj->u.f.function->name, obj->u.f.function->filename, obj->u.f.function->line);
jsR_calleval(J, n, obj->u.f.function, obj->u.f.scope);
--J->tracetop;
} else if (obj->type == JS_CCFUNCTION) {
jsR_pushtrace(J, obj->u.c.name, "native", 0);
jsR_callcfunction(J, n, obj->u.c.length, obj->u.c.function);
--J->tracetop;
}
BOT = savebot;
}
void js_construct(js_State *J, int n)
{
js_Object *obj;
js_Object *prototype;
js_Object *newobj;
if (!js_iscallable(J, -n-1))
js_typeerror(J, "%s is not callable", js_typeof(J, -n-1));
obj = js_toobject(J, -n-1);
/* built-in constructors create their own objects, give them a 'null' this */
if (obj->type == JS_CCFUNCTION && obj->u.c.constructor) {
int savebot = BOT;
js_pushnull(J);
if (n > 0)
js_rot(J, n + 1);
BOT = TOP - n - 1;
jsR_pushtrace(J, obj->u.c.name, "native", 0);
jsR_callcfunction(J, n, obj->u.c.length, obj->u.c.constructor);
--J->tracetop;
BOT = savebot;
return;
}
/* extract the function object's prototype property */
js_getproperty(J, -n - 1, "prototype");
if (js_isobject(J, -1))
prototype = js_toobject(J, -1);
else
prototype = J->Object_prototype;
js_pop(J, 1);
/* create a new object with above prototype, and shift it into the 'this' slot */
newobj = jsV_newobject(J, JS_COBJECT, prototype);
js_pushobject(J, newobj);
if (n > 0)
js_rot(J, n + 1);
/* call the function */
js_call(J, n);
/* if result is not an object, return the original object we created */
if (!js_isobject(J, -1)) {
js_pop(J, 1);
js_pushobject(J, newobj);
}
}
void js_eval(js_State *J)
{
if (!js_isstring(J, -1))
return;
js_loadeval(J, "(eval)", js_tostring(J, -1));
js_rot2pop1(J);
js_copy(J, 0); /* copy 'this' */
js_call(J, 0);
}
int js_pconstruct(js_State *J, int n)
{
int savetop = TOP - n - 2;
if (js_try(J)) {
/* clean up the stack to only hold the error object */
STACK[savetop] = STACK[TOP-1];
TOP = savetop + 1;
return 1;
}
js_construct(J, n);
js_endtry(J);
return 0;
}
int js_pcall(js_State *J, int n)
{
int savetop = TOP - n - 2;
if (js_try(J)) {
/* clean up the stack to only hold the error object */
STACK[savetop] = STACK[TOP-1];
TOP = savetop + 1;
return 1;
}
js_call(J, n);
js_endtry(J);
return 0;
}
/* Exceptions */
void *js_savetrypc(js_State *J, js_Instruction *pc)
{
if (J->trytop == JS_TRYLIMIT)
js_error(J, "try: exception stack overflow");
J->trybuf[J->trytop].E = J->E;
J->trybuf[J->trytop].envtop = J->envtop;
J->trybuf[J->trytop].tracetop = J->tracetop;
J->trybuf[J->trytop].top = J->top;
J->trybuf[J->trytop].bot = J->bot;
J->trybuf[J->trytop].strict = J->strict;
J->trybuf[J->trytop].pc = pc;
return J->trybuf[J->trytop++].buf;
}
void *js_savetry(js_State *J)
{
if (J->trytop == JS_TRYLIMIT)
js_error(J, "try: exception stack overflow");
J->trybuf[J->trytop].E = J->E;
J->trybuf[J->trytop].envtop = J->envtop;
J->trybuf[J->trytop].tracetop = J->tracetop;
J->trybuf[J->trytop].top = J->top;
J->trybuf[J->trytop].bot = J->bot;
J->trybuf[J->trytop].strict = J->strict;
J->trybuf[J->trytop].pc = NULL;
return J->trybuf[J->trytop++].buf;
}
void js_endtry(js_State *J)
{
if (J->trytop == 0)
js_error(J, "endtry: exception stack underflow");
--J->trytop;
}
void js_throw(js_State *J)
{
if (J->trytop > 0) {
js_Value v = *stackidx(J, -1);
--J->trytop;
J->E = J->trybuf[J->trytop].E;
J->envtop = J->trybuf[J->trytop].envtop;
J->tracetop = J->trybuf[J->trytop].tracetop;
J->top = J->trybuf[J->trytop].top;
J->bot = J->trybuf[J->trytop].bot;
J->strict = J->trybuf[J->trytop].strict;
js_pushvalue(J, v);
longjmp(J->trybuf[J->trytop].buf, 1);
}
if (J->panic)
J->panic(J);
abort();
}
/* Main interpreter loop */
static void jsR_dumpstack(js_State *J)
{
int i;
printf("stack {\n");
for (i = 0; i < TOP; ++i) {
putchar(i == BOT ? '>' : ' ');
printf("%4d: ", i);
js_dumpvalue(J, STACK[i]);
putchar('\n');
}
printf("}\n");
}
static void jsR_dumpenvironment(js_State *J, js_Environment *E, int d)
{
printf("scope %d ", d);
js_dumpobject(J, E->variables);
if (E->outer)
jsR_dumpenvironment(J, E->outer, d+1);
}
void js_stacktrace(js_State *J)
{
int n;
printf("stack trace:\n");
for (n = J->tracetop; n >= 0; --n) {
const char *name = J->trace[n].name;
const char *file = J->trace[n].file;
int line = J->trace[n].line;
if (line > 0) {
if (name[0])
printf("\tat %s (%s:%d)\n", name, file, line);
else
printf("\tat %s:%d\n", file, line);
} else
printf("\tat %s (%s)\n", name, file);
}
}
void js_trap(js_State *J, int pc)
{
if (pc > 0) {
js_Function *F = STACK[BOT-1].u.object->u.f.function;
printf("trap at %d in function ", pc);
jsC_dumpfunction(J, F);
}
jsR_dumpstack(J);
jsR_dumpenvironment(J, J->E, 0);
js_stacktrace(J);
}
static void jsR_run(js_State *J, js_Function *F)
{
js_Function **FT = F->funtab;
double *NT = F->numtab;
const char **ST = F->strtab;
const char **VT = F->vartab-1;
int lightweight = F->lightweight;
js_Instruction *pcstart = F->code;
js_Instruction *pc = F->code;
enum js_OpCode opcode;
int offset;
int savestrict;
const char *str;
js_Object *obj;
double x, y;
unsigned int ux, uy;
int ix, iy, okay;
int b;
int transient;
savestrict = J->strict;
J->strict = F->strict;
while (1) {
if (J->gccounter > J->gcthresh)
js_gc(J, 0);
J->trace[J->tracetop].line = *pc++;
opcode = *pc++;
switch (opcode) {
case OP_POP: js_pop(J, 1); break;
case OP_DUP: js_dup(J); break;
case OP_DUP2: js_dup2(J); break;
case OP_ROT2: js_rot2(J); break;
case OP_ROT3: js_rot3(J); break;
case OP_ROT4: js_rot4(J); break;
case OP_INTEGER: js_pushnumber(J, *pc++ - 32768); break;
case OP_NUMBER: js_pushnumber(J, NT[*pc++]); break;
case OP_STRING: js_pushliteral(J, ST[*pc++]); break;
case OP_CLOSURE: js_newfunction(J, FT[*pc++], J->E); break;
case OP_NEWOBJECT: js_newobject(J); break;
case OP_NEWARRAY: js_newarray(J); break;
case OP_NEWREGEXP: js_newregexp(J, ST[pc[0]], pc[1]); pc += 2; break;
case OP_UNDEF: js_pushundefined(J); break;
case OP_NULL: js_pushnull(J); break;
case OP_TRUE: js_pushboolean(J, 1); break;
case OP_FALSE: js_pushboolean(J, 0); break;
case OP_THIS:
if (J->strict) {
js_copy(J, 0);
} else {
if (js_iscoercible(J, 0))
js_copy(J, 0);
else
js_pushglobal(J);
}
break;
case OP_CURRENT:
js_currentfunction(J);
break;
case OP_GETLOCAL:
if (lightweight) {
CHECKSTACK(1);
STACK[TOP++] = STACK[BOT + *pc++];
} else {
str = VT[*pc++];
if (!js_hasvar(J, str))
js_referenceerror(J, "'%s' is not defined", str);
}
break;
case OP_SETLOCAL:
if (lightweight) {
STACK[BOT + *pc++] = STACK[TOP-1];
} else {
js_setvar(J, VT[*pc++]);
}
break;
case OP_DELLOCAL:
if (lightweight) {
++pc;
js_pushboolean(J, 0);
} else {
b = js_delvar(J, VT[*pc++]);
js_pushboolean(J, b);
}
break;
case OP_GETVAR:
str = ST[*pc++];
if (!js_hasvar(J, str))
js_referenceerror(J, "'%s' is not defined", str);
break;
case OP_HASVAR:
if (!js_hasvar(J, ST[*pc++]))
js_pushundefined(J);
break;
case OP_SETVAR:
js_setvar(J, ST[*pc++]);
break;
case OP_DELVAR:
b = js_delvar(J, ST[*pc++]);
js_pushboolean(J, b);
break;
case OP_IN:
str = js_tostring(J, -2);
if (!js_isobject(J, -1))
js_typeerror(J, "operand to 'in' is not an object");
b = js_hasproperty(J, -1, str);
js_pop(J, 2 + b);
js_pushboolean(J, b);
break;
case OP_INITPROP:
obj = js_toobject(J, -3);
str = js_tostring(J, -2);
jsR_setproperty(J, obj, str, 0);
js_pop(J, 2);
break;
case OP_INITGETTER:
obj = js_toobject(J, -3);
str = js_tostring(J, -2);
jsR_defproperty(J, obj, str, 0, NULL, jsR_tofunction(J, -1), NULL);
js_pop(J, 2);
break;
case OP_INITSETTER:
obj = js_toobject(J, -3);
str = js_tostring(J, -2);
jsR_defproperty(J, obj, str, 0, NULL, NULL, jsR_tofunction(J, -1));
js_pop(J, 2);
break;
case OP_GETPROP:
str = js_tostring(J, -1);
obj = js_toobject(J, -2);
jsR_getproperty(J, obj, str);
js_rot3pop2(J);
break;
case OP_GETPROP_S:
str = ST[*pc++];
obj = js_toobject(J, -1);
jsR_getproperty(J, obj, str);
js_rot2pop1(J);
break;
case OP_SETPROP:
str = js_tostring(J, -2);
obj = js_toobject(J, -3);
transient = !js_isobject(J, -3);
jsR_setproperty(J, obj, str, transient);
js_rot3pop2(J);
break;
case OP_SETPROP_S:
str = ST[*pc++];
obj = js_toobject(J, -2);
transient = !js_isobject(J, -2);
jsR_setproperty(J, obj, str, transient);
js_rot2pop1(J);
break;
case OP_DELPROP:
str = js_tostring(J, -1);
obj = js_toobject(J, -2);
b = jsR_delproperty(J, obj, str);
js_pop(J, 2);
js_pushboolean(J, b);
break;
case OP_DELPROP_S:
str = ST[*pc++];
obj = js_toobject(J, -1);
b = jsR_delproperty(J, obj, str);
js_pop(J, 1);
js_pushboolean(J, b);
break;
case OP_ITERATOR:
if (js_iscoercible(J, -1)) {
obj = jsV_newiterator(J, js_toobject(J, -1), 0);
js_pop(J, 1);
js_pushobject(J, obj);
}
break;
case OP_NEXTITER:
if (js_isobject(J, -1)) {
obj = js_toobject(J, -1);
str = jsV_nextiterator(J, obj);
if (str) {
js_pushliteral(J, str);
js_pushboolean(J, 1);
} else {
js_pop(J, 1);
js_pushboolean(J, 0);
}
} else {
js_pop(J, 1);
js_pushboolean(J, 0);
}
break;
/* Function calls */
case OP_EVAL:
js_eval(J);
break;
case OP_CALL:
js_call(J, *pc++);
break;
case OP_NEW:
js_construct(J, *pc++);
break;
/* Unary operators */
case OP_TYPEOF:
str = js_typeof(J, -1);
js_pop(J, 1);
js_pushliteral(J, str);
break;
case OP_POS:
x = js_tonumber(J, -1);
js_pop(J, 1);
js_pushnumber(J, x);
break;
case OP_NEG:
x = js_tonumber(J, -1);
js_pop(J, 1);
js_pushnumber(J, -x);
break;
case OP_BITNOT:
ix = js_toint32(J, -1);
js_pop(J, 1);
js_pushnumber(J, ~ix);
break;
case OP_LOGNOT:
b = js_toboolean(J, -1);
js_pop(J, 1);
js_pushboolean(J, !b);
break;
case OP_INC:
x = js_tonumber(J, -1);
js_pop(J, 1);
js_pushnumber(J, x + 1);
break;
case OP_DEC:
x = js_tonumber(J, -1);
js_pop(J, 1);
js_pushnumber(J, x - 1);
break;
case OP_POSTINC:
x = js_tonumber(J, -1);
js_pop(J, 1);
js_pushnumber(J, x + 1);
js_pushnumber(J, x);
break;
case OP_POSTDEC:
x = js_tonumber(J, -1);
js_pop(J, 1);
js_pushnumber(J, x - 1);
js_pushnumber(J, x);
break;
/* Multiplicative operators */
case OP_MUL:
x = js_tonumber(J, -2);
y = js_tonumber(J, -1);
js_pop(J, 2);
js_pushnumber(J, x * y);
break;
case OP_DIV:
x = js_tonumber(J, -2);
y = js_tonumber(J, -1);
js_pop(J, 2);
js_pushnumber(J, x / y);
break;
case OP_MOD:
x = js_tonumber(J, -2);
y = js_tonumber(J, -1);
js_pop(J, 2);
js_pushnumber(J, fmod(x, y));
break;
/* Additive operators */
case OP_ADD:
js_concat(J);
break;
case OP_SUB:
x = js_tonumber(J, -2);
y = js_tonumber(J, -1);
js_pop(J, 2);
js_pushnumber(J, x - y);
break;
/* Shift operators */
case OP_SHL:
ix = js_toint32(J, -2);
uy = js_touint32(J, -1);
js_pop(J, 2);
js_pushnumber(J, ix << (uy & 0x1F));
break;
case OP_SHR:
ix = js_toint32(J, -2);
uy = js_touint32(J, -1);
js_pop(J, 2);
js_pushnumber(J, ix >> (uy & 0x1F));
break;
case OP_USHR:
ux = js_touint32(J, -2);
uy = js_touint32(J, -1);
js_pop(J, 2);
js_pushnumber(J, ux >> (uy & 0x1F));
break;
/* Relational operators */
case OP_LT: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b < 0); break;
case OP_GT: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b > 0); break;
case OP_LE: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b <= 0); break;
case OP_GE: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b >= 0); break;
case OP_INSTANCEOF:
b = js_instanceof(J);
js_pop(J, 2);
js_pushboolean(J, b);
break;
/* Equality */
case OP_EQ: b = js_equal(J); js_pop(J, 2); js_pushboolean(J, b); break;
case OP_NE: b = js_equal(J); js_pop(J, 2); js_pushboolean(J, !b); break;
case OP_STRICTEQ: b = js_strictequal(J); js_pop(J, 2); js_pushboolean(J, b); break;
case OP_STRICTNE: b = js_strictequal(J); js_pop(J, 2); js_pushboolean(J, !b); break;
case OP_JCASE:
offset = *pc++;
b = js_strictequal(J);
if (b) {
js_pop(J, 2);
pc = pcstart + offset;
} else {
js_pop(J, 1);
}
break;
/* Binary bitwise operators */
case OP_BITAND:
ix = js_toint32(J, -2);
iy = js_toint32(J, -1);
js_pop(J, 2);
js_pushnumber(J, ix & iy);
break;
case OP_BITXOR:
ix = js_toint32(J, -2);
iy = js_toint32(J, -1);
js_pop(J, 2);
js_pushnumber(J, ix ^ iy);
break;
case OP_BITOR:
ix = js_toint32(J, -2);
iy = js_toint32(J, -1);
js_pop(J, 2);
js_pushnumber(J, ix | iy);
break;
/* Try and Catch */
case OP_THROW:
js_throw(J);
case OP_TRY:
offset = *pc++;
if (js_trypc(J, pc)) {
pc = J->trybuf[J->trytop].pc;
} else {
pc = pcstart + offset;
}
break;
case OP_ENDTRY:
js_endtry(J);
break;
case OP_CATCH:
str = ST[*pc++];
obj = jsV_newobject(J, JS_COBJECT, NULL);
js_pushobject(J, obj);
js_rot2(J);
js_setproperty(J, -2, str);
J->E = jsR_newenvironment(J, obj, J->E);
js_pop(J, 1);
break;
case OP_ENDCATCH:
J->E = J->E->outer;
break;
/* With */
case OP_WITH:
obj = js_toobject(J, -1);
J->E = jsR_newenvironment(J, obj, J->E);
js_pop(J, 1);
break;
case OP_ENDWITH:
J->E = J->E->outer;
break;
/* Branching */
case OP_DEBUGGER:
js_trap(J, (int)(pc - pcstart) - 1);
break;
case OP_JUMP:
pc = pcstart + *pc;
break;
case OP_JTRUE:
offset = *pc++;
b = js_toboolean(J, -1);
js_pop(J, 1);
if (b)
pc = pcstart + offset;
break;
case OP_JFALSE:
offset = *pc++;
b = js_toboolean(J, -1);
js_pop(J, 1);
if (!b)
pc = pcstart + offset;
break;
case OP_RETURN:
J->strict = savestrict;
return;
}
}
}
|