Ignore:
Timestamp:
Feb 24, 2008, 5:34:09 PM (17 years ago)
Author:
[email protected]
Message:

Reviewed by Oliver Hunt and Mark Rowe.

http://bugs.webkit.org/show_bug.cgi?id=17505
Add support for getting command line arguments in testkjs

  • This slightly changes the behavior of parsing arguments by requiring a '-f' before all files.
  • kjs/testkjs.cpp: (createGlobalObject): Add a global property called 'arguments' which contains an array with the parsed arguments as strings. (runWithScripts): Pass in the arguments vector so that it can be passed to the global object. (parseArguments): Change parsing rules to require a '-f' before any script file. After all '-f' and '-p' arguments have been parsed, the remaining are added to the arguments vector and exposed to the script. If there is a chance of ambiguity (the user wants to pass the string '-f' to the script), the string '--' can be used separate the options from the pass through arguments. (kjsmain):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/testkjs.cpp

    r29293 r30553  
    2727#include "JSLock.h"
    2828#include "Parser.h"
     29#include "array_object.h"
    2930#include "collector.h"
    3031#include "interpreter.h"
     
    223224}
    224225
    225 static GlobalImp* createGlobalObject()
     226static GlobalImp* createGlobalObject(Vector<UString>& arguments)
    226227{
    227228  GlobalImp* global = new GlobalImp;
     
    240241  global->put(global->globalExec(), "load", new TestFunctionImp(TestFunctionImp::Load, 1));
    241242
     243  JSObject* array = global->arrayConstructor()->construct(global->globalExec(), global->globalExec()->emptyList());
     244  for (size_t i = 0; i < arguments.size(); ++i)
     245    array->put(global->globalExec(), i, jsString(arguments[i]));
     246  global->put(global->globalExec(), "arguments", array);
     247
    242248  Interpreter::setShouldPrintExceptions(true);
    243249  return global;
     
    259265}
    260266
    261 static bool runWithScripts(const Vector<UString>& fileNames, bool prettyPrint)
    262 {
    263   GlobalImp* globalObject = createGlobalObject();
     267static bool runWithScripts(const Vector<UString>& fileNames, Vector<UString>& arguments, bool prettyPrint)
     268{
     269  GlobalImp* globalObject = createGlobalObject(arguments);
    264270  Vector<char> script;
    265271 
     
    282288}
    283289
    284 static void parseArguments(int argc, char** argv, Vector<UString>& fileNames, bool& prettyPrint)
    285 {
    286   if (argc < 2) {
    287     fprintf(stderr, "Usage: testkjs file1 [file2...]\n");
     290static void printUsageStatement()
     291{
     292    fprintf(stderr, "Usage: testkjs -f file1 [-f file2...][-p][-- arguments...]\n");
    288293    exit(-1);
    289   }
    290  
    291   for (int i = 1; i < argc; i++) {
    292     const char* fileName = argv[i];
    293     if (strcmp(fileName, "-f") == 0) // mozilla test driver script uses "-f" prefix for files
     294}
     295
     296static void parseArguments(int argc, char** argv, Vector<UString>& fileNames, Vector<UString>& arguments, bool& prettyPrint)
     297{
     298  if (argc < 3)
     299    printUsageStatement();
     300
     301  int i = 1;
     302  for (; i < argc; ++i) {
     303    const char* arg = argv[i];
     304    if (strcmp(arg, "-f") == 0) {
     305      if (++i == argc)
     306        printUsageStatement();
     307      fileNames.append(argv[i]);
    294308      continue;
    295     if (strcmp(fileName, "-p") == 0) {
     309    }
     310    if (strcmp(arg, "-p") == 0) {
    296311      prettyPrint = true;
    297312      continue;
    298313    }
    299     fileNames.append(fileName);
    300   }
     314    if (strcmp(arg, "--") == 0) {
     315      ++i;
     316      break;
     317    }
     318    break;
     319  }
     320
     321  for (; i < argc; ++i)
     322    arguments.append(argv[i]);
    301323}
    302324
     
    307329  bool prettyPrint = false;
    308330  Vector<UString> fileNames;
    309   parseArguments(argc, argv, fileNames, prettyPrint);
    310  
    311   bool success = runWithScripts(fileNames, prettyPrint);
     331  Vector<UString> arguments;
     332  parseArguments(argc, argv, fileNames, arguments, prettyPrint);
     333 
     334  bool success = runWithScripts(fileNames, arguments, prettyPrint);
    312335
    313336#ifndef NDEBUG
Note: See TracChangeset for help on using the changeset viewer.