Changeset 61860 in webkit for trunk/JavaScriptCore
- Timestamp:
- Jun 25, 2010, 6:39:17 AM (15 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/ChangeLog
r61853 r61860 1 2010-06-25 Jedrzej Nowacki <[email protected]> 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 1 23 2010-06-25 Lucas De Marchi <[email protected]> 2 24 -
trunk/JavaScriptCore/qt/api/qscriptvalue.cpp
r60725 r61860 516 516 return QScriptEnginePrivate::get(engine); 517 517 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 */ 527 QScriptValue 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 */ 543 void QScriptValue::setPrototype(const QScriptValue& prototype) 544 { 545 d_ptr->setPrototype(QScriptValuePrivate::get(prototype)); 518 546 } 519 547 -
trunk/JavaScriptCore/qt/api/qscriptvalue.h
r60725 r61860 60 60 61 61 QScriptValue& operator=(const QScriptValue& other); 62 63 QScriptValue prototype() const; 64 void setPrototype(const QScriptValue& prototype); 65 62 66 bool equals(const QScriptValue& other) const; 63 67 bool strictlyEquals(const QScriptValue& other) const; -
trunk/JavaScriptCore/qt/api/qscriptvalue_p.h
r61726 r61860 26 26 #include <JavaScriptCore/JavaScript.h> 27 27 #include <JavaScriptCore/JSRetainPtr.h> 28 #include <JSObjectRefPrivate.h> 28 29 #include <QtCore/qmath.h> 29 30 #include <QtCore/qnumeric.h> … … 109 110 inline quint32 toUInt32() const; 110 111 inline quint16 toUInt16() const; 112 111 113 inline QScriptValuePrivate* toObject(QScriptEnginePrivate* engine); 112 114 inline QScriptValuePrivate* toObject(); 115 inline QScriptValuePrivate* prototype(); 116 inline void setPrototype(QScriptValuePrivate* prototype); 113 117 114 118 inline bool equals(QScriptValuePrivate* other); … … 610 614 } 611 615 616 inline 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 629 inline 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 612 645 bool QScriptValuePrivate::equals(QScriptValuePrivate* other) 613 646 { -
trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp
r59503 r61860 430 430 431 431 QVERIFY(incr.call().isValid()); // Exception. 432 } 433 434 void 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); 432 472 } 433 473 -
trunk/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h
r60725 r61860 48 48 void constructors_data(); 49 49 void constructors(); 50 void getSetPrototype(); 50 51 void call(); 51 52 void ctor();
Note:
See TracChangeset
for help on using the changeset viewer.