QJSEngine::hasUncaughtException() to determine if an exception
occurred.
- \sa isCallable()
+ \sa isCallable(), callWithInstance()
*/
QJSValue QJSValue::call(const QJSValueList &args)
{
}
/*!
- Calls this QJSValue as a function, using \a thisObject as
+ Calls this QJSValue as a function, using \a instance as
the `this' object in the function call, and passing \a args
as arguments to the function. Returns the value returned from
the function.
If this QJSValue is not a function, call() does nothing
and returns an invalid QJSValue.
- Note that if \a thisObject is not an object, the global object
+ Note that if \a instance is not an object, the global object
(see \l{QJSEngine::globalObject()}) will be used as the
`this' object.
\snippet doc/src/snippets/code/src_script_qjsvalue.cpp 1
- \sa construct()
+ \sa call()
+*/
+QJSValue QJSValue::callWithInstance(const QJSValue &instance, const QJSValueList &args)
+{
+ Q_D(QJSValue);
+ QScriptIsolate api(d->engine());
+ return d->call(QJSValuePrivate::get(instance), args);
+}
+
+/*!
+ \obsolete
+
+ Use callWithInstance() instead.
*/
QJSValue QJSValue::call(const QJSValue& thisObject, const QJSValueList& args)
{
QCOMPARE(opaque.prototype().isValid(), true);
QEXPECT_FAIL("", "FIXME: newly created QObject's prototype is an JS Object", Continue);
QCOMPARE(opaque.prototype().isVariant(), true);
- QVERIFY(opaque.property("valueOf").call(opaque).isUndefined());
+ QVERIFY(opaque.property("valueOf").callWithInstance(opaque).isUndefined());
}
}
QJSEngine eng;
{
QJSValue object = eng.newVariant(QVariant(123));
- QJSValue value = object.property("valueOf").call(object);
+ QJSValue value = object.property("valueOf").callWithInstance(object);
QVERIFY(value.isNumber());
QCOMPARE(value.toInt(), 123);
QCOMPARE(object.toString(), QString::fromLatin1("123"));
}
{
QJSValue object = eng.newVariant(QVariant(QString::fromLatin1("hello")));
- QJSValue value = object.property("valueOf").call(object);
+ QJSValue value = object.property("valueOf").callWithInstance(object);
QVERIFY(value.isString());
QCOMPARE(value.toString(), QString::fromLatin1("hello"));
QCOMPARE(object.toString(), QString::fromLatin1("hello"));
}
{
QJSValue object = eng.newVariant(QVariant(false));
- QJSValue value = object.property("valueOf").call(object);
+ QJSValue value = object.property("valueOf").callWithInstance(object);
QVERIFY(value.isBool());
QCOMPARE(value.toBool(), false);
QCOMPARE(object.toString(), QString::fromLatin1("false"));
}
{
QJSValue object = eng.newVariant(QVariant(QPoint(10, 20)));
- QJSValue value = object.property("valueOf").call(object);
+ QJSValue value = object.property("valueOf").callWithInstance(object);
QVERIFY(value.isObject());
QVERIFY(value.strictlyEquals(object));
QCOMPARE(object.toString(), QString::fromLatin1("QVariant(QPoint)"));
}
// From QScriptValue::call()
{
- QScriptValue result = fun.call(eng.evaluate("p = { id:'foo' }") , QScriptValueList() );
+ QScriptValue result = fun.callWithInstance(eng.evaluate("p = { id:'foo' }") , QScriptValueList() );
QCOMPARE(result.property("local_bar").toString(), QString("local"));
QCOMPARE(result.property("thisObjectIdBefore").toString(), QString("foo"));
QCOMPARE(result.property("thisObjectIdAfter").toString(), QString("foo"));
}
{
- QScriptValue ret = toBaz.call(scriptZoo, QScriptValueList() << baz2Value);
+ QScriptValue ret = toBaz.callWithInstance(scriptZoo, QScriptValueList() << baz2Value);
QVERIFY(ret.isError());
QCOMPARE(ret.toString(), QLatin1String("TypeError: incompatible type of argument(s) in call to toBaz(); candidates were\n toBaz(Bar*)"));
}
}
{
- QScriptValue ret = toBaz.call(scriptZoo, QScriptValueList() << baz2Value);
+ QScriptValue ret = toBaz.callWithInstance(scriptZoo, QScriptValueList() << baz2Value);
QEXPECT_FAIL("", "Cannot convert Baz* to Bar*", Continue);
QVERIFY(!ret.isError());
QEXPECT_FAIL("", "Cannot convert Baz* to Bar*", Continue);
static QScriptValue callQsTr(QScriptContext *ctx, QScriptEngine *eng)
{
- return eng->globalObject().property("qsTr").call(ctx->thisObject(), ctx->argumentsObject());
+ return eng->globalObject().property("qsTr").callWithInstance(ctx->thisObject(), ctx->argumentsObject());
}
void tst_QJSEngine::translateScript_callQsTrFromNative()
QJSValue jsDate = eng.evaluate(QString::fromLatin1("new Date(%0)").arg(secs * 1000.0));
QDateTime qtDate = jsDate.toDateTime();
QString qtUTCDateStr = qtDate.toUTC().toString(Qt::ISODate);
- QString jsUTCDateStr = jsDate.property("toISOString").call(jsDate).toString();
+ QString jsUTCDateStr = jsDate.property("toISOString").callWithInstance(jsDate).toString();
jsUTCDateStr.remove(jsUTCDateStr.length() - 5, 4); // get rid of milliseconds (".000")
if (qtUTCDateStr != jsUTCDateStr)
QFAIL(qPrintable(jsDate.toString()));
QJSEngine eng;
for (int i = 0; i < 8000; ++i) {
QJSValue jsDate = eng.newDate(qtDate);
- QString jsUTCDateStr = jsDate.property("toISOString").call(jsDate).toString();
+ QString jsUTCDateStr = jsDate.property("toISOString").callWithInstance(jsDate).toString();
jsUTCDateStr.remove(jsUTCDateStr.length() - 5, 4); // get rid of milliseconds (".000")
QString qtUTCDateStr = qtDate.toUTC().toString(Qt::ISODate);
if (jsUTCDateStr != qtUTCDateStr)
QJSEngine eng;
QJSValue Object = eng.evaluate("Object");
QCOMPARE(Object.isCallable(), true);
- QJSValue result = Object.call(Object);
+ QJSValue result = Object.callWithInstance(Object);
QCOMPARE(result.isObject(), true);
}
QCOMPARE(Object.isCallable(), true);
QJSValueList args;
args << QJSValue(&eng, 123);
- QJSValue result = Number.call(Object, args);
+ QJSValue result = Number.callWithInstance(Object, args);
QCOMPARE(result.strictlyEquals(args.at(0)), true);
}
QCOMPARE(fun.isCallable(), true);
QJSValue numberObject = QJSValue(&eng, 123.0).toObject();
- QJSValue result = fun.call(numberObject);
+ QJSValue result = fun.callWithInstance(numberObject);
QCOMPARE(result.isObject(), true);
QCOMPARE(result.toNumber(), 123.0);
}
QJSValue fun = eng.evaluate("(function() { return arguments[0]; })");
QCOMPARE(fun.isCallable(), true);
{
- QJSValue result = fun.call(eng.undefinedValue());
+ QJSValue result = fun.callWithInstance(eng.undefinedValue());
QCOMPARE(result.isUndefined(), true);
}
{
QJSValueList args;
args << QJSValue(&eng, 123.0);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QCOMPARE(result.isNumber(), true);
QCOMPARE(result.toNumber(), 123.0);
}
{
QJSValueList args;
args << QJSValue(123.0);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QCOMPARE(result.isNumber(), true);
QCOMPARE(result.toNumber(), 123.0);
}
{
QJSValue args = eng.newArray();
args.setProperty(0, 123);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QVERIFY(result.isNumber());
QCOMPARE(result.toNumber(), 123.0);
}
{
QJSValueList args;
args << QJSValue(&eng, 123.0) << QJSValue(&eng, 456.0);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QCOMPARE(result.isNumber(), true);
QCOMPARE(result.toNumber(), 456.0);
}
QJSValue args = eng.newArray();
args.setProperty(0, 123);
args.setProperty(1, 456);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QVERIFY(result.isNumber());
QCOMPARE(result.toNumber(), 456.0);
}
{
QJSValueList args;
args << QJSValue(&eng, 123.0);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QVERIFY(!eng.hasUncaughtException());
QCOMPARE(result.isNumber(), true);
QCOMPARE(result.toNumber(), 123.0);
{
QJSValueList args;
args << QJSValue(123.0);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QCOMPARE(result.isNumber(), true);
QCOMPARE(result.toNumber(), 123.0);
}
{
QJSValue args = eng.newArray();
args.setProperty(0, 123);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QVERIFY(result.isNumber());
QCOMPARE(result.toNumber(), 123.0);
}
{
QJSValueList args;
args << QJSValue(&eng, 123.0);
- QJSValue result = fun.call(eng.undefinedValue(), args);
+ QJSValue result = fun.callWithInstance(eng.undefinedValue(), args);
QVERIFY(!eng.hasUncaughtException());
QCOMPARE(result.isNumber(), true);
QCOMPARE(result.toNumber(), 123.0);
{
QJSValueList args;
args << QJSValue();
- QJSValue ret = fun.call(args);
+ QJSValue ret = fun.callWithInstance(args);
QVERIFY(!eng.hasUncaughtException());
QCOMPARE(ret.isValid(), true);
QCOMPARE(ret.isUndefined(), true);
QTest::ignoreMessage(QtWarningMsg, "QJSValue::call() failed: "
"cannot call function with thisObject created in "
"a different engine");
- QCOMPARE(fun.call(object).isValid(), false);
+ QCOMPARE(fun.callWithInstance(object).isValid(), false);
QTest::ignoreMessage(QtWarningMsg, "QJSValue::call() failed: "
"cannot call function with argument created in "
"a different engine");