commit b1f784aebbf90355fb6d522828211c7da836fbab
parent 7f4d0b5564cb2707c8c92a597fe6748d10c4340a
Author: Tooru Fujisawa <arai_a@mac.com>
Date: Wed, 8 Oct 2025 04:04:44 +0000
Bug 1991370 - Part 4: Use FrontendContext-based EncodeStencil in ScriptPreloader. r=bthrall
Differential Revision: https://phabricator.services.mozilla.com/D266625
Diffstat:
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
@@ -633,8 +633,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
@@ -654,11 +657,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;
@@ -1271,19 +1276,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.