Bug 965860 - patch 4 - Console format string, r=khuey

This commit is contained in:
Andrea Marchesini 2014-02-27 23:39:12 +00:00
Родитель f9c15a539b
Коммит edecafae28
4 изменённых файлов: 93 добавлений и 12 удалений

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

@ -619,7 +619,44 @@ Console::ProcessArguments(JSContext* aCx,
continue;
}
nsAutoString tmp;
tmp.Append('%');
int32_t integer = -1;
int32_t mantissa = -1;
// Let's parse %<number>.<number> for %d and %f
if (*start >= '0' && *start <= '9') {
integer = 0;
do {
integer = integer * 10 + *start - '0';
tmp.Append(*start);
++start;
} while (*start >= '0' && *start <= '9');
}
if (*start == '.') {
tmp.Append(*start);
++start;
// '.' must be followed by a number.
if (*start < '0' || *start > '9') {
output.Append(tmp);
continue;
}
mantissa = 0;
do {
mantissa = mantissa * 10 + *start - '0';
tmp.Append(*start);
++start;
} while (*start >= '0' && *start <= '9');
}
char ch = *start;
tmp.Append(ch);
++start;
switch (ch) {
@ -673,7 +710,9 @@ Console::ProcessArguments(JSContext* aCx,
return;
}
output.AppendPrintf("%d", v);
nsCString format;
MakeFormatString(format, integer, mantissa, 'd');
output.AppendPrintf(format.get(), v);
}
break;
@ -686,12 +725,14 @@ Console::ProcessArguments(JSContext* aCx,
return;
}
output.AppendPrintf("%f", v);
nsCString format;
MakeFormatString(format, integer, mantissa, 'f');
output.AppendPrintf(format.get(), v);
}
break;
default:
output.Append(ch);
output.Append(tmp);
break;
}
}
@ -712,6 +753,23 @@ Console::ProcessArguments(JSContext* aCx,
}
}
void
Console::MakeFormatString(nsCString& aFormat, int32_t aInteger,
int32_t aMantissa, char aCh)
{
aFormat.Append("%");
if (aInteger >= 0) {
aFormat.AppendInt(aInteger);
}
if (aMantissa >= 0) {
aFormat.Append(".");
aFormat.AppendInt(aMantissa);
}
aFormat.Append(aCh);
}
void
Console::ComposeGroupName(JSContext* aCx,
const nsTArray<JS::Heap<JS::Value>>& aData,

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

@ -137,6 +137,10 @@ private:
ProcessArguments(JSContext* aCx, const nsTArray<JS::Heap<JS::Value>>& aData,
Sequence<JS::Value>& aSequence);
void
MakeFormatString(nsCString& aFormat, int32_t aInteger, int32_t aMantissa,
char aCh);
// Stringify and Concat all the JS::Value in a single string using ' ' as
// separator.
void

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

@ -166,10 +166,10 @@ function testConsoleGroup(aMessageObject) {
function startTraceTest() {
gLevel = "trace";
gArgs = [
{filename: TEST_URI, lineNumber: 6, functionName: "window.foobar585956c", language: 2},
{filename: TEST_URI, lineNumber: 11, functionName: "foobar585956b", language: 2},
{filename: TEST_URI, lineNumber: 15, functionName: "foobar585956a", language: 2},
{filename: TEST_URI, lineNumber: 1, functionName: "onclick", language: 2}
{filename: TEST_URI, functionName: "window.foobar585956c", language: 2, lineNumber: 6},
{filename: TEST_URI, functionName: "foobar585956b", language: 2, lineNumber: 11},
{filename: TEST_URI, functionName: "foobar585956a", language: 2, lineNumber: 15},
{filename: TEST_URI, functionName: "onclick", language: 2, lineNumber: 1}
];
let button = gWindow.document.getElementById("test-trace");
@ -190,7 +190,7 @@ function startLocationTest() {
};
gLevel = "log";
gArgs = [
{filename: TEST_URI, lineNumber: 19, functionName: "foobar646025", arguments: ["omg", "o", "d"]}
{filename: TEST_URI, functionName: "foobar646025", arguments: ["omg", "o", "d"], lineNumber: 19}
];
let button = gWindow.document.getElementById("test-location");
@ -213,15 +213,34 @@ function observeConsoleTest() {
win.console.info("arg", "extra arg");
yield undefined;
// We don't currently support width and precision qualifiers, but we don't
// choke on them either.
expect("warn", "Lesson 1: PI is approximately equal to 3.14159");
expect("warn", "Lesson 1: PI is approximately equal to 3");
win.console.warn("Lesson %d: %s is approximately equal to %1.0f",
1,
"PI",
3.14159);
yield undefined;
expect("warn", "Lesson 1: PI is approximately equal to 3.14");
win.console.warn("Lesson %d: %s is approximately equal to %1.2f",
1,
"PI",
3.14159);
yield undefined;
expect("warn", "Lesson 1: PI is approximately equal to 3.141590");
win.console.warn("Lesson %d: %s is approximately equal to %f",
1,
"PI",
3.14159);
yield undefined;
expect("warn", "Lesson 1: PI is approximately equal to 3.1415900");
win.console.warn("Lesson %d: %s is approximately equal to %0.7f",
1,
"PI",
3.14159);
yield undefined;
expect("log", "%d, %s, %l");
win.console.log("%d, %s, %l");
yield undefined;

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

@ -51,7 +51,7 @@
}
function nativeCallback() {
new Promise(function(resolve, reject) { resolve(42); }).then(console.log);
new Promise(function(resolve, reject) { resolve(42); }).then(console.log.bind(console));
}
</script>
</head>