Bug 1432901 - Part 7: Add a load context for use by the new module loader r=yulia

This will be used to hold compilation results before they are passed to the base class.

Differential Revision: https://phabricator.services.mozilla.com/D145561
This commit is contained in:
Jon Coppeard
2022-05-10 12:58:08 +00:00
parent 24c19a18f2
commit 76d2b6c960
6 changed files with 52 additions and 8 deletions

View File

@@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/ScriptLoadContext.h"
#include "mozilla/loader/ComponentModuleLoader.h"
#include "js/loader/LoadContextBase.h"
#include "js/loader/ScriptLoadRequest.h"
@@ -47,4 +48,9 @@ mozilla::dom::ScriptLoadContext* LoadContextBase::AsWindowContext() {
return static_cast<mozilla::dom::ScriptLoadContext*>(this);
}
mozilla::loader::ComponentLoadContext* LoadContextBase::AsComponentContext() {
MOZ_ASSERT(IsComponentContext());
return static_cast<mozilla::loader::ComponentLoadContext*>(this);
}
} // namespace JS::loader

View File

@@ -14,6 +14,10 @@ namespace mozilla::dom {
class ScriptLoadContext;
}
namespace mozilla::loader {
class ComponentLoadContext;
}
namespace JS::loader {
class ScriptLoadRequest;
@@ -34,7 +38,7 @@ class ScriptLoadRequest;
*/
// TODO: implement worker LoadContext
enum class ContextKind { Window };
enum class ContextKind { Window, Component };
class LoadContextBase : public nsISupports {
private:
@@ -44,7 +48,7 @@ class LoadContextBase : public nsISupports {
virtual ~LoadContextBase() = default;
public:
explicit LoadContextBase(ContextKind);
explicit LoadContextBase(ContextKind kind);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(LoadContextBase)
@@ -57,6 +61,9 @@ class LoadContextBase : public nsISupports {
bool IsWindowContext() const { return mKind == ContextKind::Window; }
mozilla::dom::ScriptLoadContext* AsWindowContext();
bool IsComponentContext() const { return mKind == ContextKind::Component; }
mozilla::loader::ComponentLoadContext* AsComponentContext();
RefPtr<JS::loader::ScriptLoadRequest> mRequest;
};

View File

@@ -1037,7 +1037,7 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext(
AUTO_PROFILER_LABEL("ModuleLoaderBase::EvaluateModule", JS);
nsAutoCString profilerLabelString;
if (aRequest->HasLoadContext()) {
if (aRequest->HasScriptLoadContext()) {
aRequest->GetScriptLoadContext()->GetProfilerLabel(profilerLabelString);
}
@@ -1048,7 +1048,7 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext(
ModuleLoadRequest* request = aRequest->AsModuleRequest();
MOZ_ASSERT(request->mModuleScript);
MOZ_ASSERT_IF(request->HasLoadContext(),
MOZ_ASSERT_IF(request->HasScriptLoadContext(),
!request->GetScriptLoadContext()->mOffThreadToken);
ModuleScript* moduleScript = request->mModuleScript;
@@ -1074,7 +1074,7 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext(
nsresult rv = InitDebuggerDataForModuleGraph(aCx, request);
NS_ENSURE_SUCCESS(rv, rv);
if (request->HasLoadContext()) {
if (request->HasScriptLoadContext()) {
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"scriptloader_evaluate_module");
}

View File

@@ -123,11 +123,21 @@ void ScriptLoadRequest::DropBytecodeCacheReferences() {
DropJSObjects(this);
}
bool ScriptLoadRequest::HasScriptLoadContext() const {
return HasLoadContext() && mLoadContext->IsWindowContext();
}
mozilla::dom::ScriptLoadContext* ScriptLoadRequest::GetScriptLoadContext() {
MOZ_ASSERT(mLoadContext);
return mLoadContext->AsWindowContext();
}
mozilla::loader::ComponentLoadContext*
ScriptLoadRequest::GetComponentLoadContext() {
MOZ_ASSERT(mLoadContext);
return mLoadContext->AsComponentContext();
}
ModuleLoadRequest* ScriptLoadRequest::AsModuleRequest() {
MOZ_ASSERT(IsModuleRequest());
return static_cast<ModuleLoadRequest*>(this);

View File

@@ -33,11 +33,13 @@
class nsICacheInfoChannel;
namespace mozilla::dom {
class ScriptLoadContext;
} // namespace mozilla::dom
namespace mozilla::loader {
class ComponentLoadContext;
} // namespace mozilla::loader
namespace JS {
class OffThreadToken;
@@ -294,10 +296,13 @@ class ScriptLoadRequest
void DropBytecodeCacheReferences();
bool HasLoadContext() { return mLoadContext; }
bool HasLoadContext() const { return mLoadContext; }
bool HasScriptLoadContext() const;
mozilla::dom::ScriptLoadContext* GetScriptLoadContext();
mozilla::loader::ComponentLoadContext* GetComponentLoadContext();
const ScriptKind mKind; // Whether this is a classic script or a module
// script.

View File

@@ -7,6 +7,7 @@
#ifndef mozilla_loader_ComponentModuleLoader_h
#define mozilla_loader_ComponentModuleLoader_h
#include "js/loader/LoadContextBase.h"
#include "js/loader/ModuleLoaderBase.h"
namespace mozilla {
@@ -75,6 +76,21 @@ class ComponentModuleLoader : public JS::loader::ModuleLoaderBase {
void OnModuleLoadComplete(ModuleLoadRequest* aRequest) override;
};
// Data specific to ComponentModuleLoader that is associated with each load
// request.
class ComponentLoadContext : public JS::loader::LoadContextBase {
public:
ComponentLoadContext()
: LoadContextBase(JS::loader::ContextKind::Component) {}
public:
// The result of loading a module script. These fields are used temporarily
// before being passed to the module loader.
nsresult mRv;
JS::PersistentRootedValue mExceptionValue;
JS::PersistentRootedScript mScript;
};
} // namespace loader
} // namespace mozilla