blob: f895756e4abc3339f97c95237761ce69b62bc1ca [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/ast/modules.h"
6
7#include "src/ast/ast-value-factory.h"
8
9namespace v8 {
10namespace internal {
11
12
13void ModuleDescriptor::AddLocalExport(const AstRawString* export_name,
14 const AstRawString* local_name,
15 Zone* zone, bool* ok) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016 void* key = const_cast<AstRawString*>(export_name);
17
18 ZoneAllocationPolicy allocator(zone);
19
20 if (exports_ == nullptr) {
21 exports_ = new (zone->New(sizeof(ZoneHashMap)))
22 ZoneHashMap(ZoneHashMap::PointersMatch,
23 ZoneHashMap::kDefaultHashMapCapacity, allocator);
24 }
25
26 ZoneHashMap::Entry* p =
27 exports_->LookupOrInsert(key, export_name->hash(), allocator);
28 DCHECK_NOT_NULL(p);
29 if (p->value != nullptr) {
30 // Duplicate export.
31 *ok = false;
32 return;
33 }
34
35 p->value = const_cast<AstRawString*>(local_name);
36}
37
38
39void ModuleDescriptor::AddModuleRequest(const AstRawString* module_specifier,
40 Zone* zone) {
41 // TODO(adamk): Avoid this O(N) operation on each insert by storing
42 // a HashMap, or by de-duping after parsing.
43 if (requested_modules_.Contains(module_specifier)) return;
44 requested_modules_.Add(module_specifier, zone);
45}
46
47
48const AstRawString* ModuleDescriptor::LookupLocalExport(
49 const AstRawString* export_name, Zone* zone) {
50 if (exports_ == nullptr) return nullptr;
51 ZoneHashMap::Entry* entry = exports_->Lookup(
52 const_cast<AstRawString*>(export_name), export_name->hash());
53 if (entry == nullptr) return nullptr;
54 DCHECK_NOT_NULL(entry->value);
55 return static_cast<const AstRawString*>(entry->value);
56}
57} // namespace internal
58} // namespace v8