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
|
/*
* Internal definitions for the JavaScript interpreter.
* Copyright (c) 1998 New Generation Software (NGS) Oy
* (c) 1999-2000 Cylant Technology, LLC
*
* Author: Markku Rossi <mtr@ngs.fi>
* Brian Bassett <bbassett@bbassett.net>
*/
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA
*/
/*
* $Source: /home/cvs/entity/libentitynjs/njs/internal.h,v $
* $Id: internal.h,v 1.7 2000/08/15 05:43:42 imain Exp $
*/
#ifndef JSINT_H
#define JSINT_H
/* We have always njs/config.h */
#include <entity-config.h>
#include <stdio.h>
#include <assert.h>
#include <setjmp.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if STDC_HEADERS
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <float.h>
#else /* not STDC_HEADERS */
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_FLOAT_H
#include <float.h>
#endif
#endif /* not STDC_HEADERS */
/* Misc system headers. */
#include <sys/types.h>
/*
* Protability kludges. If something is missing from the w32
* environment, please edit the micros/w32.{c,h} files and implement
* them.
*/
#ifndef WIN32
/* Directory handling. */
#include <dirent.h>
#else
/* WIN32 is best off getting DIR stuff from glib. */
#include <glib.h>
#endif /* not WIN32 */
#include <njs/njs.h>
#if __cplusplus
extern "C" {
#endif
/*
* Types and definitions.
*/
/* Some portability features. */
#ifdef WIN32
#define JS_HOST_LINE_BREAK "\r\n"
#define JS_HOST_LINE_BREAK_LEN 2
#define JS_HOST_DIR_SEP '\\'
#else /* not WIN32 */
#define JS_HOST_LINE_BREAK "\n"
#define JS_HOST_LINE_BREAK_LEN 1
#define JS_HOST_DIR_SEP '/'
#endif /* not WIN32 */
#define JS_BC_FILE_MAGIC 0xc0014a53
#define JS_GLOBAL_NAME ".global"
#define JS_SYMBOL_NULL ((JSSymbol) -1)
#define JS_IS_STR_WHITE_SPACE_CHAR(ch) \
((ch) == '\t' || (ch) == ' ' || (ch) == '\f' || (ch) == '\v' \
|| (ch) == '\r' || (ch) == '\n')
/*
* Read macros for byte code files.
*/
#define JS_BC_READ_INT32(cp, var) \
(var) = (cp)[0]; \
(var) <<= 8; \
(var) |= (cp)[1]; \
(var) <<= 8; \
(var) |= (cp)[2]; \
(var) <<= 8; \
(var) |= (cp)[3]
#define JS_BC_READ_INT16(cp, var) \
(var) = (cp)[0]; \
(var) <<= 8; \
(var) |= (cp)[1]
#define JS_BC_READ_INT8(cp, var) \
(var) = (cp)[0]
#define JS_BC_WRITE_INT32(cp, var) \
cp[3] = (unsigned char) ((var) & 0x000000ff); \
cp[2] = (unsigned char) (((var) >> 8) & 0x000000ff); \
cp[1] = (unsigned char) (((var) >> 16) & 0x000000ff); \
cp[0] = (unsigned char) (((var) >> 24) & 0x000000ff)
/* General VM macros. */
/* STACKFRAME */
#define JS_SP0 sp
#define JS_SP1 (sp + 1)
#define JS_SP2 (sp + 2)
#define JS_SP(n) (sp + (n))
#define JS_LOCAL(n) (fp - 4 - (n))
#define JS_ARG(n) (fp + 1 + (n))
#define JS_WITHPTR (fp - 2)
#define JS_ARGS_FIXP (fp - 1)
#define JS_PUSH() sp--
#define JS_POP() sp++
#define JS_POP_N(n) sp += (n)
#define JS_COPY(to, from) \
do { \
(to)->type = (from)->type; \
(to)->u.copy.a = (from)->u.copy.a; \
(to)->u.copy.b = (from)->u.copy.b; \
} while (0)
#define JS_CONST(n) (&vm->consts[(n)])
#define JS_GLOBAL(n) (&vm->globals[(n)])
#define JS_SAVE_REGS() \
do { \
vm->sp = sp; \
vm->pc = pc; \
} while (0)
#define JS_CALL_HOOK(event) \
do { \
int hook_result; \
\
if (vm->hook) \
if ((hook_result = (*vm->hook) ((event), vm->hook_context)) != 0) \
{ \
JS_SAVE_REGS (); \
js_vm_set_err (vm, "hook break %d", hook_result); \
js_vm_error (vm); \
/* NOTREACHED */ \
} \
} while (0)
#define JS_VM_ALLOCATE_FD(vm, where) \
do { \
if ((vm)->fd_count == 0) \
{ \
js_vm_set_err ((vm), "%s: no more file descriptors allowed", \
(where)); \
js_vm_error (vm); \
} \
(vm)->fd_count--; \
} while (0)
#define JS_VM_FREE_FD(vm) \
do { \
(vm)->fd_count++; \
} while (0)
#define JS_VM_GET_PROTOTYPE(vm, obj, proto) \
do { \
(proto)->type = JS_OBJECT; \
if ((obj)->type == JS_STRING && (obj)->u.vstring->prototype) \
(proto)->u.vobject = (obj)->u.vstring->prototype; \
else if ((obj)->type == JS_ARRAY && (obj)->u.varray->prototype) \
(proto)->u.vobject = (obj)->u.varray->prototype; \
else if ((obj)->type == JS_FUNC && (obj)->u.vfunction->prototype) \
(proto)->u.vobject = (obj)->u.vfunction->prototype; \
else if ((obj)->type == JS_OBJECT) \
js_vm_object_load_property (vm, (obj)->u.vobject, \
(vm)->syms.s___proto__, proto); \
else if ((obj)->type == JS_BUILTIN) \
{ \
if ((obj)->u.vbuiltin->prototype) \
(proto)->u.vobject = (obj)->u.vbuiltin->prototype; \
else \
(proto)->u.vobject = (obj)->u.vbuiltin->info->prototype; \
} \
else \
(proto)->u.vobject = (vm)->prim[(obj)->type]->prototype; \
} while (0)
#define JS_MAYBE_GC() \
do { \
if (vm->gc.bytes_allocated >= vm->gc.trigger) \
{ \
js_vm_garbage_collect (vm, fp, sp); \
JS_CALL_HOOK (JS_VM_EVENT_GARBAGE_COLLECT); \
} \
} while (0)
#define JS_IS_TRUE(n) ((n)->type > JS_INTEGER \
|| ((n)->type == JS_BOOLEAN && (n)->u.vboolean) \
|| ((n)->type == JS_INTEGER && (n)->u.vinteger))
#define JS_IS_FALSE(n) ((n)->type < JS_BOOLEAN \
|| ((n)->type == JS_BOOLEAN && !(n)->u.vboolean) \
|| ((n)->type == JS_INTEGER && !(n)->u.vinteger))
#define JS_RESERVE_STACK_FOR_FUNCTION 10
#define JS_SUBROUTINE_CALL(function) \
do { \
/* Check that we have enought space in the stack. */ \
if (sp - JS_RESERVE_STACK_FOR_FUNCTION < vm->stack) \
ERROR ("stack overflow"); \
\
/* STACKFRAME */ \
\
/* Save frame pointer. */ \
JS_SP0->type = JS_IPTR; \
JS_SP0->u.iptr = fp; \
\
/* Update fp. */ \
fp = JS_SP0; \
JS_PUSH (); \
\
/* Insert an empty args_fix. */ \
JS_SP0->type = JS_ARGS_FIX; \
JS_SP0->u.args_fix.argc = 0; \
JS_SP0->u.args_fix.delta = 0; \
JS_PUSH (); \
\
/* Insert empty with pointer. */ \
JS_SP0->type = JS_IPTR; \
JS_SP0->u.iptr = NULL; \
JS_PUSH (); \
\
/* Save return address. */ \
JS_SP0->type = JS_IPTR; \
JS_SP0->u.iptr = pc; \
JS_PUSH (); \
\
/* And finally, jump to the method code. */ \
CALL_USER_FUNC ((function)); \
} while (0)
#define JS_OPERAND_CMP_REL(_OP_) \
do { \
if (JS_SP2->type == JS_STRING && JS_SP1->type == JS_STRING) \
{ \
JS_SP2->u.vboolean \
= js_compare_strings (JS_SP2, JS_SP1) _OP_ 0; \
JS_SP2->type = JS_BOOLEAN; \
JS_POP (); \
} \
else if (JS_SP2->type == JS_INTEGER && JS_SP1->type == JS_INTEGER) \
{ \
JS_SP2->u.vboolean \
= JS_SP2->u.vinteger _OP_ JS_SP1->u.vinteger; \
JS_SP2->type = JS_BOOLEAN; \
JS_POP (); \
} \
else \
{ \
JSNode l, r; \
\
/* Do it the hard way. */ \
switch (JS_SP2->type) \
{ \
case JS_INTEGER: \
case JS_FLOAT: \
case JS_NAN: \
JS_COPY (&l, JS_SP2); \
break; \
\
default: \
js_vm_to_number (vm, JS_SP2, &l); \
break; \
} \
\
switch (JS_SP1->type) \
{ \
case JS_INTEGER: \
case JS_FLOAT: \
case JS_NAN: \
JS_COPY (&r, JS_SP1); \
break; \
\
default: \
js_vm_to_number (vm, JS_SP1, &r); \
break; \
} \
\
/* Do the comparison. */ \
JS_POP (); \
\
if (l.type == JS_NAN || r.type == JS_NAN) \
JS_SP1->type = JS_UNDEFINED; \
else if (l.type == JS_INTEGER && r.type == JS_INTEGER) \
{ \
JS_SP1->type = JS_BOOLEAN; \
JS_SP1->u.vboolean = l.u.vinteger _OP_ r.u.vinteger; \
} \
else \
{ \
double ld, rd; \
\
if (l.type == JS_FLOAT) \
ld = l.u.vfloat; \
else \
ld = (double) l.u.vinteger; \
\
if (r.type == JS_FLOAT) \
rd = r.u.vfloat; \
else \
rd = (double) r.u.vinteger; \
\
JS_SP1->type = JS_BOOLEAN; \
JS_SP1->u.vboolean = ld _OP_ rd; \
} \
} \
} while (0)
#define JS_OPERAND_CMP_EQ(_OP_, _VAL_) \
while (1) { \
int res; \
if (JS_SP2->type == JS_SP1->type) \
{ \
/* Comparsion between same types. */ \
switch (JS_SP2->type) \
{ \
case JS_INTEGER: \
res = JS_SP2->u.vinteger _OP_ JS_SP1->u.vinteger; \
break; \
\
case JS_STRING: \
res = js_compare_strings (JS_SP2, JS_SP1) _OP_ 0; \
break; \
\
case JS_FLOAT: \
res = JS_SP2->u.vfloat _OP_ JS_SP1->u.vfloat; \
break; \
\
case JS_NAN: \
/* 11.9.3: cases 5 and 6 */ \
res = !_VAL_; \
break; \
\
case JS_BOOLEAN: \
res = JS_SP2->u.vboolean _OP_ JS_SP1->u.vboolean; \
break; \
\
case JS_OBJECT: \
res = JS_SP2->u.vobject _OP_ JS_SP1->u.vobject; \
break; \
\
case JS_BUILTIN: \
res = ((JS_SP2->u.vbuiltin->info \
== JS_SP1->u.vbuiltin->info \
&& (JS_SP2->u.vbuiltin->instance_context \
== JS_SP1->u.vbuiltin->instance_context)) \
? _VAL_ : !_VAL_); \
break; \
\
case JS_FUNC: \
res = JS_SP2->u.vfunction _OP_ JS_SP1->u.vfunction; \
break; \
\
case JS_SYMBOL: \
res = JS_SP2->u.vsymbol _OP_ JS_SP1->u.vsymbol; \
break; \
\
case JS_IPTR: \
res = JS_SP2->u.iptr _OP_ JS_SP1->u.iptr; \
break; \
\
default: \
res = _VAL_; \
break; \
} \
} \
else \
{ \
/* Type conversions between different types. */ \
\
if ((JS_SP2->type == JS_UNDEFINED || JS_SP2->type == JS_NULL) \
&& (JS_SP1->type == JS_UNDEFINED \
|| JS_SP1->type == JS_NULL)) \
res = _VAL_; \
\
/* Numbers. */ \
else if (JS_IS_NUMBER (JS_SP2) && JS_IS_NUMBER (JS_SP1)) \
{ \
if (JS_SP2->type == JS_NAN || JS_SP1->type == JS_NAN) \
/* 11.9.3: cases 5 and 6 */ \
res = !_VAL_; \
else if (JS_SP2->type == JS_INTEGER) \
/* Integer-integer was already handled. */ \
res = (double) JS_SP2->u.vinteger _OP_ JS_SP1->u.vfloat; \
else \
/* Integer-integer was already handled. */ \
res = JS_SP2->u.vfloat _OP_ (double) JS_SP1->u.vinteger; \
} \
else \
{ \
JSNode l, r; \
\
/* Must perform type casts. */ \
\
if ((JS_SP2->type == JS_STRING || JS_SP2->type == JS_BOOLEAN \
|| JS_IS_NUMBER (JS_SP2)) \
&& (JS_SP1->type == JS_STRING \
|| JS_SP1->type == JS_BOOLEAN \
|| JS_IS_NUMBER (JS_SP1))) \
{ \
js_vm_to_number (vm, JS_SP2, &l); \
js_vm_to_number (vm, JS_SP1, &r); \
\
if (l.type == JS_NAN || r.type == JS_NAN) \
res = !_VAL_; \
else if (l.type == JS_INTEGER) \
{ \
if (r.type == JS_INTEGER) \
res = l.u.vinteger _OP_ r.u.vinteger; \
else \
res = (double) l.u.vinteger _OP_ r.u.vfloat; \
} \
else \
{ \
if (r.type == JS_INTEGER) \
res = l.u.vfloat _OP_ (double) r.u.vinteger; \
else \
res = l.u.vfloat _OP_ r.u.vfloat; \
} \
} \
else if (JS_SP2->type == JS_OBJECT \
&& (JS_SP1->type == JS_STRING \
|| JS_IS_NUMBER (JS_SP1))) \
{ \
JSNode cvt; \
\
/* ECMA 11.9.3 21. No preferred type specified. */ \
js_vm_to_primitive (vm, JS_SP2, &cvt, JS_UNDEFINED); \
JS_COPY (JS_SP2, &cvt); \
continue; \
} \
else if (JS_SP1->type == JS_OBJECT \
&& (JS_SP2->type == JS_STRING \
|| JS_IS_NUMBER (JS_SP2))) \
{ \
JSNode cvt; \
\
/* ECMA 11.9.3 20. No preferred type specified. */ \
js_vm_to_primitive (vm, JS_SP1, &cvt, JS_UNDEFINED); \
JS_COPY (JS_SP1, &cvt); \
continue; \
} \
else \
res = !_VAL_; \
} \
} \
\
JS_SP2->type = JS_BOOLEAN; \
JS_SP2->u.vboolean = res; \
JS_POP (); \
break; \
}
#define JS_OPERAND_CMP_SEQ(_OP_, _VAL_) \
do { \
int res; \
if (JS_SP2->type == JS_SP1->type) \
{ \
switch (JS_SP2->type) \
{ \
case JS_INTEGER: \
res = JS_SP2->u.vinteger _OP_ JS_SP1->u.vinteger; \
break; \
\
case JS_FLOAT: \
res = JS_SP2->u.vfloat _OP_ JS_SP1->u.vfloat; \
break; \
\
case JS_NAN: \
/* 11.9.6: cases 3 and 4 */ \
res = !_VAL_; \
break; \
\
case JS_STRING: \
res = js_compare_strings (JS_SP2, JS_SP1) _OP_ 0; \
break; \
\
case JS_BOOLEAN: \
res = JS_SP2->u.vboolean _OP_ JS_SP1->u.vboolean; \
break; \
\
case JS_OBJECT: \
res = JS_SP2->u.vobject _OP_ JS_SP1->u.vobject; \
break; \
\
case JS_BUILTIN: \
res = ((JS_SP2->u.vbuiltin->info \
== JS_SP1->u.vbuiltin->info \
&& (JS_SP2->u.vbuiltin->instance_context \
== JS_SP1->u.vbuiltin->instance_context)) \
? _VAL_ : !_VAL_); \
break; \
\
case JS_FUNC: \
res = JS_SP2->u.vfunction _OP_ JS_SP1->u.vfunction; \
break; \
\
default: \
/* 11.9.6: case 12 */ \
res = !_VAL_; \
break; \
} \
} \
else \
{ \
/* Only numbers are allowed here. */ \
if (JS_IS_NUMBER (JS_SP2) && JS_IS_NUMBER (JS_SP1)) \
{ \
if (JS_SP2->type == JS_NAN || JS_SP1->type == JS_NAN) \
/* 11.9.6: cases 3 and 4 */ \
res = !_VAL_; \
else if (JS_SP2->type == JS_INTEGER) \
res = (double) JS_SP2->u.vinteger _OP_ JS_SP1->u.vfloat; \
else \
res = JS_SP2->u.vfloat _OP_ (double) JS_SP1->u.vinteger; \
} \
else \
res = !_VAL_; \
} \
\
JS_SP2->type = JS_BOOLEAN; \
JS_SP2->u.vboolean = res; \
JS_POP (); \
\
} while (0)
#define JS_OPERAND_BINARY(_OP_) \
do { \
if (JS_SP2->type == JS_INTEGER && JS_SP1->type == JS_INTEGER) \
{ \
JS_SP2->u.vinteger = ((JSInt32) JS_SP2->u.vinteger \
_OP_ (JSInt32) JS_SP1->u.vinteger); \
JS_POP (); \
} \
else \
{ \
JSInt32 l, r; \
\
l = js_vm_to_int32 (vm, JS_SP2); \
r = js_vm_to_int32 (vm, JS_SP1); \
\
JS_SP2->u.vinteger = (l _OP_ r); \
JS_SP2->type = JS_INTEGER; \
JS_POP (); \
} \
} while (0)
#define JS_IS_NUMBER(n) \
((n)->type == JS_INTEGER || (n)->type == JS_FLOAT || (n)->type == JS_NAN)
/* Some math macros. */
#define JS_MAKE_POSITIVE_INFINITY(node) \
do { \
(node)->type = JS_FLOAT; \
(node)->u.vfloat = HUGE_VAL; \
} while (0)
#define JS_MAKE_NEGATIVE_INFINITY(node) \
do { \
(node)->type = JS_FLOAT; \
(node)->u.vfloat = -HUGE_VAL; \
} while (0)
#define JS_IS_POSITIVE_INFINITY(node) \
((node)->type == JS_FLOAT && (node)->u.vfloat == HUGE_VAL)
#define JS_IS_NEGATIVE_INFINITY(node) \
((node)->type == JS_FLOAT && (node)->u.vfloat == -HUGE_VAL)
#define JS_IS_FINITE(node) \
(!JS_IS_POSITIVE_INFINITY ((node)) \
&& !JS_IS_NEGATIVE_INFINITY ((node)) \
&& (node)->type != JS_NAN) \
#define JS_IS_PRIMITIVE_VALUE(node) \
((node)->type == JS_UNDEFINED || (node)->type == JS_NULL \
|| (node)->type == JS_BOOLEAN || JS_IS_NUMBER ((node)) \
|| (node)->type == JS_STRING)
/* Macro to clear all flags from a heap memory block. */
#define JS_HEAP_MEMORY_BLOCK_CLEAR_FLAGS(mb) \
do { \
(mb)->flag_mark = 0; \
(mb)->flag_destroyable = 0; \
} while (0)
#define JS_NUM_HEAP_FREELISTS 20
/*
* Virtual machine security flags. When these flags are enabled in
* the vm->security, the appropriate built-in modules don't implement
* insecure methods.
*/
#define JS_VM_SECURE_FILE 0x01
#define JS_VM_SECURE_SYSTEM 0x02
/*
* Noticeable virtual machine events. The `JS_VM_EVENT_OPERAND_COUNT'
* event is generated only if the interpreter was configured with the
* `--enable-operand-hooks' option.
*/
#define JS_VM_EVENT_OPERAND_COUNT 1
#define JS_VM_EVENT_GARBAGE_COLLECT 2
/*
* Integer types.
*/
typedef unsigned char JSUInt8;
typedef signed char JSInt8;
typedef unsigned short JSUInt16;
typedef short JSInt16;
#if SIZEOF_INT == 4
typedef unsigned int JSUInt32;
typedef int JSInt32;
#else /* not SIZEOF_INT == 4 */
#if SIZEOF_LONG == 4
typedef unsigned long JSUInt32;
typedef long JSInt32;
#else /* not SIZEOF_LONG == 4 */
#error "do not know how to define a 32 bit long integer"
#endif /* not SIZEOF_LONG == 4 */
#endif /* not SIZEOF_INT == 4 */
/*
* An unsigned interger number that can be used to align structures to
* correct byte boundaries. On 64 bit machines (Alpha) this should be
* 64 bits long, etc. For now one, we just assume that the mashine is
* a LP64 so the `unsigned long' is a correct type for it.
*/
typedef unsigned long JSUIntAlign;
/* I/O streams. */
/* Buffer filler or flusher function. */
typedef int (*JSIOStreamIOFunc) (void *context, unsigned char *buffer,
unsigned int todo, int *error_return);
typedef int (*JSIOStreamSeek) (void *context, long offset, int whence);
typedef long (*JSIOStreamGetPosition) (void *context);
typedef long (*JSIOStreamGetLength) (void *context);
typedef void (*JSIOStreamClose) (void *context);
/* The I/O stream handle. */
struct js_io_stream_st
{
unsigned char *buffer; /* Must be reallocatable with js_realloc(). */
unsigned int buflen;
unsigned int data_in_buf;
unsigned int bufpos;
/* Flags. */
unsigned int at_eof : 1;
unsigned int autoflush : 1;
unsigned int writep : 1; /* Does the buffer contain write data? */
/* The system error code for the last operation that failed. */
int error;
/* Only one of the read and write is active. */
JSIOStreamIOFunc read;
JSIOStreamIOFunc write;
JSIOStreamSeek seek;
JSIOStreamGetPosition get_position;
JSIOStreamGetLength get_length;
JSIOStreamClose close;
void *context;
};
typedef struct js_io_stream_st JSIOStream;
/* The destroy callback for the destroyable heap blocks. */
typedef void (*JSHeapDestroyableCB) (void *ptr);
/*
* Each destroyable heap block must be castable to this structure e.g.
* the first item in the block must be pointer to the destroy function.
*/
struct js_heap_destroyable_st
{
JSHeapDestroyableCB destroy;
};
typedef struct js_heap_destroyable_st JSHeapDestroyable;
/* Interned symbol. */
typedef unsigned int JSSymbol;
/* JavaScript Types. */
typedef enum
{
JS_UNDEFINED = 0,
JS_NULL = 1,
JS_BOOLEAN = 2,
JS_INTEGER = 3, /* Integer, float and nan are `number' */
JS_STRING = 4,
JS_FLOAT = 5,
JS_ARRAY = 6,
JS_OBJECT = 7,
/*
* The following ones are the internal types, used by this implementation.
*/
JS_SYMBOL = 10,
JS_BUILTIN = 11,
JS_FUNC = 12,
JS_NAN = 13,
JS_IPTR = 14,
JS_ARGS_FIX = 15
} JSNodeType;
struct js_node_st;
struct js_vm_st;
struct js_builtin_info_st;
/* Registry information for builtin objects. */
#define JS_PROPERTY_FOUND 1
#define JS_PROPERTY_UNKNOWN 0
typedef void (*JSBuiltinGlobalMethod) (struct js_vm_st *vm,
struct js_builtin_info_st *builtin_info,
void *instance_context,
struct js_node_st *result_return,
struct js_node_st *args);
/*
* Function to call method <method> from the object. Function must return
* JS_PROPERTY_FOUND if the method was found or JS_PROPERTY_UNKNOWN
* otherwise.
*/
typedef int (*JSBuiltinMethod) (struct js_vm_st *vm,
struct js_builtin_info_st *builtin_info,
void *instance_context,
JSSymbol method,
struct js_node_st *result_return,
struct js_node_st *args);
/*
* Function to load and set property <property> of object. If <set>
* is true, property <property> should be set to value <node>. Otherwise
* function should return the value of property <property> in <node>.
* Function must return JS_PROPERTY_FOUND if the property was found or
* JS_PROPERTY_UNKNOWN otherwise.
*/
#define JS_BUILTIN_PROP_GET 0
#define JS_BUILTIN_PROP_SET 1
#define JS_BUILTIN_PROP_DELETE 2
typedef int (*JSBuiltinProperty) (struct js_vm_st *vm,
struct js_builtin_info_st *builtin_info,
void *instance_context,
JSSymbol property, int operation,
struct js_node_st *node);
typedef void (*JSBuiltinNew) (struct js_vm_st *vm,
struct js_builtin_info_st *builtin_info,
struct js_node_st *args,
struct js_node_st *result_return);
typedef void (*JSBuiltinDelete) (struct js_builtin_info_st *builtin_info,
void *instance_context);
typedef int (*JSBuiltinEnumerate) (struct js_vm_st *vm,
struct js_builtin_info_st *builtin_info,
void *instance_context, int nth,
struct js_node_st *return_result);
typedef int (*JSBuiltinQuery) (struct js_vm_st *vm,
struct js_builtin_info_st *builtin_info,
void *instance_context, JSSymbol property);
typedef void (*JSBuiltinMark) (struct js_builtin_info_st *builtin_info,
void *instance_context);
typedef void (*JSBuiltinObjectCtxDelete) (void *obj_context);
struct js_builtin_info_st
{
JSHeapDestroyableCB destroy;
JSBuiltinGlobalMethod global_method_proc;
JSBuiltinMethod method_proc;
JSBuiltinProperty property_proc;
JSBuiltinNew new_proc;
JSBuiltinDelete delete_proc;
JSBuiltinMark mark_proc;
JSBuiltinEnumerate enumerate_proc;
JSBuiltinQuery query_proc;
void *obj_context;
JSBuiltinObjectCtxDelete obj_context_delete;
struct js_object_st *prototype;
};
typedef struct js_builtin_info_st JSBuiltinInfo;
/* Builtin object / class. */
struct js_builtin_st
{
JSHeapDestroyableCB destroy;
JSBuiltinInfo *info;
void *instance_context;
struct js_object_st *prototype;
};
typedef struct js_builtin_st JSBuiltin;
/* String. */
struct js_string_st
{
/* Flags. */
unsigned int staticp : 1;
unsigned char *data;
unsigned int len;
struct js_object_st *prototype;
};
typedef struct js_string_st JSString;
/* Array. */
struct js_array_st
{
unsigned int length;
struct js_node_st *data;
struct js_object_st *prototype;
};
typedef struct js_array_st JSArray;
/* Function. */
struct js_function_st
{
void *implementation;
struct js_object_st *prototype;
};
typedef struct js_function_st JSFunction;
/* Node. */
struct js_node_st
{
JSNodeType type;
union
{
unsigned int vboolean;
JSString *vstring;
long vinteger;
double vfloat;
struct js_object_st *vobject;
JSArray *varray;
/* Internal values. */
JSSymbol vsymbol;
JSBuiltin *vbuiltin;
JSFunction *vfunction;
void *iptr;
struct
{
JSUInt32 argc;
JSUInt32 delta;
} args_fix;
struct
{
JSUInt32 a;
JSUInt32 b;
} copy;
} u;
};
typedef struct js_node_st JSNode;
/* Object. */
/* Hash node for object's properties. */
struct js_object_prop_hash_bucket_st
{
struct js_object_prop_hash_bucket_st *next;
unsigned char *data;
unsigned int len;
unsigned int value;
};
typedef struct js_object_prop_hash_bucket_st JSObjectPropHashBucket;
/* The attribute flags for object's properties. */
#define JS_ATTRIB_READONLY 1
#define JS_ATTRIB_DONTENUM 2
#define JS_ATTRIB_DONTDELETE 4
#define JS_ATTRIB_Internal 8
/* Object's property. */
struct js_property_st
{
JSSymbol name;
JSNode value;
unsigned int attributes;
};
typedef struct js_property_st JSProperty;
struct js_object_st
{
JSObjectPropHashBucket **hash;
unsigned int *hash_lengths;
unsigned int num_props; /* Number of properties in this object. */
JSProperty *props;
};
typedef struct js_object_st JSObject;
/* Byte code. */
typedef enum
{
JS_BCST_CODE = 0,
JS_BCST_CONSTANTS = 1,
JS_BCST_SYMTAB = 2,
JS_BCST_DEBUG = 3
} JSBCSectionType;
struct js_bc_sect_st
{
JSBCSectionType type;
unsigned int length;
void *data; /* <length> bytes of data */
};
typedef struct js_bc_sect_st JSBCSect;
struct js_bc_st
{
unsigned int num_sects;
JSBCSect *sects;
};
typedef struct js_bc_st JSByteCode;
/* Debug information. */
#define JS_DI_FILENAME 1
#define JS_DI_LINENUMBER 2
/* Heap block. */
struct js_heap_block_st
{
struct js_heap_block_st *next;
unsigned int size;
/* <size> bytes of data follows the structure. */
};
typedef struct js_heap_block_st JSHeapBlock;
/* Heap memory block. */
#define JS_MEM_DEBUG 0
/* All allocated blocks have this header. */
struct js_heap_memory_block_st
{
#if JS_MEM_DEBUG
JSUIntAlign magic;
#endif
JSUIntAlign flag_mark : 1;
JSUIntAlign flag_destroyable : 1;
JSUIntAlign size : (sizeof (JSUIntAlign) * 8 - 2);
/* <size> bytes of data follows this header. */
};
typedef struct js_heap_memory_block_st JSHeapMemoryBlock;
/*
* When the block is on the freelist, it has this header. The first
* sizeof (void *) bytes of the block's data is used to hold the
* freelist next pointer.
*/
struct js_heap_freelist_block_st
{
JSHeapMemoryBlock block;
JSHeapMemoryBlock *next;
};
typedef struct js_heap_freelist_block_st JSHeapFreelistBlock;
/* Parsed symbol table entry. */
struct js_symtab_entry_st
{
char *name;
unsigned int offset;
};
typedef struct js_symtab_entry_st JSSymtabEntry;
/*
* Entry points to different byte-code instruction dispatcher functions.
* Each dispatcher must implement these.
*/
typedef int (*JSVMExecute) (struct js_vm_st *vm, JSByteCode *bc,
JSSymtabEntry *symtab,
unsigned int num_symtab_entries,
unsigned int consts_offset,
unsigned int anonymous_function_offset,
unsigned char *debug_info,
unsigned int debug_info_len,
JSNode *object, JSNode *func,
unsigned int argc, JSNode *argv);
typedef const char *(*JSVMFuncName) (struct js_vm_st *vm, void *pc);
typedef const char *(*JSVMDebugPosition) (struct js_vm_st *vm,
unsigned int *linenum_return);
/*
* Definitions for the extension system.
*/
/* Directory cache linked list. */
struct js_extdir_st
{
struct js_extdir_st *next;
char *dir;
};
typedef struct js_extdir_st JSExtDirectoryList;
/* Loaded modules linked list. */
struct js_loaded_mods_st
{
struct js_loaded_mods_st *next;
char *module;
};
typedef struct js_loaded_mods_st JSLoadedModules;
/* Constants for js_ext_resolve_modulename */
#define JS_EXT_UNRESOLVED 0
#define JS_EXT_BYTECODE 1
#define JS_EXT_SOURCE 2
#define JS_EXT_LIBTOOL 3
/* Virtual Machine. */
#define JS_HASH_TABLE_SIZE 256
struct js_hash_bucket_st
{
struct js_hash_bucket_st *next;
char *name;
union
{
void *data;
unsigned int ui;
} u;
};
typedef struct js_hash_bucket_st JSHashBucket;
/* Error handler frame. */
struct js_error_handler_frame_st
{
struct js_error_handler_frame_st *next;
jmp_buf error_jmp;
/* The value thrown by the throw operand. */
JSNode thrown;
/* Saved state for the `try_push' operand. */
JSNode *sp;
JSNode *fp;
void *pc;
JSInt32 pc_delta;
};
typedef struct js_error_handler_frame_st JSErrorHandlerFrame;
struct js_vm_st
{
/* Options for the virtual machine. */
unsigned int verbose; /* verbosity has different levels. */
unsigned int stacktrace_on_error : 1;
unsigned int verbose_stacktrace : 1;
unsigned int warn_undef : 1;
/* Security flags. */
unsigned long security;
/* The default system streams. */
JSIOStream *s_stdin;
JSIOStream *s_stdout;
JSIOStream *s_stderr;
/* The byte-code instruction dispatcher. */
JSVMDispatchMethod dispatch_method;
const char *dispatch_method_name;
JSVMExecute dispatch_execute;
JSVMFuncName dispatch_func_name;
JSVMDebugPosition dispatch_debug_position;
/* Constants pool. */
JSNode *consts;
unsigned int num_consts;
unsigned int consts_alloc;
/*
* Global symbols (both functions and variables). <globals_hash> is
* a name-to-index mapping between symbol names and their positions
* in <globals>.
*/
JSHashBucket *globals_hash[JS_HASH_TABLE_SIZE];
JSNode *globals;
unsigned int num_globals;
unsigned int globals_alloc;
/* The next anonymous function id. */
unsigned int anonymous_function_next_id;
/* Stack. */
JSNode *stack;
unsigned int stack_size;
JSNode *sp; /* Fuzzy stack pointer. */
void *pc; /* Fuzzy program counter. */
/* Builtin objects for the primitive datatypes. */
JSBuiltinInfo *prim[JS_IPTR + 1];
/* Some commonly used symbols. */
struct
{
JSSymbol s___proto__;
JSSymbol s_prototype;
JSSymbol s_toSource;
JSSymbol s_toString;
JSSymbol s_valueOf;
} syms;
/* Heap. */
JSHeapBlock *heap;
JSHeapMemoryBlock *heap_freelists[JS_NUM_HEAP_FREELISTS];
unsigned long heap_size;
/* Information for the garbage collector. */
struct
{
unsigned long trigger;
unsigned long bytes_allocated;
unsigned long bytes_free;
unsigned long count;
} gc;
/* Error handler frames. */
JSErrorHandlerFrame *error_handler;
/* Buffer for the error message. Sorry, we don't support long errors ;-) */
/* No.. you just prefer to have them run over and trample the rest of your
* structure.. heh.. heh.. heh.. umm.. */
char error[1024];
/*
* The result from the latest evaluation. This is set when the
* js_vm_execute(), js_vm_apply(), or js_vm_call_method() functions
* return to the caller.
*/
JSNode exec_result;
/* Event callback hook. */
int (*hook) (int event, void *context);
void *hook_context;
unsigned int hook_operand_count;
unsigned int hook_operand_count_trigger;
/* How many file descriptors can be allocated. */
unsigned long fd_count;
#if PROFILING
/* Byte-code operand profiling support. */
unsigned int prof_count[256];
unsigned char prof_op;
#endif /* PROFILING */
/* Extension stuff */
JSExtDirectoryList *dirlist;
JSLoadedModules *modules;
};
typedef struct js_vm_st JSVirtualMachine;
/*
* Global variables.
*/
extern unsigned char js_latin1_tolower[256];
extern unsigned char js_latin1_toupper[256];
/*
* Definitions for the extension system.
*/
/* Cache manipulation functions. */
int js_ext_purge_extdir(JSVirtualMachine *vm);
/* Resolve a module name to a file. */
int js_ext_resolve_modulename(JSVirtualMachine *vm, const char *module,
char *buffer, int buffer_len);
/* Module cache manipulation functions. */
int js_ext_add_loadedmodule(JSVirtualMachine *vm, const char *module);
int js_ext_purge_loadedmodule(JSVirtualMachine *vm);
int js_ext_module_loaded(JSVirtualMachine *vm, const char *module);
/* The workhorses of the extension system. */
int js_ext_vm_load_module(JSVirtualMachine *vm, const char *module);
int js_ext_vm_load_module_from_symbol(JSVirtualMachine *vm, JSSymbol sym);
/*
* Prototypes for global functions.
*/
/*
* Memory allocation routines. If the allocation request fails, the
* error recovery is performed according to the argument <vm>. If
* <vm> is not NULL, an error message is formatted to vm->error and an
* error is raise with js_vm_error(). If the <vm> is NULL, the
* functions will return value NULL. It is an error to call these
* functions with a non-NULL <vm> that has no error handler
* initialized.
*/
#ifndef JS_DEBUG_MEMORY_LEAKS
#define JS_DEBUG_MEMORY_LEAKS 0
#endif /* not JS_DEBUG_MEMORY_LEAKS */
#if JS_DEBUG_MEMORY_LEAKS
#define js_malloc(vm, size) js_malloc_i ((vm), (size), \
__FILE__, __LINE__)
#define js_calloc(vm, num, size) js_calloc_i ((vm), (num), (size), \
__FILE__, __LINE__)
#define js_realloc(vm, ptr, size) js_realloc_i ((vm), (ptr), (size), \
__FILE__, __LINE__)
#define js_strdup(vm, str) js_strdup_i ((vm), (str), \
__FILE__, __LINE__)
void *js_malloc_i (JSVirtualMachine *vm, size_t size, char *, int);
void *js_calloc_i (JSVirtualMachine *vm, size_t num, size_t size, char *, int);
void *js_realloc_i (JSVirtualMachine *vm, void *ptr, size_t size, char *, int);
void js_free (void *ptr);
char *js_strdup_i (JSVirtualMachine *vm, const char *str, char *, int);
#else /* not JS_DEBUG_MEMORY_LEAKS */
void *js_malloc (JSVirtualMachine *vm, size_t size);
void *js_calloc (JSVirtualMachine *vm, size_t num, size_t size);
void *js_realloc (JSVirtualMachine *vm, void *ptr, size_t size);
void js_free (void *ptr);
char *js_strdup (JSVirtualMachine *vm, const char *str);
#endif /* not JS_DEBUG_MEMORY_LEAKS */
/* Byte code. */
JSByteCode *js_bc_read_file (FILE *fp);
JSByteCode *js_bc_read_data (unsigned char *data, unsigned int datalen);
void js_bc_free (JSByteCode *bc);
/* I/O streams. */
/* Allocate one I/O stream handle. */
JSIOStream *js_iostream_new ();
JSIOStream *js_iostream_file (FILE *fp, int readp, int writep, int do_close);
JSIOStream *js_iostream_pipe (FILE *fp, int readp);
size_t js_iostream_read (JSIOStream *stream, void *ptr, size_t size);
size_t js_iostream_write (JSIOStream *stream, void *ptr, size_t size);
int js_iostream_flush (JSIOStream *stream);
int js_iostream_unget (JSIOStream *stream, int byte);
int js_iostream_close (JSIOStream *stream);
int js_iostream_seek (JSIOStream *stream, long offset, int whence);
long js_iostream_get_position (JSIOStream *stream);
long js_iostream_get_length (JSIOStream *stream);
void js_iostream_fill_buffer (JSIOStream *stream);
/* Virtual machine. */
JSVirtualMachine *js_vm_create (unsigned int stack_size,
JSVMDispatchMethod dispatch_method,
unsigned int verbose, int stacktrace_on_error,
JSIOStream *s_stdin, JSIOStream *s_stdout,
JSIOStream *s_stderr);
void js_vm_destroy (JSVirtualMachine *vm);
/*
* Execute byte code <bc>. Function returns 1 if the operation was
* successful or 0 if any errors were encountered. In case of errors,
* the error message is stored at vm->error.
*/
int js_vm_execute (JSVirtualMachine *vm, JSByteCode *bc);
/*
* Apply function <func_name> to arguments <argc, argv>. If
* function's name <func_name> is NULL, then <func> must specify function
* to which arguments are applied.
*/
int js_vm_apply (JSVirtualMachine *vm, char *func_name, JSNode *func,
unsigned int argc, JSNode *argv);
/*
* Call method <method_name> from object <objet> with arguments <argc, argv>.
*/
int js_vm_call_method (JSVirtualMachine *vm, JSNode *object,
const char *method_name, unsigned int argc,
JSNode *argv);
/* Map program counter to the source file line. */
const char *js_vm_debug_position (JSVirtualMachine *vm,
unsigned int *linenum_return);
/* Fetch the function name from the program counter value. */
const char *js_vm_func_name (JSVirtualMachine *vm, void *pc);
/* Intern symbol <name, len> to virtual machine and return its JSSymbol id. */
JSSymbol js_vm_intern_with_len (JSVirtualMachine *vm, const char *name,
unsigned int len);
/* Intern symbol <name> to virtual machine and return its JSSymbol id. */
static inline JSSymbol
js_vm_intern (JSVirtualMachine *vm, const char *name)
{
return js_vm_intern_with_len (vm, name, strlen (name));
}
/* Return the name of symbol <sym>. */
const char *js_vm_symname (JSVirtualMachine *vm, JSSymbol sym);
/*
* ToPrimitive(). Convert node <n> to its primitive value and return
* the result in <result_return>.
*/
void js_vm_to_primitive (JSVirtualMachine *vm, const JSNode *n,
JSNode *result_return, JSNodeType preferred_type);
/*
* ToString(). Convert node <n> to its string presentations and
* return the result in <result_return>.
*/
void js_vm_to_string (JSVirtualMachine *vm, const JSNode *n,
JSNode *result_return);
/*
* ToNumber(). Convert node <n> to its number presentations and
* return the result in <result_return>.
*/
void js_vm_to_number (JSVirtualMachine *vm, const JSNode *n,
JSNode *result_return);
/* ToObject(). Convert node <n> to object according to its type. */
void js_vm_to_object (JSVirtualMachine *vm, const JSNode *n,
JSNode *result_return);
/*
* ToInt32(). Convert node <n> to its signed 32 bit integer
* presentation and return the result.
*/
JSInt32 js_vm_to_int32 (JSVirtualMachine *vm, JSNode *n);
/*
* ToBoolean(). Convert node <n> to a boolean value and return the
* result.
*/
int js_vm_to_boolean (JSVirtualMachine *vm, JSNode *n);
/*
* Save an error. This writes an error message into the the vm->error
* for use with js_vm_error () function below. Returns the number of
* characters written.
*/
int js_vm_set_err (JSVirtualMachine *vm, const char *fmt, ...);
/*
* Raise an error. The error message must have been saved to vm->error
* before this function is called. The function never returns.
*/
void js_vm_error (JSVirtualMachine *vm);
/*
* portable snprintf(). returns the number of characters written into
* 'str'
*/
int js_snprintf (char *str, unsigned long len, const char *fmt, ...);
/*
* Count a hash value for <data_len> bytes of data <data>. The resulting
* hash value should be re-mapped to the correct range, for example,
* with the mod operand.
*/
static inline unsigned int
js_count_hash (const char *data, unsigned int data_len)
{
unsigned int val = 0, i;
for (i = 0; i < data_len; i++)
val = (val << 5) - val + (unsigned char) data[i];
return val;
}
/* Prototypes for the different instruction dispatcher implementations. */
#if ALL_DISPATCHERS
int js_vm_switch0_exec (JSVirtualMachine *vm, JSByteCode *bc,
JSSymtabEntry *symtab,
unsigned int num_symtab_entries,
unsigned int consts_offset,
unsigned int anonymous_function_offset,
unsigned char *debug_info,
unsigned int debug_info_len,
JSNode *object, JSNode *func,
unsigned int argc, JSNode *argv);
const char *js_vm_switch0_func_name (JSVirtualMachine *vm, void *pc);
const char *js_vm_switch0_debug_position (JSVirtualMachine *vm,
unsigned int *linenum_return);
#endif /* ALL_DISPATCHERS */
int js_vm_switch_exec (JSVirtualMachine *vm, JSByteCode *bc,
JSSymtabEntry *symtab,
unsigned int num_symtab_entries,
unsigned int consts_offset,
unsigned int anonymous_function_offset,
unsigned char *debug_info, unsigned int debug_info_len,
JSNode *object, JSNode *func,
unsigned int argc, JSNode *argv);
const char *js_vm_switch_func_name (JSVirtualMachine *vm, void *pc);
const char *js_vm_switch_debug_position (JSVirtualMachine *vm,
unsigned int *linenum_return);
int js_vm_jumps_exec (JSVirtualMachine *vm, JSByteCode *bc,
JSSymtabEntry *symtab,
unsigned int num_symtab_entries,
unsigned int consts_offset,
unsigned int anonymous_function_offset,
unsigned char *debug_info, unsigned int debug_info_len,
JSNode *object, JSNode *func,
unsigned int argc, JSNode *argv);
const char *js_vm_jumps_func_name (JSVirtualMachine *vm, void *pc);
const char *js_vm_jumps_debug_position (JSVirtualMachine *vm,
unsigned int *linenum_return);
/* Heap. */
void *js_vm_alloc (JSVirtualMachine *vm, unsigned int size);
void *js_vm_alloc_destroyable (JSVirtualMachine *vm, unsigned int size);
void *js_vm_realloc (JSVirtualMachine *vm, void *ptr, unsigned int new_size);
void js_vm_free (JSVirtualMachine *vm, void *ptr);
void js_vm_garbage_collect (JSVirtualMachine *vm, JSNode *fp, JSNode *sp);
void js_vm_clear_heap (JSVirtualMachine *vm);
void js_vm_mark (JSNode *node);
int js_vm_mark_ptr (void *ptr);
int js_vm_is_marked_ptr (void *ptr);
/* Function. */
static inline JSFunction *
js_vm_make_function (JSVirtualMachine *vm, void *implementation)
{
JSFunction *f = (JSFunction *) js_vm_alloc (vm, sizeof (*f));
f->implementation = implementation;
f->prototype = (JSObject *) NULL;
return f;
}
/* Built-in. */
/* Create a new built-in info. */
JSBuiltinInfo *js_vm_builtin_info_create (JSVirtualMachine *vm);
/* Create a new builtin object with <info, instance_context> to <result>. */
void js_vm_builtin_create (JSVirtualMachine *vm, JSNode *result,
JSBuiltinInfo *info, void *instance_context);
/* Array. */
static inline void
js_vm_make_array (JSVirtualMachine *vm, JSNode *n, unsigned int length)
{
unsigned int i;
n->type = JS_ARRAY;
n->u.varray = (JSArray *) js_vm_alloc (vm, sizeof (*n->u.varray));
n->u.varray->prototype = NULL;
n->u.varray->length = length;
n->u.varray->data = (JSNode *) js_vm_alloc (vm, length * sizeof (JSNode));
for (i = 0; i < length; i++)
n->u.varray->data[i].type = JS_UNDEFINED;
}
static inline void
js_vm_expand_array (JSVirtualMachine *vm, JSNode *n, unsigned int length)
{
if (n->u.varray->length < length)
{
n->u.varray->data = (JSNode *) js_vm_realloc (vm, n->u.varray->data,
length * sizeof (JSNode));
for (; n->u.varray->length < length; n->u.varray->length++)
n->u.varray->data[n->u.varray->length].type = JS_UNDEFINED;
}
}
/* File. */
/* Enter file <fp> to the system. */
void js_builtin_File_new (JSVirtualMachine *vm, JSNode *result_return,
char *path, JSIOStream *stream, int dont_close);
/* RegExp. */
/*
* Create a new regular expression node from <source, sourcelen> according
* to <flags>. The argument <immutable> defines whether the created
* regexp is immutable. The new regexp is returned in <result_return>.
* If the <info> is NULL, the function will resolve it. Otherwise the given
* value is used.
*/
void js_builtin_RegExp_new (JSVirtualMachine *vm, char *source,
unsigned int source_len, unsigned int flags,
int immutable, JSBuiltinInfo *info,
JSNode *result_return);
/*
* Do search-replace for the string <data, datalen> by replacing
* matches of <regexp> with <repl, repl_len>. The resulting string is
* returned in <result_return>
*/
void js_builtin_RegExp_replace (JSVirtualMachine *vm, char *data,
unsigned int datalen, JSNode *regexp,
char *repl, unsigned int repl_len,
JSNode *result_return);
/*
* Do regexp match against <data, datalen>. Format the result array
* to <result_return>.
*/
void js_builtin_RegExp_match (JSVirtualMachine *vm, char *data,
unsigned int datalen, JSNode *regexp,
JSNode *result_return);
/*
* Do regexp search against <data, datalen>. Return the start index of
* the match in <result_return>.
*/
void js_builtin_RegExp_search (JSVirtualMachine *vm, char *data,
unsigned int datalen, JSNode *regexp,
JSNode *result_return);
/*
* Split the string <data, datalen> by regular expression <regexp>.
* Function returns an array containing the substrings.
*/
void js_builtin_RegExp_split (JSVirtualMachine *vm, char *data,
unsigned int datalen, JSNode *regexp,
unsigned int limit, JSNode *result_return);
/* Object. */
JSObject *js_vm_object_new (JSVirtualMachine *vm);
void js_vm_object_mark (JSObject *obj);
int js_vm_object_load_property (JSVirtualMachine *vm, JSObject *obj,
JSSymbol prop, JSNode *value_return);
void js_vm_object_store_property (JSVirtualMachine *vm, JSObject *obj,
JSSymbol prop, JSNode *value);
void js_vm_object_delete_property (JSVirtualMachine *vm, JSObject *obj,
JSSymbol prop);
void js_vm_object_load_array (JSVirtualMachine *vm, JSObject *obj, JSNode *sel,
JSNode *value_return);
void js_vm_object_store_array (JSVirtualMachine *vm, JSObject *obj,
JSNode *sel, JSNode *value);
void js_vm_object_delete_array (JSVirtualMachine *vm, JSObject *obj,
JSNode *sel);
int js_vm_object_nth (JSVirtualMachine *vm, JSObject *obj, int nth,
JSNode *value_return);
/* Debug. */
void js_vm_stacktrace (JSVirtualMachine *vm, unsigned int num_frames);
/* Strings. */
static inline void
js_vm_make_string (JSVirtualMachine *vm, JSNode *n, const char *data,
unsigned int data_len)
{
n->type = JS_STRING;
n->u.vstring = (JSString *) js_vm_alloc (vm, sizeof (*n->u.vstring));
n->u.vstring->staticp = 0;
n->u.vstring->prototype = NULL;
n->u.vstring->len = data_len;
n->u.vstring->data = (unsigned char *) js_vm_alloc (vm, data_len);
if (data)
memcpy (n->u.vstring->data, data, data_len);
}
static inline void
js_vm_make_static_string (JSVirtualMachine *vm, JSNode *n, const char *data,
unsigned int data_len)
{
n->type = JS_STRING;
n->u.vstring = (JSString *) js_vm_alloc (vm, sizeof (*n->u.vstring));
n->u.vstring->staticp = 1;
n->u.vstring->prototype = NULL;
n->u.vstring->len = data_len;
n->u.vstring->data = (unsigned char *) data;
}
static inline int
js_compare_strings (JSNode *a, JSNode *b)
{
unsigned int i;
for (i = 0; i < a->u.vstring->len && i < b->u.vstring->len; i++)
{
if (a->u.vstring->data[i] < b->u.vstring->data[i])
return -1;
if (a->u.vstring->data[i] > b->u.vstring->data[i])
return 1;
}
if (a->u.vstring->len < b->u.vstring->len)
return -1;
if (a->u.vstring->len > b->u.vstring->len)
return 1;
return 0;
}
static inline char *
js_string_to_c_string (JSVirtualMachine *vm, const JSNode *a)
{
char *cp;
cp = (char *) js_malloc (vm, a->u.vstring->len + 1);
memcpy (cp, a->u.vstring->data, a->u.vstring->len);
cp[a->u.vstring->len] = '\0';
return cp;
}
/* Dynamic loading. */
/*
* Try to open shared library <filename>. If the opening was
* successful, a handle to the library is returned. Otherwise, the
* function returns NULL, and an error message is returned in
* <error_return>. The argument <error_return_len> specifies the
* maximum length of the error message the function should return.
*/
void *js_dl_open (const char *filename, char *error_return,
unsigned int error_return_len);
/*
* Try to fetch the address of the symbol <symbol> from shared library
* <library>.
*/
void *js_dl_sym (void *library, char *symbol, char *error_return,
unsigned int error_return_len);
/* Misc helper functions. */
unsigned long js_crc32 (const unsigned char *s, unsigned int len);
/*
* Definitions for the JavaScript part of the JavaScript interp.
*/
/* Flags for the compiler. See `jsc/entry.js'. */
#define JSC_FLAG_VERBOSE 0x00000001
#define JSC_FLAG_ANNOTATE_ASSEMBLER 0x00000002
#define JSC_FLAG_GENERATE_DEBUG_INFO 0x00000004
#define JSC_FLAG_GENERATE_EXECUTABLE_BC_FILES 0x00000008
#define JSC_FLAG_OPTIMIZE_PEEPHOLE 0x00000020
#define JSC_FLAG_OPTIMIZE_JUMPS 0x00000040
#define JSC_FLAG_OPTIMIZE_BC_SIZE 0x00000080
#define JSC_FLAG_OPTIMIZE_HEAVY 0x00000100
#define JSC_FLAG_OPTIMIZE_MASK 0x0000fff0
#define JSC_FLAG_WARN_UNUSED_ARGUMENT 0x00010000
#define JSC_FLAG_WARN_UNUSED_VARIABLE 0x00020000
#define JSC_FLAG_WARN_SHADOW 0x00040000
#define JSC_FLAG_WARN_WITH_CLOBBER 0x00080000
#define JSC_FLAG_WARN_MISSING_SEMICOLON 0x00100000
#define JSC_FLAG_WARN_STRICT_ECMA 0x00200000
#define JSC_FLAG_WARN_DEPRECATED 0x00400000
#define JSC_FLAG_WARN_MASK 0xffff0000
/* JavaScript interpreter handle. */
struct js_interp_st
{
JSInterpOptions options;
JSVirtualMachine *vm;
};
#ifdef __cplusplus
}
#endif
#endif /* not JSINT_H */
|