Changeset 34470 in webkit


Ignore:
Timestamp:
Jun 9, 2008 10:10:27 AM (16 years ago)
Author:
cwzwarich@webkit.org
Message:

2008-06-09 Cameron Zwarich <cwzwarich@uwaterloo.ca>

Reviewed by Darin.

Bug 17531: Add interactive mode to testkjs
<https://bugs.webkit.org/show_bug.cgi?id=17531>

This is a cleaned up version of Sam's earlier patch to add an
interactive mode to testkjs.

Readline support is only enabled on Darwin platforms for now, but
other ports can enable it by defining HAVE_READLINE in kjs/config.h.

  • JavaScriptCore.xcodeproj/project.pbxproj:
  • kjs/config.h:
  • kjs/testkjs.cpp: (Options::Options): (runWithScripts): (runInteractive): (printUsageStatement): (parseArguments): (kjsmain):
Location:
trunk/JavaScriptCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r34457 r34470  
     12008-06-09  Cameron Zwarich  <cwzwarich@uwaterloo.ca>
     2
     3        Reviewed by Darin.
     4
     5        Bug 17531: Add interactive mode to testkjs
     6        <https://bugs.webkit.org/show_bug.cgi?id=17531>
     7
     8        This is a cleaned up version of Sam's earlier patch to add an
     9        interactive mode to testkjs.
     10
     11        Readline support is only enabled on Darwin platforms for now, but
     12        other ports can enable it by defining HAVE_READLINE in kjs/config.h.
     13
     14        * JavaScriptCore.xcodeproj/project.pbxproj:
     15        * kjs/config.h:
     16        * kjs/testkjs.cpp:
     17        (Options::Options):
     18        (runWithScripts):
     19        (runInteractive):
     20        (printUsageStatement):
     21        (parseArguments):
     22        (kjsmain):
     23
    1242008-06-08  Cameron Zwarich  <cwzwarich@uwaterloo.ca>
    225
  • trunk/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

    r34412 r34470  
    228228                A8E894320CD0602400367179 /* JSCallbackObjectFunctions.h in Headers */ = {isa = PBXBuildFile; fileRef = A8E894310CD0602400367179 /* JSCallbackObjectFunctions.h */; };
    229229                A8E894340CD0603F00367179 /* JSGlobalObject.h in Headers */ = {isa = PBXBuildFile; fileRef = A8E894330CD0603F00367179 /* JSGlobalObject.h */; settings = {ATTRIBUTES = (Private, ); }; };
     230                BC8E03F60D72A9FC006FC608 /* libedit.2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = BC8E03F50D72A9FC006FC608 /* libedit.2.dylib */; };
    230231                BC8F3CED0DAF1A8000577A80 /* ConstructData.h in Headers */ = {isa = PBXBuildFile; fileRef = BC8F3CCF0DAF17BA00577A80 /* ConstructData.h */; settings = {ATTRIBUTES = (Private, ); }; };
    231232                BCF655590A2049710038A194 /* MathExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = BCF6553B0A2048DE0038A194 /* MathExtras.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    575576                A8E894310CD0602400367179 /* JSCallbackObjectFunctions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSCallbackObjectFunctions.h; sourceTree = "<group>"; };
    576577                A8E894330CD0603F00367179 /* JSGlobalObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSGlobalObject.h; sourceTree = "<group>"; };
     578                BC8E03F50D72A9FC006FC608 /* libedit.2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libedit.2.dylib; path = /usr/lib/libedit.2.dylib; sourceTree = "<absolute>"; };
    577579                BC8F3CCF0DAF17BA00577A80 /* ConstructData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ConstructData.h; sourceTree = "<group>"; };
    578580                BCF6553B0A2048DE0038A194 /* MathExtras.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = MathExtras.h; sourceTree = "<group>"; };
     
    687689                        files = (
    688690                                932F5BEA0822A1C700736975 /* JavaScriptCore.framework in Frameworks */,
     691                                BC8E03F60D72A9FC006FC608 /* libedit.2.dylib in Frameworks */,
    689692                        );
    690693                        runOnlyForDeploymentPostprocessing = 0;
     
    746749                                9322A00306C341D3009067BB /* libicucore.dylib */,
    747750                                51F0EC0705C86C9A00E6DF1B /* libobjc.dylib */,
     751                                BC8E03F50D72A9FC006FC608 /* libedit.2.dylib */,
    748752                        );
    749753                        name = Frameworks;
  • trunk/JavaScriptCore/kjs/config.h

    r34067 r34470  
    2727#define HAVE_MMAP 1
    2828#define HAVE_MERGESORT 1
     29#define HAVE_READLINE 1
    2930#define HAVE_SBRK 1
    3031#define HAVE_STRINGS_H 1
  • trunk/JavaScriptCore/kjs/testkjs.cpp

    r34437 r34470  
    4545#endif
    4646
     47#if HAVE(READLINE)
     48#include <readline/readline.h>
     49#endif
     50
    4751#if HAVE(SYS_TIME_H)
    4852#include <sys/time.h>
     
    7680static JSValue* functionQuit(ExecState*, JSObject*, const List&);
    7781
     82struct Options {
     83    Options()
     84        : interactive(false)
     85        , prettyPrint(false)
     86        , dump(false)
     87    {
     88    }
     89
     90    bool interactive;
     91    bool prettyPrint;
     92    bool dump;
     93    Vector<UString> fileNames;
     94    Vector<UString> arguments;
     95};
     96
     97static const char interactivePrompt[] = "> ";
     98static const UString interpreterName("Interpreter");
     99
    78100class StopWatch {
    79101public:
     
    293315}
    294316
    295 static bool runWithScripts(const Vector<UString>& fileNames, Vector<UString>& arguments, bool prettyPrint, bool dump)
    296 {
    297     GlobalObject* globalObject = new GlobalObject(arguments);
     317static bool runWithScripts(GlobalObject* globalObject, const Vector<UString>& fileNames, bool prettyPrint, bool dump)
     318{
    298319    Vector<char> script;
    299320
     
    302323
    303324    bool success = true;
    304 
    305325    for (size_t i = 0; i < fileNames.size(); i++) {
    306326        UString fileName = fileNames[i];
     
    325345}
    326346
     347static void runInteractive(GlobalObject* globalObject)
     348{   
     349    bool done = false;
     350    while (!done) {
     351#if HAVE_READLINE
     352        char* line = readline(interactivePrompt);
     353        if (!line)
     354            break;
     355        if (line[0])
     356            add_history(line);
     357        Completion completion = Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), interpreterName, 1, line);
     358        free(line);
     359#else
     360        printf(interactivePrompt);
     361        Vector<char, 256> line;
     362        int c;
     363        while ((c = getchar()) != EOF) {
     364            // FIXME: Should we also break on \r?
     365            if (c == '\n')
     366                break;
     367            line.append(c);
     368        }
     369        line.append('\0');
     370        Completion completion = Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), interpreterName, 1, line.data());
     371#endif
     372        if (completion.isValueCompletion())
     373            printf("%s\n", completion.value()->toString(globalObject->globalExec()).UTF8String().c_str());
     374    }
     375}
     376
    327377static void printUsageStatement()
    328378{
    329379    fprintf(stderr, "Usage: testkjs [options] [files] [-- arguments]\n");
    330     fprintf(stderr, "  -f  Specifies a source file (deprecated)\n");
    331     fprintf(stderr, "  -p  Prints formatted source code\n");
    332     fprintf(stderr, "  -d  Dumps bytecode (debug builds only)\n");
    333     fprintf(stderr, "  -s  Installs signal handlers that exit on a crash (Unix platforms only)\n");
     380    fprintf(stderr, "  -d         Dumps bytecode (debug builds only)\n");
     381    fprintf(stderr, "  -f         Specifies a source file (deprecated)\n");
     382    fprintf(stderr, "  -h|--help  Prints this help message\n");
     383    fprintf(stderr, "  -i         Enables interactive mode (default if no files are specified)\n");
     384    fprintf(stderr, "  -p         Prints formatted source code\n");
     385    fprintf(stderr, "  -s         Installs signal handlers that exit on a crash (Unix platforms only)\n");
    334386    exit(-1);
    335387}
    336388
    337 static void parseArguments(int argc, char** argv, Vector<UString>& fileNames, Vector<UString>& arguments, bool& prettyPrint, bool& dump)
    338 {
    339     if (argc < 2)
    340         printUsageStatement();
    341 
     389static void parseArguments(int argc, char** argv, Options& options)
     390{
    342391    int i = 1;
    343392    for (; i < argc; ++i) {
     
    346395            if (++i == argc)
    347396                printUsageStatement();
    348             fileNames.append(argv[i]);
     397            options.fileNames.append(argv[i]);
    349398            continue;
    350399        }
     400        if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
     401            printUsageStatement();
     402        }
     403        if (strcmp(arg, "-i") == 0) {
     404            options.interactive = true;
     405            continue;
     406        }
    351407        if (strcmp(arg, "-p") == 0) {
    352             prettyPrint = true;
     408            options.prettyPrint = true;
    353409            continue;
    354410        }
    355411        if (strcmp(arg, "-d") == 0) {
    356             dump = true;
     412            options.dump = true;
    357413            continue;
    358414        }
     
    370426            break;
    371427        }
    372         fileNames.append(argv[i]);
     428        options.fileNames.append(argv[i]);
    373429    }
    374430   
    375     if (fileNames.isEmpty())
    376         printUsageStatement();
     431    if (options.fileNames.isEmpty())
     432        options.interactive = true;
    377433   
    378434    for (; i < argc; ++i)
    379         arguments.append(argv[i]);
     435        options.arguments.append(argv[i]);
    380436}
    381437
     
    386442    JSLock lock;
    387443
    388     bool prettyPrint = false;
    389     bool dump = false;
    390     Vector<UString> fileNames;
    391     Vector<UString> arguments;
    392     parseArguments(argc, argv, fileNames, arguments, prettyPrint, dump);
    393 
    394     bool success = runWithScripts(fileNames, arguments, prettyPrint, dump);
     444    Options options;
     445    parseArguments(argc, argv, options);
     446
     447    GlobalObject* globalObject = new GlobalObject(options.arguments);
     448    bool success = runWithScripts(globalObject, options.fileNames, options.prettyPrint, options.dump);
     449    if (options.interactive && success)
     450        runInteractive(globalObject);
    395451
    396452#ifndef NDEBUG
Note: See TracChangeset for help on using the changeset viewer.