diff options
Diffstat (limited to 'tests/auto/qscriptcontext/tst_qscriptcontext.cpp')
-rw-r--r-- | tests/auto/qscriptcontext/tst_qscriptcontext.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp index eee67df..9d7e896 100644 --- a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp +++ b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp @@ -91,6 +91,11 @@ private slots: void pushAndPopScope_globalContext(); void pushAndPopScope_globalContext2(); void getSetActivationObject_globalContext(); + void pushScopeEvaluate(); + void pushScopeCall(); + void popScopeSimple(); + void pushAndPopGlobalObjectSimple(); + void pushAndPopIterative(); void getSetActivationObject_customContext(); void inheritActivationAndThisObject(); void toString(); @@ -919,6 +924,50 @@ void tst_QScriptContext::backtrace_data() QTest::newRow("js recursive") << source << expected; } + + { + QString source = QString::fromLatin1( + "[0].forEach(\n" + " function() {\n" + " result = bt();\n" + "}); result"); + + QStringList expected; + expected << "<native>() at -1" + << "<anonymous>(0, 0, 0) at testfile:3" + << QString::fromLatin1("forEach(%0) at -1") + // Because the JIT doesn't store the arguments in the frame + // for built-in functions, arguments are not available. + // Will work when the copy of JavaScriptCore is updated + // (QTBUG-16568). + .arg(qt_script_isJITEnabled() + ? "" + : "function () {\n result = bt();\n}") + << "<global>() at testfile:4"; + QTest::newRow("js callback from built-in") << source << expected; + } + + { + QString source = QString::fromLatin1( + "[10,20].forEach(\n" + " function() {\n" + " result = bt();\n" + "}); result"); + + QStringList expected; + expected << "<native>() at -1" + << "<anonymous>(20, 1, 10,20) at testfile:3" + << QString::fromLatin1("forEach(%0) at -1") + // Because the JIT doesn't store the arguments in the frame + // for built-in functions, arguments are not available. + // Will work when the copy of JavaScriptCore is updated + // (QTBUG-16568). + .arg(qt_script_isJITEnabled() + ? "" + : "function () {\n result = bt();\n}") + << "<global>() at testfile:4"; + QTest::newRow("js callback from built-in") << source << expected; + } } @@ -1006,6 +1055,72 @@ void tst_QScriptContext::scopeChain_withStatement() } } +void tst_QScriptContext::pushScopeEvaluate() +{ + QScriptEngine engine; + QScriptValue object = engine.newObject(); + object.setProperty("foo", 1234); + object.setProperty(1, 1234); + engine.currentContext()->pushScope(object); + object.setProperty("bar", 4321); + object.setProperty(2, 4321); + QVERIFY(engine.evaluate("foo").equals(1234)); + QVERIFY(engine.evaluate("bar").equals(4321)); +} + +void tst_QScriptContext::pushScopeCall() +{ + QScriptEngine engine; + QScriptValue object = engine.globalObject(); + QScriptValue thisObject = engine.newObject(); + QScriptValue function = engine.evaluate("(function(property){return this[property]; })"); + QVERIFY(function.isFunction()); + object.setProperty("foo", 1234); + thisObject.setProperty("foo", "foo"); + engine.currentContext()->pushScope(object); + object.setProperty("bar", 4321); + thisObject.setProperty("bar", "bar"); + QVERIFY(function.call(QScriptValue(), QScriptValueList() << "foo").equals(1234)); + QVERIFY(function.call(QScriptValue(), QScriptValueList() << "bar").equals(4321)); + QVERIFY(function.call(thisObject, QScriptValueList() << "foo").equals("foo")); + QVERIFY(function.call(thisObject, QScriptValueList() << "bar").equals("bar")); +} + +void tst_QScriptContext::popScopeSimple() +{ + QScriptEngine engine; + QScriptValue object = engine.newObject(); + QScriptValue globalObject = engine.globalObject(); + engine.currentContext()->pushScope(object); + QVERIFY(engine.currentContext()->popScope().strictlyEquals(object)); + QVERIFY(engine.globalObject().strictlyEquals(globalObject)); +} + +void tst_QScriptContext::pushAndPopGlobalObjectSimple() +{ + QScriptEngine engine; + QScriptValue globalObject = engine.globalObject(); + engine.currentContext()->pushScope(globalObject); + QVERIFY(engine.currentContext()->popScope().strictlyEquals(globalObject)); + QVERIFY(engine.globalObject().strictlyEquals(globalObject)); +} + +void tst_QScriptContext::pushAndPopIterative() +{ + QScriptEngine engine; + for (uint repeat = 0; repeat < 2; ++repeat) { + for (uint i = 1; i < 11; ++i) { + QScriptValue object = engine.newObject(); + object.setProperty("x", i + 10 * repeat); + engine.currentContext()->pushScope(object); + } + for (uint i = 10; i > 0; --i) { + QScriptValue object = engine.currentContext()->popScope(); + QVERIFY(object.property("x").equals(i + 10 * repeat)); + } + } +} + void tst_QScriptContext::pushAndPopScope_globalContext() { QScriptEngine eng; |