Changeset 60725 in webkit


Ignore:
Timestamp:
Jun 4, 2010 5:23:07 PM (14 years ago)
Author:
eric@webkit.org
Message:

2010-06-04 Jedrzej Nowacki <jedrzej.nowacki@nokia.com>

Reviewed by Kenneth Rohde Christiansen.

New QtScript API; QScriptValue::instanceOf.

New function create an easy way to check value's prototype hierarchy.

[Qt] QScriptValue should have an instanceOf method
https://bugs.webkit.org/show_bug.cgi?id=40120

  • qt/api/qscriptvalue.cpp: (QScriptValue::instanceOf):
  • qt/api/qscriptvalue.h:
  • qt/api/qscriptvalue_p.h: (QScriptValuePrivate::instanceOf):
  • qt/tests/qscriptvalue/tst_qscriptvalue.h:
  • qt/tests/qscriptvalue/tst_qscriptvalue_generated_comparison.cpp: (tst_QScriptValue::instanceOf_initData): (tst_QScriptValue::instanceOf_makeData): (tst_QScriptValue::instanceOf_test):
Location:
trunk/JavaScriptCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r60720 r60725  
     12010-06-04  Jedrzej Nowacki  <jedrzej.nowacki@nokia.com>
     2
     3        Reviewed by Kenneth Rohde Christiansen.
     4
     5        New QtScript API; QScriptValue::instanceOf.
     6
     7        New function create an easy way to check value's prototype hierarchy.
     8
     9        [Qt] QScriptValue should have an instanceOf method
     10        https://bugs.webkit.org/show_bug.cgi?id=40120
     11
     12        * qt/api/qscriptvalue.cpp:
     13        (QScriptValue::instanceOf):
     14        * qt/api/qscriptvalue.h:
     15        * qt/api/qscriptvalue_p.h:
     16        (QScriptValuePrivate::instanceOf):
     17        * qt/tests/qscriptvalue/tst_qscriptvalue.h:
     18        * qt/tests/qscriptvalue/tst_qscriptvalue_generated_comparison.cpp:
     19        (tst_QScriptValue::instanceOf_initData):
     20        (tst_QScriptValue::instanceOf_makeData):
     21        (tst_QScriptValue::instanceOf_test):
     22
    1232010-06-04  Gavin Barraclough  <barraclough@apple.com>
    224
  • trunk/JavaScriptCore/qt/api/qscriptvalue.cpp

    r60661 r60725  
    586586    return d_ptr->strictlyEquals(QScriptValuePrivate::get(other));
    587587}
     588
     589/*!
     590    Returns true if this QScriptValue is an instance of
     591    \a other; otherwise returns false.
     592
     593    This QScriptValue is considered to be an instance of \a other if
     594    \a other is a function and the value of the \c{prototype}
     595    property of \a other is in the prototype chain of this
     596    QScriptValue.
     597*/
     598bool QScriptValue::instanceOf(const QScriptValue& other) const
     599{
     600    return d_ptr->instanceOf(QScriptValuePrivate::get(other));
     601}
  • trunk/JavaScriptCore/qt/api/qscriptvalue.h

    r59503 r60725  
    6262    bool equals(const QScriptValue& other) const;
    6363    bool strictlyEquals(const QScriptValue& other) const;
     64    bool instanceOf(const QScriptValue& other) const;
    6465
    6566    QScriptEngine* engine() const;
  • trunk/JavaScriptCore/qt/api/qscriptvalue_p.h

    r60661 r60725  
    110110    inline bool equals(QScriptValuePrivate* other);
    111111    inline bool strictlyEquals(QScriptValuePrivate* other);
     112    inline bool instanceOf(QScriptValuePrivate* other);
    112113    inline bool assignEngine(QScriptEnginePrivate* engine);
    113114
     
    671672}
    672673
     674inline bool QScriptValuePrivate::instanceOf(QScriptValuePrivate* other)
     675{
     676    if (!isJSBased() || !other->isObject())
     677        return false;
     678    return JSValueIsInstanceOfConstructor(context(), value(), other->object(), /* exception */ 0);
     679}
     680
    673681/*!
    674682  Tries to assign \a engine to this value. Returns true on success; otherwise returns false.
  • trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h

    r60661 r60725  
    110110    void strictlyEquals();
    111111
     112    void instanceOf_data();
     113    void instanceOf();
     114
    112115private:
    113116    typedef void (tst_QScriptValue::*InitDataFunction)();
     
    199202    void strictlyEquals_test(const char*, const QScriptValue&);
    200203
     204    void instanceOf_initData();
     205    void instanceOf_makeData(const char*);
     206    void instanceOf_test(const char*, const QScriptValue&);
     207
    201208private:
    202209    QScriptEngine* engine;
  • trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue_generated_comparison.cpp

    r60661 r60725  
    16781678
    16791679DEFINE_TEST_FUNCTION(strictlyEquals)
     1680
     1681
     1682void tst_QScriptValue::instanceOf_initData()
     1683{
     1684    QTest::addColumn<QScriptValue>("other");
     1685    QTest::addColumn<bool>("expected");
     1686    initScriptValues();
     1687}
     1688
     1689static QString instanceOf_array[] = {
     1690    "engine->evaluate(\"[]\") <=> engine->evaluate(\"Object\")",
     1691    "engine->evaluate(\"[]\") <=> engine->evaluate(\"Array\")",
     1692    "engine->evaluate(\"Date.prototype\") <=> engine->evaluate(\"Object\")",
     1693    "engine->evaluate(\"Array.prototype\") <=> engine->evaluate(\"Object\")",
     1694    "engine->evaluate(\"Function.prototype\") <=> engine->evaluate(\"Object\")",
     1695    "engine->evaluate(\"Error.prototype\") <=> engine->evaluate(\"Object\")",
     1696    "engine->evaluate(\"Object\") <=> engine->evaluate(\"Object\")",
     1697    "engine->evaluate(\"Object\") <=> engine->evaluate(\"Function\")",
     1698    "engine->evaluate(\"Array\") <=> engine->evaluate(\"Object\")",
     1699    "engine->evaluate(\"Array\") <=> engine->evaluate(\"Function\")",
     1700    "engine->evaluate(\"Number\") <=> engine->evaluate(\"Object\")",
     1701    "engine->evaluate(\"Number\") <=> engine->evaluate(\"Function\")",
     1702    "engine->evaluate(\"Function\") <=> engine->evaluate(\"Object\")",
     1703    "engine->evaluate(\"Function\") <=> engine->evaluate(\"Function\")",
     1704    "engine->evaluate(\"(function() { return 1; })\") <=> engine->evaluate(\"Object\")",
     1705    "engine->evaluate(\"(function() { return 1; })\") <=> engine->evaluate(\"Function\")",
     1706    "engine->evaluate(\"(function() { return 'ciao'; })\") <=> engine->evaluate(\"Object\")",
     1707    "engine->evaluate(\"(function() { return 'ciao'; })\") <=> engine->evaluate(\"Function\")",
     1708    "engine->evaluate(\"(function() { throw new Error('foo' })\") <=> engine->evaluate(\"Object\")",
     1709    "engine->evaluate(\"/foo/\") <=> engine->evaluate(\"Object\")",
     1710    "engine->evaluate(\"new Object()\") <=> engine->evaluate(\"Object\")",
     1711    "engine->evaluate(\"new Array()\") <=> engine->evaluate(\"Object\")",
     1712    "engine->evaluate(\"new Array()\") <=> engine->evaluate(\"Array\")",
     1713    "engine->evaluate(\"new Error()\") <=> engine->evaluate(\"Object\")",
     1714    "engine->evaluate(\"a = new Object( a.foo = 22; a.foo\") <=> engine->evaluate(\"Object\")",
     1715    "engine->evaluate(\"Undefined\") <=> engine->evaluate(\"Object\")",
     1716    "engine->evaluate(\"Null\") <=> engine->evaluate(\"Object\")",
     1717    "engine->evaluate(\"True\") <=> engine->evaluate(\"Object\")",
     1718    "engine->evaluate(\"False\") <=> engine->evaluate(\"Object\")"};
     1719
     1720void tst_QScriptValue::instanceOf_makeData(const char *expr)
     1721{
     1722    static QSet<QString> equals;
     1723    if (equals.isEmpty()) {
     1724        equals.reserve(29);
     1725        for (unsigned i = 0; i < 29; ++i)
     1726            equals.insert(instanceOf_array[i]);
     1727    }
     1728    QHash<QString, QScriptValue>::const_iterator it;
     1729    for (it = m_values.constBegin(); it != m_values.constEnd(); ++it) {
     1730        QString tag = QString::fromLatin1("%20 <=> %21").arg(expr).arg(it.key());
     1731        newRow(tag.toLatin1()) << it.value() << equals.contains(tag);
     1732    }
     1733}
     1734
     1735void tst_QScriptValue::instanceOf_test(const char *, const QScriptValue& value)
     1736{
     1737    QFETCH(QScriptValue, other);
     1738    QFETCH(bool, expected);
     1739    QCOMPARE(value.instanceOf(other), expected);
     1740}
     1741
     1742DEFINE_TEST_FUNCTION(instanceOf)
Note: See TracChangeset for help on using the changeset viewer.