Bug 1819287 - Pass an immutable Handle to nsContentUtils::StringifyJSON; r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D171212
This commit is contained in:
Ms2ger 2023-03-02 15:47:38 +00:00
parent 327e593212
commit ead15dfc06
9 changed files with 14 additions and 19 deletions

View File

@ -133,7 +133,7 @@ void ConvertSerializedStackToJSON(UniquePtr<SerializedStackHolder> aStackHolder,
}
JS::Rooted<JS::Value> convertedValue(cx, JS::ObjectValue(*converted));
if (!nsContentUtils::StringifyJSON(cx, &convertedValue, aStackString)) {
if (!nsContentUtils::StringifyJSON(cx, convertedValue, aStackString)) {
JS_ClearPendingException(cx);
return;
}

View File

@ -10628,12 +10628,11 @@ nsresult nsContentUtils::AnonymizeURI(nsIURI* aURI, nsCString& aAnonymizedURI) {
}
/* static */
bool nsContentUtils::StringifyJSON(JSContext* aCx,
JS::MutableHandle<JS::Value> aValue,
bool nsContentUtils::StringifyJSON(JSContext* aCx, JS::Handle<JS::Value> aValue,
nsAString& aOutStr) {
MOZ_ASSERT(aCx);
aOutStr.Truncate();
JS::Rooted<JS::Value> value(aCx, aValue.get());
JS::Rooted<JS::Value> value(aCx, aValue);
nsAutoString serializedValue;
NS_ENSURE_TRUE(JS_Stringify(aCx, &value, nullptr, JS::NullHandleValue,
JSONCreator, &serializedValue),

View File

@ -3260,9 +3260,9 @@ class nsContentUtils {
*
* Usage:
* nsAutoString serializedValue;
* nsContentUtils::StringifyJSON(cx, &value, serializedValue);
* nsContentUtils::StringifyJSON(cx, value, serializedValue);
*/
static bool StringifyJSON(JSContext* aCx, JS::MutableHandle<JS::Value> vp,
static bool StringifyJSON(JSContext* aCx, JS::Handle<JS::Value> aValue,
nsAString& aOutStr);
/**

View File

@ -472,7 +472,7 @@ bool nsFrameMessageManager::GetParamsForMessage(JSContext* aCx,
// properly cases when interface is implemented in JS and used
// as a dictionary.
nsAutoString json;
NS_ENSURE_TRUE(nsContentUtils::StringifyJSON(aCx, &v, json), false);
NS_ENSURE_TRUE(nsContentUtils::StringifyJSON(aCx, v, json), false);
NS_ENSURE_TRUE(!json.IsEmpty(), false);
JS::Rooted<JS::Value> val(aCx, JS::NullValue());

View File

@ -75,9 +75,8 @@ TEST(DOM_Base_ContentUtils, StringifyJSON_EmptyValue)
JSContext* cx = jsAPI.cx();
nsAutoString serializedValue;
JS::Rooted<JS::Value> jsValue(cx);
ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, &jsValue, serializedValue));
ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, JS::UndefinedHandleValue,
serializedValue));
ASSERT_TRUE(serializedValue.EqualsLiteral("null"));
}
@ -97,7 +96,7 @@ TEST(DOM_Base_ContentUtils, StringifyJSON_Object)
ASSERT_TRUE(JS_DefineProperty(cx, jsObj, "key1", valueStr, JSPROP_ENUMERATE));
JS::Rooted<JS::Value> jsValue(cx, JS::ObjectValue(*jsObj));
ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, &jsValue, serializedValue));
ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, jsValue, serializedValue));
ASSERT_TRUE(serializedValue.EqualsLiteral("{\"key1\":\"Hello World!\"}"));
}

View File

@ -48,7 +48,7 @@ nsresult GetAsString(const RefPtr<Promise>& aPromise, nsAString& aString) {
}
case Promise::PromiseState::Resolved: {
if (nsContentUtils::StringifyJSON(cx, &vp, aString)) {
if (nsContentUtils::StringifyJSON(cx, vp, aString)) {
return NS_OK;
}

View File

@ -142,7 +142,7 @@ void DOMLocalization::SetAttributes(
if (aArgs.WasPassed() && aArgs.Value()) {
nsAutoString data;
JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*aArgs.Value()));
if (!nsContentUtils::StringifyJSON(aCx, &val, data)) {
if (!nsContentUtils::StringifyJSON(aCx, val, data)) {
aRv.NoteJSContextException(aCx);
return;
}

View File

@ -22,12 +22,9 @@ nsresult SerializeFromJSObject(JSContext* aCx, JS::Handle<JSObject*> aObject,
nsresult SerializeFromJSVal(JSContext* aCx, JS::Handle<JS::Value> aValue,
nsAString& aSerializedValue) {
aSerializedValue.Truncate();
nsAutoString serializedValue;
JS::Rooted<JS::Value> value(aCx, aValue.get());
NS_ENSURE_TRUE(nsContentUtils::StringifyJSON(aCx, &value, serializedValue),
NS_ENSURE_TRUE(nsContentUtils::StringifyJSON(aCx, aValue, aSerializedValue),
NS_ERROR_XPC_BAD_CONVERT_JS);
NS_ENSURE_TRUE(!serializedValue.IsEmpty(), NS_ERROR_FAILURE);
aSerializedValue = serializedValue;
NS_ENSURE_TRUE(!aSerializedValue.IsEmpty(), NS_ERROR_FAILURE);
return NS_OK;
}

View File

@ -230,7 +230,7 @@ class UntrustedModulesFixture : public TelemetryTestFixture {
serializer.GetObject(&jsval);
nsAutoString json;
EXPECT_TRUE(nsContentUtils::StringifyJSON(cx.GetJSContext(), &jsval, json));
EXPECT_TRUE(nsContentUtils::StringifyJSON(cx.GetJSContext(), jsval, json));
JS::Rooted<JSObject*> re(
cx.GetJSContext(),