1 // Copyright 2017 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 #ifndef V8_OBJECTS_MODULE_INL_H_
6 #define V8_OBJECTS_MODULE_INL_H_
7
8 #include "src/objects/module.h"
9 #include "src/objects/objects-inl.h" // Needed for write barriers
10 #include "src/objects/scope-info.h"
11 #include "src/objects/source-text-module.h"
12 #include "src/objects/string-inl.h"
13 #include "src/objects/synthetic-module.h"
14
15 // Has to be the last include (doesn't have include guards):
16 #include "src/objects/object-macros.h"
17
18 namespace v8 {
19 namespace internal {
20
21 #include "torque-generated/src/objects/module-tq-inl.inc"
22
23 TQ_OBJECT_CONSTRUCTORS_IMPL(Module)
24 TQ_OBJECT_CONSTRUCTORS_IMPL(JSModuleNamespace)
25 TQ_OBJECT_CONSTRUCTORS_IMPL(ScriptOrModule)
26
27 NEVER_READ_ONLY_SPACE_IMPL(Module)
28 NEVER_READ_ONLY_SPACE_IMPL(ModuleRequest)
29 NEVER_READ_ONLY_SPACE_IMPL(SourceTextModule)
30 NEVER_READ_ONLY_SPACE_IMPL(SyntheticModule)
31
32 BOOL_ACCESSORS(SourceTextModule, flags, async, AsyncBit::kShift)
33 BIT_FIELD_ACCESSORS(SourceTextModule, flags, async_evaluating_ordinal,
34 SourceTextModule::AsyncEvaluatingOrdinalBits)
35 ACCESSORS(SourceTextModule, async_parent_modules, ArrayList,
36 kAsyncParentModulesOffset)
37
38 struct Module::Hash {
operatorHash39 V8_INLINE size_t operator()(Module const& module) const {
40 return module.hash();
41 }
42 };
43
info()44 SourceTextModuleInfo SourceTextModule::info() const {
45 return GetSharedFunctionInfo().scope_info().ModuleDescriptorInfo();
46 }
47
OBJECT_CONSTRUCTORS_IMPL(SourceTextModuleInfo,FixedArray)48 OBJECT_CONSTRUCTORS_IMPL(SourceTextModuleInfo, FixedArray)
49 CAST_ACCESSOR(SourceTextModuleInfo)
50
51 FixedArray SourceTextModuleInfo::module_requests() const {
52 return FixedArray::cast(get(kModuleRequestsIndex));
53 }
54
special_exports()55 FixedArray SourceTextModuleInfo::special_exports() const {
56 return FixedArray::cast(get(kSpecialExportsIndex));
57 }
58
regular_exports()59 FixedArray SourceTextModuleInfo::regular_exports() const {
60 return FixedArray::cast(get(kRegularExportsIndex));
61 }
62
regular_imports()63 FixedArray SourceTextModuleInfo::regular_imports() const {
64 return FixedArray::cast(get(kRegularImportsIndex));
65 }
66
namespace_imports()67 FixedArray SourceTextModuleInfo::namespace_imports() const {
68 return FixedArray::cast(get(kNamespaceImportsIndex));
69 }
70
71 #ifdef DEBUG
Equals(SourceTextModuleInfo other)72 bool SourceTextModuleInfo::Equals(SourceTextModuleInfo other) const {
73 return regular_exports() == other.regular_exports() &&
74 regular_imports() == other.regular_imports() &&
75 special_exports() == other.special_exports() &&
76 namespace_imports() == other.namespace_imports() &&
77 module_requests() == other.module_requests();
78 }
79 #endif
80
81 struct ModuleHandleHash {
operatorModuleHandleHash82 V8_INLINE size_t operator()(Handle<Module> module) const {
83 return module->hash();
84 }
85 };
86
87 struct ModuleHandleEqual {
operatorModuleHandleEqual88 V8_INLINE bool operator()(Handle<Module> lhs, Handle<Module> rhs) const {
89 return *lhs == *rhs;
90 }
91 };
92
93 class UnorderedModuleSet
94 : public std::unordered_set<Handle<Module>, ModuleHandleHash,
95 ModuleHandleEqual,
96 ZoneAllocator<Handle<Module>>> {
97 public:
UnorderedModuleSet(Zone * zone)98 explicit UnorderedModuleSet(Zone* zone)
99 : std::unordered_set<Handle<Module>, ModuleHandleHash, ModuleHandleEqual,
100 ZoneAllocator<Handle<Module>>>(
101 2 /* bucket count */, ModuleHandleHash(), ModuleHandleEqual(),
102 ZoneAllocator<Handle<Module>>(zone)) {}
103 };
104
GetCycleRoot(Isolate * isolate)105 Handle<SourceTextModule> SourceTextModule::GetCycleRoot(
106 Isolate* isolate) const {
107 CHECK_GE(status(), kEvaluated);
108 DCHECK(!cycle_root().IsTheHole(isolate));
109 Handle<SourceTextModule> root(SourceTextModule::cast(cycle_root()), isolate);
110 return root;
111 }
112
AddAsyncParentModule(Isolate * isolate,Handle<SourceTextModule> module,Handle<SourceTextModule> parent)113 void SourceTextModule::AddAsyncParentModule(Isolate* isolate,
114 Handle<SourceTextModule> module,
115 Handle<SourceTextModule> parent) {
116 Handle<ArrayList> async_parent_modules(module->async_parent_modules(),
117 isolate);
118 Handle<ArrayList> new_array_list =
119 ArrayList::Add(isolate, async_parent_modules, parent);
120 module->set_async_parent_modules(*new_array_list);
121 }
122
GetAsyncParentModule(Isolate * isolate,int index)123 Handle<SourceTextModule> SourceTextModule::GetAsyncParentModule(
124 Isolate* isolate, int index) {
125 Handle<SourceTextModule> module(
126 SourceTextModule::cast(async_parent_modules().Get(index)), isolate);
127 return module;
128 }
129
AsyncParentModuleCount()130 int SourceTextModule::AsyncParentModuleCount() {
131 return async_parent_modules().Length();
132 }
133
IsAsyncEvaluating()134 bool SourceTextModule::IsAsyncEvaluating() const {
135 return async_evaluating_ordinal() >= kFirstAsyncEvaluatingOrdinal;
136 }
137
HasPendingAsyncDependencies()138 bool SourceTextModule::HasPendingAsyncDependencies() {
139 DCHECK_GE(pending_async_dependencies(), 0);
140 return pending_async_dependencies() > 0;
141 }
142
IncrementPendingAsyncDependencies()143 void SourceTextModule::IncrementPendingAsyncDependencies() {
144 set_pending_async_dependencies(pending_async_dependencies() + 1);
145 }
146
DecrementPendingAsyncDependencies()147 void SourceTextModule::DecrementPendingAsyncDependencies() {
148 set_pending_async_dependencies(pending_async_dependencies() - 1);
149 }
150
151 } // namespace internal
152 } // namespace v8
153
154 #include "src/objects/object-macros-undef.h"
155
156 #endif // V8_OBJECTS_MODULE_INL_H_
157