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-inl.h"
12 #include "src/objects/source-text-module.h"
13 #include "src/objects/string-inl.h"
14 #include "src/objects/synthetic-module.h"
15 
16 // Has to be the last include (doesn't have include guards):
17 #include "src/objects/object-macros.h"
18 
19 namespace v8 {
20 namespace internal {
21 
22 #include "torque-generated/src/objects/module-tq-inl.inc"
23 
24 OBJECT_CONSTRUCTORS_IMPL(Module, HeapObject)
25 TQ_OBJECT_CONSTRUCTORS_IMPL(JSModuleNamespace)
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 CAST_ACCESSOR(Module)
33 ACCESSORS(Module, exports, ObjectHashTable, kExportsOffset)
34 ACCESSORS(Module, module_namespace, HeapObject, kModuleNamespaceOffset)
35 ACCESSORS(Module, exception, Object, kExceptionOffset)
36 SMI_ACCESSORS(Module, status, kStatusOffset)
37 SMI_ACCESSORS(Module, hash, kHashOffset)
38 
39 BOOL_ACCESSORS(SourceTextModule, flags, async, AsyncBit::kShift)
40 BOOL_ACCESSORS(SourceTextModule, flags, async_evaluating,
41                AsyncEvaluatingBit::kShift)
42 ACCESSORS(SourceTextModule, async_parent_modules, ArrayList,
43           kAsyncParentModulesOffset)
44 ACCESSORS(SourceTextModule, top_level_capability, HeapObject,
45           kTopLevelCapabilityOffset)
46 
47 struct Module::Hash {
operatorHash48   V8_INLINE size_t operator()(Module const& module) const {
49     return module.hash();
50   }
51 };
52 
info()53 SourceTextModuleInfo SourceTextModule::info() const {
54   return status() == kErrored
55              ? SourceTextModuleInfo::cast(code())
56              : GetSharedFunctionInfo().scope_info().ModuleDescriptorInfo();
57 }
58 
OBJECT_CONSTRUCTORS_IMPL(SourceTextModuleInfo,FixedArray)59 OBJECT_CONSTRUCTORS_IMPL(SourceTextModuleInfo, FixedArray)
60 CAST_ACCESSOR(SourceTextModuleInfo)
61 
62 FixedArray SourceTextModuleInfo::module_requests() const {
63   return FixedArray::cast(get(kModuleRequestsIndex));
64 }
65 
special_exports()66 FixedArray SourceTextModuleInfo::special_exports() const {
67   return FixedArray::cast(get(kSpecialExportsIndex));
68 }
69 
regular_exports()70 FixedArray SourceTextModuleInfo::regular_exports() const {
71   return FixedArray::cast(get(kRegularExportsIndex));
72 }
73 
regular_imports()74 FixedArray SourceTextModuleInfo::regular_imports() const {
75   return FixedArray::cast(get(kRegularImportsIndex));
76 }
77 
namespace_imports()78 FixedArray SourceTextModuleInfo::namespace_imports() const {
79   return FixedArray::cast(get(kNamespaceImportsIndex));
80 }
81 
module_request_positions()82 FixedArray SourceTextModuleInfo::module_request_positions() const {
83   return FixedArray::cast(get(kModuleRequestPositionsIndex));
84 }
85 
86 #ifdef DEBUG
Equals(SourceTextModuleInfo other)87 bool SourceTextModuleInfo::Equals(SourceTextModuleInfo other) const {
88   return regular_exports() == other.regular_exports() &&
89          regular_imports() == other.regular_imports() &&
90          special_exports() == other.special_exports() &&
91          namespace_imports() == other.namespace_imports() &&
92          module_requests() == other.module_requests() &&
93          module_request_positions() == other.module_request_positions();
94 }
95 #endif
96 
97 struct ModuleHandleHash {
operatorModuleHandleHash98   V8_INLINE size_t operator()(Handle<Module> module) const {
99     return module->hash();
100   }
101 };
102 
103 struct ModuleHandleEqual {
operatorModuleHandleEqual104   V8_INLINE bool operator()(Handle<Module> lhs, Handle<Module> rhs) const {
105     return *lhs == *rhs;
106   }
107 };
108 
109 class UnorderedModuleSet
110     : public std::unordered_set<Handle<Module>, ModuleHandleHash,
111                                 ModuleHandleEqual,
112                                 ZoneAllocator<Handle<Module>>> {
113  public:
UnorderedModuleSet(Zone * zone)114   explicit UnorderedModuleSet(Zone* zone)
115       : std::unordered_set<Handle<Module>, ModuleHandleHash, ModuleHandleEqual,
116                            ZoneAllocator<Handle<Module>>>(
117             2 /* bucket count */, ModuleHandleHash(), ModuleHandleEqual(),
118             ZoneAllocator<Handle<Module>>(zone)) {}
119 };
120 
AddAsyncParentModule(Isolate * isolate,Handle<SourceTextModule> module,Handle<SourceTextModule> parent)121 void SourceTextModule::AddAsyncParentModule(Isolate* isolate,
122                                             Handle<SourceTextModule> module,
123                                             Handle<SourceTextModule> parent) {
124   Handle<ArrayList> async_parent_modules(module->async_parent_modules(),
125                                          isolate);
126   Handle<ArrayList> new_array_list =
127       ArrayList::Add(isolate, async_parent_modules, parent);
128   module->set_async_parent_modules(*new_array_list);
129 }
130 
GetAsyncParentModule(Isolate * isolate,int index)131 Handle<SourceTextModule> SourceTextModule::GetAsyncParentModule(
132     Isolate* isolate, int index) {
133   Handle<SourceTextModule> module(
134       SourceTextModule::cast(async_parent_modules().Get(index)), isolate);
135   return module;
136 }
137 
AsyncParentModuleCount()138 int SourceTextModule::AsyncParentModuleCount() {
139   return async_parent_modules().Length();
140 }
141 
HasPendingAsyncDependencies()142 bool SourceTextModule::HasPendingAsyncDependencies() {
143   DCHECK_GE(pending_async_dependencies(), 0);
144   return pending_async_dependencies() > 0;
145 }
146 
IncrementPendingAsyncDependencies()147 void SourceTextModule::IncrementPendingAsyncDependencies() {
148   set_pending_async_dependencies(pending_async_dependencies() + 1);
149 }
150 
DecrementPendingAsyncDependencies()151 void SourceTextModule::DecrementPendingAsyncDependencies() {
152   set_pending_async_dependencies(pending_async_dependencies() - 1);
153 }
154 
155 }  // namespace internal
156 }  // namespace v8
157 
158 #include "src/objects/object-macros-undef.h"
159 
160 #endif  // V8_OBJECTS_MODULE_INL_H_
161