@@ -491,7 +491,8 @@ ScriptLoader::CreateModuleScript(ModuleLoadRequest* aRequest)
491491 rv = FillCompileOptionsForRequest (aes, aRequest, global, &options);
492492
493493 if (NS_SUCCEEDED (rv)) {
494- SourceBufferHolder srcBuf = GetScriptSource (cx, aRequest);
494+ nsAutoString inlineData;
495+ SourceBufferHolder srcBuf = GetScriptSource (aRequest, inlineData);
495496 rv = nsJSUtils::CompileModule (cx, srcBuf, global, options, &module );
496497 }
497498 }
@@ -1769,16 +1770,14 @@ OffThreadScriptLoaderCallback(JS::OffThreadToken* aToken, void* aCallbackData)
17691770}
17701771
17711772nsresult
1772- ScriptLoader::AttemptAsyncScriptCompile (ScriptLoadRequest* aRequest,
1773- bool * aCouldCompileOut)
1773+ ScriptLoader::AttemptAsyncScriptCompile (ScriptLoadRequest* aRequest)
17741774{
17751775 MOZ_ASSERT_IF (!aRequest->IsModuleRequest (), aRequest->IsReadyToRun ());
17761776 MOZ_ASSERT (!aRequest->mWasCompiledOMT );
1777- MOZ_ASSERT (aCouldCompileOut && !*aCouldCompileOut);
17781777
17791778 // Don't off-thread compile inline scripts.
17801779 if (aRequest->mIsInline ) {
1781- return NS_OK ;
1780+ return NS_ERROR_FAILURE ;
17821781 }
17831782
17841783 nsCOMPtr<nsIScriptGlobalObject> globalObject = GetScriptGlobalObject ();
@@ -1802,19 +1801,19 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
18021801
18031802 if (aRequest->IsTextSource ()) {
18041803 if (!JS::CanCompileOffThread (cx, options, aRequest->ScriptText ().length ())) {
1805- return NS_OK ;
1804+ return NS_ERROR_FAILURE ;
18061805 }
18071806#ifdef JS_BUILD_BINAST
18081807 } else if (aRequest->IsBinASTSource ()) {
18091808 if (!JS::CanDecodeBinASTOffThread (cx, options, aRequest->ScriptBinASTData ().length ())) {
1810- return NS_OK ;
1809+ return NS_ERROR_FAILURE ;
18111810 }
18121811#endif
18131812 } else {
18141813 MOZ_ASSERT (aRequest->IsBytecode ());
18151814 size_t length = aRequest->mScriptBytecode .length () - aRequest->mBytecodeOffset ;
18161815 if (!JS::CanDecodeOffThread (cx, options, length)) {
1817- return NS_OK ;
1816+ return NS_ERROR_FAILURE ;
18181817 }
18191818 }
18201819
@@ -1823,9 +1822,9 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
18231822
18241823 if (aRequest->IsModuleRequest ()) {
18251824 MOZ_ASSERT (aRequest->IsTextSource ());
1826- SourceBufferHolder srcBuf = GetScriptSource (cx, aRequest);
18271825 if (!JS::CompileOffThreadModule (cx, options,
1828- srcBuf,
1826+ aRequest->ScriptText ().begin (),
1827+ aRequest->ScriptText ().length (),
18291828 OffThreadScriptLoaderCallback,
18301829 static_cast <void *>(runnable))) {
18311830 return NS_ERROR_OUT_OF_MEMORY ;
@@ -1851,9 +1850,9 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
18511850#endif
18521851 } else {
18531852 MOZ_ASSERT (aRequest->IsTextSource ());
1854- SourceBufferHolder srcBuf = GetScriptSource (cx, aRequest);
18551853 if (!JS::CompileOffThread (cx, options,
1856- srcBuf,
1854+ aRequest->ScriptText ().begin (),
1855+ aRequest->ScriptText ().length (),
18571856 OffThreadScriptLoaderCallback,
18581857 static_cast <void *>(runnable))) {
18591858 return NS_ERROR_OUT_OF_MEMORY ;
@@ -1866,7 +1865,6 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
18661865 // to call ScriptLoader::ProcessOffThreadRequest with the same request.
18671866 aRequest->mProgress = ScriptLoadRequest::Progress::eCompiling;
18681867
1869- *aCouldCompileOut = true ;
18701868 Unused << runnable.forget ();
18711869 return NS_OK ;
18721870}
@@ -1881,42 +1879,33 @@ ScriptLoader::CompileOffThreadOrProcessRequest(ScriptLoadRequest* aRequest)
18811879 NS_ASSERTION (!aRequest->InCompilingStage (),
18821880 "Candidate for off-thread compile is already in compiling stage.");
18831881
1884- bool couldCompile = false ;
1885- nsresult rv = AttemptAsyncScriptCompile(aRequest, &couldCompile);
1886- if (NS_FAILED (rv)) {
1887- HandleLoadError (aRequest, rv);
1882+ nsresult rv = AttemptAsyncScriptCompile(aRequest);
1883+ if (NS_SUCCEEDED (rv)) {
18881884 return rv;
18891885 }
18901886
1891- if (couldCompile) {
1892- return NS_OK ;
1893- }
1894-
18951887 return ProcessRequest (aRequest);
18961888}
18971889
18981890SourceBufferHolder
1899- ScriptLoader::GetScriptSource (JSContext* aCx, ScriptLoadRequest* aRequest )
1891+ ScriptLoader::GetScriptSource (ScriptLoadRequest* aRequest, nsAutoString& inlineData )
19001892{
19011893 // Return a SourceBufferHolder object holding the script's source text.
1902- // Ownership of the buffer is transferred to the resulting SourceBufferHolder .
1894+ // |inlineData| is used to hold the text for inline objects .
19031895
19041896 // If there's no script text, we try to get it from the element
19051897 if (aRequest->mIsInline ) {
1906- nsAutoString inlineData;
1898+ // XXX This is inefficient - GetText makes multiple
1899+ // copies.
19071900 aRequest->mElement ->GetScriptText (inlineData);
1908-
1909- size_t nbytes = inlineData.Length () * sizeof (char16_t );
1910- JS ::UniqueTwoByteChars chars (static_cast <char16_t *>(JS_malloc (aCx, nbytes)));
1911- MOZ_RELEASE_ASSERT (chars);
1912- memcpy (chars.get (), inlineData.get (), nbytes);
1913- return SourceBufferHolder (std::move (chars), inlineData.Length ());
1901+ return SourceBufferHolder (inlineData.get (),
1902+ inlineData.Length (),
1903+ SourceBufferHolder::NoOwnership);
19141904 }
19151905
1916- size_t length = aRequest->ScriptText ().length ();
1917- return SourceBufferHolder (aRequest->ScriptText ().extractOrCopyRawBuffer (),
1918- length,
1919- SourceBufferHolder::GiveOwnership);
1906+ return SourceBufferHolder (aRequest->ScriptText ().begin (),
1907+ aRequest->ScriptText ().length (),
1908+ SourceBufferHolder::NoOwnership);
19201909}
19211910
19221911nsresult
@@ -2361,7 +2350,8 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest)
23612350 &script);
23622351 } else {
23632352 MOZ_ASSERT (aRequest->IsTextSource ());
2364- SourceBufferHolder srcBuf = GetScriptSource (cx, aRequest);
2353+ nsAutoString inlineData;
2354+ SourceBufferHolder srcBuf = GetScriptSource (aRequest, inlineData);
23652355 rv = exec.CompileAndExec (options, srcBuf, &script);
23662356 }
23672357 }
@@ -3190,12 +3180,11 @@ ScriptLoader::PrepareLoadedRequest(ScriptLoadRequest* aRequest,
31903180 channel->GetURI (getter_AddRefs (request->mBaseURL ));
31913181 }
31923182
3183+
31933184 // Attempt to compile off main thread.
3194- bool couldCompile = false ;
3195- rv = AttemptAsyncScriptCompile (request, &couldCompile);
3196- NS_ENSURE_SUCCESS (rv, rv);
3197- if (couldCompile) {
3198- return NS_OK ;
3185+ rv = AttemptAsyncScriptCompile (request);
3186+ if (NS_SUCCEEDED (rv)) {
3187+ return rv;
31993188 }
32003189
32013190 // Otherwise compile it right away and start fetching descendents.
@@ -3208,15 +3197,18 @@ ScriptLoader::PrepareLoadedRequest(ScriptLoadRequest* aRequest,
32083197 // If this is currently blocking the parser, attempt to compile it off-main-thread.
32093198 if (aRequest == mParserBlockingRequest && NumberOfProcessors() > 1) {
32103199 MOZ_ASSERT (!aRequest->IsModuleRequest ());
3211- bool couldCompile = false ;
3212- nsresult rv = AttemptAsyncScriptCompile (aRequest, &couldCompile);
3213- NS_ENSURE_SUCCESS (rv, rv);
3214- if (couldCompile) {
3200+ nsresult rv = AttemptAsyncScriptCompile (aRequest);
3201+ if (rv == NS_OK ) {
32153202 MOZ_ASSERT (aRequest->mProgress == ScriptLoadRequest::Progress::eCompiling,
32163203 " Request should be off-thread compiling now." );
32173204 return NS_OK ;
32183205 }
32193206
3207+ // If off-thread compile errored, return the error.
3208+ if (rv != NS_ERROR_FAILURE ) {
3209+ return rv;
3210+ }
3211+
32203212 // If off-thread compile was rejected, continue with regular processing.
32213213 }
32223214
0 commit comments