tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 999dfe06ecfe09211c109ef2902328d464523591
parent 2925df16e930bfb9974ccb22d3bcda61125251e3
Author: Tooru Fujisawa <arai_a@mac.com>
Date:   Tue, 21 Oct 2025 15:14:57 +0000

Bug 1991370 - Part 4: Use FrontendContext-based EncodeStencil in ScriptPreloader. r=bthrall

Differential Revision: https://phabricator.services.mozilla.com/D266625

Diffstat:
Mjs/xpconnect/loader/ScriptCacheActors.cpp | 11++++++++---
Mjs/xpconnect/loader/ScriptPreloader.cpp | 18++++++++++++------
Mjs/xpconnect/loader/ScriptPreloader.h | 9+++++----
3 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/js/xpconnect/loader/ScriptCacheActors.cpp b/js/xpconnect/loader/ScriptCacheActors.cpp @@ -34,13 +34,16 @@ void ScriptCacheChild::SendScriptsAndFinalize( ScriptPreloader::ScriptHash& scripts) { MOZ_ASSERT(mWantCacheData); - AutoSafeJSAPI jsapi; - auto matcher = ScriptPreloader::Match<ScriptPreloader::ScriptStatus::Saved>(); + JS::FrontendContext* fc = JS::NewFrontendContext(); + if (!fc) { + return; + } + nsTArray<ScriptData> dataArray; for (auto& script : IterHash(scripts, matcher)) { - if (!script->mSize && !script->XDREncode(jsapi.cx())) { + if (!script->mSize && !script->XDREncode(fc)) { continue; } @@ -57,6 +60,8 @@ void ScriptCacheChild::SendScriptsAndFinalize( } } + JS::DestroyFrontendContext(fc); + Send__delete__(this, dataArray); } diff --git a/js/xpconnect/loader/ScriptPreloader.cpp b/js/xpconnect/loader/ScriptPreloader.cpp @@ -631,8 +631,11 @@ void ScriptPreloader::PrepareCacheWriteInternal() { return; } - AutoSafeJSAPI jsapi; - JSAutoRealm ar(jsapi.cx(), xpc::PrivilegedJunkScope()); + JS::FrontendContext* fc = JS::NewFrontendContext(); + if (!fc) { + return; + } + bool found = false; for (auto& script : IterHash(mScripts, Match<ScriptStatus::Saved>())) { // Don't write any scripts that are also in the child cache. They'll be @@ -652,11 +655,13 @@ void ScriptPreloader::PrepareCacheWriteInternal() { found = true; } - if (!script->mSize && !script->XDREncode(jsapi.cx())) { + if (!script->mSize && !script->XDREncode(fc)) { script.Remove(); } } + JS::DestroyFrontendContext(fc); + if (!found) { mSaveComplete = true; return; @@ -1268,19 +1273,20 @@ ScriptPreloader::CachedStencil::CachedStencil(ScriptPreloader& cache, mProcessTypes = {}; } -bool ScriptPreloader::CachedStencil::XDREncode(JSContext* cx) { +bool ScriptPreloader::CachedStencil::XDREncode(JS::FrontendContext* aFc) { auto cleanup = MakeScopeExit([&]() { MaybeDropStencil(); }); mXDRData.construct<JS::TranscodeBuffer>(); - JS::TranscodeResult code = JS::EncodeStencil(cx, mStencil, Buffer()); + JS::TranscodeResult code = JS::EncodeStencil(aFc, mStencil, Buffer()); + if (code == JS::TranscodeResult::Ok) { mXDRRange.emplace(Buffer().begin(), Buffer().length()); mSize = Range().length(); return true; } mXDRData.destroy(); - JS_ClearPendingException(cx); + JS::ClearFrontendErrors(aFc); return false; } diff --git a/js/xpconnect/loader/ScriptPreloader.h b/js/xpconnect/loader/ScriptPreloader.h @@ -31,9 +31,10 @@ #include "nsITimer.h" #include "js/CompileOptions.h" // JS::DecodeOptions, JS::ReadOnlyDecodeOptions -#include "js/experimental/JSStencil.h" // JS::Stencil -#include "js/GCAnnotations.h" // for JS_HAZ_NON_GC_POINTER -#include "js/RootingAPI.h" // for Handle, Heap +#include "js/experimental/CompileScript.h" // JS::FrontendContext +#include "js/experimental/JSStencil.h" // JS::Stencil +#include "js/GCAnnotations.h" // for JS_HAZ_NON_GC_POINTER +#include "js/RootingAPI.h" // for Handle, Heap #include "js/Transcoding.h" // for TranscodeBuffer, TranscodeRange, TranscodeSource #include "js/TypeDecls.h" // for HandleObject, HandleScript @@ -273,7 +274,7 @@ class ScriptPreloader : public nsIObserver, // Encodes this script into XDR data, and stores the result in mXDRData. // Returns true on success, false on failure. - bool XDREncode(JSContext* cx); + bool XDREncode(JS::FrontendContext* cx); // Encodes or decodes this script, in the storage format required by the // script cache file.