Skip to content

Commit

Permalink
Bug 1800641 - Part 9: Add LoadedScript::mURI and call SetBaseURL sepa…
Browse files Browse the repository at this point in the history
…rately. r=nbp

Depends on D197846

Differential Revision: https://phabricator.services.mozilla.com/D197847
  • Loading branch information
arai-a committed Jan 8, 2024
1 parent 976c794 commit bdc574a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
3 changes: 2 additions & 1 deletion dom/script/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,8 @@ nsresult ScriptLoader::EvaluateScript(nsIGlobalObject* aGlobalObject,

// Create a ClassicScript object and associate it with the JSScript.
RefPtr<ClassicScript> classicScript = new ClassicScript(
aRequest->ReferrerPolicy(), aRequest->mFetchOptions, aRequest->mBaseURL);
aRequest->ReferrerPolicy(), aRequest->mFetchOptions, aRequest->mURI);
classicScript->SetBaseURL(aRequest->mBaseURL);
JS::Rooted<JS::Value> classicScriptValue(cx, JS::PrivateValue(classicScript));

JS::CompileOptions options(cx);
Expand Down
3 changes: 2 additions & 1 deletion dom/workers/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,8 @@ bool WorkerScriptLoader::EvaluateScript(JSContext* aCx,
requestBaseURI = aRequest->mBaseURL;
}
classicScript = new JS::loader::ClassicScript(
aRequest->ReferrerPolicy(), aRequest->mFetchOptions, requestBaseURI);
aRequest->ReferrerPolicy(), aRequest->mFetchOptions, aRequest->mURI);
classicScript->SetBaseURL(requestBaseURI);
}

JS::Rooted<JSScript*> script(aCx);
Expand Down
30 changes: 17 additions & 13 deletions js/loader/LoadedScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(LoadedScript)

LoadedScript::LoadedScript(ScriptKind aKind,
mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
ScriptFetchOptions* aFetchOptions, nsIURI* aURI)
: mKind(aKind),
mReferrerPolicy(aReferrerPolicy),
mFetchOptions(aFetchOptions),
mBaseURL(aBaseURL) {
mURI(aURI) {
MOZ_ASSERT(mFetchOptions);
MOZ_ASSERT(mBaseURL);
MOZ_ASSERT(mURI);
}

LoadedScript::~LoadedScript() { mozilla::DropJSObjects(this); }

void LoadedScript::AssociateWithScript(JSScript* aScript) {
// Verify that the rewritten URL is available when manipulating LoadedScript.
MOZ_ASSERT(mBaseURL);

// Set a JSScript's private value to point to this object. The JS engine will
// increment our reference count by calling HostAddRefTopLevelScript(). This
// is decremented by HostReleaseTopLevelScript() below when the JSScript dies.
Expand Down Expand Up @@ -92,19 +95,21 @@ void HostReleaseTopLevelScript(const JS::Value& aPrivate) {
//////////////////////////////////////////////////////////////

EventScript::EventScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eEvent, aReferrerPolicy, aFetchOptions,
aBaseURL) {}
ScriptFetchOptions* aFetchOptions, nsIURI* aURI)
: LoadedScript(ScriptKind::eEvent, aReferrerPolicy, aFetchOptions, aURI) {
// EventScripts are not using ScriptLoadRequest, and mBaseURL and mURI are
// the same thing.
SetBaseURL(aURI);
}

//////////////////////////////////////////////////////////////
// ClassicScript
//////////////////////////////////////////////////////////////

ClassicScript::ClassicScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions,
nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eClassic, aReferrerPolicy, aFetchOptions,
aBaseURL) {}
ScriptFetchOptions* aFetchOptions, nsIURI* aURI)
: LoadedScript(ScriptKind::eClassic, aReferrerPolicy, aFetchOptions, aURI) {
}

//////////////////////////////////////////////////////////////
// ModuleScript
Expand All @@ -130,9 +135,8 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(ModuleScript, LoadedScript)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

ModuleScript::ModuleScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eModule, aReferrerPolicy, aFetchOptions,
aBaseURL),
ScriptFetchOptions* aFetchOptions, nsIURI* aURI)
: LoadedScript(ScriptKind::eModule, aReferrerPolicy, aFetchOptions, aURI),
mDebuggerDataInitialized(false) {
MOZ_ASSERT(!ModuleRecord());
MOZ_ASSERT(!HasParseError());
Expand Down
14 changes: 10 additions & 4 deletions js/loader/LoadedScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ class LoadedScript : public nsISupports {
ScriptKind mKind;
const mozilla::dom::ReferrerPolicy mReferrerPolicy;
RefPtr<ScriptFetchOptions> mFetchOptions;
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIURI> mBaseURL;

protected:
LoadedScript(ScriptKind aKind, mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
ScriptFetchOptions* aFetchOptions, nsIURI* aURI);

virtual ~LoadedScript();

Expand All @@ -54,6 +55,11 @@ class LoadedScript : public nsISupports {
return mReferrerPolicy;
}

nsIURI* GetURI() const { return mURI; }
void SetBaseURL(nsIURI* aBaseURL) {
MOZ_ASSERT(!mBaseURL);
mBaseURL = aBaseURL;
}
nsIURI* BaseURL() const { return mBaseURL; }

void AssociateWithScript(JSScript* aScript);
Expand All @@ -64,15 +70,15 @@ class ClassicScript final : public LoadedScript {

public:
ClassicScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
ScriptFetchOptions* aFetchOptions, nsIURI* aURI);
};

class EventScript final : public LoadedScript {
~EventScript() = default;

public:
EventScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
ScriptFetchOptions* aFetchOptions, nsIURI* aURI);
};

// A single module script. May be used to satisfy multiple load requests.
Expand All @@ -91,7 +97,7 @@ class ModuleScript final : public LoadedScript {
LoadedScript)

ModuleScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
ScriptFetchOptions* aFetchOptions, nsIURI* aURI);

void SetModuleRecord(JS::Handle<JSObject*> aModuleRecord);
void SetParseError(const JS::Value& aError);
Expand Down
6 changes: 3 additions & 3 deletions js/loader/ModuleLoaderBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,9 @@ nsresult ModuleLoaderBase::CreateModuleScript(ModuleLoadRequest* aRequest) {
}
}

RefPtr<ModuleScript> moduleScript =
new ModuleScript(aRequest->ReferrerPolicy(), aRequest->mFetchOptions,
aRequest->mBaseURL);
RefPtr<ModuleScript> moduleScript = new ModuleScript(
aRequest->ReferrerPolicy(), aRequest->mFetchOptions, aRequest->mURI);
moduleScript->SetBaseURL(aRequest->mBaseURL);
aRequest->mModuleScript = moduleScript;

if (!module) {
Expand Down

0 comments on commit bdc574a

Please sign in to comment.