| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* JavaScript modules (as in, the syntactic construct) implementation. */ |
| 8 |
|
| 9 |
#include "vm/Modules.h" |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 12 |
#include "mozilla/ScopeExit.h" |
| 13 |
#include "mozilla/Utf8.h" // mozilla::Utf8Unit |
| 14 |
|
| 15 |
#include <stdint.h> // uint32_t |
| 16 |
|
| 17 |
#include "jstypes.h" // JS_PUBLIC_API |
| 18 |
|
| 19 |
#include "builtin/JSON.h" // js::ParseJSONWithReviver |
| 20 |
#include "builtin/ModuleObject.h" // js::FinishDynamicModuleImport, js::{,Requested}ModuleObject |
| 21 |
#include "builtin/Promise.h" // js::CreatePromiseObjectForAsync, js::AsyncFunctionReturned |
| 22 |
#include "ds/Sort.h" |
| 23 |
#include "frontend/BytecodeCompiler.h" // js::frontend::CompileModule |
| 24 |
#include "frontend/FrontendContext.h" // js::AutoReportFrontendContext |
| 25 |
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin |
| 26 |
#include "js/Context.h" // js::AssertHeapIsIdle |
| 27 |
#include "js/ErrorReport.h" // JSErrorBase |
| 28 |
#include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit |
| 29 |
#include "js/RootingAPI.h" // JS::MutableHandle |
| 30 |
#include "js/Value.h" // JS::Value |
| 31 |
#include "vm/EnvironmentObject.h" // js::ModuleEnvironmentObject |
| 32 |
#include "vm/JSAtomUtils.h" // AtomizeString |
| 33 |
#include "vm/JSContext.h" // CHECK_THREAD, JSContext |
| 34 |
#include "vm/JSObject.h" // JSObject |
| 35 |
#include "vm/JSONParser.h" // JSONParser |
| 36 |
#include "vm/List.h" // ListObject |
| 37 |
#include "vm/Runtime.h" // JSRuntime |
| 38 |
|
| 39 |
#include "vm/JSAtomUtils-inl.h" // AtomToId |
| 40 |
#include "vm/JSContext-inl.h" // JSContext::{c,releaseC}heck |
| 41 |
#include "vm/JSObject-inl.h" |
| 42 |
#include "vm/NativeObject-inl.h" |
| 43 |
|
| 44 |
using namespace js; |
| 45 |
|
| 46 |
using mozilla::Utf8Unit; |
| 47 |
|
| 48 |
static bool ModuleLink(JSContext* cx, Handle<ModuleObject*> module); |
| 49 |
static bool ModuleEvaluate(JSContext* cx, Handle<ModuleObject*> module, |
| 50 |
MutableHandle<Value> rval); |
| 51 |
static bool SyntheticModuleEvaluate(JSContext* cx, Handle<ModuleObject*> module, |
| 52 |
MutableHandle<Value> rval); |
| 53 |
|
| 54 |
//////////////////////////////////////////////////////////////////////////////// |
| 55 |
// Public API |
| 56 |
JS_PUBLIC_API JS::ModuleResolveHook JS::GetModuleResolveHook(JSRuntime* rt) { |
| 57 |
AssertHeapIsIdle(); |
| 58 |
|
| 59 |
return rt->moduleResolveHook; |
| 60 |
} |
| 61 |
|
| 62 |
JS_PUBLIC_API void JS::SetModuleResolveHook(JSRuntime* rt, |
| 63 |
ModuleResolveHook func) { |
| 64 |
AssertHeapIsIdle(); |
| 65 |
|
| 66 |
rt->moduleResolveHook = func; |
| 67 |
} |
| 68 |
|
| 69 |
JS_PUBLIC_API JS::ModuleMetadataHook JS::GetModuleMetadataHook(JSRuntime* rt) { |
| 70 |
AssertHeapIsIdle(); |
| 71 |
|
| 72 |
return rt->moduleMetadataHook; |
| 73 |
} |
| 74 |
|
| 75 |
JS_PUBLIC_API void JS::SetModuleMetadataHook(JSRuntime* rt, |
| 76 |
ModuleMetadataHook func) { |
| 77 |
AssertHeapIsIdle(); |
| 78 |
|
| 79 |
rt->moduleMetadataHook = func; |
| 80 |
} |
| 81 |
|
| 82 |
JS_PUBLIC_API JS::ModuleDynamicImportHook JS::GetModuleDynamicImportHook( |
| 83 |
JSRuntime* rt) { |
| 84 |
AssertHeapIsIdle(); |
| 85 |
|
| 86 |
return rt->moduleDynamicImportHook; |
| 87 |
} |
| 88 |
|
| 89 |
JS_PUBLIC_API void JS::SetModuleDynamicImportHook( |
| 90 |
JSRuntime* rt, ModuleDynamicImportHook func) { |
| 91 |
AssertHeapIsIdle(); |
| 92 |
|
| 93 |
rt->moduleDynamicImportHook = func; |
| 94 |
} |
| 95 |
|
| 96 |
JS_PUBLIC_API bool JS::FinishDynamicModuleImport( |
| 97 |
JSContext* cx, Handle<JSObject*> evaluationPromise, |
| 98 |
Handle<Value> referencingPrivate, Handle<JSObject*> moduleRequest, |
| 99 |
Handle<JSObject*> promise) { |
| 100 |
AssertHeapIsIdle(); |
| 101 |
CHECK_THREAD(cx); |
| 102 |
cx->check(referencingPrivate, promise); |
| 103 |
|
| 104 |
return js::FinishDynamicModuleImport( |
| 105 |
cx, evaluationPromise, referencingPrivate, moduleRequest, promise); |
| 106 |
} |
| 107 |
|
| 108 |
template <typename Unit> |
| 109 |
static JSObject* CompileModuleHelper(JSContext* cx, |
| 110 |
const JS::ReadOnlyCompileOptions& options, |
| 111 |
JS::SourceText<Unit>& srcBuf) { |
| 112 |
MOZ_ASSERT(!cx->zone()->isAtomsZone()); |
| 113 |
AssertHeapIsIdle(); |
| 114 |
CHECK_THREAD(cx); |
| 115 |
|
| 116 |
JS::Rooted<JSObject*> mod(cx); |
| 117 |
{ |
| 118 |
AutoReportFrontendContext fc(cx); |
| 119 |
mod = frontend::CompileModule(cx, &fc, options, srcBuf); |
| 120 |
} |
| 121 |
return mod; |
| 122 |
} |
| 123 |
|
| 124 |
JS_PUBLIC_API JSObject* JS::CompileModule(JSContext* cx, |
| 125 |
const ReadOnlyCompileOptions& options, |
| 126 |
SourceText<char16_t>& srcBuf) { |
| 127 |
return CompileModuleHelper(cx, options, srcBuf); |
| 128 |
} |
| 129 |
|
| 130 |
JS_PUBLIC_API JSObject* JS::CompileModule(JSContext* cx, |
| 131 |
const ReadOnlyCompileOptions& options, |
| 132 |
SourceText<Utf8Unit>& srcBuf) { |
| 133 |
return CompileModuleHelper(cx, options, srcBuf); |
| 134 |
} |
| 135 |
|
| 136 |
JS_PUBLIC_API JSObject* JS::CompileJsonModule( |
| 137 |
JSContext* cx, const ReadOnlyCompileOptions& options, |
| 138 |
SourceText<mozilla::Utf8Unit>& srcBuf) { |
| 139 |
size_t length = srcBuf.length(); |
| 140 |
auto chars = |
| 141 |
UniqueTwoByteChars(UTF8CharsToNewTwoByteCharsZ( |
| 142 |
cx, JS::UTF8Chars(srcBuf.get(), srcBuf.length()), |
| 143 |
&length, js::MallocArena) |
| 144 |
.get()); |
| 145 |
if (!chars) { |
| 146 |
return nullptr; |
| 147 |
} |
| 148 |
|
| 149 |
JS::SourceText<char16_t> source; |
| 150 |
if (!source.init(cx, std::move(chars), length)) { |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
|
| 154 |
return CompileJsonModule(cx, options, source); |
| 155 |
} |
| 156 |
|
| 157 |
JS_PUBLIC_API JSObject* JS::CompileJsonModule( |
| 158 |
JSContext* cx, const ReadOnlyCompileOptions& options, |
| 159 |
SourceText<char16_t>& srcBuf) { |
| 160 |
MOZ_ASSERT(!cx->zone()->isAtomsZone()); |
| 161 |
AssertHeapIsIdle(); |
| 162 |
CHECK_THREAD(cx); |
| 163 |
|
| 164 |
auto charRange = |
| 165 |
mozilla::Range<const char16_t>(srcBuf.get(), srcBuf.length()); |
| 166 |
Rooted<JSONParser<char16_t>> parser( |
| 167 |
cx, cx, charRange, JSONParser<char16_t>::ParseType::JSONParse); |
| 168 |
|
| 169 |
parser.reportLineNumbersFromParsedData(true); |
| 170 |
parser.setFilename(options.filename()); |
| 171 |
|
| 172 |
JS::RootedValue jsonValue(cx); |
| 173 |
if (!parser.parse(&jsonValue)) { |
| 174 |
return nullptr; |
| 175 |
} |
| 176 |
|
| 177 |
Rooted<ExportNameVector> exportNames(cx); |
| 178 |
if (!exportNames.append(cx->names().default_)) { |
| 179 |
ReportOutOfMemory(cx); |
| 180 |
return nullptr; |
| 181 |
} |
| 182 |
|
| 183 |
Rooted<ModuleObject*> moduleObject( |
| 184 |
cx, ModuleObject::createSynthetic(cx, &exportNames)); |
| 185 |
if (!moduleObject) { |
| 186 |
return nullptr; |
| 187 |
} |
| 188 |
|
| 189 |
RootedVector<Value> exportValues(cx); |
| 190 |
if (!exportValues.append(jsonValue)) { |
| 191 |
ReportOutOfMemory(cx); |
| 192 |
return nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
if (!ModuleObject::createSyntheticEnvironment(cx, moduleObject, |
| 196 |
exportValues)) { |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
return moduleObject; |
| 201 |
} |
| 202 |
|
| 203 |
JS_PUBLIC_API void JS::SetModulePrivate(JSObject* module, const Value& value) { |
| 204 |
JSRuntime* rt = module->zone()->runtimeFromMainThread(); |
| 205 |
module->as<ModuleObject>().scriptSourceObject()->setPrivate(rt, value); |
| 206 |
} |
| 207 |
|
| 208 |
JS_PUBLIC_API void JS::ClearModulePrivate(JSObject* module) { |
| 209 |
// |module| may be gray, be careful not to create edges to it. |
| 210 |
JSRuntime* rt = module->zone()->runtimeFromMainThread(); |
| 211 |
module->as<ModuleObject>().scriptSourceObject()->clearPrivate(rt); |
| 212 |
} |
| 213 |
|
| 214 |
JS_PUBLIC_API JS::Value JS::GetModulePrivate(JSObject* module) { |
| 215 |
return module->as<ModuleObject>().scriptSourceObject()->getPrivate(); |
| 216 |
} |
| 217 |
|
| 218 |
JS_PUBLIC_API bool JS::IsCyclicModule(JSObject* module) { |
| 219 |
return module->as<ModuleObject>().hasCyclicModuleFields(); |
| 220 |
} |
| 221 |
|
| 222 |
JS_PUBLIC_API bool JS::ModuleLink(JSContext* cx, Handle<JSObject*> moduleArg) { |
| 223 |
AssertHeapIsIdle(); |
| 224 |
CHECK_THREAD(cx); |
| 225 |
cx->releaseCheck(moduleArg); |
| 226 |
|
| 227 |
return ::ModuleLink(cx, moduleArg.as<ModuleObject>()); |
| 228 |
} |
| 229 |
|
| 230 |
JS_PUBLIC_API bool JS::ModuleEvaluate(JSContext* cx, |
| 231 |
Handle<JSObject*> moduleRecord, |
| 232 |
MutableHandle<JS::Value> rval) { |
| 233 |
AssertHeapIsIdle(); |
| 234 |
CHECK_THREAD(cx); |
| 235 |
cx->releaseCheck(moduleRecord); |
| 236 |
|
| 237 |
cx->isEvaluatingModule++; |
| 238 |
auto guard = mozilla::MakeScopeExit([cx] { |
| 239 |
MOZ_ASSERT(cx->isEvaluatingModule != 0); |
| 240 |
cx->isEvaluatingModule--; |
| 241 |
}); |
| 242 |
|
| 243 |
if (moduleRecord.as<ModuleObject>()->hasSyntheticModuleFields()) { |
| 244 |
return SyntheticModuleEvaluate(cx, moduleRecord.as<ModuleObject>(), rval); |
| 245 |
} |
| 246 |
|
| 247 |
return ::ModuleEvaluate(cx, moduleRecord.as<ModuleObject>(), rval); |
| 248 |
} |
| 249 |
|
| 250 |
JS_PUBLIC_API bool JS::ThrowOnModuleEvaluationFailure( |
| 251 |
JSContext* cx, Handle<JSObject*> evaluationPromise, |
| 252 |
ModuleErrorBehaviour errorBehaviour) { |
| 253 |
AssertHeapIsIdle(); |
| 254 |
CHECK_THREAD(cx); |
| 255 |
cx->releaseCheck(evaluationPromise); |
| 256 |
|
| 257 |
return OnModuleEvaluationFailure(cx, evaluationPromise, errorBehaviour); |
| 258 |
} |
| 259 |
|
| 260 |
JS_PUBLIC_API uint32_t |
| 261 |
JS::GetRequestedModulesCount(JSContext* cx, Handle<JSObject*> moduleRecord) { |
| 262 |
AssertHeapIsIdle(); |
| 263 |
CHECK_THREAD(cx); |
| 264 |
cx->check(moduleRecord); |
| 265 |
|
| 266 |
return moduleRecord->as<ModuleObject>().requestedModules().Length(); |
| 267 |
} |
| 268 |
|
| 269 |
JS_PUBLIC_API JSString* JS::GetRequestedModuleSpecifier( |
| 270 |
JSContext* cx, Handle<JSObject*> moduleRecord, uint32_t index) { |
| 271 |
AssertHeapIsIdle(); |
| 272 |
CHECK_THREAD(cx); |
| 273 |
cx->check(moduleRecord); |
| 274 |
|
| 275 |
auto* moduleRequest = moduleRecord->as<ModuleObject>() |
| 276 |
.requestedModules()[index] |
| 277 |
.moduleRequest(); |
| 278 |
|
| 279 |
// This implements step 7.1.1 in HostLoadImportedModule. |
| 280 |
// https://html.spec.whatwg.org/multipage/webappapis.html#hostloadimportedmodule |
| 281 |
// |
| 282 |
// If moduleRequest.[[Attributes]] contains a Record entry such that |
| 283 |
// entry.[[Key]] is not "type", |
| 284 |
if (moduleRequest->hasFirstUnsupportedAttributeKey()) { |
| 285 |
UniqueChars printableKey = AtomToPrintableString( |
| 286 |
cx, moduleRequest->getFirstUnsupportedAttributeKey()); |
| 287 |
JS_ReportErrorNumberASCII( |
| 288 |
cx, GetErrorMessage, nullptr, |
| 289 |
JSMSG_IMPORT_ATTRIBUTES_STATIC_IMPORT_UNSUPPORTED_ATTRIBUTE, |
| 290 |
printableKey ? printableKey.get() : ""); |
| 291 |
return nullptr; |
| 292 |
} |
| 293 |
|
| 294 |
// This implements step 7.1.5 in HostLoadImportedModule. |
| 295 |
// https://html.spec.whatwg.org/multipage/webappapis.html#validate-requested-module-specifiers |
| 296 |
// |
| 297 |
// If the result of running the module type allowed steps given moduleType and |
| 298 |
// settings is false. |
| 299 |
if (moduleRequest->moduleType() == JS::ModuleType::Unknown) { |
| 300 |
JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, |
| 301 |
JSMSG_BAD_MODULE_TYPE); |
| 302 |
return nullptr; |
| 303 |
} |
| 304 |
|
| 305 |
return moduleRequest->specifier(); |
| 306 |
} |
| 307 |
|
| 308 |
JS_PUBLIC_API void JS::GetRequestedModuleSourcePos( |
| 309 |
JSContext* cx, Handle<JSObject*> moduleRecord, uint32_t index, |
| 310 |
uint32_t* lineNumber, JS::ColumnNumberOneOrigin* columnNumber) { |
| 311 |
AssertHeapIsIdle(); |
| 312 |
CHECK_THREAD(cx); |
| 313 |
cx->check(moduleRecord); |
| 314 |
MOZ_ASSERT(lineNumber); |
| 315 |
MOZ_ASSERT(columnNumber); |
| 316 |
|
| 317 |
auto& module = moduleRecord->as<ModuleObject>(); |
| 318 |
*lineNumber = module.requestedModules()[index].lineNumber(); |
| 319 |
*columnNumber = module.requestedModules()[index].columnNumber(); |
| 320 |
} |
| 321 |
|
| 322 |
JS_PUBLIC_API JS::ModuleType JS::GetRequestedModuleType( |
| 323 |
JSContext* cx, Handle<JSObject*> moduleRecord, uint32_t index) { |
| 324 |
AssertHeapIsIdle(); |
| 325 |
CHECK_THREAD(cx); |
| 326 |
cx->check(moduleRecord); |
| 327 |
|
| 328 |
auto& module = moduleRecord->as<ModuleObject>(); |
| 329 |
return module.requestedModules()[index].moduleRequest()->moduleType(); |
| 330 |
} |
| 331 |
|
| 332 |
JS_PUBLIC_API JSScript* JS::GetModuleScript(JS::HandleObject moduleRecord) { |
| 333 |
AssertHeapIsIdle(); |
| 334 |
|
| 335 |
auto& module = moduleRecord->as<ModuleObject>(); |
| 336 |
|
| 337 |
// A synthetic module does not have a script associated with it. |
| 338 |
if (module.hasSyntheticModuleFields()) { |
| 339 |
return nullptr; |
| 340 |
} |
| 341 |
|
| 342 |
return module.script(); |
| 343 |
} |
| 344 |
|
| 345 |
JS_PUBLIC_API JSObject* JS::GetModuleObject(HandleScript moduleScript) { |
| 346 |
AssertHeapIsIdle(); |
| 347 |
MOZ_ASSERT(moduleScript->isModule()); |
| 348 |
|
| 349 |
return moduleScript->module(); |
| 350 |
} |
| 351 |
|
| 352 |
JS_PUBLIC_API JSObject* JS::GetModuleNamespace(JSContext* cx, |
| 353 |
HandleObject moduleRecord) { |
| 354 |
AssertHeapIsIdle(); |
| 355 |
CHECK_THREAD(cx); |
| 356 |
cx->check(moduleRecord); |
| 357 |
MOZ_ASSERT(moduleRecord->is<ModuleObject>()); |
| 358 |
|
| 359 |
return GetOrCreateModuleNamespace(cx, moduleRecord.as<ModuleObject>()); |
| 360 |
} |
| 361 |
|
| 362 |
JS_PUBLIC_API JSObject* JS::GetModuleForNamespace( |
| 363 |
JSContext* cx, HandleObject moduleNamespace) { |
| 364 |
AssertHeapIsIdle(); |
| 365 |
CHECK_THREAD(cx); |
| 366 |
cx->check(moduleNamespace); |
| 367 |
MOZ_ASSERT(moduleNamespace->is<ModuleNamespaceObject>()); |
| 368 |
|
| 369 |
return &moduleNamespace->as<ModuleNamespaceObject>().module(); |
| 370 |
} |
| 371 |
|
| 372 |
JS_PUBLIC_API JSObject* JS::GetModuleEnvironment(JSContext* cx, |
| 373 |
Handle<JSObject*> moduleObj) { |
| 374 |
AssertHeapIsIdle(); |
| 375 |
CHECK_THREAD(cx); |
| 376 |
cx->check(moduleObj); |
| 377 |
MOZ_ASSERT(moduleObj->is<ModuleObject>()); |
| 378 |
|
| 379 |
return moduleObj->as<ModuleObject>().environment(); |
| 380 |
} |
| 381 |
|
| 382 |
JS_PUBLIC_API JSObject* JS::CreateModuleRequest(JSContext* cx, |
| 383 |
Handle<JSString*> specifierArg, |
| 384 |
JS::ModuleType moduleType) { |
| 385 |
AssertHeapIsIdle(); |
| 386 |
CHECK_THREAD(cx); |
| 387 |
|
| 388 |
Rooted<JSAtom*> specifierAtom(cx, AtomizeString(cx, specifierArg)); |
| 389 |
if (!specifierAtom) { |
| 390 |
return nullptr; |
| 391 |
} |
| 392 |
|
| 393 |
return ModuleRequestObject::create(cx, specifierAtom, moduleType); |
| 394 |
} |
| 395 |
|
| 396 |
JS_PUBLIC_API JSString* JS::GetModuleRequestSpecifier( |
| 397 |
JSContext* cx, Handle<JSObject*> moduleRequestArg) { |
| 398 |
AssertHeapIsIdle(); |
| 399 |
CHECK_THREAD(cx); |
| 400 |
cx->check(moduleRequestArg); |
| 401 |
|
| 402 |
return moduleRequestArg->as<ModuleRequestObject>().specifier(); |
| 403 |
} |
| 404 |
|
| 405 |
JS_PUBLIC_API JS::ModuleType JS::GetModuleRequestType( |
| 406 |
JSContext* cx, Handle<JSObject*> moduleRequestArg) { |
| 407 |
AssertHeapIsIdle(); |
| 408 |
CHECK_THREAD(cx); |
| 409 |
cx->check(moduleRequestArg); |
| 410 |
|
| 411 |
return moduleRequestArg->as<ModuleRequestObject>().moduleType(); |
| 412 |
} |
| 413 |
|
| 414 |
JS_PUBLIC_API void JS::ClearModuleEnvironment(JSObject* moduleObj) { |
| 415 |
MOZ_ASSERT(moduleObj); |
| 416 |
AssertHeapIsIdle(); |
| 417 |
|
| 418 |
js::ModuleEnvironmentObject* env = |
| 419 |
moduleObj->as<js::ModuleObject>().environment(); |
| 420 |
if (!env) { |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
const JSClass* clasp = env->getClass(); |
| 425 |
uint32_t numReserved = JSCLASS_RESERVED_SLOTS(clasp); |
| 426 |
uint32_t numSlots = env->slotSpan(); |
| 427 |
for (uint32_t i = numReserved; i < numSlots; i++) { |
| 428 |
env->setSlot(i, UndefinedValue()); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
JS_PUBLIC_API bool JS::ModuleIsLinked(JSObject* moduleObj) { |
| 433 |
AssertHeapIsIdle(); |
| 434 |
return moduleObj->as<ModuleObject>().status() != ModuleStatus::Unlinked; |
| 435 |
} |
| 436 |
|
| 437 |
//////////////////////////////////////////////////////////////////////////////// |
| 438 |
// Internal implementation |
| 439 |
|
| 440 |
class ResolveSetEntry { |
| 441 |
ModuleObject* module_; |
| 442 |
JSAtom* exportName_; |
| 443 |
|
| 444 |
public: |
| 445 |
ResolveSetEntry(ModuleObject* module, JSAtom* exportName) |
| 446 |
: module_(module), exportName_(exportName) {} |
| 447 |
|
| 448 |
ModuleObject* module() const { return module_; } |
| 449 |
JSAtom* exportName() const { return exportName_; } |
| 450 |
|
| 451 |
void trace(JSTracer* trc) { |
| 452 |
TraceRoot(trc, &module_, "ResolveSetEntry::module_"); |
| 453 |
TraceRoot(trc, &exportName_, "ResolveSetEntry::exportName_"); |
| 454 |
} |
| 455 |
}; |
| 456 |
|
| 457 |
using ResolveSet = GCVector<ResolveSetEntry, 0, SystemAllocPolicy>; |
| 458 |
|
| 459 |
using ModuleSet = |
| 460 |
GCHashSet<ModuleObject*, DefaultHasher<ModuleObject*>, SystemAllocPolicy>; |
| 461 |
|
| 462 |
static ModuleObject* HostResolveImportedModule( |
| 463 |
JSContext* cx, Handle<ModuleObject*> module, |
| 464 |
Handle<ModuleRequestObject*> moduleRequest, |
| 465 |
ModuleStatus expectedMinimumStatus); |
| 466 |
static bool CyclicModuleResolveExport(JSContext* cx, |
| 467 |
Handle<ModuleObject*> module, |
| 468 |
Handle<JSAtom*> exportName, |
| 469 |
MutableHandle<ResolveSet> resolveSet, |
| 470 |
MutableHandle<Value> result, |
| 471 |
ModuleErrorInfo* errorInfoOut = nullptr); |
| 472 |
static bool SyntheticModuleResolveExport(JSContext* cx, |
| 473 |
Handle<ModuleObject*> module, |
| 474 |
Handle<JSAtom*> exportName, |
| 475 |
MutableHandle<Value> result, |
| 476 |
ModuleErrorInfo* errorInfoOut); |
| 477 |
static ModuleNamespaceObject* ModuleNamespaceCreate( |
| 478 |
JSContext* cx, Handle<ModuleObject*> module, |
| 479 |
MutableHandle<UniquePtr<ExportNameVector>> exports); |
| 480 |
static bool InnerModuleLinking(JSContext* cx, Handle<ModuleObject*> module, |
| 481 |
MutableHandle<ModuleVector> stack, size_t index, |
| 482 |
size_t* indexOut); |
| 483 |
static bool InnerModuleEvaluation(JSContext* cx, Handle<ModuleObject*> module, |
| 484 |
MutableHandle<ModuleVector> stack, |
| 485 |
size_t index, size_t* indexOut); |
| 486 |
static bool ExecuteAsyncModule(JSContext* cx, Handle<ModuleObject*> module); |
| 487 |
static bool GatherAvailableModuleAncestors( |
| 488 |
JSContext* cx, Handle<ModuleObject*> module, |
| 489 |
MutableHandle<ModuleVector> execList); |
| 490 |
|
| 491 |
static const char* ModuleStatusName(ModuleStatus status) { |
| 492 |
switch (status) { |
| 493 |
case ModuleStatus::Unlinked: |
| 494 |
return "Unlinked"; |
| 495 |
case ModuleStatus::Linking: |
| 496 |
return "Linking"; |
| 497 |
case ModuleStatus::Linked: |
| 498 |
return "Linked"; |
| 499 |
case ModuleStatus::Evaluating: |
| 500 |
return "Evaluating"; |
| 501 |
case ModuleStatus::EvaluatingAsync: |
| 502 |
return "EvaluatingAsync"; |
| 503 |
case ModuleStatus::Evaluated: |
| 504 |
return "Evaluated"; |
| 505 |
default: |
| 506 |
MOZ_CRASH("Unexpected ModuleStatus"); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
static bool ContainsElement(const ExportNameVector& list, JSAtom* atom) { |
| 511 |
for (JSAtom* a : list) { |
| 512 |
if (a == atom) { |
| 513 |
return true; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
static bool ContainsElement(Handle<ModuleVector> stack, ModuleObject* module) { |
| 521 |
for (ModuleObject* m : stack) { |
| 522 |
if (m == module) { |
| 523 |
return true; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
return false; |
| 528 |
} |
| 529 |
|
| 530 |
#ifdef DEBUG |
| 531 |
static size_t CountElements(Handle<ModuleVector> stack, ModuleObject* module) { |
| 532 |
size_t count = 0; |
| 533 |
for (ModuleObject* m : stack) { |
| 534 |
if (m == module) { |
| 535 |
count++; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
return count; |
| 540 |
} |
| 541 |
#endif |
| 542 |
|
| 543 |
// https://tc39.es/proposal-json-modules/#sec-smr-getexportednames |
| 544 |
static bool SyntheticModuleGetExportedNames( |
| 545 |
JSContext* cx, Handle<ModuleObject*> module, |
| 546 |
MutableHandle<ExportNameVector> exportedNames) { |
| 547 |
MOZ_ASSERT(exportedNames.empty()); |
| 548 |
|
| 549 |
if (!exportedNames.appendAll(module->syntheticExportNames())) { |
| 550 |
ReportOutOfMemory(cx); |
| 551 |
return false; |
| 552 |
} |
| 553 |
|
| 554 |
return true; |
| 555 |
} |
| 556 |
|
| 557 |
// https://tc39.es/ecma262/#sec-getexportednames |
| 558 |
// ES2023 16.2.1.6.2 GetExportedNames |
| 559 |
static bool ModuleGetExportedNames( |
| 560 |
JSContext* cx, Handle<ModuleObject*> module, |
| 561 |
MutableHandle<ModuleSet> exportStarSet, |
| 562 |
MutableHandle<ExportNameVector> exportedNames) { |
| 563 |
// Step 4. Let exportedNames be a new empty List. |
| 564 |
MOZ_ASSERT(exportedNames.empty()); |
| 565 |
|
| 566 |
if (module->hasSyntheticModuleFields()) { |
| 567 |
return SyntheticModuleGetExportedNames(cx, module, exportedNames); |
| 568 |
} |
| 569 |
|
| 570 |
// Step 2. If exportStarSet contains module, then: |
| 571 |
if (exportStarSet.has(module)) { |
| 572 |
// Step 2.a. We've reached the starting point of an export * circularity. |
| 573 |
// Step 2.b. Return a new empty List. |
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
// Step 3. Append module to exportStarSet. |
| 578 |
if (!exportStarSet.put(module)) { |
| 579 |
ReportOutOfMemory(cx); |
| 580 |
return false; |
| 581 |
} |
| 582 |
|
| 583 |
// Step 5. For each ExportEntry Record e of module.[[LocalExportEntries]], do: |
| 584 |
for (const ExportEntry& e : module->localExportEntries()) { |
| 585 |
// Step 5.a. Assert: module provides the direct binding for this export. |
| 586 |
// Step 5.b. Append e.[[ExportName]] to exportedNames. |
| 587 |
if (!exportedNames.append(e.exportName())) { |
| 588 |
ReportOutOfMemory(cx); |
| 589 |
return false; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
// Step 6. For each ExportEntry Record e of module.[[IndirectExportEntries]], |
| 594 |
// do: |
| 595 |
for (const ExportEntry& e : module->indirectExportEntries()) { |
| 596 |
// Step 6.a. Assert: module imports a specific binding for this export. |
| 597 |
// Step 6.b. Append e.[[ExportName]] to exportedNames. |
| 598 |
if (!exportedNames.append(e.exportName())) { |
| 599 |
ReportOutOfMemory(cx); |
| 600 |
return false; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
// Step 7. For each ExportEntry Record e of module.[[StarExportEntries]], do: |
| 605 |
Rooted<ModuleRequestObject*> moduleRequest(cx); |
| 606 |
Rooted<ModuleObject*> requestedModule(cx); |
| 607 |
Rooted<JSAtom*> name(cx); |
| 608 |
for (const ExportEntry& e : module->starExportEntries()) { |
| 609 |
// Step 7.a. Let requestedModule be ? HostResolveImportedModule(module, |
| 610 |
// e.[[ModuleRequest]]). |
| 611 |
moduleRequest = e.moduleRequest(); |
| 612 |
requestedModule = HostResolveImportedModule(cx, module, moduleRequest, |
| 613 |
ModuleStatus::Unlinked); |
| 614 |
if (!requestedModule) { |
| 615 |
return false; |
| 616 |
} |
| 617 |
|
| 618 |
// Step 7.b. Let starNames be ? |
| 619 |
// requestedModule.GetExportedNames(exportStarSet). |
| 620 |
Rooted<ExportNameVector> starNames(cx); |
| 621 |
if (!ModuleGetExportedNames(cx, requestedModule, exportStarSet, |
| 622 |
&starNames)) { |
| 623 |
return false; |
| 624 |
} |
| 625 |
|
| 626 |
// Step 7.c. For each element n of starNames, do: |
| 627 |
for (JSAtom* name : starNames) { |
| 628 |
// Step 7.c.i. If SameValue(n, "default") is false, then: |
| 629 |
if (name != cx->names().default_) { |
| 630 |
// Step 7.c.i.1. If n is not an element of exportedNames, then: |
| 631 |
if (!ContainsElement(exportedNames, name)) { |
| 632 |
// Step 7.c.i.1.a. Append n to exportedNames. |
| 633 |
if (!exportedNames.append(name)) { |
| 634 |
ReportOutOfMemory(cx); |
| 635 |
return false; |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
// Step 8. Return exportedNames. |
| 643 |
return true; |
| 644 |
} |
| 645 |
|
| 646 |
static void ThrowUnexpectedModuleStatus(JSContext* cx, ModuleStatus status) { |
| 647 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 648 |
JSMSG_BAD_MODULE_STATUS, ModuleStatusName(status)); |
| 649 |
} |
| 650 |
|
| 651 |
static ModuleObject* HostResolveImportedModule( |
| 652 |
JSContext* cx, Handle<ModuleObject*> module, |
| 653 |
Handle<ModuleRequestObject*> moduleRequest, |
| 654 |
ModuleStatus expectedMinimumStatus) { |
| 655 |
MOZ_ASSERT(module); |
| 656 |
MOZ_ASSERT(moduleRequest); |
| 657 |
|
| 658 |
Rooted<Value> referencingPrivate(cx, JS::GetModulePrivate(module)); |
| 659 |
Rooted<ModuleObject*> requestedModule(cx); |
| 660 |
requestedModule = |
| 661 |
CallModuleResolveHook(cx, referencingPrivate, moduleRequest); |
| 662 |
if (!requestedModule) { |
| 663 |
return nullptr; |
| 664 |
} |
| 665 |
if (requestedModule->status() < expectedMinimumStatus) { |
| 666 |
ThrowUnexpectedModuleStatus(cx, requestedModule->status()); |
| 667 |
return nullptr; |
| 668 |
} |
| 669 |
return requestedModule; |
| 670 |
} |
| 671 |
|
| 672 |
static bool ModuleResolveExportImpl(JSContext* cx, Handle<ModuleObject*> module, |
| 673 |
Handle<JSAtom*> exportName, |
| 674 |
MutableHandle<ResolveSet> resolveSet, |
| 675 |
MutableHandle<Value> result, |
| 676 |
ModuleErrorInfo* errorInfoOut = nullptr) { |
| 677 |
if (module->hasSyntheticModuleFields()) { |
| 678 |
return SyntheticModuleResolveExport(cx, module, exportName, result, |
| 679 |
errorInfoOut); |
| 680 |
} |
| 681 |
|
| 682 |
return CyclicModuleResolveExport(cx, module, exportName, resolveSet, result, |
| 683 |
errorInfoOut); |
| 684 |
} |
| 685 |
|
| 686 |
// https://tc39.es/ecma262/#sec-resolveexport |
| 687 |
// ES2023 16.2.1.6.3 ResolveExport |
| 688 |
// |
| 689 |
// Returns an value describing the location of the resolved export or indicating |
| 690 |
// a failure. |
| 691 |
// |
| 692 |
// On success this returns a resolved binding record: { module, bindingName } |
| 693 |
// |
| 694 |
// There are two failure cases: |
| 695 |
// |
| 696 |
// - If no definition was found or the request is found to be circular, *null* |
| 697 |
// is returned. |
| 698 |
// |
| 699 |
// - If the request is found to be ambiguous, the string `"ambiguous"` is |
| 700 |
// returned. |
| 701 |
// |
| 702 |
static bool ModuleResolveExport(JSContext* cx, Handle<ModuleObject*> module, |
| 703 |
Handle<JSAtom*> exportName, |
| 704 |
MutableHandle<Value> result, |
| 705 |
ModuleErrorInfo* errorInfoOut = nullptr) { |
| 706 |
// Step 1. If resolveSet is not present, set resolveSet to a new empty List. |
| 707 |
Rooted<ResolveSet> resolveSet(cx); |
| 708 |
|
| 709 |
return ModuleResolveExportImpl(cx, module, exportName, &resolveSet, result, |
| 710 |
errorInfoOut); |
| 711 |
} |
| 712 |
|
| 713 |
static bool CreateResolvedBindingObject(JSContext* cx, |
| 714 |
Handle<ModuleObject*> module, |
| 715 |
Handle<JSAtom*> bindingName, |
| 716 |
MutableHandle<Value> result) { |
| 717 |
Rooted<ResolvedBindingObject*> obj( |
| 718 |
cx, ResolvedBindingObject::create(cx, module, bindingName)); |
| 719 |
if (!obj) { |
| 720 |
return false; |
| 721 |
} |
| 722 |
|
| 723 |
result.setObject(*obj); |
| 724 |
return true; |
| 725 |
} |
| 726 |
|
| 727 |
static bool CyclicModuleResolveExport(JSContext* cx, |
| 728 |
Handle<ModuleObject*> module, |
| 729 |
Handle<JSAtom*> exportName, |
| 730 |
MutableHandle<ResolveSet> resolveSet, |
| 731 |
MutableHandle<Value> result, |
| 732 |
ModuleErrorInfo* errorInfoOut) { |
| 733 |
// Step 2. For each Record { [[Module]], [[ExportName]] } r of resolveSet, do: |
| 734 |
for (const auto& entry : resolveSet) { |
| 735 |
// Step 2.a. If module and r.[[Module]] are the same Module Record and |
| 736 |
// SameValue(exportName, r.[[ExportName]]) is true, then: |
| 737 |
if (entry.module() == module && entry.exportName() == exportName) { |
| 738 |
// Step 2.a.i. Assert: This is a circular import request. |
| 739 |
// Step 2.a.ii. Return null. |
| 740 |
result.setNull(); |
| 741 |
if (errorInfoOut) { |
| 742 |
errorInfoOut->setCircularImport(cx, module); |
| 743 |
} |
| 744 |
return true; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
// Step 3. Append the Record { [[Module]]: module, [[ExportName]]: exportName |
| 749 |
// } to resolveSet. |
| 750 |
if (!resolveSet.emplaceBack(module, exportName)) { |
| 751 |
ReportOutOfMemory(cx); |
| 752 |
return false; |
| 753 |
} |
| 754 |
|
| 755 |
// Step 4. For each ExportEntry Record e of module.[[LocalExportEntries]], do: |
| 756 |
for (const ExportEntry& e : module->localExportEntries()) { |
| 757 |
// Step 4.a. If SameValue(exportName, e.[[ExportName]]) is true, then: |
| 758 |
if (exportName == e.exportName()) { |
| 759 |
// Step 4.a.i. Assert: module provides the direct binding for this export. |
| 760 |
// Step 4.a.ii. Return ResolvedBinding Record { [[Module]]: module, |
| 761 |
// [[BindingName]]: e.[[LocalName]] }. |
| 762 |
Rooted<JSAtom*> localName(cx, e.localName()); |
| 763 |
return CreateResolvedBindingObject(cx, module, localName, result); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
// Step 5. For each ExportEntry Record e of module.[[IndirectExportEntries]], |
| 768 |
// do: |
| 769 |
Rooted<ModuleRequestObject*> moduleRequest(cx); |
| 770 |
Rooted<ModuleObject*> importedModule(cx); |
| 771 |
Rooted<JSAtom*> name(cx); |
| 772 |
for (const ExportEntry& e : module->indirectExportEntries()) { |
| 773 |
// Step 5.a. If SameValue(exportName, e.[[ExportName]]) is true, then: |
| 774 |
if (exportName == e.exportName()) { |
| 775 |
// Step 5.a.i. Let importedModule be ? HostResolveImportedModule(module, |
| 776 |
// e.[[ModuleRequest]]). |
| 777 |
moduleRequest = e.moduleRequest(); |
| 778 |
importedModule = HostResolveImportedModule(cx, module, moduleRequest, |
| 779 |
ModuleStatus::Unlinked); |
| 780 |
if (!importedModule) { |
| 781 |
return false; |
| 782 |
} |
| 783 |
|
| 784 |
// Step 5.a.ii. If e.[[ImportName]] is all, then: |
| 785 |
if (!e.importName()) { |
| 786 |
// Step 5.a.ii.1. Assert: module does not provide the direct binding for |
| 787 |
// this export. |
| 788 |
// Step 5.a.ii.2. Return ResolvedBinding Record { [[Module]]: |
| 789 |
// importedModule, [[BindingName]]: namespace }. |
| 790 |
name = cx->names().star_namespace_star_; |
| 791 |
return CreateResolvedBindingObject(cx, importedModule, name, result); |
| 792 |
} else { |
| 793 |
// Step 5.a.iii.1. Assert: module imports a specific binding for this |
| 794 |
// export. |
| 795 |
// Step 5.a.iii.2. Return ? |
| 796 |
// importedModule.ResolveExport(e.[[ImportName]], |
| 797 |
// resolveSet). |
| 798 |
name = e.importName(); |
| 799 |
|
| 800 |
return ModuleResolveExportImpl(cx, importedModule, name, resolveSet, |
| 801 |
result, errorInfoOut); |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
// Step 6. If SameValue(exportName, "default") is true, then: |
| 807 |
if (exportName == cx->names().default_) { |
| 808 |
// Step 6.a. Assert: A default export was not explicitly defined by this |
| 809 |
// module. |
| 810 |
// Step 6.b. Return null. |
| 811 |
// Step 6.c. NOTE: A default export cannot be provided by an export * from |
| 812 |
// "mod" declaration. |
| 813 |
result.setNull(); |
| 814 |
if (errorInfoOut) { |
| 815 |
errorInfoOut->setImportedModule(cx, module); |
| 816 |
} |
| 817 |
return true; |
| 818 |
} |
| 819 |
|
| 820 |
// Step 7. Let starResolution be null. |
| 821 |
Rooted<ResolvedBindingObject*> starResolution(cx); |
| 822 |
|
| 823 |
// Step 8. For each ExportEntry Record e of module.[[StarExportEntries]], do: |
| 824 |
Rooted<Value> resolution(cx); |
| 825 |
Rooted<ResolvedBindingObject*> binding(cx); |
| 826 |
for (const ExportEntry& e : module->starExportEntries()) { |
| 827 |
// Step 8.a. Let importedModule be ? HostResolveImportedModule(module, |
| 828 |
// e.[[ModuleRequest]]). |
| 829 |
moduleRequest = e.moduleRequest(); |
| 830 |
importedModule = HostResolveImportedModule(cx, module, moduleRequest, |
| 831 |
ModuleStatus::Unlinked); |
| 832 |
if (!importedModule) { |
| 833 |
return false; |
| 834 |
} |
| 835 |
|
| 836 |
// Step 8.b. Let resolution be ? importedModule.ResolveExport(exportName, |
| 837 |
// resolveSet). |
| 838 |
if (!CyclicModuleResolveExport(cx, importedModule, exportName, resolveSet, |
| 839 |
&resolution, errorInfoOut)) { |
| 840 |
return false; |
| 841 |
} |
| 842 |
|
| 843 |
// Step 8.c. If resolution is ambiguous, return ambiguous. |
| 844 |
if (resolution == StringValue(cx->names().ambiguous)) { |
| 845 |
result.set(resolution); |
| 846 |
return true; |
| 847 |
} |
| 848 |
|
| 849 |
// Step 8.d. If resolution is not null, then: |
| 850 |
if (!resolution.isNull()) { |
| 851 |
// Step 8.d.i. Assert: resolution is a ResolvedBinding Record. |
| 852 |
binding = &resolution.toObject().as<ResolvedBindingObject>(); |
| 853 |
|
| 854 |
// Step 8.d.ii. If starResolution is null, set starResolution to |
| 855 |
// resolution. |
| 856 |
if (!starResolution) { |
| 857 |
starResolution = binding; |
| 858 |
} else { |
| 859 |
// Step 8.d.iii. Else: |
| 860 |
// Step 8.d.iii.1. Assert: There is more than one * import that includes |
| 861 |
// the requested name. |
| 862 |
// Step 8.d.iii.2. If resolution.[[Module]] and |
| 863 |
// starResolution.[[Module]] are not the same Module |
| 864 |
// Record, return ambiguous. |
| 865 |
// Step 8.d.iii.3. If resolution.[[BindingName]] is namespace and |
| 866 |
// starResolution.[[BindingName]] is not namespace, or |
| 867 |
// if resolution.[[BindingName]] is not namespace and |
| 868 |
// starResolution.[[BindingName]] is namespace, return |
| 869 |
// ambiguous. |
| 870 |
// Step 8.d.iii.4. If resolution.[[BindingName]] is a String, |
| 871 |
// starResolution.[[BindingName]] is a String, and |
| 872 |
// SameValue(resolution.[[BindingName]], |
| 873 |
// starResolution.[[BindingName]]) is false, return |
| 874 |
// ambiguous. |
| 875 |
if (binding->module() != starResolution->module() || |
| 876 |
binding->bindingName() != starResolution->bindingName()) { |
| 877 |
result.set(StringValue(cx->names().ambiguous)); |
| 878 |
|
| 879 |
if (errorInfoOut) { |
| 880 |
Rooted<ModuleObject*> module1(cx, starResolution->module()); |
| 881 |
Rooted<ModuleObject*> module2(cx, binding->module()); |
| 882 |
errorInfoOut->setForAmbiguousImport(cx, module, module1, module2); |
| 883 |
} |
| 884 |
return true; |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
// Step 9. Return starResolution. |
| 891 |
result.setObjectOrNull(starResolution); |
| 892 |
if (!starResolution && errorInfoOut) { |
| 893 |
errorInfoOut->setImportedModule(cx, module); |
| 894 |
} |
| 895 |
return true; |
| 896 |
} |
| 897 |
|
| 898 |
// https://tc39.es/proposal-json-modules/#sec-smr-resolveexport |
| 899 |
static bool SyntheticModuleResolveExport(JSContext* cx, |
| 900 |
Handle<ModuleObject*> module, |
| 901 |
Handle<JSAtom*> exportName, |
| 902 |
MutableHandle<Value> result, |
| 903 |
ModuleErrorInfo* errorInfoOut) { |
| 904 |
// Step 2. If module.[[ExportNames]] does not contain exportName, return null. |
| 905 |
if (!ContainsElement(module->syntheticExportNames(), exportName)) { |
| 906 |
result.setNull(); |
| 907 |
if (errorInfoOut) { |
| 908 |
errorInfoOut->setImportedModule(cx, module); |
| 909 |
} |
| 910 |
return true; |
| 911 |
} |
| 912 |
|
| 913 |
// Step 3. Return ResolvedBinding Record { [[Module]]: module, |
| 914 |
// [[BindingName]]: exportName }. |
| 915 |
return CreateResolvedBindingObject(cx, module, exportName, result); |
| 916 |
} |
| 917 |
|
| 918 |
// https://tc39.es/ecma262/#sec-getmodulenamespace |
| 919 |
// ES2023 16.2.1.10 GetModuleNamespace |
| 920 |
ModuleNamespaceObject* js::GetOrCreateModuleNamespace( |
| 921 |
JSContext* cx, Handle<ModuleObject*> module) { |
| 922 |
// Step 1. Assert: If module is a Cyclic Module Record, then module.[[Status]] |
| 923 |
// is not unlinked. |
| 924 |
MOZ_ASSERT(module->status() != ModuleStatus::Unlinked); |
| 925 |
|
| 926 |
// Step 2. Let namespace be module.[[Namespace]]. |
| 927 |
Rooted<ModuleNamespaceObject*> ns(cx, module->namespace_()); |
| 928 |
|
| 929 |
// Step 3. If namespace is empty, then: |
| 930 |
if (!ns) { |
| 931 |
// Step 3.a. Let exportedNames be ? module.GetExportedNames(). |
| 932 |
Rooted<ModuleSet> exportStarSet(cx); |
| 933 |
Rooted<ExportNameVector> exportedNames(cx); |
| 934 |
if (!ModuleGetExportedNames(cx, module, &exportStarSet, &exportedNames)) { |
| 935 |
return nullptr; |
| 936 |
} |
| 937 |
|
| 938 |
// Step 3.b. Let unambiguousNames be a new empty List. |
| 939 |
Rooted<UniquePtr<ExportNameVector>> unambiguousNames( |
| 940 |
cx, cx->make_unique<ExportNameVector>()); |
| 941 |
if (!unambiguousNames) { |
| 942 |
return nullptr; |
| 943 |
} |
| 944 |
|
| 945 |
// Step 3.c. For each element name of exportedNames, do: |
| 946 |
Rooted<JSAtom*> name(cx); |
| 947 |
Rooted<Value> resolution(cx); |
| 948 |
for (JSAtom* atom : exportedNames) { |
| 949 |
name = atom; |
| 950 |
|
| 951 |
// Step 3.c.i. Let resolution be ? module.ResolveExport(name). |
| 952 |
if (!ModuleResolveExport(cx, module, name, &resolution)) { |
| 953 |
return nullptr; |
| 954 |
} |
| 955 |
|
| 956 |
// Step 3.c.ii. If resolution is a ResolvedBinding Record, append name to |
| 957 |
// unambiguousNames. |
| 958 |
if (resolution.isObject() && !unambiguousNames->append(name)) { |
| 959 |
ReportOutOfMemory(cx); |
| 960 |
return nullptr; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
// Step 3.d. Set namespace to ModuleNamespaceCreate(module, |
| 965 |
// unambiguousNames). |
| 966 |
ns = ModuleNamespaceCreate(cx, module, &unambiguousNames); |
| 967 |
} |
| 968 |
|
| 969 |
// Step 4. Return namespace. |
| 970 |
return ns; |
| 971 |
} |
| 972 |
|
| 973 |
static bool IsResolvedBinding(JSContext* cx, Handle<Value> resolution) { |
| 974 |
MOZ_ASSERT(resolution.isObjectOrNull() || |
| 975 |
resolution.toString() == cx->names().ambiguous); |
| 976 |
return resolution.isObject(); |
| 977 |
} |
| 978 |
|
| 979 |
static void InitNamespaceBinding(JSContext* cx, |
| 980 |
Handle<ModuleEnvironmentObject*> env, |
| 981 |
Handle<JSAtom*> name, |
| 982 |
Handle<ModuleNamespaceObject*> ns) { |
| 983 |
// The property already exists in the evironment but is not writable, so set |
| 984 |
// the slot directly. |
| 985 |
RootedId id(cx, AtomToId(name)); |
| 986 |
mozilla::Maybe<PropertyInfo> prop = env->lookup(cx, id); |
| 987 |
MOZ_ASSERT(prop.isSome()); |
| 988 |
env->setSlot(prop->slot(), ObjectValue(*ns)); |
| 989 |
} |
| 990 |
|
| 991 |
static bool ComputeNamespaceBindings(JSContext* cx, |
| 992 |
Handle<ModuleObject*> module, |
| 993 |
Handle<ModuleNamespaceObject*> ns) { |
| 994 |
Rooted<JSAtom*> name(cx); |
| 995 |
Rooted<Value> resolution(cx); |
| 996 |
Rooted<ResolvedBindingObject*> binding(cx); |
| 997 |
Rooted<ModuleObject*> importedModule(cx); |
| 998 |
Rooted<ModuleNamespaceObject*> importedNamespace(cx); |
| 999 |
Rooted<JSAtom*> bindingName(cx); |
| 1000 |
for (JSAtom* atom : ns->exports()) { |
| 1001 |
name = atom; |
| 1002 |
|
| 1003 |
if (!ModuleResolveExport(cx, module, name, &resolution)) { |
| 1004 |
return false; |
| 1005 |
} |
| 1006 |
|
| 1007 |
MOZ_ASSERT(IsResolvedBinding(cx, resolution)); |
| 1008 |
binding = &resolution.toObject().as<ResolvedBindingObject>(); |
| 1009 |
importedModule = binding->module(); |
| 1010 |
bindingName = binding->bindingName(); |
| 1011 |
|
| 1012 |
if (bindingName == cx->names().star_namespace_star_) { |
| 1013 |
importedNamespace = GetOrCreateModuleNamespace(cx, importedModule); |
| 1014 |
if (!importedNamespace) { |
| 1015 |
return false; |
| 1016 |
} |
| 1017 |
|
| 1018 |
// The spec uses an immutable binding here but we have already generated |
| 1019 |
// bytecode for an indirect binding. Instead, use an indirect binding to |
| 1020 |
// "*namespace*" slot of the target environment. |
| 1021 |
Rooted<ModuleEnvironmentObject*> env( |
| 1022 |
cx, &importedModule->initialEnvironment()); |
| 1023 |
InitNamespaceBinding(cx, env, bindingName, importedNamespace); |
| 1024 |
} |
| 1025 |
|
| 1026 |
if (!ns->addBinding(cx, name, importedModule, bindingName)) { |
| 1027 |
return false; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
return true; |
| 1032 |
} |
| 1033 |
|
| 1034 |
struct AtomComparator { |
| 1035 |
bool operator()(JSAtom* a, JSAtom* b, bool* lessOrEqualp) { |
| 1036 |
int32_t result = CompareStrings(a, b); |
| 1037 |
*lessOrEqualp = (result <= 0); |
| 1038 |
return true; |
| 1039 |
} |
| 1040 |
}; |
| 1041 |
|
| 1042 |
// https://tc39.es/ecma262/#sec-modulenamespacecreate |
| 1043 |
// ES2023 10.4.6.12 ModuleNamespaceCreate |
| 1044 |
static ModuleNamespaceObject* ModuleNamespaceCreate( |
| 1045 |
JSContext* cx, Handle<ModuleObject*> module, |
| 1046 |
MutableHandle<UniquePtr<ExportNameVector>> exports) { |
| 1047 |
// Step 1. Assert: module.[[Namespace]] is empty. |
| 1048 |
MOZ_ASSERT(!module->namespace_()); |
| 1049 |
|
| 1050 |
// Step 6. Let sortedExports be a List whose elements are the elements of |
| 1051 |
// exports ordered as if an Array of the same values had been sorted |
| 1052 |
// using %Array.prototype.sort% using undefined as comparefn. |
| 1053 |
ExportNameVector scratch; |
| 1054 |
if (!scratch.resize(exports->length())) { |
| 1055 |
ReportOutOfMemory(cx); |
| 1056 |
return nullptr; |
| 1057 |
} |
| 1058 |
MOZ_ALWAYS_TRUE(MergeSort(exports->begin(), exports->length(), |
| 1059 |
scratch.begin(), AtomComparator())); |
| 1060 |
|
| 1061 |
// Steps 2 - 5. |
| 1062 |
Rooted<ModuleNamespaceObject*> ns( |
| 1063 |
cx, ModuleObject::createNamespace(cx, module, exports)); |
| 1064 |
if (!ns) { |
| 1065 |
return nullptr; |
| 1066 |
} |
| 1067 |
|
| 1068 |
// Pre-compute all binding mappings now instead of on each access. |
| 1069 |
// See: |
| 1070 |
// https://tc39.es/ecma262/#sec-module-namespace-exotic-objects-get-p-receiver |
| 1071 |
// ES2023 10.4.6.8 Module Namespace Exotic Object [[Get]] |
| 1072 |
if (!ComputeNamespaceBindings(cx, module, ns)) { |
| 1073 |
module->clearNamespaceOnFailure(); |
| 1074 |
return nullptr; |
| 1075 |
} |
| 1076 |
|
| 1077 |
// Step 10. Return M. |
| 1078 |
return ns; |
| 1079 |
} |
| 1080 |
|
| 1081 |
void ModuleErrorInfo::setImportedModule(JSContext* cx, |
| 1082 |
ModuleObject* importedModule) { |
| 1083 |
imported = importedModule->filename(); |
| 1084 |
} |
| 1085 |
|
| 1086 |
void ModuleErrorInfo::setCircularImport(JSContext* cx, |
| 1087 |
ModuleObject* importedModule) { |
| 1088 |
setImportedModule(cx, importedModule); |
| 1089 |
isCircular = true; |
| 1090 |
} |
| 1091 |
|
| 1092 |
void ModuleErrorInfo::setForAmbiguousImport(JSContext* cx, |
| 1093 |
ModuleObject* importedModule, |
| 1094 |
ModuleObject* module1, |
| 1095 |
ModuleObject* module2) { |
| 1096 |
setImportedModule(cx, importedModule); |
| 1097 |
entry1 = module1->filename(); |
| 1098 |
entry2 = module2->filename(); |
| 1099 |
} |
| 1100 |
|
| 1101 |
static void CreateErrorNumberMessageUTF8(JSContext* cx, unsigned errorNumber, |
| 1102 |
JSErrorReport* reportOut, ...) { |
| 1103 |
va_list ap; |
| 1104 |
va_start(ap, reportOut); |
| 1105 |
AutoReportFrontendContext fc(cx); |
| 1106 |
if (!ExpandErrorArgumentsVA(&fc, GetErrorMessage, nullptr, errorNumber, |
| 1107 |
ArgumentsAreUTF8, reportOut, ap)) { |
| 1108 |
ReportOutOfMemory(cx); |
| 1109 |
return; |
| 1110 |
} |
| 1111 |
|
| 1112 |
va_end(ap); |
| 1113 |
} |
| 1114 |
|
| 1115 |
static void ThrowResolutionError(JSContext* cx, Handle<ModuleObject*> module, |
| 1116 |
Handle<Value> resolution, Handle<JSAtom*> name, |
| 1117 |
ModuleErrorInfo* errorInfo) { |
| 1118 |
MOZ_ASSERT(errorInfo); |
| 1119 |
auto chars = StringToNewUTF8CharsZ(cx, *name); |
| 1120 |
if (!chars) { |
| 1121 |
ReportOutOfMemory(cx); |
| 1122 |
return; |
| 1123 |
} |
| 1124 |
|
| 1125 |
bool isAmbiguous = resolution == StringValue(cx->names().ambiguous); |
| 1126 |
|
| 1127 |
unsigned errorNumber; |
| 1128 |
if (errorInfo->isCircular) { |
| 1129 |
errorNumber = JSMSG_MODULE_CIRCULAR_IMPORT; |
| 1130 |
} else if (isAmbiguous) { |
| 1131 |
errorNumber = JSMSG_MODULE_AMBIGUOUS; |
| 1132 |
} else { |
| 1133 |
errorNumber = JSMSG_MODULE_NO_EXPORT; |
| 1134 |
} |
| 1135 |
|
| 1136 |
JSErrorReport report; |
| 1137 |
report.isWarning_ = false; |
| 1138 |
report.errorNumber = errorNumber; |
| 1139 |
|
| 1140 |
if (errorNumber == JSMSG_MODULE_AMBIGUOUS) { |
| 1141 |
CreateErrorNumberMessageUTF8(cx, errorNumber, &report, errorInfo->imported, |
| 1142 |
chars.get(), errorInfo->entry1, |
| 1143 |
errorInfo->entry2); |
| 1144 |
} else { |
| 1145 |
CreateErrorNumberMessageUTF8(cx, errorNumber, &report, errorInfo->imported, |
| 1146 |
chars.get()); |
| 1147 |
} |
| 1148 |
|
| 1149 |
Rooted<JSString*> message(cx, report.newMessageString(cx)); |
| 1150 |
if (!message) { |
| 1151 |
ReportOutOfMemory(cx); |
| 1152 |
return; |
| 1153 |
} |
| 1154 |
|
| 1155 |
const char* file = module->filename(); |
| 1156 |
RootedString filename( |
| 1157 |
cx, JS_NewStringCopyUTF8Z(cx, JS::ConstUTF8CharsZ(file, strlen(file)))); |
| 1158 |
if (!filename) { |
| 1159 |
ReportOutOfMemory(cx); |
| 1160 |
return; |
| 1161 |
} |
| 1162 |
|
| 1163 |
RootedValue error(cx); |
| 1164 |
if (!JS::CreateError(cx, JSEXN_SYNTAXERR, nullptr, filename, |
| 1165 |
errorInfo->lineNumber, errorInfo->columnNumber, nullptr, |
| 1166 |
message, JS::NothingHandleValue, &error)) { |
| 1167 |
ReportOutOfMemory(cx); |
| 1168 |
return; |
| 1169 |
} |
| 1170 |
|
| 1171 |
cx->setPendingException(error, nullptr); |
| 1172 |
} |
| 1173 |
|
| 1174 |
// https://tc39.es/ecma262/#sec-source-text-module-record-initialize-environment |
| 1175 |
// ES2023 16.2.1.6.4 InitializeEnvironment |
| 1176 |
static bool ModuleInitializeEnvironment(JSContext* cx, |
| 1177 |
Handle<ModuleObject*> module) { |
| 1178 |
MOZ_ASSERT(module->status() == ModuleStatus::Linking); |
| 1179 |
|
| 1180 |
// Step 1. For each ExportEntry Record e of module.[[IndirectExportEntries]], |
| 1181 |
// do: |
| 1182 |
Rooted<JSAtom*> exportName(cx); |
| 1183 |
Rooted<Value> resolution(cx); |
| 1184 |
for (const ExportEntry& e : module->indirectExportEntries()) { |
| 1185 |
// Step 1.a. Assert: e.[[ExportName]] is not null. |
| 1186 |
MOZ_ASSERT(e.exportName()); |
| 1187 |
|
| 1188 |
// Step 1.b. Let resolution be ? module.ResolveExport(e.[[ExportName]]). |
| 1189 |
exportName = e.exportName(); |
| 1190 |
ModuleErrorInfo errorInfo{e.lineNumber(), e.columnNumber()}; |
| 1191 |
if (!ModuleResolveExport(cx, module, exportName, &resolution, &errorInfo)) { |
| 1192 |
return false; |
| 1193 |
} |
| 1194 |
|
| 1195 |
// Step 1.c. If resolution is either null or AMBIGUOUS, throw a SyntaxError |
| 1196 |
// exception. |
| 1197 |
if (!IsResolvedBinding(cx, resolution)) { |
| 1198 |
ThrowResolutionError(cx, module, resolution, exportName, &errorInfo); |
| 1199 |
return false; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
// Step 5. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]). |
| 1204 |
// Step 6. Set module.[[Environment]] to env. |
| 1205 |
// Note that we have already created the environment by this point. |
| 1206 |
Rooted<ModuleEnvironmentObject*> env(cx, &module->initialEnvironment()); |
| 1207 |
|
| 1208 |
// Step 7. For each ImportEntry Record in of module.[[ImportEntries]], do: |
| 1209 |
Rooted<ModuleRequestObject*> moduleRequest(cx); |
| 1210 |
Rooted<ModuleObject*> importedModule(cx); |
| 1211 |
Rooted<JSAtom*> importName(cx); |
| 1212 |
Rooted<JSAtom*> localName(cx); |
| 1213 |
Rooted<ModuleObject*> sourceModule(cx); |
| 1214 |
Rooted<JSAtom*> bindingName(cx); |
| 1215 |
for (const ImportEntry& in : module->importEntries()) { |
| 1216 |
// Step 7.a. Let importedModule be ! HostResolveImportedModule(module, |
| 1217 |
// in.[[ModuleRequest]]). |
| 1218 |
moduleRequest = in.moduleRequest(); |
| 1219 |
importedModule = HostResolveImportedModule(cx, module, moduleRequest, |
| 1220 |
ModuleStatus::Linking); |
| 1221 |
if (!importedModule) { |
| 1222 |
return false; |
| 1223 |
} |
| 1224 |
|
| 1225 |
localName = in.localName(); |
| 1226 |
importName = in.importName(); |
| 1227 |
|
| 1228 |
// Step 7.c. If in.[[ImportName]] is namespace-object, then: |
| 1229 |
if (!importName) { |
| 1230 |
// Step 7.c.i. Let namespace be ? GetModuleNamespace(importedModule). |
| 1231 |
Rooted<ModuleNamespaceObject*> ns( |
| 1232 |
cx, GetOrCreateModuleNamespace(cx, importedModule)); |
| 1233 |
if (!ns) { |
| 1234 |
return false; |
| 1235 |
} |
| 1236 |
|
| 1237 |
// Step 7.c.ii. Perform ! env.CreateImmutableBinding(in.[[LocalName]], |
| 1238 |
// true). This happens when the environment is created. |
| 1239 |
|
| 1240 |
// Step 7.c.iii. Perform ! env.InitializeBinding(in.[[LocalName]], |
| 1241 |
// namespace). |
| 1242 |
InitNamespaceBinding(cx, env, localName, ns); |
| 1243 |
} else { |
| 1244 |
// Step 7.d. Else: |
| 1245 |
// Step 7.d.i. Let resolution be ? |
| 1246 |
// importedModule.ResolveExport(in.[[ImportName]]). |
| 1247 |
ModuleErrorInfo errorInfo{in.lineNumber(), in.columnNumber()}; |
| 1248 |
if (!ModuleResolveExport(cx, importedModule, importName, &resolution, |
| 1249 |
&errorInfo)) { |
| 1250 |
return false; |
| 1251 |
} |
| 1252 |
|
| 1253 |
// Step 7.d.ii. If resolution is null or ambiguous, throw a SyntaxError |
| 1254 |
// exception. |
| 1255 |
if (!IsResolvedBinding(cx, resolution)) { |
| 1256 |
ThrowResolutionError(cx, module, resolution, importName, &errorInfo); |
| 1257 |
return false; |
| 1258 |
} |
| 1259 |
|
| 1260 |
auto* binding = &resolution.toObject().as<ResolvedBindingObject>(); |
| 1261 |
sourceModule = binding->module(); |
| 1262 |
bindingName = binding->bindingName(); |
| 1263 |
|
| 1264 |
// Step 7.d.iii. If resolution.[[BindingName]] is namespace, then: |
| 1265 |
if (bindingName == cx->names().star_namespace_star_) { |
| 1266 |
// Step 7.d.iii.1. Let namespace be ? |
| 1267 |
// GetModuleNamespace(resolution.[[Module]]). |
| 1268 |
Rooted<ModuleNamespaceObject*> ns( |
| 1269 |
cx, GetOrCreateModuleNamespace(cx, sourceModule)); |
| 1270 |
if (!ns) { |
| 1271 |
return false; |
| 1272 |
} |
| 1273 |
|
| 1274 |
// Step 7.d.iii.2. Perform ! |
| 1275 |
// env.CreateImmutableBinding(in.[[LocalName]], true). |
| 1276 |
// Step 7.d.iii.3. Perform ! env.InitializeBinding(in.[[LocalName]], |
| 1277 |
// namespace). |
| 1278 |
// |
| 1279 |
// This should be InitNamespaceBinding, but we have already generated |
| 1280 |
// bytecode assuming an indirect binding. Instead, ensure a special |
| 1281 |
// "*namespace*"" binding exists on the target module's environment. We |
| 1282 |
// then generate an indirect binding to this synthetic binding. |
| 1283 |
Rooted<ModuleEnvironmentObject*> sourceEnv( |
| 1284 |
cx, &sourceModule->initialEnvironment()); |
| 1285 |
InitNamespaceBinding(cx, sourceEnv, bindingName, ns); |
| 1286 |
if (!env->createImportBinding(cx, localName, sourceModule, |
| 1287 |
bindingName)) { |
| 1288 |
return false; |
| 1289 |
} |
| 1290 |
} else { |
| 1291 |
// Step 7.d.iv. Else: |
| 1292 |
// Step 7.d.iv.1. 1. Perform env.CreateImportBinding(in.[[LocalName]], |
| 1293 |
// resolution.[[Module]], resolution.[[BindingName]]). |
| 1294 |
if (!env->createImportBinding(cx, localName, sourceModule, |
| 1295 |
bindingName)) { |
| 1296 |
return false; |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
// Steps 8-26. |
| 1303 |
// |
| 1304 |
// Some of these do not need to happen for practical purposes. For steps |
| 1305 |
// 21-23, the bindings that can be handled in a similar way to regulars |
| 1306 |
// scripts are done separately. Function Declarations are special due to |
| 1307 |
// hoisting and are handled within this function. See ModuleScope and |
| 1308 |
// ModuleEnvironmentObject for further details. |
| 1309 |
|
| 1310 |
// Step 24. For each element d of lexDeclarations, do: |
| 1311 |
// Step 24.a. For each element dn of the BoundNames of d, do: |
| 1312 |
// Step 24.a.iii. If d is a FunctionDeclaration, a GeneratorDeclaration, an |
| 1313 |
// AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration, |
| 1314 |
// then: |
| 1315 |
// Step 24.a.iii.1 Let fo be InstantiateFunctionObject of d with arguments env |
| 1316 |
// and privateEnv. |
| 1317 |
// Step 24.a.iii.2. Perform ! env.InitializeBinding(dn, fo). |
| 1318 |
return ModuleObject::instantiateFunctionDeclarations(cx, module); |
| 1319 |
} |
| 1320 |
|
| 1321 |
// https://tc39.es/ecma262/#sec-moduledeclarationlinking |
| 1322 |
// ES2023 16.2.1.5.1 Link |
| 1323 |
static bool ModuleLink(JSContext* cx, Handle<ModuleObject*> module) { |
| 1324 |
if (!module->hasCyclicModuleFields()) { |
| 1325 |
return true; |
| 1326 |
} |
| 1327 |
|
| 1328 |
// Step 1. Assert: module.[[Status]] is not linking or evaluating. |
| 1329 |
ModuleStatus status = module->status(); |
| 1330 |
if (status == ModuleStatus::Linking || status == ModuleStatus::Evaluating) { |
| 1331 |
ThrowUnexpectedModuleStatus(cx, status); |
| 1332 |
return false; |
| 1333 |
} |
| 1334 |
|
| 1335 |
// Step 2. Let stack be a new empty List. |
| 1336 |
Rooted<ModuleVector> stack(cx); |
| 1337 |
|
| 1338 |
// Step 3. Let result be Completion(InnerModuleLinking(module, stack, 0)). |
| 1339 |
size_t ignored; |
| 1340 |
bool ok = InnerModuleLinking(cx, module, &stack, 0, &ignored); |
| 1341 |
|
| 1342 |
// Step 4. If result is an abrupt completion, then: |
| 1343 |
if (!ok) { |
| 1344 |
// Step 4.a. For each Cyclic Module Record m of stack, do: |
| 1345 |
for (ModuleObject* m : stack) { |
| 1346 |
// Step 4.a.i. Assert: m.[[Status]] is linking. |
| 1347 |
MOZ_ASSERT(m->status() == ModuleStatus::Linking); |
| 1348 |
// Step 4.a.ii. Set m.[[Status]] to unlinked. |
| 1349 |
m->setStatus(ModuleStatus::Unlinked); |
| 1350 |
m->clearDfsIndexes(); |
| 1351 |
} |
| 1352 |
|
| 1353 |
// Step 4.b. Assert: module.[[Status]] is unlinked. |
| 1354 |
MOZ_ASSERT(module->status() == ModuleStatus::Unlinked); |
| 1355 |
|
| 1356 |
// Step 4.c. |
| 1357 |
return false; |
| 1358 |
} |
| 1359 |
|
| 1360 |
// Step 5. Assert: module.[[Status]] is linked, evaluating-async, or |
| 1361 |
// evaluated. |
| 1362 |
MOZ_ASSERT(module->status() == ModuleStatus::Linked || |
| 1363 |
module->status() == ModuleStatus::EvaluatingAsync || |
| 1364 |
module->status() == ModuleStatus::Evaluated); |
| 1365 |
|
| 1366 |
// Step 6. Assert: stack is empty. |
| 1367 |
MOZ_ASSERT(stack.empty()); |
| 1368 |
|
| 1369 |
// Step 7. Return unused. |
| 1370 |
return true; |
| 1371 |
} |
| 1372 |
|
| 1373 |
// https://tc39.es/ecma262/#sec-InnerModuleLinking |
| 1374 |
// ES2023 16.2.1.5.1.1 InnerModuleLinking |
| 1375 |
static bool InnerModuleLinking(JSContext* cx, Handle<ModuleObject*> module, |
| 1376 |
MutableHandle<ModuleVector> stack, size_t index, |
| 1377 |
size_t* indexOut) { |
| 1378 |
// Step 1. If module is not a Cyclic Module Record, then |
| 1379 |
if (!module->hasCyclicModuleFields()) { |
| 1380 |
// Step 1.a. Perform ? module.Link(). |
| 1381 |
// (Skipped as we have already created the environment for these modules). |
| 1382 |
// Step 1.b. Return index. |
| 1383 |
*indexOut = index; |
| 1384 |
return true; |
| 1385 |
} |
| 1386 |
|
| 1387 |
// Step 2. If module.[[Status]] is linking, linked, evaluating-async, or |
| 1388 |
// evaluated, then: |
| 1389 |
if (module->status() == ModuleStatus::Linking || |
| 1390 |
module->status() == ModuleStatus::Linked || |
| 1391 |
module->status() == ModuleStatus::EvaluatingAsync || |
| 1392 |
module->status() == ModuleStatus::Evaluated) { |
| 1393 |
// Step 2.a. Return index. |
| 1394 |
*indexOut = index; |
| 1395 |
return true; |
| 1396 |
} |
| 1397 |
|
| 1398 |
// Step 3. Assert: module.[[Status]] is unlinked. |
| 1399 |
if (module->status() != ModuleStatus::Unlinked) { |
| 1400 |
ThrowUnexpectedModuleStatus(cx, module->status()); |
| 1401 |
return false; |
| 1402 |
} |
| 1403 |
|
| 1404 |
// Step 8. Append module to stack. |
| 1405 |
// Do this before changing the status so that we can recover on failure. |
| 1406 |
if (!stack.append(module)) { |
| 1407 |
ReportOutOfMemory(cx); |
| 1408 |
return false; |
| 1409 |
} |
| 1410 |
|
| 1411 |
// Step 4. Set module.[[Status]] to linking. |
| 1412 |
module->setStatus(ModuleStatus::Linking); |
| 1413 |
|
| 1414 |
// Step 5. Set module.[[DFSIndex]] to index. |
| 1415 |
module->setDfsIndex(index); |
| 1416 |
|
| 1417 |
// Step 6. Set module.[[DFSAncestorIndex]] to index. |
| 1418 |
module->setDfsAncestorIndex(index); |
| 1419 |
|
| 1420 |
// Step 7. Set index to index + 1. |
| 1421 |
index++; |
| 1422 |
|
| 1423 |
AutoCheckRecursionLimit recursion(cx); |
| 1424 |
if (!recursion.check(cx)) { |
| 1425 |
return false; |
| 1426 |
} |
| 1427 |
|
| 1428 |
// Step 9. For each String required that is an element of |
| 1429 |
// module.[[RequestedModules]], do: |
| 1430 |
Rooted<ModuleRequestObject*> moduleRequest(cx); |
| 1431 |
Rooted<ModuleObject*> requiredModule(cx); |
| 1432 |
for (const RequestedModule& request : module->requestedModules()) { |
| 1433 |
moduleRequest = request.moduleRequest(); |
| 1434 |
|
| 1435 |
// According to the spec, this should be in InnerModuleLoading, but |
| 1436 |
// currently, our module code is not aligned with the spec text. |
| 1437 |
// https://bugzilla.mozilla.org/show_bug.cgi?id=1894729 |
| 1438 |
if (moduleRequest->hasFirstUnsupportedAttributeKey()) { |
| 1439 |
UniqueChars printableKey = AtomToPrintableString( |
| 1440 |
cx, moduleRequest->getFirstUnsupportedAttributeKey()); |
| 1441 |
JS_ReportErrorNumberASCII( |
| 1442 |
cx, GetErrorMessage, nullptr, |
| 1443 |
JSMSG_IMPORT_ATTRIBUTES_STATIC_IMPORT_UNSUPPORTED_ATTRIBUTE, |
| 1444 |
printableKey ? printableKey.get() : ""); |
| 1445 |
|
| 1446 |
return false; |
| 1447 |
} |
| 1448 |
|
| 1449 |
// Step 9.a. Let requiredModule be ? HostResolveImportedModule(module, |
| 1450 |
// required). |
| 1451 |
requiredModule = HostResolveImportedModule(cx, module, moduleRequest, |
| 1452 |
ModuleStatus::Unlinked); |
| 1453 |
if (!requiredModule) { |
| 1454 |
return false; |
| 1455 |
} |
| 1456 |
|
| 1457 |
// Step 9.b. Set index to ? InnerModuleLinking(requiredModule, stack, |
| 1458 |
// index). |
| 1459 |
if (!InnerModuleLinking(cx, requiredModule, stack, index, &index)) { |
| 1460 |
return false; |
| 1461 |
} |
| 1462 |
|
| 1463 |
// Step 9.c. If requiredModule is a Cyclic Module Record, then: |
| 1464 |
if (requiredModule->hasCyclicModuleFields()) { |
| 1465 |
// Step 9.c.i. Assert: requiredModule.[[Status]] is either linking, |
| 1466 |
// linked, |
| 1467 |
// evaluating-async, or evaluated. |
| 1468 |
MOZ_ASSERT(requiredModule->status() == ModuleStatus::Linking || |
| 1469 |
requiredModule->status() == ModuleStatus::Linked || |
| 1470 |
requiredModule->status() == ModuleStatus::EvaluatingAsync || |
| 1471 |
requiredModule->status() == ModuleStatus::Evaluated); |
| 1472 |
|
| 1473 |
// Step 9.c.ii. Assert: requiredModule.[[Status]] is linking if and only |
| 1474 |
// if |
| 1475 |
// requiredModule is in stack. |
| 1476 |
MOZ_ASSERT((requiredModule->status() == ModuleStatus::Linking) == |
| 1477 |
ContainsElement(stack, requiredModule)); |
| 1478 |
|
| 1479 |
// Step 9.c.iii. If requiredModule.[[Status]] is linking, then: |
| 1480 |
if (requiredModule->status() == ModuleStatus::Linking) { |
| 1481 |
// Step 9.c.iii.1. Set module.[[DFSAncestorIndex]] to |
| 1482 |
// min(module.[[DFSAncestorIndex]], |
| 1483 |
// requiredModule.[[DFSAncestorIndex]]). |
| 1484 |
module->setDfsAncestorIndex(std::min( |
| 1485 |
module->dfsAncestorIndex(), requiredModule->dfsAncestorIndex())); |
| 1486 |
} |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
// Step 10. Perform ? module.InitializeEnvironment(). |
| 1491 |
if (!ModuleInitializeEnvironment(cx, module)) { |
| 1492 |
return false; |
| 1493 |
} |
| 1494 |
|
| 1495 |
// Step 11. Assert: module occurs exactly once in stack. |
| 1496 |
MOZ_ASSERT(CountElements(stack, module) == 1); |
| 1497 |
|
| 1498 |
// Step 12. Assert: module.[[DFSAncestorIndex]] <= module.[[DFSIndex]]. |
| 1499 |
MOZ_ASSERT(module->dfsAncestorIndex() <= module->dfsIndex()); |
| 1500 |
|
| 1501 |
// Step 13. If module.[[DFSAncestorIndex]] = module.[[DFSIndex]], then |
| 1502 |
if (module->dfsAncestorIndex() == module->dfsIndex()) { |
| 1503 |
// Step 13.a. |
| 1504 |
bool done = false; |
| 1505 |
|
| 1506 |
// Step 13.b. Repeat, while done is false: |
| 1507 |
while (!done) { |
| 1508 |
// Step 13.b.i. Let requiredModule be the last element in stack. |
| 1509 |
// Step 13.b.ii. Remove the last element of stack. |
| 1510 |
requiredModule = stack.popCopy(); |
| 1511 |
|
| 1512 |
// Step 13.b.iv. Set requiredModule.[[Status]] to linked. |
| 1513 |
requiredModule->setStatus(ModuleStatus::Linked); |
| 1514 |
|
| 1515 |
// Step 13.b.v. If requiredModule and module are the same Module Record, |
| 1516 |
// set done to true. |
| 1517 |
done = requiredModule == module; |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
// Step 14. Return index. |
| 1522 |
*indexOut = index; |
| 1523 |
return true; |
| 1524 |
} |
| 1525 |
|
| 1526 |
static bool SyntheticModuleEvaluate(JSContext* cx, |
| 1527 |
Handle<ModuleObject*> moduleArg, |
| 1528 |
MutableHandle<Value> rval) { |
| 1529 |
// Steps 1-12 happen elsewhere in the engine. |
| 1530 |
|
| 1531 |
// Step 13. Let pc be ! NewPromiseCapability(%Promise%). |
| 1532 |
Rooted<PromiseObject*> resultPromise(cx, CreatePromiseObjectForAsync(cx)); |
| 1533 |
if (!resultPromise) { |
| 1534 |
return false; |
| 1535 |
} |
| 1536 |
|
| 1537 |
// Since the only synthetic modules we support are JSON modules, result is |
| 1538 |
// always |undefined|. |
| 1539 |
|
| 1540 |
// Step 14. IfAbruptRejectPromise(result, pc) (Skipped) |
| 1541 |
|
| 1542 |
// 15. Perform ! pc.[[Resolve]](result). |
| 1543 |
if (!AsyncFunctionReturned(cx, resultPromise, JS::UndefinedHandleValue)) { |
| 1544 |
return false; |
| 1545 |
} |
| 1546 |
|
| 1547 |
// 16. Return pc.[[Promise]]. |
| 1548 |
rval.set(ObjectValue(*resultPromise)); |
| 1549 |
return true; |
| 1550 |
} |
| 1551 |
|
| 1552 |
// https://tc39.es/ecma262/#sec-moduleevaluation |
| 1553 |
// ES2023 16.2.1.5.2 Evaluate |
| 1554 |
static bool ModuleEvaluate(JSContext* cx, Handle<ModuleObject*> moduleArg, |
| 1555 |
MutableHandle<Value> result) { |
| 1556 |
Rooted<ModuleObject*> module(cx, moduleArg); |
| 1557 |
|
| 1558 |
// Step 2. Assert: module.[[Status]] is linked, evaluating-async, or |
| 1559 |
// evaluated. |
| 1560 |
ModuleStatus status = module->status(); |
| 1561 |
if (status != ModuleStatus::Linked && |
| 1562 |
status != ModuleStatus::EvaluatingAsync && |
| 1563 |
status != ModuleStatus::Evaluated) { |
| 1564 |
ThrowUnexpectedModuleStatus(cx, status); |
| 1565 |
return false; |
| 1566 |
} |
| 1567 |
|
| 1568 |
// Note: we return early in the error case, as the spec assumes we can get the |
| 1569 |
// cycle root of |module| which may not be available. |
| 1570 |
if (module->hadEvaluationError()) { |
| 1571 |
Rooted<PromiseObject*> capability(cx); |
| 1572 |
if (!module->hasTopLevelCapability()) { |
| 1573 |
capability = ModuleObject::createTopLevelCapability(cx, module); |
| 1574 |
if (!capability) { |
| 1575 |
return false; |
| 1576 |
} |
| 1577 |
|
| 1578 |
Rooted<Value> error(cx, module->evaluationError()); |
| 1579 |
if (!ModuleObject::topLevelCapabilityReject(cx, module, error)) { |
| 1580 |
return false; |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
capability = module->topLevelCapability(); |
| 1585 |
MOZ_ASSERT(JS::GetPromiseState(capability) == JS::PromiseState::Rejected); |
| 1586 |
MOZ_ASSERT(JS::GetPromiseResult(capability) == module->evaluationError()); |
| 1587 |
result.set(ObjectValue(*capability)); |
| 1588 |
return true; |
| 1589 |
} |
| 1590 |
|
| 1591 |
// Step 3. If module.[[Status]] is evaluating-async or evaluated, set module |
| 1592 |
// to module.[[CycleRoot]]. |
| 1593 |
if (module->status() == ModuleStatus::EvaluatingAsync || |
| 1594 |
module->status() == ModuleStatus::Evaluated) { |
| 1595 |
module = module->getCycleRoot(); |
| 1596 |
} |
| 1597 |
|
| 1598 |
// Step 4. If module.[[TopLevelCapability]] is not empty, then: |
| 1599 |
if (module->hasTopLevelCapability()) { |
| 1600 |
// Step 4.a. Return module.[[TopLevelCapability]].[[Promise]]. |
| 1601 |
result.set(ObjectValue(*module->topLevelCapability())); |
| 1602 |
return true; |
| 1603 |
} |
| 1604 |
|
| 1605 |
// Step 5. Let stack be a new empty List. |
| 1606 |
Rooted<ModuleVector> stack(cx); |
| 1607 |
|
| 1608 |
// Step 6. Let capability be ! NewPromiseCapability(%Promise%). |
| 1609 |
// Step 7. Set module.[[TopLevelCapability]] to capability. |
| 1610 |
Rooted<PromiseObject*> capability( |
| 1611 |
cx, ModuleObject::createTopLevelCapability(cx, module)); |
| 1612 |
if (!capability) { |
| 1613 |
return false; |
| 1614 |
} |
| 1615 |
|
| 1616 |
// Step 8. Let result be Completion(InnerModuleEvaluation(module, stack, 0)). |
| 1617 |
size_t ignored; |
| 1618 |
bool ok = InnerModuleEvaluation(cx, module, &stack, 0, &ignored); |
| 1619 |
|
| 1620 |
// Step 9. f result is an abrupt completion, then: |
| 1621 |
if (!ok) { |
| 1622 |
// Attempt to take any pending exception, but make sure we still handle |
| 1623 |
// uncatchable exceptions. |
| 1624 |
Rooted<Value> error(cx); |
| 1625 |
if (cx->isExceptionPending()) { |
| 1626 |
std::ignore = cx->getPendingException(&error); |
| 1627 |
cx->clearPendingException(); |
| 1628 |
} |
| 1629 |
|
| 1630 |
// Step 9.a. For each Cyclic Module Record m of stack, do |
| 1631 |
for (ModuleObject* m : stack) { |
| 1632 |
// Step 9.a.i. Assert: m.[[Status]] is evaluating. |
| 1633 |
MOZ_ASSERT(m->status() == ModuleStatus::Evaluating); |
| 1634 |
|
| 1635 |
// Step 9.a.ii. Set m.[[Status]] to evaluated. |
| 1636 |
// Step 9.a.iii. Set m.[[EvaluationError]] to result. |
| 1637 |
m->setEvaluationError(error); |
| 1638 |
} |
| 1639 |
|
| 1640 |
// Handle OOM when appending to the stack or over-recursion errors. |
| 1641 |
if (stack.empty() && !module->hadEvaluationError()) { |
| 1642 |
module->setEvaluationError(error); |
| 1643 |
} |
| 1644 |
|
| 1645 |
// Step 9.b. Assert: module.[[Status]] is evaluated. |
| 1646 |
MOZ_ASSERT(module->status() == ModuleStatus::Evaluated); |
| 1647 |
|
| 1648 |
// Step 9.c. Assert: module.[[EvaluationError]] is result. |
| 1649 |
MOZ_ASSERT(module->evaluationError() == error); |
| 1650 |
|
| 1651 |
// Step 9.d. Perform ! Call(capability.[[Reject]], undefined, |
| 1652 |
// result.[[Value]]). |
| 1653 |
if (!ModuleObject::topLevelCapabilityReject(cx, module, error)) { |
| 1654 |
return false; |
| 1655 |
} |
| 1656 |
} else { |
| 1657 |
// Step 10. Else: |
| 1658 |
// Step 10.a. Assert: module.[[Status]] is evaluating-async or evaluated. |
| 1659 |
MOZ_ASSERT(module->status() == ModuleStatus::EvaluatingAsync || |
| 1660 |
module->status() == ModuleStatus::Evaluated); |
| 1661 |
|
| 1662 |
// Step 10.b. Assert: module.[[EvaluationError]] is empty. |
| 1663 |
MOZ_ASSERT(!module->hadEvaluationError()); |
| 1664 |
|
| 1665 |
// Step 10.c. If module.[[AsyncEvaluation]] is false, then: |
| 1666 |
if (module->status() == ModuleStatus::Evaluated) { |
| 1667 |
// Step 10.c.ii. Perform ! Call(capability.[[Resolve]], undefined, |
| 1668 |
// undefined). |
| 1669 |
if (!ModuleObject::topLevelCapabilityResolve(cx, module)) { |
| 1670 |
return false; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
// Step 10.d. Assert: stack is empty. |
| 1675 |
MOZ_ASSERT(stack.empty()); |
| 1676 |
} |
| 1677 |
|
| 1678 |
// Step 11. Return capability.[[Promise]]. |
| 1679 |
result.set(ObjectValue(*capability)); |
| 1680 |
return true; |
| 1681 |
} |
| 1682 |
|
| 1683 |
// https://tc39.es/ecma262/#sec-innermoduleevaluation |
| 1684 |
// 16.2.1.5.2.1 InnerModuleEvaluation |
| 1685 |
static bool InnerModuleEvaluation(JSContext* cx, Handle<ModuleObject*> module, |
| 1686 |
MutableHandle<ModuleVector> stack, |
| 1687 |
size_t index, size_t* indexOut) { |
| 1688 |
// Step 1: If module is not a Cyclic Module Record, then |
| 1689 |
if (!module->hasCyclicModuleFields()) { |
| 1690 |
// Step 1.a. Let promise be ! module.Evaluate(). (Skipped) |
| 1691 |
// Step 1.b. Assert: promise.[[PromiseState]] is not pending. (Skipped) |
| 1692 |
// Step 1.c. If promise.[[PromiseState]] is rejected, then (Skipped) |
| 1693 |
// Step 1.c.i Return ThrowCompletion(promise.[[PromiseResult]]). (Skipped) |
| 1694 |
// Step 1.d. Return index. |
| 1695 |
*indexOut = index; |
| 1696 |
return true; |
| 1697 |
} |
| 1698 |
|
| 1699 |
// Step 2. If module.[[Status]] is evaluating-async or evaluated, then: |
| 1700 |
if (module->status() == ModuleStatus::EvaluatingAsync || |
| 1701 |
module->status() == ModuleStatus::Evaluated) { |
| 1702 |
// Step 2.a. If module.[[EvaluationError]] is empty, return index. |
| 1703 |
if (!module->hadEvaluationError()) { |
| 1704 |
*indexOut = index; |
| 1705 |
return true; |
| 1706 |
} |
| 1707 |
|
| 1708 |
// Step 2.b. Otherwise, return ? module.[[EvaluationError]]. |
| 1709 |
Rooted<Value> error(cx, module->evaluationError()); |
| 1710 |
cx->setPendingException(error, ShouldCaptureStack::Maybe); |
| 1711 |
return false; |
| 1712 |
} |
| 1713 |
|
| 1714 |
// Step 3. If module.[[Status]] is evaluating, return index. |
| 1715 |
if (module->status() == ModuleStatus::Evaluating) { |
| 1716 |
*indexOut = index; |
| 1717 |
return true; |
| 1718 |
} |
| 1719 |
|
| 1720 |
// Step 4. Assert: module.[[Status]] is linked. |
| 1721 |
MOZ_ASSERT(module->status() == ModuleStatus::Linked); |
| 1722 |
|
| 1723 |
// Step 10. Append module to stack. |
| 1724 |
// Do this before changing the status so that we can recover on failure. |
| 1725 |
if (!stack.append(module)) { |
| 1726 |
ReportOutOfMemory(cx); |
| 1727 |
return false; |
| 1728 |
} |
| 1729 |
|
| 1730 |
// Step 5. Set module.[[Status]] to evaluating. |
| 1731 |
module->setStatus(ModuleStatus::Evaluating); |
| 1732 |
|
| 1733 |
// Step 6. Set module.[[DFSIndex]] to index. |
| 1734 |
module->setDfsIndex(index); |
| 1735 |
|
| 1736 |
// Step 7. Set module.[[DFSAncestorIndex]] to index. |
| 1737 |
module->setDfsAncestorIndex(index); |
| 1738 |
|
| 1739 |
// Step 8. Set module.[[PendingAsyncDependencies]] to 0. |
| 1740 |
module->setPendingAsyncDependencies(0); |
| 1741 |
|
| 1742 |
// Step 9. Set index to index + 1. |
| 1743 |
index++; |
| 1744 |
|
| 1745 |
// Step 11. For each String required of module.[[RequestedModules]], do: |
| 1746 |
Rooted<ModuleRequestObject*> required(cx); |
| 1747 |
Rooted<ModuleObject*> requiredModule(cx); |
| 1748 |
for (const RequestedModule& request : module->requestedModules()) { |
| 1749 |
required = request.moduleRequest(); |
| 1750 |
|
| 1751 |
// Step 11.a. Let requiredModule be ! HostResolveImportedModule(module, |
| 1752 |
// required). |
| 1753 |
// Step 11.b. NOTE: Link must be completed successfully prior to invoking |
| 1754 |
// this method, so every requested module is guaranteed to |
| 1755 |
// resolve successfully. |
| 1756 |
requiredModule = |
| 1757 |
HostResolveImportedModule(cx, module, required, ModuleStatus::Linked); |
| 1758 |
if (!requiredModule) { |
| 1759 |
return false; |
| 1760 |
} |
| 1761 |
|
| 1762 |
// Step 11.c. Set index to ? InnerModuleEvaluation(requiredModule, stack, |
| 1763 |
// index). |
| 1764 |
if (!InnerModuleEvaluation(cx, requiredModule, stack, index, &index)) { |
| 1765 |
return false; |
| 1766 |
} |
| 1767 |
|
| 1768 |
// Step 11.d. If requiredModule is a Cyclic Module Record, then: |
| 1769 |
if (requiredModule->hasCyclicModuleFields()) { |
| 1770 |
// Step 11.d.i. Assert: requiredModule.[[Status]] is either evaluating, |
| 1771 |
// evaluating-async, or evaluated. |
| 1772 |
MOZ_ASSERT(requiredModule->status() == ModuleStatus::Evaluating || |
| 1773 |
requiredModule->status() == ModuleStatus::EvaluatingAsync || |
| 1774 |
requiredModule->status() == ModuleStatus::Evaluated); |
| 1775 |
|
| 1776 |
// Step 11.d.ii. Assert: requiredModule.[[Status]] is evaluating if and |
| 1777 |
// only if requiredModule is in stack. |
| 1778 |
MOZ_ASSERT((requiredModule->status() == ModuleStatus::Evaluating) == |
| 1779 |
ContainsElement(stack, requiredModule)); |
| 1780 |
|
| 1781 |
// Step 11.d.iii. If requiredModule.[[Status]] is evaluating, then: |
| 1782 |
if (requiredModule->status() == ModuleStatus::Evaluating) { |
| 1783 |
// Step 11.d.iii.1. Set module.[[DFSAncestorIndex]] to |
| 1784 |
// min(module.[[DFSAncestorIndex]], |
| 1785 |
// requiredModule.[[DFSAncestorIndex]]). |
| 1786 |
module->setDfsAncestorIndex(std::min( |
| 1787 |
module->dfsAncestorIndex(), requiredModule->dfsAncestorIndex())); |
| 1788 |
} else { |
| 1789 |
// Step 11.d.iv. Else: |
| 1790 |
// Step 11.d.iv.1. Set requiredModule to requiredModule.[[CycleRoot]]. |
| 1791 |
requiredModule = requiredModule->getCycleRoot(); |
| 1792 |
|
| 1793 |
// Step 11.d.iv.2. Assert: requiredModule.[[Status]] is evaluating-async |
| 1794 |
// or evaluated. |
| 1795 |
MOZ_ASSERT(requiredModule->status() >= ModuleStatus::EvaluatingAsync || |
| 1796 |
requiredModule->status() == ModuleStatus::Evaluated); |
| 1797 |
|
| 1798 |
// Step 11.d.iv.3. If requiredModule.[[EvaluationError]] is not empty, |
| 1799 |
// return ? requiredModule.[[EvaluationError]]. |
| 1800 |
if (requiredModule->hadEvaluationError()) { |
| 1801 |
Rooted<Value> error(cx, requiredModule->evaluationError()); |
| 1802 |
cx->setPendingException(error, ShouldCaptureStack::Maybe); |
| 1803 |
return false; |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
// Step 11.d.v. If requiredModule.[[AsyncEvaluation]] is true, then: |
| 1808 |
if (requiredModule->isAsyncEvaluating() && |
| 1809 |
requiredModule->status() != ModuleStatus::Evaluated) { |
| 1810 |
// Step 11.d.v.2. Append module to |
| 1811 |
// requiredModule.[[AsyncParentModules]]. |
| 1812 |
if (!ModuleObject::appendAsyncParentModule(cx, requiredModule, |
| 1813 |
module)) { |
| 1814 |
return false; |
| 1815 |
} |
| 1816 |
|
| 1817 |
// Step 11.d.v.1. Set module.[[PendingAsyncDependencies]] to |
| 1818 |
// module.[[PendingAsyncDependencies]] + 1. |
| 1819 |
module->setPendingAsyncDependencies(module->pendingAsyncDependencies() + |
| 1820 |
1); |
| 1821 |
} |
| 1822 |
} |
| 1823 |
} |
| 1824 |
|
| 1825 |
// Step 12. If module.[[PendingAsyncDependencies]] > 0 or module.[[HasTLA]] is |
| 1826 |
// true, then: |
| 1827 |
if (module->pendingAsyncDependencies() > 0 || module->hasTopLevelAwait()) { |
| 1828 |
// Step 12.a. Assert: module.[[AsyncEvaluation]] is false and was never |
| 1829 |
// previously set to true. |
| 1830 |
MOZ_ASSERT(!module->isAsyncEvaluating()); |
| 1831 |
|
| 1832 |
// Step 12.b. Set module.[[AsyncEvaluation]] to true. |
| 1833 |
// Step 12.c. NOTE: The order in which module records have their |
| 1834 |
// [[AsyncEvaluation]] fields transition to true is |
| 1835 |
// significant. (See 16.2.1.5.2.4.) |
| 1836 |
module->setAsyncEvaluating(); |
| 1837 |
|
| 1838 |
// Step 12.d. If module.[[PendingAsyncDependencies]] is 0, perform |
| 1839 |
// ExecuteAsyncModule(module). |
| 1840 |
if (module->pendingAsyncDependencies() == 0) { |
| 1841 |
if (!ExecuteAsyncModule(cx, module)) { |
| 1842 |
return false; |
| 1843 |
} |
| 1844 |
} |
| 1845 |
} else { |
| 1846 |
// Step 13. Otherwise, perform ? module.ExecuteModule(). |
| 1847 |
if (!ModuleObject::execute(cx, module)) { |
| 1848 |
return false; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
|
| 1852 |
// Step 14. Assert: module occurs exactly once in stack. |
| 1853 |
MOZ_ASSERT(CountElements(stack, module) == 1); |
| 1854 |
|
| 1855 |
// Step 15. Assert: module.[[DFSAncestorIndex]] <= module.[[DFSIndex]]. |
| 1856 |
MOZ_ASSERT(module->dfsAncestorIndex() <= module->dfsIndex()); |
| 1857 |
|
| 1858 |
// Step 16. If module.[[DFSAncestorIndex]] = module.[[DFSIndex]], then: |
| 1859 |
if (module->dfsAncestorIndex() == module->dfsIndex()) { |
| 1860 |
// Step 16.a. Let done be false. |
| 1861 |
bool done = false; |
| 1862 |
|
| 1863 |
// Step 16.b. Repeat, while done is false: |
| 1864 |
while (!done) { |
| 1865 |
// Step 16.b.i. Let requiredModule be the last element in stack. |
| 1866 |
// Step 16.b.ii. Remove the last element of stack. |
| 1867 |
requiredModule = stack.popCopy(); |
| 1868 |
|
| 1869 |
// Step 16.b.iv. If requiredModule.[[AsyncEvaluation]] is false, set |
| 1870 |
// requiredModule.[[Status]] to evaluated. |
| 1871 |
if (!requiredModule->isAsyncEvaluating()) { |
| 1872 |
requiredModule->setStatus(ModuleStatus::Evaluated); |
| 1873 |
} else { |
| 1874 |
// Step 16.b.v. Otherwise, set requiredModule.[[Status]] to |
| 1875 |
// evaluating-async. |
| 1876 |
requiredModule->setStatus(ModuleStatus::EvaluatingAsync); |
| 1877 |
} |
| 1878 |
|
| 1879 |
// Step 16.b.vi. If requiredModule and module are the same Module Record, |
| 1880 |
// set done to true. |
| 1881 |
done = requiredModule == module; |
| 1882 |
|
| 1883 |
// Step 16.b.vii. Set requiredModule.[[CycleRoot]] to module. |
| 1884 |
requiredModule->setCycleRoot(module); |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
// Step 17. Return index. |
| 1889 |
*indexOut = index; |
| 1890 |
return true; |
| 1891 |
} |
| 1892 |
|
| 1893 |
// https://tc39.es/ecma262/#sec-execute-async-module |
| 1894 |
// ES2023 16.2.1.5.2.2 ExecuteAsyncModule |
| 1895 |
static bool ExecuteAsyncModule(JSContext* cx, Handle<ModuleObject*> module) { |
| 1896 |
// Step 1. Assert: module.[[Status]] is evaluating or evaluating-async. |
| 1897 |
MOZ_ASSERT(module->status() == ModuleStatus::Evaluating || |
| 1898 |
module->status() == ModuleStatus::EvaluatingAsync); |
| 1899 |
|
| 1900 |
// Step 2. Assert: module.[[HasTLA]] is true. |
| 1901 |
MOZ_ASSERT(module->hasTopLevelAwait()); |
| 1902 |
|
| 1903 |
// Steps 3 - 8 are performed by the AsyncAwait opcode. |
| 1904 |
|
| 1905 |
// Step 9. Perform ! module.ExecuteModule(capability). |
| 1906 |
// Step 10. Return unused. |
| 1907 |
return ModuleObject::execute(cx, module); |
| 1908 |
} |
| 1909 |
|
| 1910 |
// https://tc39.es/ecma262/#sec-gather-available-ancestors |
| 1911 |
// ES2023 16.2.1.5.2.3 GatherAvailableAncestors |
| 1912 |
static bool GatherAvailableModuleAncestors( |
| 1913 |
JSContext* cx, Handle<ModuleObject*> module, |
| 1914 |
MutableHandle<ModuleVector> execList) { |
| 1915 |
MOZ_ASSERT(module->status() == ModuleStatus::EvaluatingAsync); |
| 1916 |
|
| 1917 |
// Step 1. For each Cyclic Module Record m of module.[[AsyncParentModules]], |
| 1918 |
// do: |
| 1919 |
Rooted<ListObject*> asyncParentModules(cx, module->asyncParentModules()); |
| 1920 |
Rooted<ModuleObject*> m(cx); |
| 1921 |
for (uint32_t i = 0; i != asyncParentModules->length(); i++) { |
| 1922 |
m = &asyncParentModules->getDenseElement(i).toObject().as<ModuleObject>(); |
| 1923 |
|
| 1924 |
// Step 1.a. If execList does not contain m and |
| 1925 |
// m.[[CycleRoot]].[[EvaluationError]] is empty, then: |
| 1926 |
// |
| 1927 |
// Note: we also check whether m.[[EvaluationError]] is empty since an error |
| 1928 |
// in synchronous execution can prevent the CycleRoot field from being set. |
| 1929 |
if (!m->hadEvaluationError() && !m->getCycleRoot()->hadEvaluationError() && |
| 1930 |
!ContainsElement(execList, m)) { |
| 1931 |
// Step 1.a.i. Assert: m.[[Status]] is evaluating-async. |
| 1932 |
MOZ_ASSERT(m->status() == ModuleStatus::EvaluatingAsync); |
| 1933 |
|
| 1934 |
// Step 1.a.ii. Assert: m.[[EvaluationError]] is empty. |
| 1935 |
MOZ_ASSERT(!m->hadEvaluationError()); |
| 1936 |
|
| 1937 |
// Step 1.a.iii. Assert: m.[[AsyncEvaluation]] is true. |
| 1938 |
MOZ_ASSERT(m->isAsyncEvaluating()); |
| 1939 |
|
| 1940 |
// Step 1.a.iv. Assert: m.[[PendingAsyncDependencies]] > 0. |
| 1941 |
MOZ_ASSERT(m->pendingAsyncDependencies() > 0); |
| 1942 |
|
| 1943 |
// Step 1.a.v. Set m.[[PendingAsyncDependencies]] to |
| 1944 |
// m.[[PendingAsyncDependencies]] - 1. |
| 1945 |
m->setPendingAsyncDependencies(m->pendingAsyncDependencies() - 1); |
| 1946 |
|
| 1947 |
// Step 1.a.vi. If m.[[PendingAsyncDependencies]] = 0, then: |
| 1948 |
if (m->pendingAsyncDependencies() == 0) { |
| 1949 |
// Step 1.a.vi.1. Append m to execList. |
| 1950 |
if (!execList.append(m)) { |
| 1951 |
return false; |
| 1952 |
} |
| 1953 |
|
| 1954 |
// Step 1.a.vi.2. If m.[[HasTLA]] is false, perform |
| 1955 |
// GatherAvailableAncestors(m, execList). |
| 1956 |
if (!m->hasTopLevelAwait() && |
| 1957 |
!GatherAvailableModuleAncestors(cx, m, execList)) { |
| 1958 |
return false; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
} |
| 1962 |
} |
| 1963 |
|
| 1964 |
// Step 2. Return unused. |
| 1965 |
return true; |
| 1966 |
} |
| 1967 |
|
| 1968 |
struct EvalOrderComparator { |
| 1969 |
bool operator()(ModuleObject* a, ModuleObject* b, bool* lessOrEqualp) { |
| 1970 |
int32_t result = int32_t(a->getAsyncEvaluatingPostOrder()) - |
| 1971 |
int32_t(b->getAsyncEvaluatingPostOrder()); |
| 1972 |
*lessOrEqualp = (result <= 0); |
| 1973 |
return true; |
| 1974 |
} |
| 1975 |
}; |
| 1976 |
|
| 1977 |
static void RejectExecutionWithPendingException(JSContext* cx, |
| 1978 |
Handle<ModuleObject*> module) { |
| 1979 |
// If there is no exception pending then we have been interrupted or have |
| 1980 |
// OOM'd and all bets are off. We reject the execution by throwing |
| 1981 |
// undefined. Not much more we can do. |
| 1982 |
RootedValue exception(cx); |
| 1983 |
if (cx->isExceptionPending()) { |
| 1984 |
std::ignore = cx->getPendingException(&exception); |
| 1985 |
} |
| 1986 |
cx->clearPendingException(); |
| 1987 |
if (!AsyncModuleExecutionRejected(cx, module, exception)) { |
| 1988 |
MOZ_ASSERT(cx->isThrowingOverRecursed()); |
| 1989 |
} |
| 1990 |
} |
| 1991 |
|
| 1992 |
// https://tc39.es/ecma262/#sec-async-module-execution-fulfilled |
| 1993 |
// ES2023 16.2.1.5.2.4 AsyncModuleExecutionFulfilled |
| 1994 |
void js::AsyncModuleExecutionFulfilled(JSContext* cx, |
| 1995 |
Handle<ModuleObject*> module) { |
| 1996 |
// Step 1. If module.[[Status]] is evaluated, then: |
| 1997 |
if (module->status() == ModuleStatus::Evaluated) { |
| 1998 |
// Step 1.a. Assert: module.[[EvaluationError]] is not empty. |
| 1999 |
MOZ_ASSERT(module->hadEvaluationError()); |
| 2000 |
|
| 2001 |
// Step 1.b. Return unused. |
| 2002 |
return; |
| 2003 |
} |
| 2004 |
|
| 2005 |
// Step 2. Assert: module.[[Status]] is evaluating-async. |
| 2006 |
MOZ_ASSERT(module->status() == ModuleStatus::EvaluatingAsync); |
| 2007 |
|
| 2008 |
// Step 3. Assert: module.[[AsyncEvaluation]] is true. |
| 2009 |
MOZ_ASSERT(module->isAsyncEvaluating()); |
| 2010 |
|
| 2011 |
// Step 4. Assert: module.[[EvaluationError]] is empty. |
| 2012 |
MOZ_ASSERT(!module->hadEvaluationError()); |
| 2013 |
|
| 2014 |
// The following steps are performed in a different order from the |
| 2015 |
// spec. Gather available module ancestors before mutating the module object |
| 2016 |
// as this can fail in our implementation. |
| 2017 |
|
| 2018 |
// Step 8. Let execList be a new empty List. |
| 2019 |
Rooted<ModuleVector> execList(cx); |
| 2020 |
|
| 2021 |
// Step 9. Perform GatherAvailableAncestors(module, execList). |
| 2022 |
if (!GatherAvailableModuleAncestors(cx, module, &execList)) { |
| 2023 |
RejectExecutionWithPendingException(cx, module); |
| 2024 |
return; |
| 2025 |
} |
| 2026 |
|
| 2027 |
// Step 10. Let sortedExecList be a List whose elements are the elements of |
| 2028 |
// execList, in the order in which they had their [[AsyncEvaluation]] |
| 2029 |
// fields set to true in InnerModuleEvaluation. |
| 2030 |
|
| 2031 |
Rooted<ModuleVector> scratch(cx); |
| 2032 |
if (!scratch.resize(execList.length())) { |
| 2033 |
ReportOutOfMemory(cx); |
| 2034 |
RejectExecutionWithPendingException(cx, module); |
| 2035 |
return; |
| 2036 |
} |
| 2037 |
|
| 2038 |
MOZ_ALWAYS_TRUE(MergeSort(execList.begin(), execList.length(), |
| 2039 |
scratch.begin(), EvalOrderComparator())); |
| 2040 |
|
| 2041 |
// Step 11. Assert: All elements of sortedExecList have their |
| 2042 |
// [[AsyncEvaluation]] field set to true, |
| 2043 |
// [[PendingAsyncDependencies]] field set to 0, and |
| 2044 |
// [[EvaluationError]] field set to empty. |
| 2045 |
#ifdef DEBUG |
| 2046 |
for (ModuleObject* m : execList) { |
| 2047 |
MOZ_ASSERT(m->isAsyncEvaluating()); |
| 2048 |
MOZ_ASSERT(m->pendingAsyncDependencies() == 0); |
| 2049 |
MOZ_ASSERT(!m->hadEvaluationError()); |
| 2050 |
} |
| 2051 |
#endif |
| 2052 |
|
| 2053 |
// Return to original order of steps. |
| 2054 |
|
| 2055 |
ModuleObject::onTopLevelEvaluationFinished(module); |
| 2056 |
|
| 2057 |
// Step 6. Set module.[[Status]] to evaluated. |
| 2058 |
module->setStatus(ModuleStatus::Evaluated); |
| 2059 |
module->clearAsyncEvaluatingPostOrder(); |
| 2060 |
|
| 2061 |
// Step 7. If module.[[TopLevelCapability]] is not empty, then: |
| 2062 |
if (module->hasTopLevelCapability()) { |
| 2063 |
// Step 7.a. Assert: module.[[CycleRoot]] is module. |
| 2064 |
MOZ_ASSERT(module->getCycleRoot() == module); |
| 2065 |
|
| 2066 |
// Step 7.b. Perform ! Call(module.[[TopLevelCapability]].[[Resolve]], |
| 2067 |
// undefined, undefined). |
| 2068 |
if (!ModuleObject::topLevelCapabilityResolve(cx, module)) { |
| 2069 |
// If Resolve fails, there's nothing more we can do here. |
| 2070 |
cx->clearPendingException(); |
| 2071 |
} |
| 2072 |
} |
| 2073 |
|
| 2074 |
// Step 12. For each Cyclic Module Record m of sortedExecList, do: |
| 2075 |
Rooted<ModuleObject*> m(cx); |
| 2076 |
for (ModuleObject* obj : execList) { |
| 2077 |
m = obj; |
| 2078 |
|
| 2079 |
// Step 12.a. If m.[[Status]] is evaluated, then: |
| 2080 |
if (m->status() == ModuleStatus::Evaluated) { |
| 2081 |
// Step 12.a.i. Assert: m.[[EvaluationError]] is not empty. |
| 2082 |
MOZ_ASSERT(m->hadEvaluationError()); |
| 2083 |
} else if (m->hasTopLevelAwait()) { |
| 2084 |
// Step 12.b. Else if m.[[HasTLA]] is true, then: |
| 2085 |
// Step 12.b.i. Perform ExecuteAsyncModule(m). |
| 2086 |
if (!ExecuteAsyncModule(cx, m)) { |
| 2087 |
MOZ_ASSERT(cx->isThrowingOutOfMemory() || cx->isThrowingOverRecursed()); |
| 2088 |
cx->clearPendingException(); |
| 2089 |
} |
| 2090 |
} else { |
| 2091 |
// Step 12.c. Else: |
| 2092 |
// Step 12.c.i. Let result be m.ExecuteModule(). |
| 2093 |
bool ok = ModuleObject::execute(cx, m); |
| 2094 |
|
| 2095 |
// Step 12.c.ii. If result is an abrupt completion, then: |
| 2096 |
if (!ok) { |
| 2097 |
// Step 12.c.ii.1. Perform AsyncModuleExecutionRejected(m, |
| 2098 |
// result.[[Value]]). |
| 2099 |
RejectExecutionWithPendingException(cx, m); |
| 2100 |
} else { |
| 2101 |
// Step 12.c.iii. Else: |
| 2102 |
// Step 12.c.iii.1. Set m.[[Status]] to evaluated. |
| 2103 |
m->setStatus(ModuleStatus::Evaluated); |
| 2104 |
m->clearAsyncEvaluatingPostOrder(); |
| 2105 |
|
| 2106 |
// Step 12.c.iii.2. If m.[[TopLevelCapability]] is not empty, then: |
| 2107 |
if (m->hasTopLevelCapability()) { |
| 2108 |
// Step 12.c.iii.2.a. Assert: m.[[CycleRoot]] is m. |
| 2109 |
MOZ_ASSERT(m->getCycleRoot() == m); |
| 2110 |
|
| 2111 |
// Step 12.c.iii.2.b. Perform ! |
| 2112 |
// Call(m.[[TopLevelCapability]].[[Resolve]], |
| 2113 |
// undefined, undefined). |
| 2114 |
if (!ModuleObject::topLevelCapabilityResolve(cx, m)) { |
| 2115 |
// If Resolve fails, there's nothing more we can do here. |
| 2116 |
cx->clearPendingException(); |
| 2117 |
} |
| 2118 |
} |
| 2119 |
} |
| 2120 |
} |
| 2121 |
} |
| 2122 |
|
| 2123 |
// Step 13. Return unused. |
| 2124 |
} |
| 2125 |
|
| 2126 |
// https://tc39.es/ecma262/#sec-async-module-execution-rejected |
| 2127 |
// ES2023 16.2.1.5.2.5 AsyncModuleExecutionRejected |
| 2128 |
bool js::AsyncModuleExecutionRejected(JSContext* cx, |
| 2129 |
Handle<ModuleObject*> module, |
| 2130 |
HandleValue error) { |
| 2131 |
AutoCheckRecursionLimit recursion(cx); |
| 2132 |
if (!recursion.check(cx)) { |
| 2133 |
return false; |
| 2134 |
} |
| 2135 |
|
| 2136 |
// Step 1. If module.[[Status]] is evaluated, then: |
| 2137 |
if (module->status() == ModuleStatus::Evaluated) { |
| 2138 |
// Step 1.a. Assert: module.[[EvaluationError]] is not empty |
| 2139 |
MOZ_ASSERT(module->hadEvaluationError()); |
| 2140 |
|
| 2141 |
// Step 1.b. Return unused. |
| 2142 |
return true; |
| 2143 |
} |
| 2144 |
|
| 2145 |
// Step 2. Assert: module.[[Status]] is evaluating-async. |
| 2146 |
MOZ_ASSERT(module->status() == ModuleStatus::EvaluatingAsync); |
| 2147 |
|
| 2148 |
// Step 3. Assert: module.[[AsyncEvaluationOrder]] is an integer. |
| 2149 |
MOZ_ASSERT(module->isAsyncEvaluating()); |
| 2150 |
|
| 2151 |
// Step 4. Assert: module.[[EvaluationError]] is empty. |
| 2152 |
MOZ_ASSERT(!module->hadEvaluationError()); |
| 2153 |
|
| 2154 |
ModuleObject::onTopLevelEvaluationFinished(module); |
| 2155 |
|
| 2156 |
// Step 5. Set module.[[EvaluationError]] to ThrowCompletion(error). |
| 2157 |
module->setEvaluationError(error); |
| 2158 |
|
| 2159 |
// Step 6. Set module.[[Status]] to evaluated. |
| 2160 |
MOZ_ASSERT(module->status() == ModuleStatus::Evaluated); |
| 2161 |
|
| 2162 |
// Step 7. Set module.[[AsyncEvaluationOrder]] to done. |
| 2163 |
module->clearAsyncEvaluatingPostOrder(); |
| 2164 |
|
| 2165 |
// Step 9. If module.[[TopLevelCapability]] is not empty, then: |
| 2166 |
if (module->hasTopLevelCapability()) { |
| 2167 |
// Step 9.a. Assert: module.[[CycleRoot]] is module. |
| 2168 |
MOZ_ASSERT(module->getCycleRoot() == module); |
| 2169 |
|
| 2170 |
// Step 9.b. Perform ! Call(module.[[TopLevelCapability]].[[Reject]], |
| 2171 |
// undefined, error). |
| 2172 |
if (!ModuleObject::topLevelCapabilityReject(cx, module, error)) { |
| 2173 |
// If Reject fails, there's nothing more we can do here. |
| 2174 |
cx->clearPendingException(); |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
// Step 10. For each Cyclic Module Record m of module.[[AsyncParentModules]], |
| 2179 |
// do: |
| 2180 |
Rooted<ListObject*> parents(cx, module->asyncParentModules()); |
| 2181 |
Rooted<ModuleObject*> parent(cx); |
| 2182 |
for (uint32_t i = 0; i < parents->length(); i++) { |
| 2183 |
parent = &parents->get(i).toObject().as<ModuleObject>(); |
| 2184 |
|
| 2185 |
// Step 10.a. Perform AsyncModuleExecutionRejected(m, error). |
| 2186 |
if (!AsyncModuleExecutionRejected(cx, parent, error)) { |
| 2187 |
return false; |
| 2188 |
} |
| 2189 |
} |
| 2190 |
|
| 2191 |
// Step 11. Return unused. |
| 2192 |
return true; |
| 2193 |
} |