1 // Copyright 2019 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_SOURCE_TEXT_MODULE_H_ 6 #define V8_OBJECTS_SOURCE_TEXT_MODULE_H_ 7 8 #include "src/objects/module.h" 9 #include "src/objects/promise.h" 10 #include "torque-generated/bit-fields.h" 11 12 // Has to be the last include (doesn't have include guards): 13 #include "src/objects/object-macros.h" 14 15 namespace v8 { 16 namespace internal { 17 18 class UnorderedModuleSet; 19 20 #include "torque-generated/src/objects/source-text-module-tq.inc" 21 22 // The runtime representation of an ECMAScript Source Text Module Record. 23 // https://tc39.github.io/ecma262/#sec-source-text-module-records 24 class SourceTextModule 25 : public TorqueGeneratedSourceTextModule<SourceTextModule, Module> { 26 public: 27 NEVER_READ_ONLY_SPACE 28 DECL_VERIFIER(SourceTextModule) 29 DECL_PRINTER(SourceTextModule) 30 31 // The shared function info in case {status} is not kEvaluating, kEvaluated or 32 // kErrored. 33 SharedFunctionInfo GetSharedFunctionInfo() const; 34 35 // Whether or not this module is an async module. Set during module creation 36 // and does not change afterwards. 37 DECL_BOOLEAN_ACCESSORS(async) 38 39 // Get the SourceTextModuleInfo associated with the code. 40 inline SourceTextModuleInfo info() const; 41 42 Cell GetCell(int cell_index); 43 static Handle<Object> LoadVariable(Isolate* isolate, 44 Handle<SourceTextModule> module, 45 int cell_index); 46 static void StoreVariable(Handle<SourceTextModule> module, int cell_index, 47 Handle<Object> value); 48 49 static int ImportIndex(int cell_index); 50 static int ExportIndex(int cell_index); 51 52 // Used by builtins to fulfill or reject the promise associated 53 // with async SourceTextModules. 54 static void AsyncModuleExecutionFulfilled(Isolate* isolate, 55 Handle<SourceTextModule> module); 56 static void AsyncModuleExecutionRejected(Isolate* isolate, 57 Handle<SourceTextModule> module, 58 Handle<Object> exception); 59 60 // Get the namespace object for [module_request] of [module]. If it doesn't 61 // exist yet, it is created. 62 static Handle<JSModuleNamespace> GetModuleNamespace( 63 Isolate* isolate, Handle<SourceTextModule> module, int module_request); 64 65 // Get the import.meta object of [module]. If it doesn't exist yet, it is 66 // created and passed to the embedder callback for initialization. 67 V8_EXPORT_PRIVATE static MaybeHandle<JSObject> GetImportMeta( 68 Isolate* isolate, Handle<SourceTextModule> module); 69 70 using BodyDescriptor = 71 SubclassBodyDescriptor<Module::BodyDescriptor, 72 FixedBodyDescriptor<kCodeOffset, kSize, kSize>>; 73 74 private: 75 friend class Factory; 76 friend class Module; 77 78 // Appends a tuple of module and generator to the async parent modules 79 // ArrayList. 80 inline static void AddAsyncParentModule(Isolate* isolate, 81 Handle<SourceTextModule> module, 82 Handle<SourceTextModule> parent); 83 84 // Returns a SourceTextModule, the 85 // ith parent in depth first traversal order of a given async child. 86 inline Handle<SourceTextModule> GetAsyncParentModule(Isolate* isolate, 87 int index); 88 89 // Returns the number of async parent modules for a given async child. 90 inline int AsyncParentModuleCount(); 91 92 inline bool HasPendingAsyncDependencies(); 93 inline void IncrementPendingAsyncDependencies(); 94 inline void DecrementPendingAsyncDependencies(); 95 96 // Bits for flags. 97 DEFINE_TORQUE_GENERATED_SOURCE_TEXT_MODULE_FLAGS() 98 99 // async_evaluating, top_level_capability, pending_async_dependencies, and 100 // async_parent_modules are used exclusively during evaluation of async 101 // modules and the modules which depend on them. 102 // 103 // Whether or not this module is async and evaluating or currently evaluating 104 // an async child. 105 DECL_BOOLEAN_ACCESSORS(async_evaluating) 106 107 // The top level promise capability of this module. Will only be defined 108 // for cycle roots. 109 DECL_ACCESSORS(top_level_capability, HeapObject) 110 111 // The parent modules of a given async dependency, use async_parent_modules() 112 // to retrieve the ArrayList representation. 113 DECL_ACCESSORS(async_parent_modules, ArrayList) 114 115 // Helpers for Instantiate and Evaluate. 116 static void CreateExport(Isolate* isolate, Handle<SourceTextModule> module, 117 int cell_index, Handle<FixedArray> names); 118 static void CreateIndirectExport(Isolate* isolate, 119 Handle<SourceTextModule> module, 120 Handle<String> name, 121 Handle<SourceTextModuleInfoEntry> entry); 122 123 static V8_WARN_UNUSED_RESULT MaybeHandle<Cell> ResolveExport( 124 Isolate* isolate, Handle<SourceTextModule> module, 125 Handle<String> module_specifier, Handle<String> export_name, 126 MessageLocation loc, bool must_resolve, ResolveSet* resolve_set); 127 static V8_WARN_UNUSED_RESULT MaybeHandle<Cell> ResolveImport( 128 Isolate* isolate, Handle<SourceTextModule> module, Handle<String> name, 129 int module_request_index, MessageLocation loc, bool must_resolve, 130 ResolveSet* resolve_set); 131 132 static V8_WARN_UNUSED_RESULT MaybeHandle<Cell> ResolveExportUsingStarExports( 133 Isolate* isolate, Handle<SourceTextModule> module, 134 Handle<String> module_specifier, Handle<String> export_name, 135 MessageLocation loc, bool must_resolve, ResolveSet* resolve_set); 136 137 static V8_WARN_UNUSED_RESULT bool PrepareInstantiate( 138 Isolate* isolate, Handle<SourceTextModule> module, 139 v8::Local<v8::Context> context, v8::Module::ResolveCallback callback); 140 static V8_WARN_UNUSED_RESULT bool FinishInstantiate( 141 Isolate* isolate, Handle<SourceTextModule> module, 142 ZoneForwardList<Handle<SourceTextModule>>* stack, unsigned* dfs_index, 143 Zone* zone); 144 static V8_WARN_UNUSED_RESULT bool RunInitializationCode( 145 Isolate* isolate, Handle<SourceTextModule> module); 146 147 static void FetchStarExports(Isolate* isolate, 148 Handle<SourceTextModule> module, Zone* zone, 149 UnorderedModuleSet* visited); 150 151 // Implementation of spec concrete method Evaluate. 152 static V8_WARN_UNUSED_RESULT MaybeHandle<Object> EvaluateMaybeAsync( 153 Isolate* isolate, Handle<SourceTextModule> module); 154 155 // Continued implementation of spec concrete method Evaluate. 156 static V8_WARN_UNUSED_RESULT MaybeHandle<Object> Evaluate( 157 Isolate* isolate, Handle<SourceTextModule> module); 158 159 // Implementation of spec abstract operation InnerModuleEvaluation. 160 static V8_WARN_UNUSED_RESULT MaybeHandle<Object> InnerModuleEvaluation( 161 Isolate* isolate, Handle<SourceTextModule> module, 162 ZoneForwardList<Handle<SourceTextModule>>* stack, unsigned* dfs_index); 163 164 static V8_WARN_UNUSED_RESULT bool MaybeTransitionComponent( 165 Isolate* isolate, Handle<SourceTextModule> module, 166 ZoneForwardList<Handle<SourceTextModule>>* stack, Status new_status); 167 168 // Implementation of spec GetAsyncCycleRoot. 169 static V8_WARN_UNUSED_RESULT Handle<SourceTextModule> GetAsyncCycleRoot( 170 Isolate* isolate, Handle<SourceTextModule> module); 171 172 // Implementation of spec ExecuteModule is broken up into 173 // InnerExecuteAsyncModule for asynchronous modules and ExecuteModule 174 // for synchronous modules. 175 static V8_WARN_UNUSED_RESULT MaybeHandle<Object> InnerExecuteAsyncModule( 176 Isolate* isolate, Handle<SourceTextModule> module, 177 Handle<JSPromise> capability); 178 179 static V8_WARN_UNUSED_RESULT MaybeHandle<Object> ExecuteModule( 180 Isolate* isolate, Handle<SourceTextModule> module); 181 182 // Implementation of spec ExecuteAsyncModule. 183 static void ExecuteAsyncModule(Isolate* isolate, 184 Handle<SourceTextModule> module); 185 186 static void Reset(Isolate* isolate, Handle<SourceTextModule> module); 187 188 TQ_OBJECT_CONSTRUCTORS(SourceTextModule) 189 }; 190 191 // SourceTextModuleInfo is to SourceTextModuleDescriptor what ScopeInfo is to 192 // Scope. 193 class SourceTextModuleInfo : public FixedArray { 194 public: 195 DECL_CAST(SourceTextModuleInfo) 196 197 template <typename LocalIsolate> 198 static Handle<SourceTextModuleInfo> New(LocalIsolate* isolate, Zone* zone, 199 SourceTextModuleDescriptor* descr); 200 201 inline FixedArray module_requests() const; 202 inline FixedArray special_exports() const; 203 inline FixedArray regular_exports() const; 204 inline FixedArray regular_imports() const; 205 inline FixedArray namespace_imports() const; 206 inline FixedArray module_request_positions() const; 207 208 // Accessors for [regular_exports]. 209 int RegularExportCount() const; 210 String RegularExportLocalName(int i) const; 211 int RegularExportCellIndex(int i) const; 212 FixedArray RegularExportExportNames(int i) const; 213 214 #ifdef DEBUG 215 inline bool Equals(SourceTextModuleInfo other) const; 216 #endif 217 218 private: 219 template <typename Impl> 220 friend class FactoryBase; 221 friend class SourceTextModuleDescriptor; 222 enum { 223 kModuleRequestsIndex, 224 kSpecialExportsIndex, 225 kRegularExportsIndex, 226 kNamespaceImportsIndex, 227 kRegularImportsIndex, 228 kModuleRequestPositionsIndex, 229 kLength 230 }; 231 enum { 232 kRegularExportLocalNameOffset, 233 kRegularExportCellIndexOffset, 234 kRegularExportExportNamesOffset, 235 kRegularExportLength 236 }; 237 238 OBJECT_CONSTRUCTORS(SourceTextModuleInfo, FixedArray); 239 }; 240 241 class ModuleRequest 242 : public TorqueGeneratedModuleRequest<ModuleRequest, Struct> { 243 public: 244 NEVER_READ_ONLY_SPACE 245 DECL_VERIFIER(ModuleRequest) 246 247 template <typename LocalIsolate> 248 static Handle<ModuleRequest> New(LocalIsolate* isolate, 249 Handle<String> specifier, 250 Handle<FixedArray> import_assertions); 251 252 TQ_OBJECT_CONSTRUCTORS(ModuleRequest) 253 }; 254 255 class SourceTextModuleInfoEntry 256 : public TorqueGeneratedSourceTextModuleInfoEntry<SourceTextModuleInfoEntry, 257 Struct> { 258 public: 259 DECL_PRINTER(SourceTextModuleInfoEntry) 260 DECL_VERIFIER(SourceTextModuleInfoEntry) 261 262 template <typename LocalIsolate> 263 static Handle<SourceTextModuleInfoEntry> New( 264 LocalIsolate* isolate, Handle<PrimitiveHeapObject> export_name, 265 Handle<PrimitiveHeapObject> local_name, 266 Handle<PrimitiveHeapObject> import_name, int module_request, 267 int cell_index, int beg_pos, int end_pos); 268 269 TQ_OBJECT_CONSTRUCTORS(SourceTextModuleInfoEntry) 270 }; 271 272 } // namespace internal 273 } // namespace v8 274 275 #include "src/objects/object-macros-undef.h" 276 277 #endif // V8_OBJECTS_SOURCE_TEXT_MODULE_H_ 278