Skip to content

Commit

Permalink
Bug 1475228 - Refactor ScriptLoader::GetScriptSource() to remove inli…
Browse files Browse the repository at this point in the history
…ne data argument r=baku
  • Loading branch information
jonco3 committed Jul 17, 2018
1 parent 173b4c6 commit 1ac45e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
31 changes: 14 additions & 17 deletions dom/script/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ ScriptLoader::CreateModuleScript(ModuleLoadRequest* aRequest)
rv = FillCompileOptionsForRequest(aes, aRequest, global, &options);

if (NS_SUCCEEDED(rv)) {
nsAutoString inlineData;
SourceBufferHolder srcBuf = GetScriptSource(aRequest, inlineData);
SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest);
rv = nsJSUtils::CompileModule(cx, srcBuf, global, options, &module);
}
}
Expand Down Expand Up @@ -1822,9 +1821,7 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest)

if (aRequest->IsModuleRequest()) {
MOZ_ASSERT(aRequest->IsTextSource());
JS::SourceBufferHolder srcBuf(aRequest->ScriptText().begin(),
aRequest->ScriptText().length(),
JS::SourceBufferHolder::NoOwnership);
JS::SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest);
if (!JS::CompileOffThreadModule(cx, options,
srcBuf,
OffThreadScriptLoaderCallback,
Expand Down Expand Up @@ -1852,9 +1849,7 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest)
#endif
} else {
MOZ_ASSERT(aRequest->IsTextSource());
JS::SourceBufferHolder srcBuf(aRequest->ScriptText().begin(),
aRequest->ScriptText().length(),
JS::SourceBufferHolder::NoOwnership);
JS::SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest);
if (!JS::CompileOffThread(cx, options,
srcBuf,
OffThreadScriptLoaderCallback,
Expand Down Expand Up @@ -1892,19 +1887,22 @@ ScriptLoader::CompileOffThreadOrProcessRequest(ScriptLoadRequest* aRequest)
}

SourceBufferHolder
ScriptLoader::GetScriptSource(ScriptLoadRequest* aRequest, nsAutoString& inlineData)
ScriptLoader::GetScriptSource(JSContext* aCx, ScriptLoadRequest* aRequest)
{
// Return a SourceBufferHolder object holding the script's source text.
// |inlineData| is used to hold the text for inline objects.

// If there's no script text, we try to get it from the element
if (aRequest->mIsInline) {
// XXX This is inefficient - GetText makes multiple
// copies.
nsAutoString inlineData;
aRequest->mElement->GetScriptText(inlineData);
return SourceBufferHolder(inlineData.get(),
inlineData.Length(),
SourceBufferHolder::NoOwnership);

// Copy string to JS allocated buffer and transfer ownership to
// SourceBufferHolder result.
size_t nbytes = inlineData.Length() * sizeof(char16_t);
JS::UniqueTwoByteChars chars(static_cast<char16_t*>(JS_malloc(aCx, nbytes)));
MOZ_RELEASE_ASSERT(chars);
memcpy(chars.get(), inlineData.get(), nbytes);
return SourceBufferHolder(std::move(chars), inlineData.Length());
}

return SourceBufferHolder(aRequest->ScriptText().begin(),
Expand Down Expand Up @@ -2354,8 +2352,7 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest)
&script);
} else {
MOZ_ASSERT(aRequest->IsTextSource());
nsAutoString inlineData;
SourceBufferHolder srcBuf = GetScriptSource(aRequest, inlineData);
SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest);
rv = exec.CompileAndExec(options, srcBuf, &script);
}
}
Expand Down
4 changes: 2 additions & 2 deletions dom/script/ScriptLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ class ScriptLoader final : public nsISupports

void MaybeMoveToLoadedList(ScriptLoadRequest* aRequest);

JS::SourceBufferHolder GetScriptSource(ScriptLoadRequest* aRequest,
nsAutoString& inlineData);
JS::SourceBufferHolder GetScriptSource(JSContext* aCx,
ScriptLoadRequest* aRequest);

void SetModuleFetchStarted(ModuleLoadRequest *aRequest);
void SetModuleFetchFinishedAndResumeWaitingRequests(ModuleLoadRequest* aRequest,
Expand Down

0 comments on commit 1ac45e7

Please sign in to comment.