Bug 1167423 - patch 1 - Handle return values of FallibleTArray functions in Console API, r=smaug

This commit is contained in:
Andrea Marchesini 2015-05-25 12:50:15 +01:00
Родитель b6e238cc5e
Коммит 0379b80f15
2 изменённых файлов: 79 добавлений и 42 удалений

Просмотреть файл

@ -174,7 +174,9 @@ public:
mMethodString = aString;
for (uint32_t i = 0; i < aArguments.Length(); ++i) {
mArguments.AppendElement(aArguments[i]);
if (!mArguments.AppendElement(aArguments[i])) {
return;
}
}
}
@ -666,7 +668,9 @@ private:
return;
}
arguments.AppendElement(value);
if (!arguments.AppendElement(value)) {
return;
}
}
mConsole->ProfileMethod(aCx, mAction, arguments);
@ -826,8 +830,8 @@ Console::Time(JSContext* aCx, const JS::Handle<JS::Value> aTime)
Sequence<JS::Value> data;
SequenceRooter<JS::Value> rooter(aCx, &data);
if (!aTime.isUndefined()) {
data.AppendElement(aTime);
if (!aTime.isUndefined() && !data.AppendElement(aTime)) {
return;
}
Method(aCx, MethodTime, NS_LITERAL_STRING("time"), data);
@ -839,8 +843,8 @@ Console::TimeEnd(JSContext* aCx, const JS::Handle<JS::Value> aTime)
Sequence<JS::Value> data;
SequenceRooter<JS::Value> rooter(aCx, &data);
if (!aTime.isUndefined()) {
data.AppendElement(aTime);
if (!aTime.isUndefined() && !data.AppendElement(aTime)) {
return;
}
Method(aCx, MethodTimeEnd, NS_LITERAL_STRING("timeEnd"), data);
@ -852,8 +856,8 @@ Console::TimeStamp(JSContext* aCx, const JS::Handle<JS::Value> aData)
Sequence<JS::Value> data;
SequenceRooter<JS::Value> rooter(aCx, &data);
if (aData.isString()) {
data.AppendElement(aData);
if (aData.isString() && !data.AppendElement(aData)) {
return;
}
Method(aCx, MethodTimeStamp, NS_LITERAL_STRING("timeStamp"), data);
@ -892,7 +896,9 @@ Console::ProfileMethod(JSContext* aCx, const nsAString& aAction,
Sequence<JS::Value>& sequence = event.mArguments.Value();
for (uint32_t i = 0; i < aData.Length(); ++i) {
sequence.AppendElement(aData[i]);
if (!sequence.AppendElement(aData[i])) {
return;
}
}
JS::Rooted<JS::Value> eventValue(aCx);
@ -1293,13 +1299,18 @@ Console::ProcessCallData(ConsoleCallData* aData)
case MethodAssert:
event.mArguments.Construct();
event.mStyles.Construct();
ProcessArguments(cx, aData->mArguments, event.mArguments.Value(),
event.mStyles.Value());
if (!ProcessArguments(cx, aData->mArguments, event.mArguments.Value(),
event.mStyles.Value())) {
return;
}
break;
default:
event.mArguments.Construct();
ArgumentsToValueList(aData->mArguments, event.mArguments.Value());
if (!ArgumentsToValueList(aData->mArguments, event.mArguments.Value())) {
return;
}
}
if (aData->mMethodName == MethodGroup ||
@ -1418,48 +1429,52 @@ Console::ProcessCallData(ConsoleCallData* aData)
namespace {
// Helper method for ProcessArguments. Flushes output, if non-empty, to aSequence.
void
FlushOutput(JSContext* aCx, Sequence<JS::Value>& aSequence, nsString &output)
bool
FlushOutput(JSContext* aCx, Sequence<JS::Value>& aSequence, nsString &aOutput)
{
if (!output.IsEmpty()) {
if (!aOutput.IsEmpty()) {
JS::Rooted<JSString*> str(aCx, JS_NewUCStringCopyN(aCx,
output.get(),
output.Length()));
aOutput.get(),
aOutput.Length()));
if (!str) {
return;
return false;
}
aSequence.AppendElement(JS::StringValue(str));
output.Truncate();
if (!aSequence.AppendElement(JS::StringValue(str))) {
return false;
}
aOutput.Truncate();
}
return true;
}
} // anonymous namespace
void
bool
Console::ProcessArguments(JSContext* aCx,
const nsTArray<JS::Heap<JS::Value>>& aData,
Sequence<JS::Value>& aSequence,
Sequence<JS::Value>& aStyles)
{
if (aData.IsEmpty()) {
return;
return true;
}
if (aData.Length() == 1 || !aData[0].isString()) {
ArgumentsToValueList(aData, aSequence);
return;
return ArgumentsToValueList(aData, aSequence);
}
JS::Rooted<JS::Value> format(aCx, aData[0]);
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, format));
if (!jsString) {
return;
return false;
}
nsAutoJSString string;
if (!string.init(aCx, jsString)) {
return;
return false;
}
nsString::const_iterator start, end;
@ -1547,35 +1562,47 @@ Console::ProcessArguments(JSContext* aCx,
case 'o':
case 'O':
{
FlushOutput(aCx, aSequence, output);
if (!FlushOutput(aCx, aSequence, output)) {
return false;
}
JS::Rooted<JS::Value> v(aCx);
if (index < aData.Length()) {
v = aData[index++];
}
aSequence.AppendElement(v);
if (!aSequence.AppendElement(v)) {
return false;
}
break;
}
case 'c':
{
FlushOutput(aCx, aSequence, output);
if (!FlushOutput(aCx, aSequence, output)) {
return false;
}
if (index < aData.Length()) {
JS::Rooted<JS::Value> v(aCx, aData[index++]);
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, v));
if (!jsString) {
return;
return false;
}
int32_t diff = aSequence.Length() - aStyles.Length();
if (diff > 0) {
for (int32_t i = 0; i < diff; i++) {
aStyles.AppendElement(JS::NullValue());
if (!aStyles.AppendElement(JS::NullValue())) {
return false;
}
}
}
aStyles.AppendElement(JS::StringValue(jsString));
if (!aStyles.AppendElement(JS::StringValue(jsString))) {
return false;
}
}
break;
}
@ -1585,12 +1612,12 @@ Console::ProcessArguments(JSContext* aCx,
JS::Rooted<JS::Value> value(aCx, aData[index++]);
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, value));
if (!jsString) {
return;
return false;
}
nsAutoJSString v;
if (!v.init(aCx, jsString)) {
return;
return false;
}
output.Append(v);
@ -1604,7 +1631,7 @@ Console::ProcessArguments(JSContext* aCx,
int32_t v;
if (!JS::ToInt32(aCx, value, &v)) {
return;
return false;
}
nsCString format;
@ -1619,7 +1646,7 @@ Console::ProcessArguments(JSContext* aCx,
double v;
if (!JS::ToNumber(aCx, value, &v)) {
return;
return false;
}
nsCString format;
@ -1634,7 +1661,9 @@ Console::ProcessArguments(JSContext* aCx,
}
}
FlushOutput(aCx, aSequence, output);
if (!FlushOutput(aCx, aSequence, output)) {
return false;
}
// Discard trailing style element if there is no output to apply it to.
if (aStyles.Length() > aSequence.Length()) {
@ -1643,8 +1672,12 @@ Console::ProcessArguments(JSContext* aCx,
// The rest of the array, if unused by the format string.
for (; index < aData.Length(); ++index) {
aSequence.AppendElement(aData[index]);
if (!aSequence.AppendElement(aData[index])) {
return false;
}
}
return true;
}
void
@ -1770,13 +1803,17 @@ Console::StopTimer(JSContext* aCx, const JS::Value& aName,
return value;
}
void
bool
Console::ArgumentsToValueList(const nsTArray<JS::Heap<JS::Value>>& aData,
Sequence<JS::Value>& aSequence)
{
for (uint32_t i = 0; i < aData.Length(); ++i) {
aSequence.AppendElement(aData[i]);
if (!aSequence.AppendElement(aData[i])) {
return false;
}
}
return true;
}
JS::Value

Просмотреть файл

@ -158,7 +158,7 @@ private:
// finds based the format string. The index of the styles matches the indexes
// of elements that need the custom styling from aSequence. For elements with
// no custom styling the array is padded with null elements.
void
bool
ProcessArguments(JSContext* aCx, const nsTArray<JS::Heap<JS::Value>>& aData,
Sequence<JS::Value>& aSequence,
Sequence<JS::Value>& aStyles);
@ -182,7 +182,7 @@ private:
DOMHighResTimeStamp aTimestamp);
// The method populates a Sequence from an array of JS::Value.
void
bool
ArgumentsToValueList(const nsTArray<JS::Heap<JS::Value>>& aData,
Sequence<JS::Value>& aSequence);