Changeset 34470 in webkit for trunk/JavaScriptCore/kjs
- Timestamp:
- Jun 9, 2008, 10:10:27 AM (17 years ago)
- Location:
- trunk/JavaScriptCore/kjs
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/config.h
r34067 r34470 27 27 #define HAVE_MMAP 1 28 28 #define HAVE_MERGESORT 1 29 #define HAVE_READLINE 1 29 30 #define HAVE_SBRK 1 30 31 #define HAVE_STRINGS_H 1 -
trunk/JavaScriptCore/kjs/testkjs.cpp
r34437 r34470 45 45 #endif 46 46 47 #if HAVE(READLINE) 48 #include <readline/readline.h> 49 #endif 50 47 51 #if HAVE(SYS_TIME_H) 48 52 #include <sys/time.h> … … 76 80 static JSValue* functionQuit(ExecState*, JSObject*, const List&); 77 81 82 struct 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 97 static const char interactivePrompt[] = "> "; 98 static const UString interpreterName("Interpreter"); 99 78 100 class StopWatch { 79 101 public: … … 293 315 } 294 316 295 static bool runWithScripts(const Vector<UString>& fileNames, Vector<UString>& arguments, bool prettyPrint, bool dump) 296 { 297 GlobalObject* globalObject = new GlobalObject(arguments); 317 static bool runWithScripts(GlobalObject* globalObject, const Vector<UString>& fileNames, bool prettyPrint, bool dump) 318 { 298 319 Vector<char> script; 299 320 … … 302 323 303 324 bool success = true; 304 305 325 for (size_t i = 0; i < fileNames.size(); i++) { 306 326 UString fileName = fileNames[i]; … … 325 345 } 326 346 347 static 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 327 377 static void printUsageStatement() 328 378 { 329 379 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"); 334 386 exit(-1); 335 387 } 336 388 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 389 static void parseArguments(int argc, char** argv, Options& options) 390 { 342 391 int i = 1; 343 392 for (; i < argc; ++i) { … … 346 395 if (++i == argc) 347 396 printUsageStatement(); 348 fileNames.append(argv[i]);397 options.fileNames.append(argv[i]); 349 398 continue; 350 399 } 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 } 351 407 if (strcmp(arg, "-p") == 0) { 352 prettyPrint = true;408 options.prettyPrint = true; 353 409 continue; 354 410 } 355 411 if (strcmp(arg, "-d") == 0) { 356 dump = true;412 options.dump = true; 357 413 continue; 358 414 } … … 370 426 break; 371 427 } 372 fileNames.append(argv[i]);428 options.fileNames.append(argv[i]); 373 429 } 374 430 375 if ( fileNames.isEmpty())376 printUsageStatement();431 if (options.fileNames.isEmpty()) 432 options.interactive = true; 377 433 378 434 for (; i < argc; ++i) 379 arguments.append(argv[i]);435 options.arguments.append(argv[i]); 380 436 } 381 437 … … 386 442 JSLock lock; 387 443 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); 395 451 396 452 #ifndef NDEBUG
Note:
See TracChangeset
for help on using the changeset viewer.