2017-05-08 09:24:44 +03:00
|
|
|
/* -*- 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"
|
2018-12-07 00:52:15 +03:00
|
|
|
|
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
#include "ModuleScript.h"
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
#undef LOG
|
|
|
|
#define LOG(args) \
|
|
|
|
MOZ_LOG(ScriptLoader::gScriptLoaderLog, mozilla::LogLevel::Debug, args)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ModuleLoadRequest)
|
2017-05-08 09:24:44 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(ScriptLoadRequest)
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ModuleLoadRequest)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ModuleLoadRequest,
|
|
|
|
ScriptLoadRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBaseURL, mLoader, mModuleScript, mImports)
|
|
|
|
tmp->ClearDynamicImport();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ModuleLoadRequest,
|
|
|
|
ScriptLoadRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBaseURL, mLoader, mModuleScript, mImports)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(ModuleLoadRequest,
|
|
|
|
ScriptLoadRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mDynamicReferencingPrivate)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mDynamicSpecifier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mDynamicPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2017-05-08 09:24:44 +03:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
static VisitedURLSet* NewVisitedSetForTopLevelImport(nsIURI* aURI) {
|
|
|
|
auto set = new VisitedURLSet();
|
|
|
|
set->PutEntry(aURI);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ ModuleLoadRequest* ModuleLoadRequest::CreateTopLevel(
|
|
|
|
nsIURI* aURI, ScriptFetchOptions* aFetchOptions,
|
|
|
|
const SRIMetadata& aIntegrity, nsIURI* aReferrer, ScriptLoader* aLoader) {
|
|
|
|
return new ModuleLoadRequest(aURI, aFetchOptions, aIntegrity, aReferrer,
|
|
|
|
true, /* is top level */
|
|
|
|
false, /* is dynamic import */
|
|
|
|
aLoader, NewVisitedSetForTopLevelImport(aURI));
|
2017-12-15 00:13:57 +03:00
|
|
|
}
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
/* static */ ModuleLoadRequest* ModuleLoadRequest::CreateStaticImport(
|
|
|
|
nsIURI* aURI, ModuleLoadRequest* aParent) {
|
|
|
|
auto request =
|
|
|
|
new ModuleLoadRequest(aURI, aParent->mFetchOptions, SRIMetadata(),
|
|
|
|
aParent->mURI, false, /* is top level */
|
|
|
|
false, /* is dynamic import */
|
|
|
|
aParent->mLoader, aParent->mVisitedSet);
|
|
|
|
|
|
|
|
request->mIsInline = false;
|
|
|
|
request->mScriptMode = aParent->mScriptMode;
|
2017-12-15 00:13:57 +03:00
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
return request;
|
2017-12-15 00:13:57 +03:00
|
|
|
}
|
2017-05-08 09:24:44 +03:00
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
/* static */ ModuleLoadRequest* ModuleLoadRequest::CreateDynamicImport(
|
|
|
|
nsIURI* aURI, ModuleScript* aScript,
|
|
|
|
JS::Handle<JS::Value> aReferencingPrivate, JS::Handle<JSString*> aSpecifier,
|
|
|
|
JS::Handle<JSObject*> aPromise) {
|
|
|
|
MOZ_ASSERT(aSpecifier);
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
|
|
|
|
auto request = new ModuleLoadRequest(
|
|
|
|
aURI, aScript->FetchOptions(), SRIMetadata(), aScript->BaseURL(),
|
|
|
|
true, /* is top level */
|
|
|
|
true, /* is dynamic import */
|
|
|
|
aScript->Loader(), NewVisitedSetForTopLevelImport(aURI));
|
|
|
|
|
|
|
|
request->mIsInline = false;
|
|
|
|
request->mScriptMode = ScriptMode::eAsync;
|
|
|
|
request->mDynamicReferencingPrivate = aReferencingPrivate;
|
|
|
|
request->mDynamicSpecifier = aSpecifier;
|
|
|
|
request->mDynamicPromise = aPromise;
|
|
|
|
|
|
|
|
HoldJSObjects(request);
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleLoadRequest::ModuleLoadRequest(
|
|
|
|
nsIURI* aURI, ScriptFetchOptions* aFetchOptions,
|
|
|
|
const SRIMetadata& aIntegrity, nsIURI* aReferrer, bool aIsTopLevel,
|
|
|
|
bool aIsDynamicImport, ScriptLoader* aLoader, VisitedURLSet* aVisitedSet)
|
|
|
|
: ScriptLoadRequest(ScriptKind::eModule, aURI, aFetchOptions, aIntegrity,
|
|
|
|
aReferrer),
|
|
|
|
mIsTopLevel(aIsTopLevel),
|
|
|
|
mIsDynamicImport(aIsDynamicImport),
|
|
|
|
mLoader(aLoader),
|
|
|
|
mVisitedSet(aVisitedSet) {}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
void ModuleLoadRequest::Cancel() {
|
|
|
|
ScriptLoadRequest::Cancel();
|
|
|
|
mModuleScript = nullptr;
|
2018-01-08 18:17:33 +03:00
|
|
|
mProgress = ScriptLoadRequest::Progress::eReady;
|
2017-08-22 12:34:14 +03:00
|
|
|
CancelImports();
|
|
|
|
mReady.RejectIfExists(NS_ERROR_DOM_ABORT_ERR, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleLoadRequest::CancelImports() {
|
2017-05-08 09:24:44 +03:00
|
|
|
for (size_t i = 0; i < mImports.Length(); i++) {
|
|
|
|
mImports[i]->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleLoadRequest::SetReady() {
|
2017-05-02 20:01:51 +03:00
|
|
|
// 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.
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
#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.
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
LOG(("ScriptLoadRequest (%p): Module loaded", this));
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
mModuleScript = mLoader->GetFetchedModule(mURI);
|
2017-12-06 17:54:58 +03:00
|
|
|
if (!mModuleScript || mModuleScript->HasParseError()) {
|
2017-08-22 12:34:14 +03:00
|
|
|
ModuleErrored();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
mLoader->StartFetchingModuleDependencies(this);
|
|
|
|
}
|
|
|
|
|
2017-08-22 12:34:14 +03:00
|
|
|
void ModuleLoadRequest::ModuleErrored() {
|
|
|
|
LOG(("ScriptLoadRequest (%p): Module errored", this));
|
|
|
|
|
|
|
|
mLoader->CheckModuleDependenciesLoaded(this);
|
2017-12-06 17:54:58 +03:00
|
|
|
MOZ_ASSERT(!mModuleScript || mModuleScript->HasParseError());
|
2017-08-22 12:34:14 +03:00
|
|
|
|
|
|
|
CancelImports();
|
|
|
|
SetReady();
|
|
|
|
LoadFinished();
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
void ModuleLoadRequest::DependenciesLoaded() {
|
|
|
|
// The module and all of its dependencies have been successfully fetched and
|
|
|
|
// compiled.
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
LOG(("ScriptLoadRequest (%p): Module dependencies loaded", this));
|
|
|
|
|
2017-08-22 12:34:14 +03:00
|
|
|
MOZ_ASSERT(mModuleScript);
|
|
|
|
|
|
|
|
mLoader->CheckModuleDependenciesLoaded(this);
|
2017-05-08 09:24:44 +03:00
|
|
|
SetReady();
|
2017-08-22 12:34:14 +03:00
|
|
|
LoadFinished();
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleLoadRequest::LoadFailed() {
|
2017-08-22 12:34:14 +03:00
|
|
|
// We failed to load the source text or an error occurred unrelated to the
|
|
|
|
// content of the module (e.g. OOM).
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
LOG(("ScriptLoadRequest (%p): Module load failed", this));
|
|
|
|
|
2017-08-22 12:34:14 +03:00
|
|
|
MOZ_ASSERT(!mModuleScript);
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
Cancel();
|
2017-08-22 12:34:14 +03:00
|
|
|
LoadFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleLoadRequest::LoadFinished() {
|
2017-05-08 09:24:44 +03:00
|
|
|
mLoader->ProcessLoadedModuleTree(this);
|
2017-08-22 12:34:14 +03:00
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
mLoader = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
void ModuleLoadRequest::ClearDynamicImport() {
|
|
|
|
mDynamicReferencingPrivate = JS::UndefinedValue();
|
|
|
|
mDynamicSpecifier = nullptr;
|
|
|
|
mDynamicPromise = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|