Bug 1763586 - Remove Element from LoadScript and child classes; r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D143139
This commit is contained in:
Yulia Startsev 2022-04-21 09:53:34 +00:00
parent 9c4d96a57d
commit 7b6ada3766
13 changed files with 19 additions and 164 deletions

View File

@ -1240,7 +1240,7 @@ nsresult EventListenerManager::CompileEventHandlerInternal(
aElement->OwnerDoc()->NodePrincipal());
RefPtr<JS::loader::EventScript> eventScript =
new JS::loader::EventScript(fetchOptions, uri, aElement);
new JS::loader::EventScript(fetchOptions, uri);
JS::CompileOptions options(cx);
// Use line 0 to make the function body starts from line 1.

View File

@ -822,8 +822,9 @@ already_AddRefed<ScriptLoadRequest> ScriptLoader::CreateLoadRequest(
RefPtr<ScriptLoadContext> context = new ScriptLoadContext();
if (aKind == ScriptKind::eClassic) {
RefPtr<ScriptLoadRequest> aRequest = new ScriptLoadRequest(
aKind, aURI, fetchOptions, aIntegrity, referrer, context);
RefPtr<ScriptLoadRequest> aRequest =
new ScriptLoadRequest(aKind, aURI, fetchOptions, aIntegrity, referrer,
new ScriptLoadContext());
return aRequest.forget();
}
@ -2267,8 +2268,7 @@ nsresult ScriptLoader::EvaluateScript(nsIGlobalObject* aGlobalObject,
// Create a ClassicScript object and associate it with the JSScript.
RefPtr<ClassicScript> classicScript =
new ClassicScript(aRequest->mFetchOptions, aRequest->mBaseURL,
aRequest->mFetchOptions->mElement);
new ClassicScript(aRequest->mFetchOptions, aRequest->mBaseURL);
JS::RootedValue classicScriptValue(cx, JS::PrivateValue(classicScript));
JS::CompileOptions options(cx);

View File

@ -51,34 +51,6 @@
namespace mozilla {
namespace dom {
JSObject* SourceElementCallback(JSContext* aCx, JS::HandleValue aPrivateValue) {
// NOTE: The result of this is only used by DevTools for matching sources, so
// it is safe to silently ignore any errors and return nullptr for them.
JS::loader::LoadedScript* script =
static_cast<JS::loader::LoadedScript*>(aPrivateValue.toPrivate());
JS::Rooted<JS::Value> elementValue(aCx);
{
nsCOMPtr<Element> domElement = script->GetScriptElement();
if (!domElement) {
return nullptr;
}
JSObject* globalObject =
domElement->OwnerDoc()->GetScopeObject()->GetGlobalJSObject();
JSAutoRealm ar(aCx, globalObject);
nsresult rv = nsContentUtils::WrapNative(aCx, domElement, &elementValue,
/* aAllowWrapping = */ true);
if (NS_FAILED(rv)) {
return nullptr;
}
}
return &elementValue.toObject();
}
static MOZ_THREAD_LOCAL(ScriptSettingsStackEntry*) sScriptSettingsTLS;
class ScriptSettingsStack {
@ -340,7 +312,6 @@ void AutoJSAPI::InitInternal(nsIGlobalObject* aGlobalObject, JSObject* aGlobal,
mOldWarningReporter.emplace(JS::GetWarningReporter(aCx));
JS::SetWarningReporter(aCx, WarningOnlyErrorReporter);
JS::SetSourceElementCallback(aCx, SourceElementCallback);
#ifdef DEBUG
if (haveException) {

View File

@ -7,7 +7,6 @@
#include "LoadedScript.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/dom/Element.h"
#include "jsfriendapi.h"
#include "js/Modules.h" // JS::{Get,Set}ModulePrivate
@ -26,11 +25,10 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(LoadedScript)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(LoadedScript)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFetchOptions)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBaseURL)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(LoadedScript)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchOptions, mElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchOptions)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(LoadedScript)
@ -40,11 +38,8 @@ NS_IMPL_CYCLE_COLLECTING_ADDREF(LoadedScript)
NS_IMPL_CYCLE_COLLECTING_RELEASE(LoadedScript)
LoadedScript::LoadedScript(ScriptKind aKind, ScriptFetchOptions* aFetchOptions,
nsIURI* aBaseURL, mozilla::dom::Element* aElement)
: mKind(aKind),
mFetchOptions(aFetchOptions),
mBaseURL(aBaseURL),
mElement(aElement) {
nsIURI* aBaseURL)
: mKind(aKind), mFetchOptions(aFetchOptions), mBaseURL(aBaseURL) {
MOZ_ASSERT(mFetchOptions);
MOZ_ASSERT(mBaseURL);
}
@ -94,17 +89,16 @@ void HostReleaseTopLevelScript(const JS::Value& aPrivate) {
// EventScript
//////////////////////////////////////////////////////////////
EventScript::EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
mozilla::dom::Element* aElement)
: LoadedScript(ScriptKind::eEvent, aFetchOptions, aBaseURL, aElement) {}
EventScript::EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eEvent, aFetchOptions, aBaseURL) {}
//////////////////////////////////////////////////////////////
// ClassicScript
//////////////////////////////////////////////////////////////
ClassicScript::ClassicScript(ScriptFetchOptions* aFetchOptions,
nsIURI* aBaseURL, mozilla::dom::Element* aElement)
: LoadedScript(ScriptKind::eClassic, aFetchOptions, aBaseURL, aElement) {}
nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eClassic, aFetchOptions, aBaseURL) {}
//////////////////////////////////////////////////////////////
// ModuleScript
@ -133,9 +127,8 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_ADDREF_INHERITED(ModuleScript, LoadedScript)
NS_IMPL_RELEASE_INHERITED(ModuleScript, LoadedScript)
ModuleScript::ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
mozilla::dom::Element* aElement)
: LoadedScript(ScriptKind::eModule, aFetchOptions, aBaseURL, aElement),
ModuleScript::ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eModule, aFetchOptions, aBaseURL),
mDebuggerDataInitialized(false) {
MOZ_ASSERT(!ModuleRecord());
MOZ_ASSERT(!HasParseError());

View File

@ -27,11 +27,10 @@ class LoadedScript : public nsISupports {
ScriptKind mKind;
RefPtr<ScriptFetchOptions> mFetchOptions;
nsCOMPtr<nsIURI> mBaseURL;
RefPtr<mozilla::dom::Element> mElement;
protected:
LoadedScript(ScriptKind aKind, ScriptFetchOptions* aFetchOptions,
nsIURI* aBaseURL, mozilla::dom::Element* aElement);
nsIURI* aBaseURL);
virtual ~LoadedScript();
@ -49,9 +48,6 @@ class LoadedScript : public nsISupports {
// Used to propagate Fetch Options to child modules
ScriptFetchOptions* GetFetchOptions() const { return mFetchOptions; }
// Used by the Debugger to get the associated Script Element
mozilla::dom::Element* GetScriptElement() const { return mElement; }
nsIURI* BaseURL() const { return mBaseURL; }
void AssociateWithScript(JSScript* aScript);
@ -61,16 +57,14 @@ class ClassicScript final : public LoadedScript {
~ClassicScript() = default;
public:
ClassicScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
mozilla::dom::Element* aElement);
ClassicScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
};
class EventScript final : public LoadedScript {
~EventScript() = default;
public:
EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
mozilla::dom::Element* aElement);
EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
};
// A single module script. May be used to satisfy multiple load requests.
@ -88,8 +82,7 @@ class ModuleScript final : public LoadedScript {
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ModuleScript,
LoadedScript)
ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
mozilla::dom::Element* aElement);
ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
void SetModuleRecord(JS::Handle<JSObject*> aModuleRecord);
void SetParseError(const JS::Value& aError);

View File

@ -491,8 +491,7 @@ nsresult ModuleLoaderBase::CreateModuleScript(ModuleLoadRequest* aRequest) {
}
RefPtr<ModuleScript> moduleScript =
new ModuleScript(aRequest->mFetchOptions, aRequest->mBaseURL,
aRequest->mFetchOptions->mElement);
new ModuleScript(aRequest->mFetchOptions, aRequest->mBaseURL);
aRequest->mModuleScript = moduleScript;
if (!module) {

View File

@ -90,11 +90,6 @@ extern JS_PUBLIC_API bool JS_ExecuteScript(JSContext* cx,
JS::HandleObjectVector envChain,
JS::Handle<JSScript*> script);
// Callback for the embedding to map from a ScriptSourceObject private-value to
// an object that is exposed as the source "element" in debugger API. This hook
// must be infallible, but can return nullptr if no such element exists.
using JSSourceElementCallback = JSObject* (*)(JSContext*, JS::HandleValue);
namespace JS {
/**
@ -251,14 +246,6 @@ extern JS_PUBLIC_API bool UpdateDebugMetadata(
HandleValue privateValue, HandleString elementAttributeName,
HandleScript introScript, HandleScript scriptOrModule);
// The debugger API exposes an optional "element" property on DebuggerSource
// objects. The callback defined here provides that value. SpiderMonkey
// doesn't particularly care about this, but within Firefox the "element" is the
// HTML script tag for the script which DevTools can use for a better debugging
// experience.
extern JS_PUBLIC_API void SetSourceElementCallback(
JSContext* cx, JSSourceElementCallback callback);
} /* namespace JS */
#endif /* js_CompilationAndEvaluation_h */

View File

@ -171,7 +171,6 @@ struct MOZ_STACK_CLASS DebuggerSource::CallData {
bool getStartLine();
bool getId();
bool getDisplayURL();
bool getElement();
bool getElementProperty();
bool getIntroductionScript();
bool getIntroductionOffset();
@ -384,29 +383,6 @@ bool DebuggerSource::CallData::getDisplayURL() {
return true;
}
struct DebuggerSourceGetElementMatcher {
JSContext* mCx = nullptr;
explicit DebuggerSourceGetElementMatcher(JSContext* cx_) : mCx(cx_) {}
using ReturnType = JSObject*;
ReturnType match(HandleScriptSourceObject sourceObject) {
return sourceObject->unwrappedElement(mCx);
}
ReturnType match(Handle<WasmInstanceObject*> wasmInstance) { return nullptr; }
};
bool DebuggerSource::CallData::getElement() {
DebuggerSourceGetElementMatcher matcher(cx);
RootedValue elementValue(cx);
if (JSObject* element = referent.match(matcher)) {
elementValue.setObject(*element);
if (!obj->owner()->wrapDebuggeeValue(cx, &elementValue)) {
return false;
}
}
args.rval().set(elementValue);
return true;
}
struct DebuggerSourceGetElementPropertyMatcher {
using ReturnType = Value;
ReturnType match(HandleScriptSourceObject sourceObject) {
@ -674,7 +650,6 @@ const JSPropertySpec DebuggerSource::properties_[] = {
JS_DEBUG_PSG("url", getURL),
JS_DEBUG_PSG("startLine", getStartLine),
JS_DEBUG_PSG("id", getId),
JS_DEBUG_PSG("element", getElement),
JS_DEBUG_PSG("displayURL", getDisplayURL),
JS_DEBUG_PSG("introductionScript", getIntroductionScript),
JS_DEBUG_PSG("introductionOffset", getIntroductionOffset),

View File

@ -4444,38 +4444,6 @@ static void DestroyShellCompartmentPrivate(JS::GCContext* gcx,
static void SetWorkerContextOptions(JSContext* cx);
static bool ShellBuildId(JS::BuildIdCharVector* buildId);
static JSObject* ShellSourceElementCallback(JSContext* cx,
JS::HandleValue privateValue) {
if (!privateValue.isObject()) {
return nullptr;
}
// Due to nukeCCW shenanigans in the shell, we need to check for dead-proxy
// objects that may have replaced an CCW. Otherwise the GetProperty below
// would throw an exception which we do not want to support in this callback.
if (js::IsDeadProxyObject(&privateValue.toObject())) {
return nullptr;
}
RootedObject infoObject(cx,
CheckedUnwrapStatic(privateValue.toObjectOrNull()));
AutoRealm ar(cx, infoObject);
RootedValue elementValue(cx);
if (!JS_GetProperty(cx, infoObject, "element", &elementValue)) {
// This shouldn't happen in the shell, as ParseDebugMetadata always
// creates the infoObject with this property. In any case, this callback
// must not leave an exception pending, so:
MOZ_CRASH("error getting source element");
}
if (elementValue.isObject()) {
return &elementValue.toObject();
}
return nullptr;
}
static constexpr size_t gWorkerStackSize = 2 * 128 * sizeof(size_t) * 1024;
static void WorkerMain(UniquePtr<WorkerInput> input) {
@ -4511,7 +4479,6 @@ static void WorkerMain(UniquePtr<WorkerInput> input) {
DummyHasReleasedWrapperCallback);
JS_InitDestroyPrincipalsCallback(cx, ShellPrincipals::destroy);
JS_SetDestroyCompartmentCallback(cx, DestroyShellCompartmentPrivate);
JS::SetSourceElementCallback(cx, ShellSourceElementCallback);
js::SetWindowProxyClass(cx, &ShellWindowProxyClass);
@ -12574,7 +12541,6 @@ int main(int argc, char** argv) {
JS_SetSecurityCallbacks(cx, &ShellPrincipals::securityCallbacks);
JS_InitDestroyPrincipalsCallback(cx, ShellPrincipals::destroy);
JS_SetDestroyCompartmentCallback(cx, DestroyShellCompartmentPrivate);
JS::SetSourceElementCallback(cx, ShellSourceElementCallback);
js::SetWindowProxyClass(cx, &ShellWindowProxyClass);

View File

@ -494,12 +494,6 @@ JS_PUBLIC_API bool JS::UpdateDebugMetadata(
return true;
}
JS_PUBLIC_API void JS::SetSourceElementCallback(
JSContext* cx, JSSourceElementCallback callback) {
MOZ_ASSERT(cx->runtime());
cx->runtime()->setSourceElementCallback(cx->runtime(), callback);
}
MOZ_NEVER_INLINE static bool ExecuteScript(JSContext* cx, HandleObject envChain,
HandleScript script,
MutableHandleValue rval) {

View File

@ -830,19 +830,6 @@ void ScriptSourceObject::setPrivate(JSRuntime* rt, const Value& value) {
rt->addRefScriptPrivate(value);
}
JSObject* ScriptSourceObject::unwrappedElement(JSContext* cx) const {
JS::RootedValue privateValue(cx, getPrivate());
if (privateValue.isUndefined()) {
return nullptr;
}
if (cx->runtime()->sourceElementCallback) {
return (*cx->runtime()->sourceElementCallback)(cx, privateValue);
}
return nullptr;
}
class ScriptSource::LoadSourceMatcher {
JSContext* const cx_;
ScriptSource* const ss_;

View File

@ -322,11 +322,6 @@ void JSRuntime::setTelemetryCallback(
rt->telemetryCallback = callback;
}
void JSRuntime::setSourceElementCallback(JSRuntime* rt,
JSSourceElementCallback callback) {
rt->sourceElementCallback = callback;
}
void JSRuntime::setUseCounter(JSObject* obj, JSUseCounter counter) {
if (useCounterCallback) {
(*useCounterCallback)(obj, counter);

View File

@ -319,8 +319,6 @@ struct JSRuntime {
/* Call this to accumulate use counter data. */
js::MainThreadData<JSSetUseCounterCallback> useCounterCallback;
js::MainThreadData<JSSourceElementCallback> sourceElementCallback;
public:
// Accumulates data for Firefox telemetry. |id| is the ID of a JS_TELEMETRY_*
// histogram. |key| provides an additional key to identify the histogram.
@ -332,9 +330,6 @@ struct JSRuntime {
void setTelemetryCallback(JSRuntime* rt,
JSAccumulateTelemetryDataCallback callback);
void setSourceElementCallback(JSRuntime* rt,
JSSourceElementCallback callback);
// Sets the use counter for a specific feature, measuring the presence or
// absence of usage of a feature on a specific web page and document which
// the passed JSObject belongs to.