Ignore:
Timestamp:
Oct 25, 2007, 1:37:23 AM (18 years ago)
Author:
eseidel
Message:

2007-10-25 Eric Seidel <[email protected]>

Reviewed by Maciej.


Bring testkjs code out of the dark ages in preparation for more
radical improvements (like long-running testing support!)

  • kjs/testkjs.cpp: (TestFunctionImp::callAsFunction): (setupInterpreter): (doIt): (fillBufferWithContentsOfFile):
File:
1 edited

Legend:

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

    r27022 r27029  
    5353
    5454static void testIsInteger();
    55 static char* createStringWithContentsOfFile(const char* fileName);
     55static bool fillBufferWithContentsOfFile(const char* fileName, Vector<char>& buffer);
    5656
    5757class StopWatch
     
    119119class TestFunctionImp : public JSObject {
    120120public:
    121   TestFunctionImp(int i, int length);
     121  enum TestFunctionType { Print, Debug, Quit, GC, Version, Run, Load };
     122
     123  TestFunctionImp(TestFunctionType i, int length);
    122124  virtual bool implementsCall() const { return true; }
    123125  virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const List &args);
    124126
    125   enum { Print, Debug, Quit, GC, Version, Run, Load };
    126 
    127127private:
    128   int id;
     128  TestFunctionType m_type;
    129129};
    130130
    131 TestFunctionImp::TestFunctionImp(int i, int length) : JSObject(), id(i)
     131TestFunctionImp::TestFunctionImp(TestFunctionType i, int length)
     132  : JSObject()
     133  , m_type(i)
    132134{
    133135  putDirect(Identifier("length"), length, DontDelete | ReadOnly | DontEnum);
     
    136138JSValue* TestFunctionImp::callAsFunction(ExecState* exec, JSObject*, const List &args)
    137139{
    138   switch (id) {
     140  switch (m_type) {
    139141    case Print:
    140142      printf("%s\n", args[0]->toString(exec).UTF8String().c_str());
     
    156158    {
    157159      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))
    162163        return throwError(exec, GeneralError, "Could not open file.");
    163       }
    164164
    165165      stopWatch.start();
    166       exec->dynamicInterpreter()->evaluate(fileName, 0, script);
     166      exec->dynamicInterpreter()->evaluate(fileName, 0, script.data());
    167167      stopWatch.stop();
    168 
    169       free(script);
    170       free(fileName);
    171168     
    172169      return jsNumber(stopWatch.getElapsedMS());
     
    174171    case Load:
    175172    {
    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))
    180176        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
    188180      return jsUndefined();
    189181    }
     
    238230}
    239231
    240 
    241 bool doIt(int argc, char** argv)
    242 {
    243   bool success = true;
    244   bool prettyPrint = false;
     232static PassRefPtr<Interpreter> setupInterpreter()
     233{
    245234  GlobalImp* global = new GlobalImp();
    246 
    247   // create interpreter
    248235  RefPtr<Interpreter> interp = new Interpreter(global);
    249236  // add debug() function
     
    259246  global->put(interp->globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1));
    260247  global->put(interp->globalExec(), "load", new TestFunctionImp(TestFunctionImp::Load, 1));
    261  
     248
    262249  Interpreter::setShouldPrintExceptions(true);
     250  return interp.release();
     251}
     252
     253static 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
     267static 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;
    263274 
    264275  for (int i = 1; i < argc; i++) {
     
    271282    }
    272283   
    273     char* script = createStringWithContentsOfFile(fileName);
    274     if (!script) {
     284    script.clear();
     285    if (!fillBufferWithContentsOfFile(fileName, script)) {
    275286      success = false;
    276287      break; // fail early so we can catch missing files
    277288    }
    278289   
    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());
    294294      success = success && completion.complType() != Throw;
    295295    }
    296    
    297     free(script);
    298296  }
    299297
     
    351349}
    352350
    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  
     351static bool fillBufferWithContentsOfFile(const char* fileName, Vector<char>& buffer)
     352{
    361353  FILE* f = fopen(fileName, "r");
    362354  if (!f) {
    363355    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);
    367363 
    368364  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);
    370366    if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
    371367      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    }
    377370  }
    378371  fclose(f);
    379372  buffer[buffer_size] = '\0';
    380373 
    381   return buffer;
    382 }
     374  return true;
     375}
Note: See TracChangeset for help on using the changeset viewer.