Changeset 62007 in webkit for trunk/JavaScriptCore


Ignore:
Timestamp:
Jun 28, 2010, 8:19:59 AM (15 years ago)
Author:
[email protected]
Message:

2010-06-28 Caio Marcelo de Oliveira Filho <[email protected]>

Reviewed by Kenneth Rohde Christiansen.

[Qt] QScriptValue should have API for accessing object properties
https://bugs.webkit.org/show_bug.cgi?id=40903

Make possible to access properties inside QScriptValues. While this
still doesn't support the ResolveLocal parameter, it is already useful
for testing the API.

The tests from upstream QtScript weren't imported since most of them
depend on the setProperty() function as well. A simple test was created.

  • qt/api/qscriptvalue.cpp: (QScriptValue::property):
  • qt/api/qscriptvalue.h: (QScriptValue::):
  • qt/api/qscriptvalue_p.h: (QScriptValuePrivate::property):
  • qt/tests/qscriptvalue/tst_qscriptvalue.cpp: (tst_QScriptValue::propertySimple):
  • qt/tests/qscriptvalue/tst_qscriptvalue.h:
Location:
trunk/JavaScriptCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r62001 r62007  
     12010-06-28  Caio Marcelo de Oliveira Filho  <[email protected]>
     2
     3        Reviewed by Kenneth Rohde Christiansen.
     4
     5        [Qt] QScriptValue should have API for accessing object properties
     6        https://bugs.webkit.org/show_bug.cgi?id=40903
     7
     8        Make possible to access properties inside QScriptValues. While this
     9        still doesn't support the ResolveLocal parameter, it is already useful
     10        for testing the API.
     11
     12        The tests from upstream QtScript weren't imported since most of them
     13        depend on the setProperty() function as well. A simple test was created.
     14
     15        * qt/api/qscriptvalue.cpp:
     16        (QScriptValue::property):
     17        * qt/api/qscriptvalue.h:
     18        (QScriptValue::):
     19        * qt/api/qscriptvalue_p.h:
     20        (QScriptValuePrivate::property):
     21        * qt/tests/qscriptvalue/tst_qscriptvalue.cpp:
     22        (tst_QScriptValue::propertySimple):
     23        * qt/tests/qscriptvalue/tst_qscriptvalue.h:
     24
    1252010-06-28  Xan Lopez  <[email protected]>
    226
  • trunk/JavaScriptCore/qt/api/qscriptvalue.cpp

    r61860 r62007  
    628628    return d_ptr->instanceOf(QScriptValuePrivate::get(other));
    629629}
     630
     631/*!
     632  Returns the value of this QScriptValue's property with the given \a name,
     633  using the given \a mode to resolve the property.
     634
     635  If no such property exists, an invalid QScriptValue is returned.
     636
     637  If the property is implemented using a getter function (i.e. has the
     638  PropertyGetter flag set), calling property() has side-effects on the
     639  script engine, since the getter function will be called (possibly
     640  resulting in an uncaught script exception). If an exception
     641  occurred, property() returns the value that was thrown (typically
     642  an \c{Error} object).
     643
     644  \sa setProperty(), propertyFlags(), QScriptValueIterator
     645*/
     646QScriptValue QScriptValue::property(const QString& name, const ResolveFlags& mode) const
     647{
     648    return QScriptValuePrivate::get(d_ptr->property(name, mode));
     649}
     650
     651/*!
     652  \overload
     653
     654  Returns the property at the given \a arrayIndex, using the given \a
     655  mode to resolve the property.
     656
     657  This function is provided for convenience and performance when
     658  working with array objects.
     659
     660  If this QScriptValue is not an Array object, this function behaves
     661  as if property() was called with the string representation of \a
     662  arrayIndex.
     663*/
     664QScriptValue QScriptValue::property(quint32 arrayIndex, const ResolveFlags& mode) const
     665{
     666    return QScriptValuePrivate::get(d_ptr->property(arrayIndex, mode));
     667}
  • trunk/JavaScriptCore/qt/api/qscriptvalue.h

    r61860 r62007  
    3333
    3434class QScriptValue {
    35 public:   
     35public:
     36    enum ResolveFlag {
     37        ResolveLocal     = 0x00,
     38        ResolvePrototype = 0x01
     39    };
     40
     41    Q_DECLARE_FLAGS(ResolveFlags, ResolveFlag)
     42
    3643    enum SpecialValue {
    3744        NullValue,
     
    6774    bool strictlyEquals(const QScriptValue& other) const;
    6875    bool instanceOf(const QScriptValue& other) const;
     76
     77    QScriptValue property(const QString& name, const ResolveFlags& mode = ResolvePrototype) const;
     78    QScriptValue property(quint32 arrayIndex, const ResolveFlags& mode = ResolvePrototype) const;
    6979
    7080    QScriptEngine* engine() const;
  • trunk/JavaScriptCore/qt/api/qscriptvalue_p.h

    r61860 r62007  
    120120    inline bool instanceOf(QScriptValuePrivate* other);
    121121    inline bool assignEngine(QScriptEnginePrivate* engine);
     122
     123    inline QScriptValuePrivate* property(const QString& name, const QScriptValue::ResolveFlags& mode);
     124    inline QScriptValuePrivate* property(quint32 arrayIndex, const QScriptValue::ResolveFlags& mode);
    122125
    123126    inline QScriptValuePrivate* call(const QScriptValuePrivate* , const QScriptValueList& args);
     
    757760}
    758761
     762inline QScriptValuePrivate* QScriptValuePrivate::property(const QString& name, const QScriptValue::ResolveFlags& mode)
     763{
     764    if (!isObject())
     765        return new QScriptValuePrivate;
     766
     767    if (mode & QScriptValue::ResolveLocal) {
     768        qWarning("QScriptValue::property(): ResolveLocal not supported yet.");
     769        return new QScriptValuePrivate;
     770    }
     771
     772    JSRetainPtr<JSStringRef> nameRef(Adopt, QScriptConverter::toString(name));
     773    QScriptValuePrivate* result = new QScriptValuePrivate(m_engine.constData(), JSObjectGetProperty(*m_engine, *this, nameRef.get(), /* exception */ 0));
     774
     775    return result;
     776}
     777
     778inline QScriptValuePrivate* QScriptValuePrivate::property(quint32 arrayIndex, const QScriptValue::ResolveFlags& mode)
     779{
     780    if (!isObject())
     781        return new QScriptValuePrivate;
     782
     783    if (mode & QScriptValue::ResolveLocal) {
     784        qWarning("QScriptValue::property(): ResolveLocal not supported yet.");
     785        return new QScriptValuePrivate;
     786    }
     787
     788    return new QScriptValuePrivate(m_engine.constData(), JSObjectGetPropertyAtIndex(*m_engine, *this, arrayIndex, /* exception */ 0));
     789}
     790
    759791QScriptValuePrivate* QScriptValuePrivate::call(const QScriptValuePrivate*, const QScriptValueList& args)
    760792{
  • trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp

    r61860 r62007  
    581581}
    582582
     583void tst_QScriptValue::propertySimple()
     584{
     585    QScriptEngine eng;
     586
     587    QScriptValue simpleObject(eng.evaluate("new Object({ test: 1, other: 2 })"));
     588    QCOMPARE(simpleObject.property("test").toUInt32(), quint32(1));
     589    QCOMPARE(simpleObject.property("other").toUInt32(), quint32(2));
     590
     591    QScriptValue simpleArray(eng.evaluate("new Array(7, 8, 9)"));
     592    QCOMPARE(simpleArray.property("length").toUInt32(), quint32(3));
     593    QCOMPARE(simpleArray.property(2).toUInt32(), quint32(9));
     594}
     595
    583596QTEST_MAIN(tst_QScriptValue)
  • trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h

    r61860 r62007  
    5252    void ctor();
    5353    void toObjectSimple();
     54    void propertySimple();
    5455
    5556    // Generated test functions.
Note: See TracChangeset for help on using the changeset viewer.