From 5ffe27bb03d8ac7b979007721c2699e891c77268 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Wed, 1 Jul 2020 10:15:24 +0000 Subject: Issue #1603 - Part 2: Split some classes out of ScriptLoader.cpp This splits ScriptLoader up the same way Mozilla did with the exception of ScriptRequest due to the fact that ScriptLoader and ScriptRequest are interdependent and would create a circular dependency if split apart when not using unified building. --- dom/script/ModuleLoadRequest.cpp | 102 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 dom/script/ModuleLoadRequest.cpp (limited to 'dom/script/ModuleLoadRequest.cpp') diff --git a/dom/script/ModuleLoadRequest.cpp b/dom/script/ModuleLoadRequest.cpp new file mode 100644 index 000000000..e72edca2e --- /dev/null +++ b/dom/script/ModuleLoadRequest.cpp @@ -0,0 +1,102 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "ModuleLoadRequest.h" +#include "ModuleScript.h" +#include "ScriptLoader.h" + +namespace mozilla { +namespace dom { + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ModuleLoadRequest) +NS_INTERFACE_MAP_END_INHERITING(ScriptLoadRequest) + +NS_IMPL_CYCLE_COLLECTION_INHERITED(ModuleLoadRequest, ScriptLoadRequest, + mBaseURL, + mLoader, + mParent, + mModuleScript, + mImports) + +NS_IMPL_ADDREF_INHERITED(ModuleLoadRequest, ScriptLoadRequest) +NS_IMPL_RELEASE_INHERITED(ModuleLoadRequest, ScriptLoadRequest) + +ModuleLoadRequest::ModuleLoadRequest(nsIScriptElement* aElement, + uint32_t aVersion, + CORSMode aCORSMode, + const SRIMetadata &aIntegrity, + ScriptLoader* aLoader) + : ScriptLoadRequest(ScriptKind::Module, + aElement, + aVersion, + aCORSMode, + aIntegrity), + mIsTopLevel(true), + mLoader(aLoader) +{} + +void ModuleLoadRequest::Cancel() +{ + ScriptLoadRequest::Cancel(); + mModuleScript = nullptr; + mProgress = ScriptLoadRequest::Progress::Ready; + for (size_t i = 0; i < mImports.Length(); i++) { + mImports[i]->Cancel(); + } + mReady.RejectIfExists(NS_ERROR_FAILURE, __func__); +} + +void +ModuleLoadRequest::SetReady() +{ +#ifdef DEBUG + for (size_t i = 0; i < mImports.Length(); i++) { + MOZ_ASSERT(mImports[i]->IsReadyToRun()); + } +#endif + + ScriptLoadRequest::SetReady(); + mReady.ResolveIfExists(true, __func__); +} + +void +ModuleLoadRequest::ModuleLoaded() +{ + // A module that was found to be marked as fetching in the module map has now + // been loaded. + + mModuleScript = mLoader->GetFetchedModule(mURI); + mLoader->StartFetchingModuleDependencies(this); +} + +void +ModuleLoadRequest::DependenciesLoaded() +{ + // The module and all of its dependencies have been successfully fetched and + // compiled. + + if (!mLoader->InstantiateModuleTree(this)) { + LoadFailed(); + return; + } + + SetReady(); + mLoader->ProcessLoadedModuleTree(this); + mLoader = nullptr; + mParent = nullptr; +} + +void +ModuleLoadRequest::LoadFailed() +{ + Cancel(); + mLoader->ProcessLoadedModuleTree(this); + mLoader = nullptr; + mParent = nullptr; +} + +} // dom namespace +} // mozilla namespace \ No newline at end of file -- cgit v1.2.3 From ab0501702637f3448ec16a188a050e5b9f688787 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Wed, 1 Jul 2020 21:12:49 +0000 Subject: Issue #618 - Add clarifying code comments. --- dom/script/ModuleLoadRequest.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'dom/script/ModuleLoadRequest.cpp') diff --git a/dom/script/ModuleLoadRequest.cpp b/dom/script/ModuleLoadRequest.cpp index e72edca2e..98106df1b 100644 --- a/dom/script/ModuleLoadRequest.cpp +++ b/dom/script/ModuleLoadRequest.cpp @@ -52,6 +52,14 @@ void ModuleLoadRequest::Cancel() void ModuleLoadRequest::SetReady() { + // Mark a module as ready to execute. This means that this module and all it + // dependencies have had their source loaded, parsed as a module and the + // modules instantiated. + // + // The mReady promise is used to ensure that when all dependencies of a module + // have become ready, DependenciesLoaded is called on that module + // request. This is set up in StartFetchingModuleDependencies. + #ifdef DEBUG for (size_t i = 0; i < mImports.Length(); i++) { MOZ_ASSERT(mImports[i]->IsReadyToRun()); -- cgit v1.2.3 From 1e14031a5940f6846f6ecce1b3c889b6a3d9b67a Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sat, 4 Jul 2020 10:35:22 +0000 Subject: Issue #618 - Remove eager instantiation This backs out the stuff added in Bug 1295978. Ref: BZ 1295978, 1388728 --- dom/script/ModuleLoadRequest.cpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'dom/script/ModuleLoadRequest.cpp') diff --git a/dom/script/ModuleLoadRequest.cpp b/dom/script/ModuleLoadRequest.cpp index 98106df1b..8871dcdab 100644 --- a/dom/script/ModuleLoadRequest.cpp +++ b/dom/script/ModuleLoadRequest.cpp @@ -86,11 +86,6 @@ ModuleLoadRequest::DependenciesLoaded() // The module and all of its dependencies have been successfully fetched and // compiled. - if (!mLoader->InstantiateModuleTree(this)) { - LoadFailed(); - return; - } - SetReady(); mLoader->ProcessLoadedModuleTree(this); mLoader = nullptr; -- cgit v1.2.3 From f6a6900a6b14d1d54da46370015b28d4d8a152a7 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sat, 4 Jul 2020 16:28:30 +0000 Subject: Issue #618 - Further align error handling for module scripts with the spec Ref: BZ 1388728 --- dom/script/ModuleLoadRequest.cpp | 42 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'dom/script/ModuleLoadRequest.cpp') diff --git a/dom/script/ModuleLoadRequest.cpp b/dom/script/ModuleLoadRequest.cpp index 8871dcdab..d62214304 100644 --- a/dom/script/ModuleLoadRequest.cpp +++ b/dom/script/ModuleLoadRequest.cpp @@ -43,10 +43,16 @@ void ModuleLoadRequest::Cancel() ScriptLoadRequest::Cancel(); mModuleScript = nullptr; mProgress = ScriptLoadRequest::Progress::Ready; + CancelImports(); + mReady.RejectIfExists(NS_ERROR_DOM_ABORT_ERR, __func__); +} + +void +ModuleLoadRequest::CancelImports() +{ for (size_t i = 0; i < mImports.Length(); i++) { mImports[i]->Cancel(); } - mReady.RejectIfExists(NS_ERROR_FAILURE, __func__); } void @@ -77,25 +83,53 @@ ModuleLoadRequest::ModuleLoaded() // been loaded. mModuleScript = mLoader->GetFetchedModule(mURI); + if (!mModuleScript || mModuleScript->IsErrored()) { + ModuleErrored(); + return; + } + mLoader->StartFetchingModuleDependencies(this); } +void +ModuleLoadRequest::ModuleErrored() +{ + mLoader->CheckModuleDependenciesLoaded(this); + MOZ_ASSERT(!mModuleScript || mModuleScript->IsErrored()); + + CancelImports(); + SetReady(); + LoadFinished(); +} + void ModuleLoadRequest::DependenciesLoaded() { // The module and all of its dependencies have been successfully fetched and // compiled. + MOZ_ASSERT(mModuleScript); + + mLoader->CheckModuleDependenciesLoaded(this); SetReady(); - mLoader->ProcessLoadedModuleTree(this); - mLoader = nullptr; - mParent = nullptr; + LoadFinished(); } void ModuleLoadRequest::LoadFailed() { + // We failed to load the source text or an error occurred unrelated to the + // content of the module (e.g. OOM). + + MOZ_ASSERT(!mModuleScript); + Cancel(); + LoadFinished(); +} + +void +ModuleLoadRequest::LoadFinished() +{ mLoader->ProcessLoadedModuleTree(this); mLoader = nullptr; mParent = nullptr; -- cgit v1.2.3