Changeset 40663 in webkit


Ignore:
Timestamp:
Feb 4, 2009 9:31:40 PM (15 years ago)
Author:
barraclough@apple.com
Message:

2009-02-04 Gavin Barraclough <barraclough@apple.com>

Reviewed by Oliver 'the nun' Hunt.

Add -e switch to jsc to enable evaluation of scripts passed on the command line.

  • jsc.cpp: (Script::Script): (runWithScripts): (printUsageStatement): (parseArguments): (jscmain):
Location:
trunk/JavaScriptCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r40660 r40663  
     12009-02-04  Gavin Barraclough  <barraclough@apple.com>
     2
     3        Reviewed by Oliver 'the nun' Hunt.
     4
     5        Add -e switch to jsc to enable evaluation of scripts passed on the command line.
     6
     7        * jsc.cpp:
     8        (Script::Script):
     9        (runWithScripts):
     10        (printUsageStatement):
     11        (parseArguments):
     12        (jscmain):
     13
    1142009-02-04  Gavin Barraclough  <barraclough@apple.com>
    215
  • trunk/JavaScriptCore/jsc.cpp

    r40419 r40663  
    7777static NO_RETURN JSValuePtr functionQuit(ExecState*, JSObject*, JSValuePtr, const ArgList&);
    7878
     79struct Script {
     80    bool isFile;
     81    char *argument;
     82   
     83    Script(bool isFile, char *argument)
     84        : isFile(isFile)
     85        , argument(argument)
     86    {
     87    }
     88};
     89
    7990struct Options {
    8091    Options()
     
    8697    bool interactive;
    8798    bool dump;
    88     Vector<UString> fileNames;
     99    Vector<Script> scripts;
    89100    Vector<UString> arguments;
    90101};
     
    312323}
    313324
    314 static bool runWithScripts(GlobalObject* globalObject, const Vector<UString>& fileNames, bool dump)
    315 {
    316     Vector<char> script;
     325static bool runWithScripts(GlobalObject* globalObject, const Vector<Script>& scripts, bool dump)
     326{
     327    UString script;
     328    UString fileName;
     329    Vector<char> scriptBuffer;
    317330
    318331    if (dump)
     
    325338
    326339    bool success = true;
    327     for (size_t i = 0; i < fileNames.size(); i++) {
    328         UString fileName = fileNames[i];
    329 
    330         if (!fillBufferWithContentsOfFile(fileName, script))
    331             return false; // fail early so we can catch missing files
     340    for (size_t i = 0; i < scripts.size(); i++) {
     341        if (scripts[i].isFile) {
     342            fileName = scripts[i].argument;
     343            if (!fillBufferWithContentsOfFile(fileName, scriptBuffer))
     344                return false; // fail early so we can catch missing files
     345            script = scriptBuffer.data();
     346        } else {
     347            script = scripts[i].argument;
     348            fileName = "[Command Line]";
     349        }
    332350
    333351#if ENABLE(OPCODE_SAMPLING)
    334352        interpreter->sampler()->start();
    335353#endif
    336         Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
     354        Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script, fileName));
    337355        success = success && completion.complType() != Throw;
    338356        if (dump) {
     
    391409}
    392410
    393 static NO_RETURN void printUsageStatement()
     411static NO_RETURN void printUsageStatement(bool help = false)
    394412{
    395413    fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
    396414    fprintf(stderr, "  -d         Dumps bytecode (debug builds only)\n");
     415    fprintf(stderr, "  -e         Evaluate argument as script code\n");
    397416    fprintf(stderr, "  -f         Specifies a source file (deprecated)\n");
    398417    fprintf(stderr, "  -h|--help  Prints this help message\n");
    399418    fprintf(stderr, "  -i         Enables interactive mode (default if no files are specified)\n");
    400419    fprintf(stderr, "  -s         Installs signal handlers that exit on a crash (Unix platforms only)\n");
    401     exit(EXIT_FAILURE);
     420    exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
    402421}
    403422
     
    410429            if (++i == argc)
    411430                printUsageStatement();
    412             options.fileNames.append(argv[i]);
     431            options.scripts.append(Script(true, argv[i]));
    413432            continue;
    414433        }
     434        if (strcmp(arg, "-e") == 0) {
     435            if (++i == argc)
     436                printUsageStatement();
     437            options.scripts.append(Script(false, argv[i]));
     438            continue;
     439        }
    415440        if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
    416             printUsageStatement();
     441            printUsageStatement(true);
    417442        }
    418443        if (strcmp(arg, "-i") == 0) {
     
    437462            break;
    438463        }
    439         options.fileNames.append(argv[i]);
     464        options.scripts.append(Script(true, argv[i]));
    440465    }
    441466   
    442     if (options.fileNames.isEmpty())
     467    if (options.scripts.isEmpty())
    443468        options.interactive = true;
    444469   
     
    455480
    456481    GlobalObject* globalObject = new (globalData) GlobalObject(options.arguments);
    457     bool success = runWithScripts(globalObject, options.fileNames, options.dump);
     482    bool success = runWithScripts(globalObject, options.scripts, options.dump);
    458483    if (options.interactive && success)
    459484        runInteractive(globalObject);
Note: See TracChangeset for help on using the changeset viewer.