/**************************************************************************** ** ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the test suite of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** No Commercial Usage ** This file contains pre-release code and may not be distributed. ** You may use this file in accordance with the terms and conditions ** contained in the Technology Preview License Agreement accompanying ** this package. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain additional ** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** If you have questions regarding the use of this file, please contact ** Nokia at qt-info@nokia.com. ** ** ** ** ** ** ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include Q_DECLARE_METATYPE(QJSValue) //TESTED_FILES= class tst_QJSEngine : public QObject { Q_OBJECT public: tst_QJSEngine(); virtual ~tst_QJSEngine(); public slots: void init(); void cleanup(); private slots: void constructor(); #if 0 // No defaultPrototype for now void defaultPrototype(); void setDefaultPrototype(); #endif void evaluate_data(); void evaluate(); #if 0 // No program void evaluateProgram_data(); void evaluateProgram(); #endif #if 0 // no connections for now void connectAndDisconnect(); #endif void globalObject(); void hasUncaughtException(); #if 0 // no is Evaluating for now void isEvaluating(); #endif void newArray_data(); void newArray(); void newDate(); void newDateFromMs(); void newObject(); #if 0 // No ScriptClass void newObjectWithScriptClass(); #endif #if 0 // no qmetaobject void newQMetaObject(); #endif void newQObject(); #if 0 // no native functions for now void newFunction(); #endif void newRegExp(); void newRegExpFromString(); void newVariant(); void nullValue(); void undefinedValue(); void collectGarbage(); #if 0 // No extensions void availableExtensions(); void importedExtensions(); #endif #if 0 // no context void currentContext(); void pushAndPopContext(); #endif void toObject_data(); void toObject(); #if 0 // no stringhandle void toStringHandle(); #endif void castValueToQreal(); #if 0 // no native functions for now void nativeCall(); #endif #if 0 // no translations void installTranslatorFunctions(); void translation_data(); void translation(); #endif #if 0 // no declarative class void readScopeProperty_data(); void readScopeProperty(); #endif #if 0 // no context void evaluateInNewContext(); void evaluateInNewContextWithScope(); #endif #if 0 // no pushScope void evaluateBindingExpression(); #endif private: void defineStandardTestValues(); void newEngine() { delete m_engine; m_engine = new QJSEngine; } QJSEngine *m_engine; }; tst_QJSEngine::tst_QJSEngine() : m_engine(0) { } tst_QJSEngine::~tst_QJSEngine() { delete m_engine; } void tst_QJSEngine::init() { } void tst_QJSEngine::cleanup() { } void tst_QJSEngine::constructor() { QBENCHMARK { QJSEngine engine; (void)engine.parent(); } } #if 0 // No defaultPrototype for now void tst_QJSEngine::defaultPrototype() { newEngine(); int type = qMetaTypeId(); m_engine->setDefaultPrototype(type, m_engine->newObject()); QBENCHMARK { m_engine->defaultPrototype(type); } } void tst_QJSEngine::setDefaultPrototype() { newEngine(); int type = qMetaTypeId(); QJSValue proto = m_engine->newObject(); QBENCHMARK { m_engine->setDefaultPrototype(type, proto); } } #endif void tst_QJSEngine::evaluate_data() { QTest::addColumn("code"); QTest::newRow("empty script") << QString::fromLatin1(""); QTest::newRow("number literal") << QString::fromLatin1("123"); QTest::newRow("string literal") << QString::fromLatin1("'ciao'"); QTest::newRow("regexp literal") << QString::fromLatin1("/foo/gim"); QTest::newRow("null literal") << QString::fromLatin1("null"); QTest::newRow("undefined literal") << QString::fromLatin1("undefined"); QTest::newRow("null literal") << QString::fromLatin1("null"); QTest::newRow("empty object literal") << QString::fromLatin1("{}"); QTest::newRow("this") << QString::fromLatin1("this"); QTest::newRow("object literal with one property") << QString::fromLatin1("{ foo: 123 }"); QTest::newRow("object literal with two properties") << QString::fromLatin1("{ foo: 123, bar: 456 }"); QTest::newRow("object literal with many properties") << QString::fromLatin1("{ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }"); QTest::newRow("empty array literal") << QString::fromLatin1("[]"); QTest::newRow("array literal with one element") << QString::fromLatin1("[1]"); QTest::newRow("array literal with two elements") << QString::fromLatin1("[1,2]"); QTest::newRow("array literal with many elements") << QString::fromLatin1("[1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1]"); QTest::newRow("empty function definition") << QString::fromLatin1("function foo() { }"); QTest::newRow("function definition") << QString::fromLatin1("function foo() { return 123; }"); QTest::newRow("for loop with empty body (1000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000; ++i) {}"); QTest::newRow("for loop with empty body (10000 iterations)") << QString::fromLatin1("for (i = 0; i < 10000; ++i) {}"); QTest::newRow("for loop with empty body (100000 iterations)") << QString::fromLatin1("for (i = 0; i < 100000; ++i) {}"); QTest::newRow("for loop with empty body (1000000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000000; ++i) {}"); QTest::newRow("for loop (1000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000; ++i) { j += i; }; j"); QTest::newRow("for loop (10000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 10000; ++i) { j += i; }; j"); QTest::newRow("for loop (100000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 100000; ++i) { j += i; }; j"); QTest::newRow("for loop (1000000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000000; ++i) { j += i; }; j"); QTest::newRow("assignments") << QString::fromLatin1("a = 1; b = 2; c = 3; d = 4"); QTest::newRow("while loop (1000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000) { ++i; }; i"); QTest::newRow("while loop (10000 iterations)") << QString::fromLatin1("i = 0; while (i < 10000) { ++i; }; i"); QTest::newRow("while loop (100000 iterations)") << QString::fromLatin1("i = 0; while (i < 100000) { ++i; }; i"); QTest::newRow("while loop (1000000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000000) { ++i; }; i"); QTest::newRow("function expression") << QString::fromLatin1("(function(a, b, c){ return a + b + c; })(1, 2, 3)"); } void tst_QJSEngine::evaluate() { QFETCH(QString, code); newEngine(); QBENCHMARK { (void)m_engine->evaluate(code); } } #if 0 void tst_QJSEngine::connectAndDisconnect() { newEngine(); QJSValue fun = m_engine->evaluate("(function() { })"); QBENCHMARK { qScriptConnect(m_engine, SIGNAL(destroyed()), QJSValue(), fun); qScriptDisconnect(m_engine, SIGNAL(destroyed()), QJSValue(), fun); } } void tst_QJSEngine::evaluateProgram_data() { evaluate_data(); } void tst_QJSEngine::evaluateProgram() { QFETCH(QString, code); QScriptProgram program(code); newEngine(); QBENCHMARK { (void)m_engine->evaluate(program); } } #endif void tst_QJSEngine::globalObject() { newEngine(); QBENCHMARK { m_engine->globalObject(); } } void tst_QJSEngine::hasUncaughtException() { newEngine(); QBENCHMARK { m_engine->hasUncaughtException(); } } #if 0 void tst_QJSEngine::isEvaluating() { newEngine(); QBENCHMARK { m_engine->isEvaluating(); } } #endif void tst_QJSEngine::newArray_data() { QTest::addColumn("size"); QTest::newRow("size=0") << 0; QTest::newRow("size=10") << 10; QTest::newRow("size=100") << 0; QTest::newRow("size=1000") << 0; QTest::newRow("size=10000") << 0; QTest::newRow("size=50000") << 0; } void tst_QJSEngine::newArray() { QFETCH(int, size); newEngine(); QBENCHMARK { m_engine->newArray(size); } } void tst_QJSEngine::newDate() { newEngine(); QDateTime dt = QDateTime::currentDateTime(); QBENCHMARK { m_engine->newDate(dt); } } void tst_QJSEngine::newDateFromMs() { newEngine(); QBENCHMARK { m_engine->newDate(0); } } void tst_QJSEngine::newObject() { newEngine(); QBENCHMARK { (void)m_engine->newObject(); } } #if 0 void tst_QJSEngine::newObjectWithScriptClass() { newEngine(); QScriptClass cls(m_engine); QBENCHMARK { m_engine->newObject(&cls); } } void tst_QJSEngine::newQMetaObject() { newEngine(); QBENCHMARK { m_engine->newQMetaObject(&QJSEngine::staticMetaObject); } } #endif void tst_QJSEngine::newQObject() { newEngine(); QBENCHMARK { (void)m_engine->newQObject(QCoreApplication::instance()); } } #if 0 static QJSValue testFunction(QScriptContext *, QJSEngine *) { return 0; } void tst_QJSEngine::newFunction() { newEngine(); QBENCHMARK { (void)m_engine->newFunction(testFunction); } } #endif void tst_QJSEngine::newRegExp() { newEngine(); QRegExp re = QRegExp("foo"); QBENCHMARK { m_engine->newRegExp(re); } } void tst_QJSEngine::newRegExpFromString() { newEngine(); QString pattern("foo"); QString flags("gim"); QBENCHMARK { m_engine->newRegExp(pattern, flags); } } void tst_QJSEngine::newVariant() { newEngine(); QVariant var(123); QBENCHMARK { (void)m_engine->newVariant(var); } } void tst_QJSEngine::nullValue() { newEngine(); QBENCHMARK { m_engine->nullValue(); } } void tst_QJSEngine::undefinedValue() { newEngine(); QBENCHMARK { m_engine->undefinedValue(); } } void tst_QJSEngine::collectGarbage() { newEngine(); QBENCHMARK { m_engine->collectGarbage(); } } #if 0 void tst_QJSEngine::availableExtensions() { newEngine(); QBENCHMARK { m_engine->availableExtensions(); } } void tst_QJSEngine::importedExtensions() { newEngine(); QBENCHMARK { m_engine->importedExtensions(); } } void tst_QJSEngine::currentContext() { newEngine(); QBENCHMARK { m_engine->currentContext(); } } void tst_QJSEngine::pushAndPopContext() { newEngine(); QBENCHMARK { (void)m_engine->pushContext(); m_engine->popContext(); } } #endif void tst_QJSEngine::toObject_data() { newEngine(); QTest::addColumn("val"); QTest::newRow("bool") << m_engine->evaluate("true"); QTest::newRow("number") << m_engine->evaluate("123"); QTest::newRow("string") << m_engine->evaluate("'ciao'"); QTest::newRow("null") << m_engine->evaluate("null"); QTest::newRow("undefined") << m_engine->evaluate("undefined"); QTest::newRow("object") << m_engine->evaluate("({foo:123})"); QTest::newRow("array") << m_engine->evaluate("[10,20,30]"); QTest::newRow("function") << m_engine->evaluate("(function foo(a, b, c) { return a + b + c; })"); QTest::newRow("date") << m_engine->evaluate("new Date"); QTest::newRow("regexp") << m_engine->evaluate("new RegExp('foo')"); QTest::newRow("error") << m_engine->evaluate("new Error"); QTest::newRow("qobject") << m_engine->newQObject(this); #if 0 // no QMetaObject QTest::newRow("qmetaobject") << m_engine->newQMetaObject(&QJSEngine::staticMetaObject); #endif QTest::newRow("variant") << m_engine->newVariant(123); #if 0 //no classes QTest::newRow("qscriptclassobject") << m_engine->newObject(new QScriptClass(m_engine)); #endif QTest::newRow("invalid") << QJSValue(); QTest::newRow("bool-no-engine") << QJSValue(true); QTest::newRow("number-no-engine") << QJSValue(123.0); QTest::newRow("string-no-engine") << QJSValue(QString::fromLatin1("hello")); QTest::newRow("null-no-engine") << QJSValue(QJSValue::NullValue); QTest::newRow("undefined-no-engine") << QJSValue(QJSValue::UndefinedValue); } void tst_QJSEngine::toObject() { QFETCH(QJSValue, val); QBENCHMARK { m_engine->toObject(val); } } #if 0 void tst_QJSEngine::toStringHandle() { newEngine(); QString str = QString::fromLatin1("foobarbaz"); QBENCHMARK { (void)m_engine->toStringHandle(str); } } #endif void tst_QJSEngine::castValueToQreal() { QJSValue val(123); QBENCHMARK { (void)qjsvalue_cast(val); } } #if 0 static QJSValue native_function(QScriptContext *, QJSEngine *) { return 42; } void tst_QJSEngine::nativeCall() { newEngine(); m_engine->globalObject().setProperty("fun", m_engine->newFunction(native_function)); QBENCHMARK{ #if !defined(Q_OS_SYMBIAN) m_engine->evaluate("var w = 0; for (i = 0; i < 100000; ++i) {\n" " w += fun() + fun(); w -= fun(); fun(); w -= fun(); }"); #else m_engine->evaluate("var w = 0; for (i = 0; i < 25000; ++i) {\n" " w += fun() + fun(); w -= fun(); fun(); w -= fun(); }"); #endif } } void tst_QJSEngine::installTranslatorFunctions() { newEngine(); QBENCHMARK { m_engine->installTranslatorFunctions(); } } void tst_QJSEngine::translation_data() { QTest::addColumn("text"); QTest::addColumn("fileName"); QTest::newRow("no translation") << "\"hello world\"" << ""; QTest::newRow("qsTr") << "qsTr(\"hello world\")" << ""; QTest::newRow("qsTranslate") << "qsTranslate(\"\", \"hello world\")" << ""; QTest::newRow("qsTr:script.js") << "qsTr(\"hello world\")" << "script.js"; } void tst_QJSEngine::translation() { QFETCH(QString, text); QFETCH(QString, fileName); newEngine(); m_engine->installTranslatorFunctions(); QBENCHMARK { (void)m_engine->evaluate(text, fileName); } } #endif #if 0 void tst_QJSEngine::readScopeProperty_data() { QTest::addColumn("staticScope"); QTest::addColumn("nestedScope"); QTest::newRow("single dynamic scope") << false << false; QTest::newRow("single static scope") << true << false; QTest::newRow("double dynamic scope") << false << true; QTest::newRow("double static scope") << true << true; } void tst_QJSEngine::readScopeProperty() { QFETCH(bool, staticScope); QFETCH(bool, nestedScope); newEngine(); QScriptContext *ctx = m_engine->pushContext(); QJSValue scope; if (staticScope) scope = QScriptDeclarativeClass::newStaticScopeObject(m_engine); else scope = m_engine->newObject(); scope.setProperty("foo", 123); ctx->pushScope(scope); if (nestedScope) { QJSValue scope2; if (staticScope) scope2 = QScriptDeclarativeClass::newStaticScopeObject(m_engine); else scope2 = m_engine->newObject(); scope2.setProperty("bar", 456); // ensure a miss in inner scope ctx->pushScope(scope2); } QJSValue fun = m_engine->evaluate("(function() {\n" " for (var i = 0; i < 10000; ++i) {\n" " foo; foo; foo; foo; foo; foo; foo; foo;\n" " }\n" "})"); m_engine->popContext(); QVERIFY(fun.isFunction()); QBENCHMARK { fun.call(); } } void tst_QJSEngine::evaluateInNewContext() { QJSEngine engine; QBENCHMARK { engine.pushContext(); engine.evaluate("var a = 10"); engine.popContext(); } } void tst_QJSEngine::evaluateInNewContextWithScope() { QJSEngine engine; QJSValue scope = engine.newObject(); scope.setProperty("foo", 123); QBENCHMARK { QScriptContext *ctx = engine.pushContext(); ctx->pushScope(scope); engine.evaluate("foo"); engine.popContext(); } } // Binding expressions in QML are implemented as anonymous functions // with custom scopes. void tst_QJSEngine::evaluateBindingExpression() { QJSEngine engine; QScriptContext *ctx = engine.pushContext(); QJSValue scope = engine.newObject(); scope.setProperty("foo", 123); ctx->pushScope(scope); QJSValue fun = engine.evaluate("(function() { return foo; })"); QVERIFY(fun.isFunction()); engine.popContext(); QVERIFY(fun.call().equals(scope.property("foo"))); QJSValue receiver = engine.globalObject(); QBENCHMARK { fun.call(receiver); } } #endif QTEST_MAIN(tst_QJSEngine) #include "tst_qjsengine.moc"