Changeset 27029 in webkit for trunk/JavaScriptCore/kjs/testkjs.cpp
- Timestamp:
- Oct 25, 2007, 1:37:23 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/testkjs.cpp
r27022 r27029 53 53 54 54 static void testIsInteger(); 55 static char* createStringWithContentsOfFile(const char* fileName);55 static bool fillBufferWithContentsOfFile(const char* fileName, Vector<char>& buffer); 56 56 57 57 class StopWatch … … 119 119 class TestFunctionImp : public JSObject { 120 120 public: 121 TestFunctionImp(int i, int length); 121 enum TestFunctionType { Print, Debug, Quit, GC, Version, Run, Load }; 122 123 TestFunctionImp(TestFunctionType i, int length); 122 124 virtual bool implementsCall() const { return true; } 123 125 virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const List &args); 124 126 125 enum { Print, Debug, Quit, GC, Version, Run, Load };126 127 127 private: 128 int id;128 TestFunctionType m_type; 129 129 }; 130 130 131 TestFunctionImp::TestFunctionImp(int i, int length) : JSObject(), id(i) 131 TestFunctionImp::TestFunctionImp(TestFunctionType i, int length) 132 : JSObject() 133 , m_type(i) 132 134 { 133 135 putDirect(Identifier("length"), length, DontDelete | ReadOnly | DontEnum); … … 136 138 JSValue* TestFunctionImp::callAsFunction(ExecState* exec, JSObject*, const List &args) 137 139 { 138 switch ( id) {140 switch (m_type) { 139 141 case Print: 140 142 printf("%s\n", args[0]->toString(exec).UTF8String().c_str()); … … 156 158 { 157 159 StopWatch stopWatch; 158 char* fileName = strdup(args[0]->toString(exec).UTF8String().c_str()); 159 char* script = createStringWithContentsOfFile(fileName); 160 if (!script) { 161 free(fileName); 160 UString fileName = args[0]->toString(exec); 161 Vector<char> script; 162 if (!fillBufferWithContentsOfFile(fileName.UTF8String().c_str(), script)) 162 163 return throwError(exec, GeneralError, "Could not open file."); 163 }164 164 165 165 stopWatch.start(); 166 exec->dynamicInterpreter()->evaluate(fileName, 0, script );166 exec->dynamicInterpreter()->evaluate(fileName, 0, script.data()); 167 167 stopWatch.stop(); 168 169 free(script);170 free(fileName);171 168 172 169 return jsNumber(stopWatch.getElapsedMS()); … … 174 171 case Load: 175 172 { 176 char* fileName = strdup(args[0]->toString(exec).UTF8String().c_str()); 177 char* script = createStringWithContentsOfFile(fileName); 178 if (!script) { 179 free(fileName); 173 UString fileName = args[0]->toString(exec); 174 Vector<char> script; 175 if (!fillBufferWithContentsOfFile(fileName.UTF8String().c_str(), script)) 180 176 return throwError(exec, GeneralError, "Could not open file."); 181 } 182 183 exec->dynamicInterpreter()->evaluate(fileName, 0, script); 184 185 free(script); 186 free(fileName); 187 177 178 exec->dynamicInterpreter()->evaluate(fileName, 0, script.data()); 179 188 180 return jsUndefined(); 189 181 } … … 238 230 } 239 231 240 241 bool doIt(int argc, char** argv) 242 { 243 bool success = true; 244 bool prettyPrint = false; 232 static PassRefPtr<Interpreter> setupInterpreter() 233 { 245 234 GlobalImp* global = new GlobalImp(); 246 247 // create interpreter248 235 RefPtr<Interpreter> interp = new Interpreter(global); 249 236 // add debug() function … … 259 246 global->put(interp->globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1)); 260 247 global->put(interp->globalExec(), "load", new TestFunctionImp(TestFunctionImp::Load, 1)); 261 248 262 249 Interpreter::setShouldPrintExceptions(true); 250 return interp.release(); 251 } 252 253 static bool prettyPrintScript(const char* fileName, const Vector<char>& script) 254 { 255 int errLine = 0; 256 UString errMsg; 257 UString s = Parser::prettyPrint(script.data(), &errLine, &errMsg); 258 if (s.isNull()) { 259 fprintf(stderr, "%s:%d: %s.\n", fileName, errLine, errMsg.UTF8String().c_str()); 260 return false; 261 } 262 263 printf("%s\n", s.UTF8String().c_str()); 264 return true; 265 } 266 267 static bool doIt(int argc, char** argv) 268 { 269 bool success = true; 270 bool prettyPrint = false; 271 272 RefPtr<Interpreter> interp = setupInterpreter(); 273 Vector<char> script; 263 274 264 275 for (int i = 1; i < argc; i++) { … … 271 282 } 272 283 273 char* script = createStringWithContentsOfFile(fileName);274 if (! script) {284 script.clear(); 285 if (!fillBufferWithContentsOfFile(fileName, script)) { 275 286 success = false; 276 287 break; // fail early so we can catch missing files 277 288 } 278 289 279 if (prettyPrint) { 280 int errLine = 0; 281 UString errMsg; 282 UString s = Parser::prettyPrint(script, &errLine, &errMsg); 283 if (s.isNull()) { 284 fprintf(stderr, "%s:%d: %s.\n", fileName, errLine, errMsg.UTF8String().c_str()); 285 success = false; 286 free(script); 287 break; 288 } 289 290 printf("%s\n", s.UTF8String().c_str()); 291 292 } else { 293 Completion completion = interp->evaluate(fileName, 0, script); 290 if (prettyPrint) 291 prettyPrintScript(fileName, script); 292 else { 293 Completion completion = interp->evaluate(fileName, 0, script.data()); 294 294 success = success && completion.complType() != Throw; 295 295 } 296 297 free(script);298 296 } 299 297 … … 351 349 } 352 350 353 static char* createStringWithContentsOfFile(const char* fileName) 354 { 355 char* buffer; 356 357 size_t buffer_size = 0; 358 size_t buffer_capacity = 1024; 359 buffer = (char*)malloc(buffer_capacity); 360 351 static bool fillBufferWithContentsOfFile(const char* fileName, Vector<char>& buffer) 352 { 361 353 FILE* f = fopen(fileName, "r"); 362 354 if (!f) { 363 355 fprintf(stderr, "Could not open file: %s\n", fileName); 364 free(buffer); 365 return 0; 366 } 356 return false; 357 } 358 359 size_t buffer_size = 0; 360 size_t buffer_capacity = 1024; 361 362 buffer.resize(buffer_capacity); 367 363 368 364 while (!feof(f) && !ferror(f)) { 369 buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);365 buffer_size += fread(buffer.data() + buffer_size, 1, buffer_capacity - buffer_size, f); 370 366 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0' 371 367 buffer_capacity *= 2; 372 buffer = (char*)realloc(buffer, buffer_capacity); 373 ASSERT(buffer); 374 } 375 376 ASSERT(buffer_size < buffer_capacity); 368 buffer.resize(buffer_capacity); 369 } 377 370 } 378 371 fclose(f); 379 372 buffer[buffer_size] = '\0'; 380 373 381 return buffer;382 } 374 return true; 375 }
Note:
See TracChangeset
for help on using the changeset viewer.