diff options
Diffstat (limited to 'JavaScriptCore/qt')
-rw-r--r-- | JavaScriptCore/qt/api/QtScript.pro | 2 | ||||
-rw-r--r-- | JavaScriptCore/qt/api/qscriptengine.cpp | 22 | ||||
-rw-r--r-- | JavaScriptCore/qt/api/qscriptengine.h | 1 | ||||
-rw-r--r-- | JavaScriptCore/qt/api/qscriptvalue.cpp | 10 | ||||
-rw-r--r-- | JavaScriptCore/qt/api/qscriptvalue.h | 1 | ||||
-rw-r--r-- | JavaScriptCore/qt/api/qscriptvalue_p.h | 71 | ||||
-rw-r--r-- | JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp | 93 | ||||
-rw-r--r-- | JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp | 108 | ||||
-rw-r--r-- | JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h | 1 |
9 files changed, 308 insertions, 1 deletions
diff --git a/JavaScriptCore/qt/api/QtScript.pro b/JavaScriptCore/qt/api/QtScript.pro index 88629c7..3c2691e 100644 --- a/JavaScriptCore/qt/api/QtScript.pro +++ b/JavaScriptCore/qt/api/QtScript.pro @@ -7,7 +7,7 @@ INCLUDEPATH += $$PWD CONFIG += building-libs isEmpty(JSC_GENERATED_SOURCES_DIR):JSC_GENERATED_SOURCES_DIR = ../../generated -CONFIG(debug, debug|release) { +!CONFIG(release, debug|release) { OBJECTS_DIR = obj/debug } else { # Release OBJECTS_DIR = obj/release diff --git a/JavaScriptCore/qt/api/qscriptengine.cpp b/JavaScriptCore/qt/api/qscriptengine.cpp index d49c578..dc08cd9 100644 --- a/JavaScriptCore/qt/api/qscriptengine.cpp +++ b/JavaScriptCore/qt/api/qscriptengine.cpp @@ -151,6 +151,28 @@ QScriptString QScriptEngine::toStringHandle(const QString& str) } /*! + Converts the given \a value to an object, if such a conversion is + possible; otherwise returns an invalid QScriptValue. The conversion + is performed according to the following table: + + \table + \header \o Input Type \o Result + \row \o Undefined \o An invalid QScriptValue. + \row \o Null \o An invalid QScriptValue. + \row \o Boolean \o A new Boolean object whose internal value is set to the value of the boolean. + \row \o Number \o A new Number object whose internal value is set to the value of the number. + \row \o String \o A new String object whose internal value is set to the value of the string. + \row \o Object \o The result is the object itself (no conversion). + \endtable + + \sa newObject() +*/ +QScriptValue QScriptEngine::toObject(const QScriptValue& value) +{ + return QScriptValuePrivate::get(QScriptValuePrivate::get(value)->toObject(d_ptr.data())); +} + +/*! Returns a QScriptValue of the primitive type Null. \sa undefinedValue() diff --git a/JavaScriptCore/qt/api/qscriptengine.h b/JavaScriptCore/qt/api/qscriptengine.h index e10888d..e19ebda 100644 --- a/JavaScriptCore/qt/api/qscriptengine.h +++ b/JavaScriptCore/qt/api/qscriptengine.h @@ -46,6 +46,7 @@ public: void reportAdditionalMemoryCost(int cost); QScriptString toStringHandle(const QString& str); + QScriptValue toObject(const QScriptValue& value); QScriptValue nullValue(); QScriptValue undefinedValue(); diff --git a/JavaScriptCore/qt/api/qscriptvalue.cpp b/JavaScriptCore/qt/api/qscriptvalue.cpp index f692817..a53a2fe 100644 --- a/JavaScriptCore/qt/api/qscriptvalue.cpp +++ b/JavaScriptCore/qt/api/qscriptvalue.cpp @@ -467,6 +467,16 @@ quint16 QScriptValue::toUInt16() const } /*! + \obsolete + + This function is obsolete; use QScriptEngine::toObject() instead. +*/ +QScriptValue QScriptValue::toObject() const +{ + return QScriptValuePrivate::get(d_ptr->toObject()); +} + +/*! Calls this QScriptValue as a function, using \a thisObject as the `this' object in the function call, and passing \a args as arguments to the function. Returns the value returned from diff --git a/JavaScriptCore/qt/api/qscriptvalue.h b/JavaScriptCore/qt/api/qscriptvalue.h index d45aed3..182f311 100644 --- a/JavaScriptCore/qt/api/qscriptvalue.h +++ b/JavaScriptCore/qt/api/qscriptvalue.h @@ -83,6 +83,7 @@ public: qint32 toInt32() const; quint32 toUInt32() const; quint16 toUInt16() const; + QScriptValue toObject() const; QScriptValue call(const QScriptValue& thisObject = QScriptValue(), const QScriptValueList& args = QScriptValueList()); diff --git a/JavaScriptCore/qt/api/qscriptvalue_p.h b/JavaScriptCore/qt/api/qscriptvalue_p.h index 03e8621..5959c03 100644 --- a/JavaScriptCore/qt/api/qscriptvalue_p.h +++ b/JavaScriptCore/qt/api/qscriptvalue_p.h @@ -104,6 +104,8 @@ public: inline qint32 toInt32() const; inline quint32 toUInt32() const; inline quint16 toUInt16() const; + inline QScriptValuePrivate* toObject(QScriptEnginePrivate* engine); + inline QScriptValuePrivate* toObject(); inline bool equals(QScriptValuePrivate* other); inline bool strictlyEquals(const QScriptValuePrivate* other) const; @@ -523,6 +525,75 @@ quint16 QScriptValuePrivate::toUInt16() const return toInt32(); } +/*! + Creates a copy of this value and converts it to an object. If this value is an object + then pointer to this value will be returned. + \attention it should not happen but if this value is bounded to a different engine that the given, the first + one will be used. + \internal + */ +QScriptValuePrivate* QScriptValuePrivate::toObject(QScriptEnginePrivate* engine) +{ + switch (m_state) { + case Invalid: + case CSpecial: + return new QScriptValuePrivate; + case CString: + { + // Exception can't occur here. + JSObjectRef object = JSValueToObject(engine->context(), engine->makeJSValue(m_string), /* exception */ 0); + Q_ASSERT(object); + return new QScriptValuePrivate(engine, object, object); + } + case CNumber: + { + // Exception can't occur here. + JSObjectRef object = JSValueToObject(engine->context(), engine->makeJSValue(m_number), /* exception */ 0); + Q_ASSERT(object); + return new QScriptValuePrivate(engine, object, object); + } + case CBool: + { + // Exception can't occure here. + JSObjectRef object = JSValueToObject(engine->context(), engine->makeJSValue(static_cast<bool>(m_number)), /* exception */ 0); + Q_ASSERT(object); + return new QScriptValuePrivate(engine, object, object); + } + case JSValue: + if (refinedJSValue() != JSPrimitive) + break; + // Fall-through. + case JSPrimitive: + { + if (engine != this->engine()) + qWarning("QScriptEngine::toObject: cannot convert value created in a different engine"); + JSObjectRef object = JSValueToObject(context(), value(), /* exception */ 0); + if (object) + return new QScriptValuePrivate(m_engine.constData(), object); + } + return new QScriptValuePrivate; + case JSObject: + break; + } + + if (engine != this->engine()) + qWarning("QScriptEngine::toObject: cannot convert value created in a different engine"); + Q_ASSERT(m_state == JSObject); + return this; +} + +/*! + This method is created only for QScriptValue::toObject() purpose which is obsolete. + \internal + */ +QScriptValuePrivate* QScriptValuePrivate::toObject() +{ + if (isJSBased()) + return toObject(m_engine.data()); + + // Without an engine there is not much we can do. + return new QScriptValuePrivate; +} bool QScriptValuePrivate::equals(QScriptValuePrivate* other) { diff --git a/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp b/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp index 1ec9ad3..38243d2 100644 --- a/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp +++ b/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp @@ -44,6 +44,8 @@ private slots: void evaluateProgram(); void checkSyntax_data(); void checkSyntax(); + void toObject(); + void toObjectTwoEngines(); }; /* Evaluating a script that throw an unhandled exception should return an invalid value. */ @@ -303,6 +305,97 @@ void tst_QScriptEngine::checkSyntax() QCOMPARE(result.errorMessage(), errorMessage); } +void tst_QScriptEngine::toObject() +{ + QScriptEngine eng; + QVERIFY(!eng.toObject(eng.undefinedValue()).isValid()); + QVERIFY(!eng.toObject(eng.nullValue()).isValid()); + QVERIFY(!eng.toObject(QScriptValue()).isValid()); + + QScriptValue falskt(false); + { + QScriptValue tmp = eng.toObject(falskt); + QVERIFY(tmp.isObject()); + QVERIFY(!falskt.isObject()); + QVERIFY(!falskt.engine()); + QCOMPARE(tmp.toNumber(), falskt.toNumber()); + } + + QScriptValue sant(true); + { + QScriptValue tmp = eng.toObject(sant); + QVERIFY(tmp.isObject()); + QVERIFY(!sant.isObject()); + QVERIFY(!sant.engine()); + QCOMPARE(tmp.toNumber(), sant.toNumber()); + } + + QScriptValue number(123.0); + { + QScriptValue tmp = eng.toObject(number); + QVERIFY(tmp.isObject()); + QVERIFY(!number.isObject()); + QVERIFY(!number.engine()); + QCOMPARE(tmp.toNumber(), number.toNumber()); + } + + QScriptValue str = QScriptValue(&eng, QString("ciao")); + { + QScriptValue tmp = eng.toObject(str); + QVERIFY(tmp.isObject()); + QVERIFY(!str.isObject()); + QCOMPARE(tmp.toString(), str.toString()); + } + + QScriptValue object = eng.evaluate("new Object"); + { + QScriptValue tmp = eng.toObject(object); + QVERIFY(tmp.isObject()); + QVERIFY(object.isObject()); + QVERIFY(tmp.strictlyEquals(object)); + } +} + +void tst_QScriptEngine::toObjectTwoEngines() +{ + QScriptEngine engine1; + QScriptEngine engine2; + + { + QScriptValue null = engine1.nullValue(); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::toObject: cannot convert value created in a different engine"); + QVERIFY(!engine2.toObject(null).isValid()); + QVERIFY(null.isValid()); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::toObject: cannot convert value created in a different engine"); + QVERIFY(engine2.toObject(null).engine() != &engine2); + } + { + QScriptValue undefined = engine1.undefinedValue(); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::toObject: cannot convert value created in a different engine"); + QVERIFY(!engine2.toObject(undefined).isValid()); + QVERIFY(undefined.isValid()); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::toObject: cannot convert value created in a different engine"); + QVERIFY(engine2.toObject(undefined).engine() != &engine2); + } + { + QScriptValue value = engine1.evaluate("1"); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::toObject: cannot convert value created in a different engine"); + QVERIFY(engine2.toObject(value).engine() != &engine2); + QVERIFY(!value.isObject()); + } + { + QScriptValue string = engine1.evaluate("'Qt'"); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::toObject: cannot convert value created in a different engine"); + QVERIFY(engine2.toObject(string).engine() != &engine2); + QVERIFY(!string.isObject()); + } + { + QScriptValue object = engine1.evaluate("new Object"); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::toObject: cannot convert value created in a different engine"); + QVERIFY(engine2.toObject(object).engine() != &engine2); + QVERIFY(object.isObject()); + } +} QTEST_MAIN(tst_QScriptEngine) #include "tst_qscriptengine.moc" diff --git a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp index 82f0901..90730c3 100644 --- a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp +++ b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp @@ -431,5 +431,113 @@ void tst_QScriptValue::call() QVERIFY(incr.call().isValid()); // Exception. } +void tst_QScriptValue::toObjectSimple() +{ + QScriptEngine eng; + + QScriptValue undefined = eng.undefinedValue(); + QCOMPARE(undefined.toObject().isValid(), false); + QScriptValue null = eng.nullValue(); + QCOMPARE(null.toObject().isValid(), false); + QCOMPARE(QScriptValue().toObject().isValid(), false); + + QScriptValue falskt = QScriptValue(&eng, false); + { + QScriptValue tmp = falskt.toObject(); + QCOMPARE(tmp.isObject(), true); + QCOMPARE(falskt.isObject(), false); + QCOMPARE(tmp.toNumber(), falskt.toNumber()); + } + + QScriptValue sant = QScriptValue(&eng, true); + { + QScriptValue tmp = sant.toObject(); + QCOMPARE(tmp.isObject(), true); + QCOMPARE(sant.isObject(), false); + QCOMPARE(tmp.toNumber(), sant.toNumber()); + } + + QScriptValue number = QScriptValue(&eng, 123.0); + { + QScriptValue tmp = number.toObject(); + QCOMPARE(tmp.isObject(), true); + QCOMPARE(number.isObject(), false); + QCOMPARE(tmp.toNumber(), number.toNumber()); + } + + QScriptValue str = QScriptValue(&eng, QString("ciao")); + { + QScriptValue tmp = str.toObject(); + QCOMPARE(tmp.isObject(), true); + QCOMPARE(str.isObject(), false); + QCOMPARE(tmp.toString(), str.toString()); + } + + + QScriptValue object = eng.evaluate("new Object"); + { + QScriptValue tmp = object.toObject(); + QVERIFY(tmp.strictlyEquals(object)); + QCOMPARE(tmp.isObject(), true); + } + + + // V2 constructors: in this case, you have to use QScriptEngine::toObject() + { + QScriptValue undefined = QScriptValue(QScriptValue::UndefinedValue); + QVERIFY(!undefined.toObject().isValid()); + QVERIFY(!eng.toObject(undefined).isValid()); + QVERIFY(!undefined.engine()); + + QScriptValue null = QScriptValue(QScriptValue::NullValue); + QVERIFY(!null.toObject().isValid()); + QVERIFY(!eng.toObject(null).isValid()); + QVERIFY(!null.engine()); + + QScriptValue falskt = QScriptValue(false); + QVERIFY(!falskt.toObject().isValid()); + QCOMPARE(falskt.isObject(), false); + QVERIFY(!falskt.engine()); + { + QScriptValue tmp = eng.toObject(falskt); + QVERIFY(tmp.isObject()); + QVERIFY(tmp.toBool()); + QVERIFY(!falskt.isObject()); + } + + QScriptValue sant = QScriptValue(true); + QVERIFY(!sant.toObject().isValid()); + QCOMPARE(sant.isObject(), false); + QVERIFY(!sant.engine()); + { + QScriptValue tmp = eng.toObject(sant); + QVERIFY(tmp.isObject()); + QVERIFY(tmp.toBool()); + QVERIFY(!sant.isObject()); + } + + QScriptValue number = QScriptValue(123.0); + QVERIFY(!number.toObject().isValid()); + QVERIFY(!number.engine()); + QCOMPARE(number.isObject(), false); + { + QScriptValue tmp = eng.toObject(number); + QVERIFY(tmp.isObject()); + QCOMPARE(tmp.toInt32(), number.toInt32()); + QVERIFY(!number.isObject()); + } + + QScriptValue str = QScriptValue(QString::fromLatin1("ciao")); + QVERIFY(!str.toObject().isValid()); + QVERIFY(!str.engine()); + QCOMPARE(str.isObject(), false); + { + QScriptValue tmp = eng.toObject(str); + QVERIFY(tmp.isObject()); + QCOMPARE(tmp.toString(), QString::fromLatin1("ciao")); + QVERIFY(!str.isObject()); + } + } +} QTEST_MAIN(tst_QScriptValue) diff --git a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h index 28154a9..27d5f99 100644 --- a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h +++ b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h @@ -47,6 +47,7 @@ private slots: void constructors(); void call(); void ctor(); + void toObjectSimple(); // Generated test functions. void isBool_data(); |