modules.cc 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/modules.h"

#include "src/ast-value-factory.h"

namespace v8 {
namespace internal {


13 14 15
void ModuleDescriptor::AddLocalExport(const AstRawString* export_name,
                                      const AstRawString* local_name,
                                      Zone* zone, bool* ok) {
16
  DCHECK(!IsFrozen());
17
  void* key = const_cast<AstRawString*>(export_name);
18 19 20

  ZoneAllocationPolicy allocator(zone);

21
  if (exports_ == nullptr) {
22 23 24
    exports_ = new (zone->New(sizeof(ZoneHashMap)))
        ZoneHashMap(ZoneHashMap::PointersMatch,
                    ZoneHashMap::kDefaultHashMapCapacity, allocator);
25 26 27
  }

  ZoneHashMap::Entry* p =
28 29 30 31
      exports_->LookupOrInsert(key, export_name->hash(), allocator);
  DCHECK_NOT_NULL(p);
  if (p->value != nullptr) {
    // Duplicate export.
32
    *ok = false;
33
    return;
34 35
  }

36
  p->value = const_cast<AstRawString*>(local_name);
37
}
38 39


40 41 42 43 44 45 46 47 48
void ModuleDescriptor::AddModuleRequest(const AstRawString* module_specifier,
                                        Zone* zone) {
  // TODO(adamk): Avoid this O(N) operation on each insert by storing
  // a HashMap, or by de-duping after parsing.
  if (requested_modules_.Contains(module_specifier)) return;
  requested_modules_.Add(module_specifier, zone);
}


49 50 51
const AstRawString* ModuleDescriptor::LookupLocalExport(
    const AstRawString* export_name, Zone* zone) {
  if (exports_ == nullptr) return nullptr;
52 53
  ZoneHashMap::Entry* entry = exports_->Lookup(
      const_cast<AstRawString*>(export_name), export_name->hash());
54 55 56 57
  if (entry == nullptr) return nullptr;
  DCHECK_NOT_NULL(entry->value);
  return static_cast<const AstRawString*>(entry->value);
}
58 59
}  // namespace internal
}  // namespace v8