Changeset 61860 in webkit


Ignore:
Timestamp:
Jun 25, 2010 6:39:17 AM (14 years ago)
Author:
commit-queue@webkit.org
Message:

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

Reviewed by Simon Hausmann.

New QtScript API; setPrototype() and prototype().

This patch implements QScriptValue's prototype accessors.

[Qt] QScriptValue should have accessors to a prototype.
https://bugs.webkit.org/show_bug.cgi?id=39356

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

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r61853 r61860  
     12010-06-25  Jedrzej Nowacki  <jedrzej.nowacki@nokia.com>
     2
     3        Reviewed by Simon Hausmann.
     4
     5        New QtScript API; setPrototype() and prototype().
     6
     7        This patch implements QScriptValue's prototype accessors.
     8
     9        [Qt] QScriptValue should have accessors to a prototype.
     10        https://bugs.webkit.org/show_bug.cgi?id=39356
     11
     12        * qt/api/qscriptvalue.cpp:
     13        (QScriptValue::prototype):
     14        (QScriptValue::setPrototype):
     15        * qt/api/qscriptvalue.h:
     16        * qt/api/qscriptvalue_p.h:
     17        (QScriptValuePrivate::prototype):
     18        (QScriptValuePrivate::setPrototype):
     19        * qt/tests/qscriptvalue/tst_qscriptvalue.cpp:
     20        (tst_QScriptValue::getSetPrototype):
     21        * qt/tests/qscriptvalue/tst_qscriptvalue.h:
     22
    1232010-06-25  Lucas De Marchi  <lucas.demarchi@profusion.mobi>
    224
  • trunk/JavaScriptCore/qt/api/qscriptvalue.cpp

    r60725 r61860  
    516516        return QScriptEnginePrivate::get(engine);
    517517    return 0;
     518}
     519
     520/*!
     521  If this QScriptValue is an object, returns the internal prototype
     522  (\c{__proto__} property) of this object; otherwise returns an
     523  invalid QScriptValue.
     524
     525  \sa setPrototype(), isObject()
     526*/
     527QScriptValue QScriptValue::prototype() const
     528{
     529    return QScriptValuePrivate::get(d_ptr->prototype());
     530}
     531
     532/*!
     533  If this QScriptValue is an object, sets the internal prototype
     534  (\c{__proto__} property) of this object to be \a prototype;
     535  otherwise does nothing.
     536
     537  The internal prototype should not be confused with the public
     538  property with name "prototype"; the public prototype is usually
     539  only set on functions that act as constructors.
     540
     541  \sa prototype(), isObject()
     542*/
     543void QScriptValue::setPrototype(const QScriptValue& prototype)
     544{
     545    d_ptr->setPrototype(QScriptValuePrivate::get(prototype));
    518546}
    519547
  • trunk/JavaScriptCore/qt/api/qscriptvalue.h

    r60725 r61860  
    6060
    6161    QScriptValue& operator=(const QScriptValue& other);
     62
     63    QScriptValue prototype() const;
     64    void setPrototype(const QScriptValue& prototype);
     65
    6266    bool equals(const QScriptValue& other) const;
    6367    bool strictlyEquals(const QScriptValue& other) const;
  • trunk/JavaScriptCore/qt/api/qscriptvalue_p.h

    r61726 r61860  
    2626#include <JavaScriptCore/JavaScript.h>
    2727#include <JavaScriptCore/JSRetainPtr.h>
     28#include <JSObjectRefPrivate.h>
    2829#include <QtCore/qmath.h>
    2930#include <QtCore/qnumeric.h>
     
    109110    inline quint32 toUInt32() const;
    110111    inline quint16 toUInt16() const;
     112
    111113    inline QScriptValuePrivate* toObject(QScriptEnginePrivate* engine);
    112114    inline QScriptValuePrivate* toObject();
     115    inline QScriptValuePrivate* prototype();
     116    inline void setPrototype(QScriptValuePrivate* prototype);
    113117
    114118    inline bool equals(QScriptValuePrivate* other);
     
    610614}
    611615
     616inline QScriptValuePrivate* QScriptValuePrivate::prototype()
     617{
     618    if (isObject()) {
     619        JSValueRef prototype = JSObjectGetPrototype(*m_engine, *this);
     620        if (JSValueIsNull(*m_engine, prototype))
     621            return new QScriptValuePrivate(engine(), prototype);
     622        // The prototype could be either a null or a JSObject, so it is safe to cast the prototype
     623        // to the JSObjectRef here.
     624        return new QScriptValuePrivate(engine(), prototype, const_cast<JSObjectRef>(prototype));
     625    }
     626    return new QScriptValuePrivate;
     627}
     628
     629inline void QScriptValuePrivate::setPrototype(QScriptValuePrivate* prototype)
     630{
     631    if (isObject() && prototype->isValid() && (prototype->isObject() || prototype->isNull())) {
     632        if (engine() != prototype->engine()) {
     633            qWarning("QScriptValue::setPrototype() failed: cannot set a prototype created in a different engine");
     634            return;
     635        }
     636        // FIXME: This could be replaced by a new, faster API
     637        // look at https://bugs.webkit.org/show_bug.cgi?id=40060
     638        JSObjectSetPrototype(*m_engine, *this, *prototype);
     639        JSValueRef proto = JSObjectGetPrototype(*m_engine, *this);
     640        if (!JSValueIsStrictEqual(*m_engine, proto, *prototype))
     641            qWarning("QScriptValue::setPrototype() failed: cyclic prototype value");
     642    }
     643}
     644
    612645bool QScriptValuePrivate::equals(QScriptValuePrivate* other)
    613646{
  • trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp

    r59503 r61860  
    430430
    431431    QVERIFY(incr.call().isValid()); // Exception.
     432}
     433
     434void tst_QScriptValue::getSetPrototype()
     435{
     436    QScriptEngine engine;
     437    QScriptValue object = engine.evaluate("new Object()");
     438    QScriptValue object2 = engine.evaluate("new Object()");
     439    object2.setPrototype(object);
     440    QCOMPARE(object2.prototype().strictlyEquals(object), true);
     441
     442    QScriptValue inv;
     443    inv.setPrototype(object);
     444    QCOMPARE(inv.prototype().isValid(), false);
     445
     446    QScriptEngine otherEngine;
     447    QScriptValue object3 = otherEngine.evaluate("new Object()");
     448    QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setPrototype() failed: cannot set a prototype created in a different engine");
     449    object2.setPrototype(object3);
     450    QCOMPARE(object2.prototype().strictlyEquals(object), true);
     451
     452    // cyclic prototypes
     453    {
     454        QScriptValue ret = engine.evaluate("o = { }; p = { }; o.__proto__ = p; p.__proto__ = o");
     455        QCOMPARE(ret.isError(), true);
     456        QCOMPARE(ret.toString(), QLatin1String("Error: cyclic __proto__ value"));
     457    }
     458    {
     459        QScriptValue ret = engine.evaluate("p.__proto__ = { }");
     460        QCOMPARE(ret.isError(), false);
     461    }
     462
     463    QScriptValue old = object.prototype();
     464    QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setPrototype() failed: cyclic prototype value");
     465    object.setPrototype(object);
     466    QCOMPARE(object.prototype().strictlyEquals(old), true);
     467
     468    object2.setPrototype(object);
     469    QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setPrototype() failed: cyclic prototype value");
     470    object.setPrototype(object2);
     471    QCOMPARE(object.prototype().strictlyEquals(old), true);
    432472}
    433473
  • trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h

    r60725 r61860  
    4848    void constructors_data();
    4949    void constructors();
     50    void getSetPrototype();
    5051    void call();
    5152    void ctor();
Note: See TracChangeset for help on using the changeset viewer.