src/compiler/CompileContext.cpp
changeset 256 3c184218462d
parent 252 1dee2330aa78
child 258 241d082d6d89
     1.1 --- a/src/compiler/CompileContext.cpp	Tue Jan 01 20:08:34 2013 -0800
     1.2 +++ b/src/compiler/CompileContext.cpp	Sun Jan 06 13:52:21 2013 -0800
     1.3 @@ -21,6 +21,8 @@
     1.4  #include "../Interpreter.h"
     1.5  #include "../Packages.h"
     1.6  #include "../FileSystem.h"
     1.7 +#include "../FixedArray.h"
     1.8 +#include "../Vector.h"
     1.9  #include "Scanner.h"
    1.10  #include "CompileContext.h"
    1.11  #include "Parser.h"
    1.12 @@ -60,7 +62,7 @@
    1.13      if (!strname)
    1.14          return false;
    1.15  
    1.16 -    PathComponent p(new ScopedRoot<String>(strname), NULL);
    1.17 +    PathComponent p(new (zone_->pool()) ScopedRoot<String>(strname), NULL);
    1.18  
    1.19      Local<Importable> importable(zone_);
    1.20      if (!zone_->packages()->findOrAdd(package, &p, importable.address()))
    1.21 @@ -145,10 +147,17 @@
    1.22  {
    1.23      PoolList<ImportStatement *> *imports = unit->tree()->imports();
    1.24  
    1.25 +    if (!imports->length())
    1.26 +        return true;
    1.27 +
    1.28 +    // We're allowed to use Handle<> here instead of ScopedRoot<> because
    1.29 +    // we get the handle from ImportStatement which has a ScopedRoot<>.
    1.30 +    Vector<Handle<Importable> > vec;
    1.31 +
    1.32      for (size_t i = 0; i < imports->length(); i++) {
    1.33          ImportStatement *import = imports->at(i);
    1.34  
    1.35 -        // Step 1. Crawl the path until we find a valid file to load.
    1.36 +        // Crawl the path until we find a valid file to load.
    1.37          Local<Importable> obj(zone_);
    1.38          Local<Package> package(zone_, unit->module()->parent());
    1.39          if (!zone_->packages()->findOrAdd(package, import->path(), obj.address()))
    1.40 @@ -170,8 +179,52 @@
    1.41              if (!add(module))
    1.42                  return false;
    1.43          }
    1.44 +
    1.45 +        import->setSource(obj);
    1.46 +        
    1.47 +        // See if there are any duplicates. This is O(n^2), but we expect
    1.48 +        // |n| to be small.
    1.49 +        unsigned index = 0;
    1.50 +        for (; index < vec.length(); index++) {
    1.51 +            if (vec[index] == obj)
    1.52 +                break;
    1.53 +        }
    1.54 +        if (index == vec.length() && !vec.append(import->source()))
    1.55 +            return false;
    1.56 +
    1.57 +        import->setImportIndex(index);
    1.58      }
    1.59  
    1.60 +    if (!IsUint32MultiplySafe(vec.length(), 2)) {
    1.61 +        zone_->reportAllocationOverflow();
    1.62 +        return false;
    1.63 +    }
    1.64 +
    1.65 +    // Create an array of the import and paths.
    1.66 +    Local<FixedArray> importables(zone_, FixedArray::New(zone_, vec.length() * 2, Heap::Tenure_Default));
    1.67 +    if (!importables)
    1.68 +        return false;
    1.69 +
    1.70 +    for (unsigned i = 0; i < vec.length(); i++) {
    1.71 +        importables->set(zone_, i * 2, vec[i]);
    1.72 +
    1.73 +        unsigned count = 0;
    1.74 +        PathComponent *path = imports->at(i)->path();
    1.75 +        for (PathComponent *iter = path; iter; iter = iter->next)
    1.76 +            count++;
    1.77 +
    1.78 +        Local<FixedArray> list(zone_, FixedArray::New(zone_, count, Heap::Tenure_Default));
    1.79 +        if (!list)
    1.80 +            return false;
    1.81 +
    1.82 +        count = 0;
    1.83 +        for (PathComponent *iter = path; iter; iter = iter->next, count++)
    1.84 +            list->set(zone_, count, iter->name);
    1.85 +
    1.86 +        importables->set(zone_, i * 2 + 1, list);
    1.87 +    }
    1.88 +
    1.89 +    unit->module()->setImports(importables);
    1.90      return true;
    1.91  }
    1.92