From edecafae284595a6a35c009496a42f502f5673f0 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Thu, 27 Feb 2014 23:39:12 +0000 Subject: [PATCH] Bug 965860 - patch 4 - Console format string, r=khuey --- dom/base/Console.cpp | 64 +++++++++++++++++++- dom/base/Console.h | 4 ++ dom/tests/browser/browser_ConsoleAPITests.js | 35 ++++++++--- dom/tests/browser/test-console-api.html | 2 +- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/dom/base/Console.cpp b/dom/base/Console.cpp index a22f67c1d9ce..97f9534f5e89 100644 --- a/dom/base/Console.cpp +++ b/dom/base/Console.cpp @@ -619,7 +619,44 @@ Console::ProcessArguments(JSContext* aCx, continue; } + nsAutoString tmp; + tmp.Append('%'); + + int32_t integer = -1; + int32_t mantissa = -1; + + // Let's parse %. 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>& aData, diff --git a/dom/base/Console.h b/dom/base/Console.h index 1cc490acf3f8..8b022fa47611 100644 --- a/dom/base/Console.h +++ b/dom/base/Console.h @@ -137,6 +137,10 @@ private: ProcessArguments(JSContext* aCx, const nsTArray>& aData, Sequence& 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 diff --git a/dom/tests/browser/browser_ConsoleAPITests.js b/dom/tests/browser/browser_ConsoleAPITests.js index 658d8899e2b1..7e3bf2d6ab5b 100644 --- a/dom/tests/browser/browser_ConsoleAPITests.js +++ b/dom/tests/browser/browser_ConsoleAPITests.js @@ -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; diff --git a/dom/tests/browser/test-console-api.html b/dom/tests/browser/test-console-api.html index 9e313d13bcdf..626860cfaf9f 100644 --- a/dom/tests/browser/test-console-api.html +++ b/dom/tests/browser/test-console-api.html @@ -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)); }