Bug 1800641 - Move some ScriptLoadRequest fields into LoadedScript. r=yulia
We are looking into caching loaded script in memory. To do so we need something to cache. At the moment, the `ScriptLoadRequest` structure hold all the fields which are loaded, and used before executing JavaScript code. Then, the `ScriptLoadRequest` is not guaranteed to out-live the first execution. Therefore, we have to move fields out of the `ScriptLoadRequest` such that they can later be used by any caching mechanism. The `LoadedScript` is the closest existing structure which exists which fit the description. This patch moves fields out of the ScriptLoadRequest into the `LoadedScript`, which already has a `LoadedScript` field. The `LoadedScript` field is initialized sooner, when the `ScriptLoadRequest` is created, to be subsituted later by a real cache implementation. At the moment the function `ScriptLoadRequest::NoCacheEntryFound` is used as a placeholder to change the state of the `ScriptLoadRequest` from `CheckingCache` to `Fetching`. Existing initializations are replaced by assertions to fail in debug build if the current patch does not reproduce the expected state properly. The `LoadedScript` get fields such as the source text, the text length, the bytecode buffer (which also contains SRI), and the offset at which the bytecode starts within the bytecode buffer. As these fields are no longer reachable by name, multiple accessors are added to work-around the issue. Using this as an opportunity to add extra assertions as part of these accessors. A new class named `LoadedScriptDelegate` is added to re-add, by inheritance, all the accessors which used to be part of `ScriptLoadRequest` as methods which are delegating to the field which is holding the `LoadedScript`. This class is using templates to avoid virtual inheritance which might hinder inlining, especially since `ScriptLoadRequest` cannot be made final, as `ModuleLoadRequest` extends it. The `ScriptFetchOptions` structure is moved to its own file to solve C++ include issues. Differential Revision: https://phabricator.services.mozilla.com/D163615
This commit is contained in:
@@ -62,13 +62,15 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(ScriptLoadRequest)
|
||||
NS_IMPL_CYCLE_COLLECTION_CLASS(ScriptLoadRequest)
|
||||
|
||||
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ScriptLoadRequest)
|
||||
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFetchOptions, mCacheInfo, mLoadContext)
|
||||
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFetchOptions, mCacheInfo, mLoadContext,
|
||||
mLoadedScript)
|
||||
tmp->mScriptForBytecodeEncoding = nullptr;
|
||||
tmp->DropBytecodeCacheReferences();
|
||||
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
||||
|
||||
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ScriptLoadRequest)
|
||||
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchOptions, mCacheInfo, mLoadContext)
|
||||
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchOptions, mCacheInfo, mLoadContext,
|
||||
mLoadedScript)
|
||||
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
||||
|
||||
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ScriptLoadRequest)
|
||||
@@ -81,15 +83,12 @@ ScriptLoadRequest::ScriptLoadRequest(
|
||||
ScriptFetchOptions* aFetchOptions, const SRIMetadata& aIntegrity,
|
||||
nsIURI* aReferrer, LoadContextBase* aContext)
|
||||
: mKind(aKind),
|
||||
mState(State::Fetching),
|
||||
mState(State::CheckingCache),
|
||||
mFetchSourceOnly(false),
|
||||
mDataType(DataType::eUnknown),
|
||||
mReferrerPolicy(aReferrerPolicy),
|
||||
mFetchOptions(aFetchOptions),
|
||||
mIntegrity(aIntegrity),
|
||||
mReferrer(aReferrer),
|
||||
mScriptTextLength(0),
|
||||
mBytecodeOffset(0),
|
||||
mURI(aURI),
|
||||
mLoadContext(aContext),
|
||||
mEarlyHintPreloaderId(0) {
|
||||
@@ -157,15 +156,25 @@ const ModuleLoadRequest* ScriptLoadRequest::AsModuleRequest() const {
|
||||
return static_cast<const ModuleLoadRequest*>(this);
|
||||
}
|
||||
|
||||
void ScriptLoadRequest::SetBytecode() {
|
||||
MOZ_ASSERT(IsUnknownDataType());
|
||||
mDataType = DataType::eBytecode;
|
||||
}
|
||||
|
||||
void ScriptLoadRequest::ClearScriptSource() {
|
||||
if (IsTextSource()) {
|
||||
ClearScriptText();
|
||||
void ScriptLoadRequest::NoCacheEntryFound() {
|
||||
MOZ_ASSERT(IsCheckingCache());
|
||||
MOZ_ASSERT(mURI);
|
||||
// At the time where we check in the cache, the mBaseURL is not set, as this
|
||||
// is resolved by the network. Thus we use the mURI, for checking the cache
|
||||
// and later replace the mBaseURL using what the Channel->GetURI will provide.
|
||||
switch (mKind) {
|
||||
case ScriptKind::eClassic:
|
||||
case ScriptKind::eImportMap:
|
||||
mLoadedScript = new ClassicScript(mReferrerPolicy, mFetchOptions, mURI);
|
||||
break;
|
||||
case ScriptKind::eModule:
|
||||
mLoadedScript = new ModuleScript(mReferrerPolicy, mFetchOptions, mURI);
|
||||
break;
|
||||
case ScriptKind::eEvent:
|
||||
MOZ_ASSERT_UNREACHABLE("EventScripts are not using ScriptLoadRequest");
|
||||
break;
|
||||
}
|
||||
mState = State::Fetching;
|
||||
}
|
||||
|
||||
void ScriptLoadRequest::MarkForBytecodeEncoding(JSScript* aScript) {
|
||||
@@ -183,66 +192,6 @@ bool ScriptLoadRequest::IsMarkedForBytecodeEncoding() const {
|
||||
return !!mScriptForBytecodeEncoding;
|
||||
}
|
||||
|
||||
nsresult ScriptLoadRequest::GetScriptSource(JSContext* aCx,
|
||||
MaybeSourceText* aMaybeSource) {
|
||||
// If there's no script text, we try to get it from the element
|
||||
if (HasScriptLoadContext() && GetScriptLoadContext()->mIsInline) {
|
||||
nsAutoString inlineData;
|
||||
GetScriptLoadContext()->GetScriptElement()->GetScriptText(inlineData);
|
||||
|
||||
size_t nbytes = inlineData.Length() * sizeof(char16_t);
|
||||
JS::UniqueTwoByteChars chars(
|
||||
static_cast<char16_t*>(JS_malloc(aCx, nbytes)));
|
||||
if (!chars) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
memcpy(chars.get(), inlineData.get(), nbytes);
|
||||
|
||||
SourceText<char16_t> srcBuf;
|
||||
if (!srcBuf.init(aCx, std::move(chars), inlineData.Length())) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
aMaybeSource->construct<SourceText<char16_t>>(std::move(srcBuf));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
size_t length = ScriptTextLength();
|
||||
if (IsUTF16Text()) {
|
||||
JS::UniqueTwoByteChars chars;
|
||||
chars.reset(ScriptText<char16_t>().extractOrCopyRawBuffer());
|
||||
if (!chars) {
|
||||
JS_ReportOutOfMemory(aCx);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
SourceText<char16_t> srcBuf;
|
||||
if (!srcBuf.init(aCx, std::move(chars), length)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
aMaybeSource->construct<SourceText<char16_t>>(std::move(srcBuf));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(IsUTF8Text());
|
||||
UniquePtr<Utf8Unit[], JS::FreePolicy> chars;
|
||||
chars.reset(ScriptText<Utf8Unit>().extractOrCopyRawBuffer());
|
||||
if (!chars) {
|
||||
JS_ReportOutOfMemory(aCx);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
SourceText<Utf8Unit> srcBuf;
|
||||
if (!srcBuf.init(aCx, std::move(chars), length)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
aMaybeSource->construct<SourceText<Utf8Unit>>(std::move(srcBuf));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
// ScriptLoadRequestList
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user