aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKai Koehne <kai.koehne@nokia.com>2011-11-03 17:58:07 +0100
committerQt by Nokia <qt-info@nokia.com>2011-11-09 08:23:57 +0100
commitcd633df026733f420b5b63ea8b6a4ab943408db9 (patch)
treeb13caa4b3f3851b4bc6f4836a0edf0740169258c /src
parent2b5432bd63483998da39aa4eadfb10d0e43ba0b6 (diff)
Add console.time / console.timeEnd API
Implement console.time() / console.timeEnd() as also provided by FireBug / Safari. Task-number: QTBUG-22347 Change-Id: I94fcadbb0c54fdf60dc2559e3ae63d613e29630b Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp22
-rw-r--r--src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h2
-rw-r--r--src/declarative/qml/v8/qv8engine.cpp22
-rw-r--r--src/declarative/qml/v8/qv8engine_p.h8
4 files changed, 54 insertions, 0 deletions
diff --git a/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp b/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp
index 96c51da0ef..1ced8687ff 100644
--- a/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp
+++ b/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp
@@ -92,6 +92,28 @@ v8::Handle<v8::Value> print(const v8::Arguments &args)
return v8::Undefined();
}
+v8::Handle<v8::Value> consoleTime(const v8::Arguments &args)
+{
+ if (args.Length() != 1)
+ V8THROW_ERROR("console.time(): Invalid arguments");
+ QString name = V8ENGINE()->toString(args[0]);
+ V8ENGINE()->startTimer(name);
+ return v8::Undefined();
+}
+
+v8::Handle<v8::Value> consoleTimeEnd(const v8::Arguments &args)
+{
+ if (args.Length() != 1)
+ V8THROW_ERROR("console.time(): Invalid arguments");
+ QString name = V8ENGINE()->toString(args[0]);
+ bool wasRunning;
+ qint64 elapsed = V8ENGINE()->stopTimer(name, &wasRunning);
+ if (wasRunning) {
+ qDebug("%s: %llims", qPrintable(name), elapsed);
+ }
+ return v8::Undefined();
+}
+
v8::Handle<v8::Value> stringArg(const v8::Arguments &args)
{
QString value = V8ENGINE()->toString(args.This()->ToString());
diff --git a/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h b/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h
index 847dc66e37..35ea885b3c 100644
--- a/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h
+++ b/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h
@@ -91,6 +91,8 @@ v8::Handle<v8::Value> qsTr(const v8::Arguments &args);
v8::Handle<v8::Value> qsTrNoOp(const v8::Arguments &args);
v8::Handle<v8::Value> qsTrId(const v8::Arguments &args);
v8::Handle<v8::Value> qsTrIdNoOp(const v8::Arguments &args);
+v8::Handle<v8::Value> consoleTime(const v8::Arguments &args);
+v8::Handle<v8::Value> consoleTimeEnd(const v8::Arguments &args);
v8::Handle<v8::Value> stringArg(const v8::Arguments &args);
}
diff --git a/src/declarative/qml/v8/qv8engine.cpp b/src/declarative/qml/v8/qv8engine.cpp
index d239f30734..6f744e5e00 100644
--- a/src/declarative/qml/v8/qv8engine.cpp
+++ b/src/declarative/qml/v8/qv8engine.cpp
@@ -519,10 +519,14 @@ void QV8Engine::initializeGlobal(v8::Handle<v8::Object> global)
{
using namespace QDeclarativeBuiltinFunctions;
v8::Local<v8::Function> printFn = V8FUNCTION(print, this);
+ v8::Local<v8::Function> consoleTimeFn = V8FUNCTION(consoleTime, this);
+ v8::Local<v8::Function> consoleTimeEndFn = V8FUNCTION(consoleTimeEnd, this);
v8::Local<v8::Object> console = v8::Object::New();
console->Set(v8::String::New("log"), printFn);
console->Set(v8::String::New("debug"), printFn);
+ console->Set(v8::String::New("time"), consoleTimeFn);
+ console->Set(v8::String::New("timeEnd"), consoleTimeEndFn);
v8::Local<v8::Object> qt = v8::Object::New();
@@ -1398,6 +1402,24 @@ void QV8Engine::emitSignalHandlerException()
emit q->signalHandlerException(scriptValueFromInternal(uncaughtException()));
}
+void QV8Engine::startTimer(const QString &timerName)
+{
+ if (!m_time.isValid())
+ m_time.start();
+ m_startedTimers[timerName] = m_time.elapsed();
+}
+
+qint64 QV8Engine::stopTimer(const QString &timerName, bool *wasRunning)
+{
+ if (!m_startedTimers.contains(timerName)) {
+ *wasRunning = false;
+ return 0;
+ }
+ *wasRunning = true;
+ qint64 startedAt = m_startedTimers.take(timerName);
+ return m_time.elapsed() - startedAt;
+}
+
QThreadStorage<QV8GCCallback::ThreadData *> QV8GCCallback::threadData;
void QV8GCCallback::initializeThreadData()
{
diff --git a/src/declarative/qml/v8/qv8engine_p.h b/src/declarative/qml/v8/qv8engine_p.h
index 92e6123ca1..7b85cfcc31 100644
--- a/src/declarative/qml/v8/qv8engine_p.h
+++ b/src/declarative/qml/v8/qv8engine_p.h
@@ -59,6 +59,7 @@
#include <QtCore/qmutex.h>
#include <QtCore/qstack.h>
#include <QtCore/qstringlist.h>
+#include <QtCore/QElapsedTimer>
#include <private/qv8_p.h>
#include <qjsengine.h>
@@ -405,6 +406,10 @@ public:
void emitSignalHandlerException();
+ // used for console.time(), console.timeEnd()
+ void startTimer(const QString &timerName);
+ qint64 stopTimer(const QString &timerName, bool *wasRunning);
+
QObject *qtObjectFromJS(v8::Handle<v8::Value> value);
QSet<int> visitedConversionObjects;
protected:
@@ -436,6 +441,9 @@ protected:
Exception m_exception;
+ QElapsedTimer m_time;
+ QHash<QString, qint64> m_startedTimers;
+
QVariant toBasicVariant(v8::Handle<v8::Value>);
void initializeGlobal(v8::Handle<v8::Object>);