Permalink
Browse files

Implement variable number of args for `console.log`

  • Loading branch information...
execjosh committed Apr 19, 2013
1 parent 127c832 commit 551a13a2a3a1e1fea4160091f33672a998520afe
Showing with 18 additions and 4 deletions.
  1. +15 −1 src/js/pjsengine.cpp
  2. +3 −3 src/main.cpp
View
@@ -67,7 +67,21 @@ bool PJSEngine::init()
if ((JS::Console *) NULL == m_console) {
m_console = new JS::Console(this);
- QJSValue console = m_js->newQObject(m_console);
+ QJSValue _console = m_js->newQObject(m_console);
+ m_js->globalObject().setProperty("_console", _console);
+ QJSValue console = m_js->evaluate(
+ "(function () {"
+ "var __slice = [].slice;"
+ "function log() {"
+ "var args = 0 === arguments.length ? [] : __slice.apply(arguments);"
+ // TODO: `printf` type arguments?
+ "return _console.log(args.join(' '));"
+ "}"
+ "return {"
+ "log: log"
+ "};"
+ "}())"
+ );
m_js->globalObject().setProperty("console", console);
}
View
@@ -54,16 +54,16 @@ int main(int argc, char** argv, const char** envp)
pjse.evaluate(
"var timeoutId = setTimeout(function () {"
- "console.log('Hello, ' + timeoutId + '!');"
+ "console.log('Hello,', timeoutId, '!');"
"phantom.exit(0);"
"}, 3000);"
"var omfg = 5;"
"var intervalId = setInterval(function () {"
"if (--omfg) {"
- "console.log('' + intervalId + ' ' + omfg);"
+ "console.log(intervalId, omfg);"
"} else {"
"clearInterval(intervalId);"
- "console.log('' + intervalId + ' ' + omfg + ' -- clearInterval');"
+ "console.log(intervalId, omfg, '-- clearInterval');"
"}"
"}, 300);"
);

0 comments on commit 551a13a

Please sign in to comment.