DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the JavaScript 2 Prototype.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */


/* JS2 Engine - */

#ifdef _WIN32
#include "msvc_pragma.h"
#endif

#include <algorithm>
#include <assert.h>
#include <list>
#include <stack>

#include "world.h"
#include "utilities.h"
#include "js2value.h"
#include "jslong.h"
#include "numerics.h"
#include "fdlibm_ns.h"

#include <map>
#include <algorithm>

#include "reader.h"
#include "parser.h"
#include "js2engine.h"
#include "regexp.h"
#include "bytecodecontainer.h"
#include "js2metadata.h"

namespace JavaScript {
namespace MetaData {

    // Begin execution of a bytecodeContainer
    js2val JS2Engine::interpret(Phase execPhase, BytecodeContainer *targetbCon, Environment *env)
    {
        packageFrame = env->getPackageFrame();
        jsr(execPhase, targetbCon, sp - execStack, JS2VAL_VOID, env);
        ActivationFrame *f = activationStackTop;
        js2val result;
        try {
            result = interpreterLoop();
        }
        catch (Exception &jsx) {
            activationStackTop = f;
            rts();
            throw jsx;
        }
        activationStackTop = f - 1; // when execution falls 'off the bottom' an rts hasn't occurred
                                    // so the activation stack is off by 1.
        return result;
    }

    // Execute the opcode sequence at pc.
    js2val JS2Engine::interpreterLoop()
    {
        js2val a = JS2VAL_VOID;
        js2val b = JS2VAL_VOID;
        js2val baseVal = JS2VAL_VOID;
        js2val indexVal = JS2VAL_VOID;
	    ParameterFrame *pFrame = NULL;
        const String *astr = NULL;
        const String *bstr = NULL;
        DEFINE_ROOTKEEPER(meta, rk1, pFrame);
        DEFINE_ROOTKEEPER(meta, rk2, astr);
        DEFINE_ROOTKEEPER(meta, rk3, bstr);

        DEFINE_ROOTKEEPER(meta, rk4, a);
        DEFINE_ROOTKEEPER(meta, rk5, b);
        DEFINE_ROOTKEEPER(meta, rk6, baseVal);
        DEFINE_ROOTKEEPER(meta, rk7, indexVal);

        retval = JS2VAL_VOID;
        while (true) {
            try {
                a = JS2VAL_VOID;
                b = JS2VAL_VOID;
#ifdef TRACE_DEBUG
                if (traceInstructions)
                    printInstruction(pc, bCon->getCodeStart(), bCon, this);
#endif
                JS2Op op = (JS2Op)*pc++;
                switch (op) {
        #include "js2op_arithmetic.cpp"
        #include "js2op_invocation.cpp"
        #include "js2op_access.cpp"
        #include "js2op_literal.cpp"
        #include "js2op_flowcontrol.cpp"
                default:
                    NOT_REACHED("Bad opcode, no biscuit");
                }
            }
            catch (Exception &jsx) {
                if (mTryStack.size() > 0) {
                    // The handler for this exception is on the top of the try stack.
                    // It specifies the activation that was active when the try block
                    // was entered. We unwind the activation stack, looking for the
                    // one that matches the handler's. The bytecode container, pc and
                    // sp are all reset appropriately, and execution continues.
                    HandlerData *hndlr = (HandlerData *)mTryStack.top();
                    ActivationFrame *curAct = (activationStackEmpty()) ? NULL : (activationStackTop - 1);
                
                    js2val x = JS2VAL_UNDEFINED;
                    if (curAct != hndlr->mActivation) {
                        ASSERT(!activationStackEmpty());
                        ActivationFrame *prev;
                        do {
                            prev = curAct;
                            if (prev->pc == NULL) {
                                // Yikes! the exception is getting thrown across a re-invocation
                                // of the interpreter loop.
                                // (pc == NULL) is the flag on an activation to indicate that the
                                // interpreter loop was re-invoked (probably a 'load' or 'eval' is
                                // in process). In this case we simply re-throw the exception and let
                                // the prior invocation deal with it.

                                throw jsx;
                            }
                            // we need to clean up each activation as we pop it off
                            // - basically this means just resetting it's frame
                            curAct = --activationStackTop;
                            localFrame = activationStackTop->localFrame;
                            parameterFrame = activationStackTop->parameterFrame;
                            parameterSlots = activationStackTop->parameterSlots;
                            if (parameterFrame)
                                parameterFrame->argSlots = parameterSlots;
                            bCon = activationStackTop->bCon;
                            if (hndlr->mActivation != curAct) {
                                while (activationStackTop->newEnv->getTopFrame() != activationStackTop->topFrame)
                                    activationStackTop->newEnv->removeTopFrame();
                                meta->env = activationStackTop->env;                        
                            }
                            else
                                break;
                        } while (true);
                        activationStackTop = prev;      // need the one before the target function to 
                                                        // be at the top, because of postincrement
                        localFrame = activationStackTop->localFrame;
                        parameterFrame = activationStackTop->parameterFrame;
                        parameterSlots = activationStackTop->parameterSlots;
                        if (parameterFrame)
                            parameterFrame->argSlots = parameterSlots;
                        bCon = activationStackTop->bCon;
                        meta->env = activationStackTop->env;
                    }
                    // make sure there's a JS object for the catch clause to work with
                    if (jsx.hasKind(Exception::userException)) {
                        x = jsx.value;
                    }
                    else {
                        js2val argv[1];
                        argv[0] = allocString(new String(jsx.fullMessage()));
                        switch (jsx.kind) {
                        case Exception::syntaxError:
                            x = SyntaxError_Constructor(meta, JS2VAL_NULL, argv, 1);
                            break;
                        case Exception::referenceError:
                            x = ReferenceError_Constructor(meta, JS2VAL_NULL, argv, 1);
                            break;
                        case Exception::typeError:
                            x = TypeError_Constructor(meta, JS2VAL_NULL, argv, 1);
                            break;
                        case Exception::rangeError:
                            x = RangeError_Constructor(meta, JS2VAL_NULL, argv, 1);
                            break;
                        default:
                            x = Error_Constructor(meta, JS2VAL_NULL, argv, 1);
                            break;
                        }
                    }
                    sp = execStack + hndlr->mStackTop;
                    pc = hndlr->mPC;
                    meta->env->setTopFrame(hndlr->mFrame);
                    push(x);
                }
                else
                    throw jsx; //reportError(Exception::uncaughtError, "No handler for throw");
            }
        }
        return retval;
    }

    // See if the double value is in the hash table, return it's pointer if so
    // If not, fill the table or return a un-hashed pointer
    float64 *JS2Engine::newDoubleValue(float64 x)
    {
        float64 *p = (float64 *)meta->alloc(sizeof(float64), PondScum::GenericFlag);
        *p = x;
        return p;

/*
        union {
            float64 x;
            uint8 a[8];
        } u;

        u.x = x;
        uint8 hash = (uint8)(u.a[0] ^ u.a[1] ^ u.a[2] ^ u.a[3] ^ u.a[4] ^ u.a[5] ^  u.a[6] ^ u.a[7]);
        if (float64Table[hash]) {
            if (*float64Table[hash] == x)
                return float64Table[hash];
            else {
                float64 *p = (float64 *)JS2Object::alloc(sizeof(float64), PondScum::GenericFlag);
                *p = x;
                return p;
            }
        }
        else {
            float64 *p = (float64 *)JS2Object::alloc(sizeof(float64), PondScum::GenericFlag);
            float64Table[hash] = p;
            *p = x;
            return p;
        }
*/
    }

/*

    runtime strings will now be allocated from a separate 'World'
    which is GC-able

*/

    String *JS2Engine::allocStringPtr(const char *s)
    { 
        String *p = (String *)(meta->alloc(sizeof(String), PondScum::StringFlag));
        size_t len = strlen(s);
        String *result = new (p) String(len, uni::null);
        for (size_t i = 0; i < len; i++) {
            (*result)[i] = widen(s[i]);
        }
//        std::transform(s, s+len, result->begin(), widen);
        return result; 
    }

    String *JS2Engine::allocStringPtr(const char16 *s, uint32 length)
    { 
        String *p = (String *)(meta->alloc(sizeof(String), PondScum::StringFlag));
        String *result = new (p) String(s, length);
        return result; 
    }

    String *JS2Engine::allocStringPtr(const String *s)
    {
        String *p = (String *)(meta->alloc(sizeof(String), PondScum::StringFlag));
        return new (p) String(*s);
    }

    String *JS2Engine::allocStringPtr(const String *s, uint32 index, uint32 length)
    {
        String *p = (String *)(meta->alloc(sizeof(String), PondScum::StringFlag));
        return new (p) String(*s, index, length);
    }

    String *JS2Engine::concatStrings(const String *s1, const String *s2)
    {
        String *p = (String *)(meta->alloc(sizeof(String), PondScum::StringFlag));
        String *result = new (p) String(*s1);
        result->append(*s2);
        return result;
    }

    // if the argument can be stored as an integer value, do so
    // otherwise get a double value
    js2val JS2Engine::allocNumber(float64 x)
    {
        int32 i;
        js2val retval;
        if (JSDOUBLE_IS_INT(x, i) && INT_FITS_IN_JS2VAL(i))
            retval = INT_TO_JS2VAL(i);
        else {
            if (JSDOUBLE_IS_NaN(x))
                return nanValue;
            retval = DOUBLE_TO_JS2VAL(newDoubleValue(x));
        }
        return retval;
    }

    // Don't store as an int, even if possible, we need to retain 'longness'
    js2val JS2Engine::allocULong(uint64 x)
    {
        uint64 *p = (uint64 *)(meta->alloc(sizeof(uint64), PondScum::GenericFlag));
        *p = x;
        return ULONG_TO_JS2VAL(p);
        
    }

    // Don't store as an int, even if possible, we need to retain 'longness'
    js2val JS2Engine::allocLong(int64 x)
    {
        int64 *p = (int64 *)(meta->alloc(sizeof(int64), PondScum::GenericFlag));
        *p = x;
        return LONG_TO_JS2VAL(p);
    }

    // Don't store as an int, even if possible, we need to retain 'floatness'
    js2val JS2Engine::allocFloat(float32 x)
    {
        float32 *p = (float32 *)(meta->alloc(sizeof(float32), PondScum::GenericFlag));
        *p = x;
        return FLOAT_TO_JS2VAL(p);
    }

    // Convert an integer to a string
    String *JS2Engine::numberToString(int32 i)
    {
        char buf[dtosStandardBufferSize];
        const char *chrp = doubleToStr(buf, dtosStandardBufferSize, i, dtosStandard, 0);
        return allocStringPtr(chrp);
    }
    // Convert an integer to a string
    String *JS2Engine::numberToString(uint32 i)
    {
        char buf[dtosStandardBufferSize];
        const char *chrp = doubleToStr(buf, dtosStandardBufferSize, i, dtosStandard, 0);
        return allocStringPtr(chrp);
    }

    // Convert a double to a string
    String *JS2Engine::numberToString(float64 *number)
    {
        char buf[dtosStandardBufferSize];
        const char *chrp = doubleToStr(buf, dtosStandardBufferSize, *number, dtosStandard, 0);
        return allocStringPtr(chrp);
    }

    // Convert an integer to a stringAtom
    StringAtom &JS2Engine::numberToStringAtom(int32 i)
    {
        char buf[dtosStandardBufferSize];
        const char *chrp = doubleToStr(buf, dtosStandardBufferSize, i, dtosStandard, 0);
        return meta->world.identifiers[chrp];
    }
    // Convert an integer to a stringAtom
    StringAtom &JS2Engine::numberToStringAtom(uint32 i)
    {
        char buf[dtosStandardBufferSize];
        const char *chrp = doubleToStr(buf, dtosStandardBufferSize, i, dtosStandard, 0);
        return meta->world.identifiers[chrp];
    }

    // Convert a double to a stringAtom
    StringAtom &JS2Engine::numberToStringAtom(float64 *number)
    {
        char buf[dtosStandardBufferSize];
        const char *chrp = doubleToStr(buf, dtosStandardBufferSize, *number, dtosStandard, 0);
        return meta->world.identifiers[chrp];
    }


    // x is a Number, validate that it has no fractional component
    int64 JS2Engine::checkInteger(js2val x)
    {
        int64 i;
        if (JS2VAL_IS_FLOAT(x)) {
            float64 f = *JS2VAL_TO_FLOAT(x);
            if (!JSDOUBLE_IS_FINITE(f))
                meta->reportError(Exception::rangeError, "Non integer", errorPos());
            JSLL_D2L(i, f);
            JSLL_L2D(f, i);
            if (f != *JS2VAL_TO_FLOAT(x))
                meta->reportError(Exception::rangeError, "Non integer", errorPos());
            return i;
        }
        else
        if (JS2VAL_IS_DOUBLE(x)) {
            float64 d = *JS2VAL_TO_DOUBLE(x);
            if (!JSDOUBLE_IS_FINITE(d))
                meta->reportError(Exception::rangeError, "Non integer", errorPos());
            JSLL_D2L(i, d);
            JSLL_L2D(d, i);
            if (d != *JS2VAL_TO_DOUBLE(x))
                meta->reportError(Exception::rangeError, "Non integer", errorPos());
            return i;
        }
        else
        if (JS2VAL_IS_LONG(x)) {
            JSLL_L2I(i, *JS2VAL_TO_LONG(x));
            return i;
        }
        else
        if (JS2VAL_IS_ULONG(x)) {
            JSLL_UL2I(i, *JS2VAL_TO_ULONG(x));
            return i;
        }
        ASSERT(JS2VAL_IS_INT(x));
        return JS2VAL_TO_INT(x);
    }

    float64 JS2Engine::truncateFloat64(float64 d)
    {
        if (d == 0)
            return d;
        if (!JSDOUBLE_IS_FINITE(d)) {
            if (JSDOUBLE_IS_NaN(d))
                return 0;
            return d;
        }
        bool neg = (d < 0);
        d = fd::floor(neg ? -d : d);
        return neg ? -d : d;
    }

    int32 JS2Engine::float64toInt32(float64 d)
    {
        if ((d == 0.0) || !JSDOUBLE_IS_FINITE(d) )
            return 0;
        d = fd::fmod(d, two32);
        d = (d >= 0) ? fd::floor(d) : fd::ceil(d) + two32;
        if (d >= two31)
            return (int32)(d - two32);
        else
            return (int32)(d);    
    }

    int64 JS2Engine::float64toInt64(float64 f)
    {
        int64 i;
        JSLL_D2L(i, f);
        return i;
    }

    uint32 JS2Engine::float64toUInt32(float64 d)
    {
        if ((d == 0.0) || !JSDOUBLE_IS_FINITE(d) )
            return 0;
        bool neg = (d < 0);
        d = fd::floor(neg ? -d : d);
        d = neg ? -d : d;
        d = fd::fmod(d, two32);
        d = (d >= 0) ? d : d + two32;
        return (uint32)(d);    
    }

    uint16 JS2Engine::float64toUInt16(float64 d)
    {
        if ((d == 0.0) || !JSDOUBLE_IS_FINITE(d))
            return 0;
        bool neg = (d < 0);
        d = fd::floor(neg ? -d : d);
        d = neg ? -d : d;
        d = fd::fmod(d, two16);
        d = (d >= 0) ? d : d + two16;
        return (uint16)(d);
    }

    // Insert x before the top count stack entries
    void JS2Engine::insert(js2val x, int count)
    {
        ASSERT(sp < execStackLimit);
        js2val *p = ++sp;
        for (int32 i = 0; i < count; i++) {
            *p = p[-1];
            --p;
        }
        *p = x;            
    }


    #define INIT_STRINGATOM(n) n##_StringAtom(allocStringPtr(#n))

    JS2Engine::JS2Engine(JS2Metadata *meta, World &world)
                : meta(meta),
                  pc(NULL),
                  bCon(NULL),
                  phase(RunPhase),
                  true_StringAtom(world.identifiers["true"]),
                  false_StringAtom(world.identifiers["false"]),
                  null_StringAtom(world.identifiers["null"]),
                  undefined_StringAtom(world.identifiers["undefined"]),
                  public_StringAtom(world.identifiers["public"]),
                  private_StringAtom(world.identifiers["private"]),
                  Function_StringAtom(world.identifiers["Function"]),
                  Object_StringAtom(world.identifiers["Object"]),
                  object_StringAtom(world.identifiers["object"]),
                  Empty_StringAtom(world.identifiers[""]),
                  Dollar_StringAtom(world.identifiers["$"]),
                  prototype_StringAtom(world.identifiers["prototype"]),
                  length_StringAtom(world.identifiers["length"]),
                  toString_StringAtom(world.identifiers["toString"]),
                  valueOf_StringAtom(world.identifiers["valueOf"]),
                  packageFrame(NULL),
                  localFrame(NULL),
                  parameterFrame(NULL),
                  parameterSlots(NULL),
				  parameterCount(0),
				  superConstructorCalled(false),
                  traceInstructions(false)
    {
        for (int i = 0; i < 256; i++)
            float64Table[i] = NULL;

        float64 *p = (float64 *)meta->alloc(sizeof(float64), PondScum::GenericFlag);
        *p = nan;
        nanValue = DOUBLE_TO_JS2VAL(p);
        posInfValue = DOUBLE_TO_JS2VAL(allocNumber(positiveInfinity));
        negInfValue = DOUBLE_TO_JS2VAL(allocNumber(negativeInfinity));

        sp = execStack = new js2val[INITIAL_EXEC_STACK];
        execStackLimit = execStack + INITIAL_EXEC_STACK;
        activationStackTop = activationStack = new ActivationFrame[MAX_ACTIVATION_STACK];
    }

    JS2Engine::~JS2Engine()
    {
        while (!mTryStack.empty()) {
            popHandler();
        }
        delete [] execStack;
        delete [] activationStack;
    }

#ifdef TRACE_DEBUG

    enum { BRANCH_OFFSET = 1, STR_PTR, TYPE_PTR, NAME_INDEX, FRAME_INDEX, BRANCH_PAIR, U16, FLOAT64, S32, BREAK_OFFSET_AND_COUNT };
    struct {
        JS2Op op;
        char *name;
        int flags;
    } opcodeData[] =
    {
        { eAdd,  "Add", 0 },
        { eSubtract,  "Subtract", 0 },
        { eMultiply,  "Multiply", 0 },
        { eDivide,  "Divide", 0 },
        { eModulo,  "Modulo", 0 },
        { eEqual,  "Equal", 0 },
        { eNotEqual,  "NotEqual", 0 },
        { eLess,  "Less", 0 },
        { eGreater,  "Greater", 0 },
        { eLessEqual,  "LessEqual", 0 },
        { eGreaterEqual,  "GreaterEqual", 0 },
        { eIdentical,  "Identical", 0 },
        { eNotIdentical,  "NotIdentical", 0 },
        { eLogicalXor,  "LogicalXor", 0 },
        { eLogicalNot,  "LogicalNot", 0 },
        { eMinus,  "Minus", 0 },
        { ePlus,  "Plus", 0 },
        { eComplement,  "Complement", 0 },
        { eLeftShift,  "LeftShift", 0 },
        { eRightShift,  "RightShift", 0 },
        { eLogicalRightShift,  "LogicalRightShift", 0 },
        { eBitwiseAnd,  "BitwiseAnd", 0 },
        { eBitwiseXor,  "BitwiseXor", 0 },
        { eBitwiseOr,  "BitwiseOr", 0 },
        { eTrue,  "True", 0 },
        { eFalse,  "False", 0 },
        { eNull,  "Null", 0 },
        { eUndefined,  "Undefined", 0 },
        { eLongZero,  "0(64)", 0 },
        { eNumber,  "Number", FLOAT64 },
        { eInteger,  "Integer", S32 },
        { eRegExp,  "RegExp", U16 },
        { eFunction,  "Function", U16 },
        { eClosure,  "Closure", U16 },
        { eUInt64,  "UInt64", 0 },
        { eInt64,  "Int64", 0 },
        { eString,  "String", STR_PTR },            // <string pointer:u32>
        { eThis,  "This", 0 },
        { eSuper,  "Super", 0 },
        { eSuperExpr,  "SuperExpr", 0 },
        { eNewObject,  "NewObject", U16 },         // <argCount:u16>
        { eNewArray,  "NewArray", U16 },          // <argCount:u16>

        { eThrow,  "Throw", 0 },
        { eTry,  "Try", BRANCH_PAIR },               // <finally displacement:s32> <catch displacement:s32>
        { eCallFinally,  "CallFinally", BRANCH_OFFSET },       // <branch displacement:s32>
        { eReturnFinally,  "ReturnFinally", 0 },
        { eHandler,  "Handler", 0 },

        { eCoerce, "Coerce", TYPE_PTR },            // <type pointer:u32>

        { eFirst,  "First", 0 },
        { eNext,  "Next", 0 },
        { eForValue,  "ForValue", 0 },

        { eFrameSlotRead, "FrameSlotRead", U16 },           // <slot index:u16>
        { eFrameSlotRef, "FrameSlotRef", U16 },             // <slot index:u16>
        { eFrameSlotWrite, "FrameSlotWrite", U16 },         // <slot index:u16>
        { eFrameSlotDelete, "FrameSlotDelete", U16 },       // <slot index:u16>
        { eFrameSlotPostInc, "FrameSlotPostInc", U16 },     // <slot index:u16>
        { eFrameSlotPostDec, "FrameSlotPostDec", U16 },     // <slot index:u16>
        { eFrameSlotPreInc, "FrameSlotPreInc", U16 },       // <slot index:u16>
        { eFrameSlotPreDec, "FrameSlotPreDec", U16 },       // <slot index:u16>

        { eParameterSlotRead, "ParameterSlotRead", U16 },           // <slot index:u16>
        { eParameterSlotRef, "ParameterSlotRef", U16 },             // <slot index:u16>
        { eParameterSlotWrite, "ParameterSlotWrite", U16 },         // <slot index:u16>
        { eParameterSlotDelete, "ParameterSlotDelete", U16 },       // <slot index:u16>
        { eParameterSlotPostInc, "ParameterSlotPostInc", U16 },     // <slot index:u16>
        { eParameterSlotPostDec, "ParameterSlotPostDec", U16 },     // <slot index:u16>
        { eParameterSlotPreInc, "ParameterSlotPreInc", U16 },       // <slot index:u16>
        { eParameterSlotPreDec, "ParameterSlotPreDec", U16 },       // <slot index:u16>

        { ePackageSlotRead, "PackageSlotRead", U16 },           // <slot index:u16>
        { ePackageSlotRef, "PackageSlotRef", U16 },             // <slot index:u16>
        { ePackageSlotWrite, "PackageSlotWrite", U16 },         // <slot index:u16>
        { ePackageSlotDelete, "PackageSlotDelete", U16 },       // <slot index:u16>
        { ePackageSlotPostInc, "PackageSlotPostInc", U16 },     // <slot index:u16>
        { ePackageSlotPostDec, "PackageSlotPostDec", U16 },     // <slot index:u16>
        { ePackageSlotPreInc, "PackageSlotPreInc", U16 },       // <slot index:u16>
        { ePackageSlotPreDec, "PackageSlotPreDec", U16 },       // <slot index:u16>

        { eLexicalRead,  "LexicalRead", NAME_INDEX },       // <multiname index:u16>
        { eLexicalWrite,  "LexicalWrite", NAME_INDEX },     // <multiname index:u16>
        { eLexicalInit,  "LexicalInit", NAME_INDEX },       // <multiname index:u16>
        { eLexicalRef,  "LexicalRef", NAME_INDEX },         // <multiname index:u16>
        { eLexicalDelete,  "LexicalDelete", NAME_INDEX },   // <multiname index:u16>
        { eDotRead,  "DotRead", NAME_INDEX },               // <multiname index:u16>
        { eDotWrite,  "DotWrite", NAME_INDEX },             // <multiname index:u16>
        { eDotRef,  "DotRef", NAME_INDEX },                 // <multiname index:u16>
        { eDotDelete,  "DotDelete", NAME_INDEX },           // <multiname index:u16>
        { eBracketRead,  "BracketRead", 0 },
        { eBracketWrite,  "BracketWrite", 0 },
        { eBracketRef,  "BracketRef", 0 },
        { eBracketReadForRef,  "BracketReadForRef", 0 },
        { eBracketWriteRef,  "BracketWriteRef", 0 },
        { eBracketDelete,  "BracketDelete", 0 },
        { eSlotRead,  "SlotRead", U16 },            // <slot index:u16>
        { eSlotWrite,  "SlotWrite", U16 },          // <slot index:u16>
        { eSlotRef,  "SlotRef", U16 },              // <slot index:u16>
        { eSlotDelete,  "SlotDelete", U16 },        // <slot index:u16>

        { eReturn,  "Return", 0 },
        { eReturnVoid,  "ReturnVoid", 0 },
        { ePushFrame,  "PushFrame", FRAME_INDEX },         // <frame index:u16>
        { ePopFrame,  "PopFrame", 0 },
        { eWithin, "With", 0 },
        { eWithout, "EndWith", 0 },
        { eBranchFalse,  "BranchFalse", BRANCH_OFFSET },        // <branch displacement:s32> XXX save space with short and long versions instead ?
        { eBranchTrue,  "BranchTrue", BRANCH_OFFSET },          // <branch displacement:s32>
        { eBranch,  "Branch", BRANCH_OFFSET },                  // <branch displacement:s32>
        { eBreak,  "Break", BREAK_OFFSET_AND_COUNT },           // <branch displacement:s32> <blockCount:u16>
        { eNew,  "New", U16 },                // <argCount:u16>
        { eCall,  "Call", U16 },              // <argCount:u16>
        { eSuperCall,  "SuperCall", U16 },    // <argCount:u16>
        { eTypeof,  "Typeof", 0 },
        { eInstanceof,  "Instanceof", 0 },
        { eIs,  "Is", 0 },
        { eIn,  "In", 0 },

        { ePopv,  "Popv", 0 },
        { ePop,  "Pop", 0 },
        { eDup,  "Dup", 0 },
        { eSwap,  "Swap", 0 },
        { eSwap2, "Swap2", 0 },
        { eVoid,  "Void", 0 },

        { eLexicalPostInc,  "LexicalPostInc", NAME_INDEX },    // <multiname index:u16>
        { eLexicalPostDec,  "LexicalPostDec", NAME_INDEX },    // <multiname index:u16>
        { eLexicalPreInc,  "LexicalPreInc", NAME_INDEX },     // <multiname index:u16>
        { eLexicalPreDec,  "LexicalPreDec", NAME_INDEX },     // <multiname index:u16>
        { eDotPostInc,  "DotPostInc", NAME_INDEX },        // <multiname index:u16>
        { eDotPostDec,  "DotPostDec", NAME_INDEX },        // <multiname index:u16>
        { eDotPreInc,  "DotPreInc", NAME_INDEX },         // <multiname index:u16>
        { eDotPreDec,  "DotPreDec", NAME_INDEX },         // <multiname index:u16>
        { eBracketPostInc,  "BracketPostInc", 0 },
        { eBracketPostDec,  "BracketPostDec", 0 },
        { eBracketPreInc,  "BracketPreInc", 0 },
        { eBracketPreDec,  "BracketPreDec", 0 },
        { eSlotPostInc,  "SlotPostInc", U16 },      // <slot index:u16>
        { eSlotPostDec, "SlotPostDec", U16 },       // <slot index:u16>
        { eSlotPreInc, "SlotPreInc", U16 },         // <slot index:u16>
        { eSlotPreDec, "SlotPreDec", U16 },         // <slot index:u16>

        
        { eNop, "Nop", 0 },

    };

    uint8 *printInstruction(uint8 *pc, uint8 *start, BytecodeContainer *bCon, JS2Engine *engine)
    {
        if (engine) {
            stdOut << bCon->fName << " ";
            if (bCon->fName.length() < 30) {
                for (uint32 i = 0; i < (30 - bCon->fName.length()); i++)
                    stdOut << " ";
            }
            printFormat(stdOut, "%.4d %.4d %.4d ", pc - start, 
                            (int32)(engine->sp - engine->execStack), 
                            (int32)(engine->activationStackTop - engine->activationStack));
        }
        else
            printFormat(stdOut, "%.4d ", pc - start);
        stdOut << opcodeData[*pc].name;
        switch (opcodeData[*pc++].flags) {
        case BRANCH_OFFSET:
            {
                int32 offset = BytecodeContainer::getOffset(pc);
                stdOut << " " << offset << " --> " << (pc - start) + offset;
                pc += sizeof(int32);
            }
            break;
        case BREAK_OFFSET_AND_COUNT:
            {
                int32 offset = BytecodeContainer::getOffset(pc);
                stdOut << " " << offset << " --> " << (pc - start) + offset;
                pc += sizeof(int32);
                printFormat(stdOut, " (%d)", BytecodeContainer::getShort(pc));
                pc += sizeof(short);
            }
            break;
        case TYPE_PTR:
            {
                JS2Class *c = BytecodeContainer::getType(pc);
                stdOut << " " << c->name;
                pc += sizeof(JS2Class *);
            }
            break;
        case STR_PTR:
            {
                uint16 index = BytecodeContainer::getShort(pc);
                stdOut << " \"" << bCon->mStringList[index] << "\"";
                pc += sizeof(short);
            }
            break;
        case NAME_INDEX:
            {
                Multiname *mn = bCon->mMultinameList[BytecodeContainer::getShort(pc)];
                stdOut << " " << mn->name;
                pc += sizeof(short);
            }
            break;
        case S32:
            {
                printFormat(stdOut, " %d", BytecodeContainer::getInt32(pc));
                pc += sizeof(int32);
            }
            break;
        case FRAME_INDEX:
        case U16:
            {
                printFormat(stdOut, " %d", BytecodeContainer::getShort(pc));
                pc += sizeof(short);
            }
            break;
        case BRANCH_PAIR:
            {
                int32 offset1 = BytecodeContainer::getOffset(pc);
                pc += sizeof(int32);
                int32 offset2 = BytecodeContainer::getOffset(pc);
                pc += sizeof(int32);
                if (offset1 == (int32)NotALabel)
                    stdOut << " no finally; ";
                else
                    stdOut << " (finally) " << offset1 << " --> " << (pc - start) + offset1 << "; ";
                if (offset2 == (int32)NotALabel)
                    stdOut << "no catch;";
                else
                    stdOut << "(catch) " << offset2 << " --> " << (pc - start) + offset2;
            }
            break;
        case FLOAT64:
            {
                stdOut << " " << BytecodeContainer::getFloat64(pc);
                pc += sizeof(float64);
            }
            break;
        }
        stdOut << "\n";
        return pc;
    }

    void dumpBytecode(BytecodeContainer *bCon)
    {
        uint8 *start = bCon->getCodeStart();
        uint8 *end = bCon->getCodeEnd();
        uint8 *pc = start;
        while (pc < end) {
            pc = printInstruction(pc, start, bCon, NULL);
        }
    }

#endif          // DEBUG

    // Return the effect of an opcode on the execution stack.
    // Some ops (e.g. eCall) have a variable effect, those are handled separately
    // (see emitOp)
    int getStackEffect(JS2Op op)
    {
        switch (op) {
        case eReturn:
            return -1;  

        case eAdd:          // pop two, push one
        case eSubtract:
        case eMultiply:
        case eDivide:
        case eModulo:
        case eEqual:
        case eNotEqual:
        case eLess:
        case eGreater:
        case eLessEqual:
        case eGreaterEqual:
        case eIdentical:
        case eNotIdentical:
        case eLogicalXor:
        case eLeftShift:
        case eRightShift:
        case eLogicalRightShift:
        case eBitwiseAnd:
        case eBitwiseXor:
        case eBitwiseOr:
            return -1;  

        case eMinus:        // pop one, push one
        case ePlus:
        case eComplement:
        case eTypeof:
        case eLogicalNot:
            return 0;

        case eIs:           // pop expr, pop type, push boolean
        case eInstanceof:
        case eIn:
            return 1;

        case eCoerce:       // pop value, push coerced value (type is arg)
            return 0;

        case eTry:          // no exec. stack impact
        case eHandler:
        case eCallFinally:
        case eReturnFinally:
            return 0;

        case eThrow:
            return -1;      // pop the exception object

        case eString:
        case eTrue:
        case eFalse:
        case eNumber:
        case eInteger:
        case eUInt64:
        case eInt64:
        case eNull:
        case eThis:
        case eSuper:
        case eRegExp:
        case eFunction:
        case eUndefined:
        case eLongZero:
            return 1;       // push literal value

        case eClosure:
            return 0;

        case eSuperExpr:
            return 0;

        case eSlotWrite:
            return -1;      // write the value, don't preserve it
        case eSlotRead:
            return 1;       // push the value
        case eSlotDelete:   
            return 1;       // push boolean result
        case eSlotRef:      
            return 2;       // push base & value

        case eLexicalInit:
            return -1;      // pop the value
        case eLexicalRead:
            return 1;       // push the value
        case eLexicalWrite:
            return 0;       // leave the value
        case eLexicalRef:
            return 2;       // push base & value
        case eLexicalDelete:
            return 1;       // push boolean result

        case eFrameSlotRead:
        case ePackageSlotRead:
        case eParameterSlotRead:
            return 1;          // push value
        case eFrameSlotWrite:
        case ePackageSlotWrite:
        case eParameterSlotWrite:
            return 0;          // leaves value on stack
        case eFrameSlotRef:
        case ePackageSlotRef:
        case eParameterSlotRef:
            return 2;          // push base and value
        case eFrameSlotDelete:
        case ePackageSlotDelete:
        case eParameterSlotDelete:
            return 1;          // push boolean result;

        case eDotRead:
            return 0;       // pop a base, push the value
        case eDotWrite:
            return -1;      // pop a base, leave the value
        case eDotRef:
            return 1;       // leave the base, push the value
        case eDotDelete:    // pop base, push boolean result
            return 0;

        case eBracketRead:
            return -1;      // pop a base and an index, push the value
        case eBracketWrite:
            return -2;      // pop a base and an index, leave the value
        case eBracketRef:
            return 0;       // leave the base, pop the index, push the value
        case eBracketDelete:
            return -1;      // pop base and index, push boolean result

        case eReturnVoid:
        case eBranch:
        case eBreak:
            return 0;

        case eVoid:         // remove top item, push undefined
            return 0;      

        case eDup:          // duplicate top item
            return 1;      

        case eSwap:         // swap top two items
        case eSwap2:        // or top three items
            return 0;      

        case ePop:          // remove top item
            return -1;      

        case ePopv:         // pop a statement result value
            return -1;      

        case ePushFrame:    // affect the frame stack...
        case ePopFrame:     // ...not the exec stack
            return 0;

        case eWithin:
            return -1;      // pop with'd object
        case eWithout:       
            return 0;       // frame stack pop only

        case eBranchFalse:
        case eBranchTrue:
            return -1;      // pop the boolean condition

        case eNew:          // pop the class or function, push the new instance
            return 0;

        case eFirst:        // pop object, push iterator helper
            return 1;       // and push boolean result value
        case eNext:         // leave iterator helper
            return 1;       // and push boolean result value
        case eForValue:     // leave the iterator helper
            return 1;       // and push iteration value

        case eFrameSlotPostInc:
        case eFrameSlotPostDec:
        case eFrameSlotPreInc:
        case eFrameSlotPreDec:
        case eParameterSlotPostInc:
        case eParameterSlotPostDec:
        case eParameterSlotPreInc:
        case eParameterSlotPreDec:
        case ePackageSlotPostInc:
        case ePackageSlotPostDec:
        case ePackageSlotPreInc:
        case ePackageSlotPreDec:
            return 1;           // push the new/old value

        case eLexicalPostInc:
        case eLexicalPostDec:
        case eLexicalPreInc:
        case eLexicalPreDec:
            return 1;           // push the new/old value

        case eDotPostInc:
        case eDotPostDec:
        case eDotPreInc:
        case eDotPreDec:
            return 0;       // pop the base, push the new/old value

        case eBracketPostInc:
        case eBracketPostDec:
        case eBracketPreInc:
        case eBracketPreDec:
            return -1;       // pop the base, pop the index, push the new/old value

        case eBracketReadForRef:    // leave base and index, push value
            return 1;

        case eBracketWriteRef:      // pop base and index, leave value
            return -2;


        case eNop:
            return 0;

        default:
            ASSERT(false);
        }
        return 0;
    }
    
    // Return the mapped source location for the current pc
    size_t JS2Engine::errorPos()
    {
        return bCon->getPosition((uint16)(pc - bCon->getCodeStart())); 
    }

    // XXX Default construction of an instance of the class
    // that is the value of the passed in 'this'
    js2val JS2Engine::defaultConstructor(JS2Metadata *meta, const js2val thisValue, js2val *argv, uint32 argc)
    {
        ASSERT(JS2VAL_IS_OBJECT(thisValue) && !JS2VAL_IS_NULL(thisValue));
        JS2Object *obj = JS2VAL_TO_OBJECT(thisValue);
        ASSERT(obj->kind == ClassKind);
        JS2Class *c = checked_cast<JS2Class *>(obj);
        if (!c->complete)
            meta->reportError(Exception::constantError, "Cannot construct an instance of a class before its definition has been compiled", meta->engine->errorPos());
        SimpleInstance *result = new (meta) SimpleInstance(meta, c->prototype, c);
        DEFINE_ROOTKEEPER(meta, rk, result);
        meta->invokeInit(c, OBJECT_TO_JS2VAL(result), argv, argc);
        return OBJECT_TO_JS2VAL(result);
    }

    // Save current engine state (pc, environment top) and
    // jump to start of new bytecodeContainer
    void JS2Engine::jsr(Phase execPhase, BytecodeContainer *new_bCon, uint32 stackBase, js2val returnVal, Environment *env)
    {
        if (activationStackTop >= (activationStack + MAX_ACTIVATION_STACK))
            meta->reportError(Exception::internalError, "out of activation stack", meta->engine->errorPos());
        activationStackTop->bCon = bCon;
        activationStackTop->pc = pc;
        activationStackTop->phase = phase;
        activationStackTop->execStackBase = stackBase;
        activationStackTop->retval = returnVal;
        activationStackTop->env = meta->env;    // save current environment, to be restored on rts
        activationStackTop->newEnv = env;       // and save the new environment, if an exception occurs, we can't depend on meta->env
        activationStackTop->topFrame = env->getTopFrame();  // remember how big the new env. is supposed to be so that local frames don't accumulate
        activationStackTop->localFrame = localFrame;
        activationStackTop->parameterFrame = parameterFrame;
        activationStackTop->parameterSlots = parameterSlots;
        activationStackTop->parameterCount = parameterCount;
        activationStackTop->superConstructorCalled = superConstructorCalled;
        activationStackTop++;
        if (new_bCon) {
            bCon = new_bCon;
            if ((int32)bCon->getMaxStack() >= (execStackLimit - sp)) {
                uint32 curDepth = sp - execStack;
                uint32 newDepth = curDepth + bCon->getMaxStack();
                js2val *newStack = new js2val[newDepth];
                ::memcpy(newStack, execStack, curDepth * sizeof(js2val));
                execStack = newStack;
                sp = execStack + curDepth;
                execStackLimit = execStack + newDepth;
            }
            pc = new_bCon->getCodeStart();
        }
        phase = execPhase;
        meta->env = env;
		retval = JS2VAL_VOID;
    }

    // Return to previously saved execution state
    void JS2Engine::rts()
    {
        ASSERT(activationStackTop > activationStack);
        activationStackTop--;

        bCon = activationStackTop->bCon;
        pc = activationStackTop->pc;
        phase = activationStackTop->phase;
        localFrame = activationStackTop->localFrame;
        parameterFrame = activationStackTop->parameterFrame;
        parameterSlots = activationStackTop->parameterSlots;
        parameterCount = activationStackTop->parameterCount;
        superConstructorCalled = activationStackTop->superConstructorCalled;
        if (parameterFrame) {
            parameterFrame->argSlots = parameterSlots;
            parameterFrame->argCount = parameterCount;
            parameterFrame->superConstructorCalled = superConstructorCalled;
        }
        // reset the env. top
        while (activationStackTop->newEnv->getTopFrame() != activationStackTop->topFrame)
            activationStackTop->newEnv->removeTopFrame();
        // reset to previous env.
        meta->env = activationStackTop->env;
        sp = execStack + activationStackTop->execStackBase;

		if (!JS2VAL_IS_VOID(activationStackTop->retval))
			retval = activationStackTop->retval;
    
	}

    // GC-mark any JS2Objects in the activation frame stack, the execution stack
    // and in structures contained in those locations.
    void JS2Engine::mark()
    {
        uint32 i;
        if (bCon)
            bCon->mark();
        for (ActivationFrame *f = activationStack; (f < activationStackTop); f++) {
            GCMARKOBJECT(f->env);
            GCMARKOBJECT(f->newEnv);
            if (f->bCon)
                f->bCon->mark();
            if (f->parameterSlots) {
                for (i = 0; i < f->parameterCount; i++)
                    GCMARKVALUE(f->parameterSlots[i]);
            }
        }
        for (js2val *e = execStack; (e < sp); e++) {
            GCMARKVALUE(*e);
        }
        JS2Object::mark(JS2VAL_TO_DOUBLE(nanValue));
        JS2Object::mark(JS2VAL_TO_DOUBLE(posInfValue));
        JS2Object::mark(JS2VAL_TO_DOUBLE(negInfValue));
        for (i = 0; i < 256; i++) {
            if (float64Table[i])
                JS2Object::mark(float64Table[i]);
        }
        if (parameterSlots) {
            for (i = 0; i < parameterCount; i++) {
                GCMARKVALUE(parameterSlots[i]);
            }
        }
        GCMARKVALUE(retval);
    }

    void JS2Engine::pushHandler(uint8 *pc)
    { 
        ActivationFrame *curAct = (activationStackEmpty()) ? NULL : (activationStackTop - 1);
        mTryStack.push(new HandlerData(pc, sp - execStack, curAct, meta->env->getTopFrame())); 
    }

    void JS2Engine::popHandler()
    {
        HandlerData *hndlr = mTryStack.top();
        mTryStack.pop();
        delete hndlr;
    }

    js2val JS2Engine::typeofString(js2val a)
    {
        if (JS2VAL_IS_UNDEFINED(a))
            a = allocString(undefined_StringAtom);
        else
        if (JS2VAL_IS_BOOLEAN(a))
            a = allocString("boolean");
        else
        if (JS2VAL_IS_NUMBER(a))
            a = allocString("number");
        else
        if (JS2VAL_IS_STRING(a))
            a = allocString("string");
        else {
            ASSERT(JS2VAL_IS_OBJECT(a));
            if (JS2VAL_IS_NULL(a))
                a = allocString(object_StringAtom);
            else {
                JS2Object *obj = JS2VAL_TO_OBJECT(a);
                switch (obj->kind) {
                case MultinameKind:
                    a = allocString("namespace"); 
                    break;
                case AttributeObjectKind:
                    a = allocString("attribute"); 
                    break;
                case ClassKind:
                    // typeof returns lower-case 'function', whereas the [[class]] value
                    // has upper-case 'Function'
                    a = allocString("function"); //a = STRING_TO_JS2VAL(Function_StringAtom); 
                    break;
                case SimpleInstanceKind:
                    if (checked_cast<SimpleInstance *>(obj)->type == meta->functionClass)
                        a = allocString("function"); //STRING_TO_JS2VAL(Function_StringAtom);
                    else
                        a = allocString(object_StringAtom);
//                    a = STRING_TO_JS2VAL(checked_cast<SimpleInstance *>(obj)->type->getName());
                    break;
                case PackageKind:
                    a = allocString(object_StringAtom);
                    break;
                default:
                    ASSERT(false);
                    break;
                }
            }
        }
        return a;
    }

    //
    // XXX Only scanning dynamic properties
    //
    // Initialize and build a list of names of properties in the object.
    //
    bool ForIteratorObject::first(JS2Engine *engine)
    {
        if (obj == NULL)
            return false;
        originalObj = obj;
        return buildNameList(engine->meta);
    }

    // Iterate over LocalBindings
    bool ForIteratorObject::buildNameList(JS2Metadata *meta)
    {
        LocalBindingMap *lMap = NULL;
        if (obj->kind == SimpleInstanceKind)
            lMap = &(checked_cast<SimpleInstance *>(obj))->localBindings;
        else
        if (obj->kind == PackageKind)
            lMap = &(checked_cast<Package *>(obj))->localBindings;
        else
        if (obj->kind == ClassKind)
            lMap = &(checked_cast<JS2Class *>(obj))->localBindings;

        if (lMap) {
            nameList = new const String *[lMap->size()];
            length = 0;
            for (LocalBindingIterator bi(*lMap); bi; ++bi) {
                LocalBindingEntry &lbe = *bi;
                for (LocalBindingEntry::NS_Iterator i = lbe.begin(), end = lbe.end(); (i != end); i++) {
                    LocalBindingEntry::NamespaceBinding ns = *i;
                    if ((ns.first == meta->publicNamespace) && ns.second->enumerable)
                        nameList[length++] = &lbe.name;
                }
            }
            if (length == 0) {
                if (obj->kind == SimpleInstanceKind) {
                    js2val protoval = (checked_cast<SimpleInstance *>(obj))->super;
                    if (!JS2VAL_IS_NULL(protoval)) {
                        if (JS2VAL_IS_OBJECT(protoval)) {
                            obj = JS2VAL_TO_OBJECT(protoval);
                            return buildNameList(meta);
                        }
                    }
                }
            }
            it = 0;
            return (length != 0);
        }
        return false;
    }

    //
    // Set the iterator to the next property in that list that is not
    // shadowed by a property higher up the prototype chain. If we get 
    // to the end of the list, bump down to the next object on the chain.
    //
    bool ForIteratorObject::next(JS2Engine *engine)
    {
        if (nameList) {
            it++;
            if (obj->kind == ClassKind) {
                return (it != length);
            }
            else {
/*
                if (originalObj != obj) {
                    while (it != length)
                        if (engine->meta->lookupDynamicProperty(originalObj, nameList[it]) != obj)
                            // shadowed by a property higher in the chain, so skip to next
                            it++;
                        else
                            break;
                }
*/
                if (it == length) {
                    if (obj->kind == SimpleInstanceKind) {
                        js2val protoval = (checked_cast<SimpleInstance *>(obj))->super;
                        if (!JS2VAL_IS_NULL(protoval)) {
                            if (JS2VAL_IS_OBJECT(protoval)) {
                                obj = JS2VAL_TO_OBJECT(protoval);
                                return buildNameList(engine->meta);
                            }
                        }
                    }
                    return false;
                }
                else
                    return true;
            }
        }
        else
            return false;
    }

    js2val ForIteratorObject::value(JS2Engine *engine)
    { 
        ASSERT(nameList);
        return engine->allocString(nameList[it]);
    }

}
}