summaryrefslogtreecommitdiff
path: root/chromium/v8/src/modules.cc
blob: 2e6cfc0723714c6dd831718017f89f0072bccc4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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/v8.h"

#include "src/modules.h"

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

namespace v8 {
namespace internal {


void ModuleDescriptor::AddLocalExport(const AstRawString* export_name,
                                      const AstRawString* local_name,
                                      Zone* zone, bool* ok) {
  DCHECK(!IsFrozen());
  void* key = const_cast<AstRawString*>(export_name);

  ZoneAllocationPolicy allocator(zone);

  if (exports_ == nullptr) {
    exports_ = new (zone->New(sizeof(ZoneHashMap)))
        ZoneHashMap(ZoneHashMap::PointersMatch,
                    ZoneHashMap::kDefaultHashMapCapacity, allocator);
  }

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

  p->value = const_cast<AstRawString*>(local_name);
}


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);
}


const AstRawString* ModuleDescriptor::LookupLocalExport(
    const AstRawString* export_name, Zone* zone) {
  if (exports_ == nullptr) return nullptr;
  ZoneHashMap::Entry* entry = exports_->Lookup(
      const_cast<AstRawString*>(export_name), export_name->hash());
  if (entry == nullptr) return nullptr;
  DCHECK_NOT_NULL(entry->value);
  return static_cast<const AstRawString*>(entry->value);
}
}  // namespace internal
}  // namespace v8