Changeset 68651 in webkit for trunk/JavaScriptCore/qt
- Timestamp:
- Sep 29, 2010, 9:31:22 AM (15 years ago)
- Location:
- trunk/JavaScriptCore/qt
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/qt/ChangeLog
r64165 r68651 1 2010-09-29 Caio Marcelo de Oliveira Filho <[email protected]> 2 3 Reviewed by Andreas Kling. 4 5 [Qt] QScriptEngine should have an API for creating Date objects 6 https://bugs.webkit.org/show_bug.cgi?id=41667 7 8 Implement newDate(), isDate() and toDateTime() functions. Use the 9 QDateTime::{to,set}MSecsSinceEpoch() functions to do the 10 calculations. 11 12 * api/qscriptengine.cpp: 13 (QScriptEngine::newDate): 14 * api/qscriptengine.h: 15 * api/qscriptengine_p.cpp: 16 (QScriptEnginePrivate::newDate): 17 * api/qscriptengine_p.h: 18 (QScriptEnginePrivate::isDate): 19 20 * api/qscriptoriginalglobalobject_p.h: 21 (QScriptOriginalGlobalObject::QScriptOriginalGlobalObject): need 22 to keep track of Date Constructor and Prototype. 23 (QScriptOriginalGlobalObject::~QScriptOriginalGlobalObject): ditto. 24 (QScriptOriginalGlobalObject::isDate): use the Date Constructor 25 and Prototype to identify Date values. 26 27 * api/qscriptvalue.cpp: 28 (QScriptValue::isDate): 29 (QScriptValue::toDateTime): 30 * api/qscriptvalue.h: 31 * api/qscriptvalue_p.h: 32 (QScriptValuePrivate::isDate): 33 (QScriptValuePrivate::toDateTime): 34 * tests/qscriptengine/tst_qscriptengine.cpp: 35 (tst_QScriptEngine::newDate): 36 1 37 2010-07-27 Jedrzej Nowacki <[email protected]> 2 38 -
trunk/JavaScriptCore/qt/api/qscriptengine.cpp
r64130 r68651 26 26 #include "qscriptsyntaxcheckresult_p.h" 27 27 #include "qscriptvalue_p.h" 28 #include <QtCore/qdatetime.h> 29 #include <QtCore/qnumeric.h> 28 30 29 31 /*! … … 369 371 370 372 /*! 373 Creates a QtScript object of class Date with the given \a value 374 (the number of milliseconds since 01 January 1970, UTC). 375 */ 376 QScriptValue QScriptEngine::newDate(qsreal value) 377 { 378 return QScriptValuePrivate::get(d_ptr->newDate(value)); 379 } 380 381 /*! 382 Creates a QtScript object of class Date from the given \a value. 383 384 \sa QScriptValue::toDateTime() 385 */ 386 QScriptValue QScriptEngine::newDate(const QDateTime& value) 387 { 388 if (value.isValid()) 389 return QScriptValuePrivate::get(d_ptr->newDate(qsreal(value.toMSecsSinceEpoch()))); 390 return QScriptValuePrivate::get(d_ptr->newDate(qSNaN())); 391 } 392 393 /*! 371 394 Returns this engine's Global Object. 372 395 -
trunk/JavaScriptCore/qt/api/qscriptengine.h
r64130 r68651 24 24 #include "qscriptstring.h" 25 25 #include "qscriptsyntaxcheckresult.h" 26 #include "qscriptvalue.h" 26 27 #include <QtCore/qobject.h> 27 28 #include <QtCore/qshareddata.h> 28 29 #include <QtCore/qstring.h> 29 30 30 class Q ScriptValue;31 class QDateTime; 31 32 class QScriptEnginePrivate; 32 33 … … 70 71 QScriptValue newObject(); 71 72 QScriptValue newArray(uint length = 0); 73 QScriptValue newDate(qsreal value); 74 QScriptValue newDate(const QDateTime& value); 72 75 QScriptValue globalObject() const; 73 76 private: -
trunk/JavaScriptCore/qt/api/qscriptengine_p.cpp
r64130 r68651 147 147 } 148 148 149 QScriptValuePrivate* QScriptEnginePrivate::newDate(qsreal value) 150 { 151 JSValueRef exception = 0; 152 JSValueRef argument = JSValueMakeNumber(m_context, value); 153 JSObjectRef result = JSObjectMakeDate(m_context, /* argumentCount */ 1, &argument, &exception); 154 155 if (exception) { 156 setException(exception, NotNullException); 157 return new QScriptValuePrivate(); 158 } 159 160 return new QScriptValuePrivate(this, result); 161 } 162 149 163 QScriptValuePrivate* QScriptEnginePrivate::globalObject() const 150 164 { -
trunk/JavaScriptCore/qt/api/qscriptengine_p.h
r64130 r68651 78 78 QScriptValuePrivate* newObject() const; 79 79 QScriptValuePrivate* newArray(uint length); 80 QScriptValuePrivate* newDate(qsreal value); 80 81 QScriptValuePrivate* globalObject() const; 81 82 … … 84 85 inline operator JSGlobalContextRef() const; 85 86 87 inline bool isDate(JSValueRef value) const; 86 88 inline bool isArray(JSValueRef value) const; 87 89 inline bool isError(JSValueRef value) const; 88 90 inline bool objectHasOwnProperty(JSObjectRef object, JSStringRef property) const; 89 91 inline QVector<JSStringRef> objectGetOwnPropertyNames(JSObjectRef object) const; 92 90 93 private: 91 94 QScriptEngine* q_ptr; … … 227 230 } 228 231 232 bool QScriptEnginePrivate::isDate(JSValueRef value) const 233 { 234 return m_originalGlobalObject.isDate(value); 235 } 236 229 237 bool QScriptEnginePrivate::isArray(JSValueRef value) const 230 238 { -
trunk/JavaScriptCore/qt/api/qscriptoriginalglobalobject_p.h
r64130 r68651 44 44 inline QVector<JSStringRef> objectGetOwnPropertyNames(JSObjectRef object) const; 45 45 46 inline bool isDate(JSValueRef value) const; 46 47 inline bool isArray(JSValueRef value) const; 47 48 inline bool isError(JSValueRef value) const; … … 62 63 JSObjectRef m_functionConstructor; 63 64 JSValueRef m_functionPrototype; 65 JSObjectRef m_dateConstructor; 66 JSValueRef m_datePrototype; 64 67 65 68 // Reference to standard JS functions that are not exposed by JSC C API. … … 79 82 initializeMember(globalObject, propertyName.get(), "Error", m_errorConstructor, m_errorPrototype); 80 83 initializeMember(globalObject, propertyName.get(), "Function", m_functionConstructor, m_functionPrototype); 84 initializeMember(globalObject, propertyName.get(), "Date", m_dateConstructor, m_datePrototype); 81 85 82 86 propertyName.adopt(JSStringCreateWithUTF8CString("hasOwnProperty")); … … 127 131 JSValueUnprotect(m_context, m_functionConstructor); 128 132 JSValueUnprotect(m_context, m_functionPrototype); 133 JSValueUnprotect(m_context, m_dateConstructor); 134 JSValueUnprotect(m_context, m_datePrototype); 129 135 JSValueUnprotect(m_context, m_hasOwnPropertyFunction); 130 136 JSValueUnprotect(m_context, m_getOwnPropertyNamesFunction); … … 171 177 } 172 178 179 inline bool QScriptOriginalGlobalObject::isDate(JSValueRef value) const 180 { 181 return isType(value, m_dateConstructor, m_datePrototype); 182 } 183 173 184 inline bool QScriptOriginalGlobalObject::isArray(JSValueRef value) const 174 185 { -
trunk/JavaScriptCore/qt/api/qscriptvalue.cpp
r62921 r68651 322 322 { 323 323 return d_ptr->isArray(); 324 } 325 326 /*! 327 Returns true if this QScriptValue is an object of the Date class; 328 otherwise returns false. 329 330 \sa QScriptEngine::newDate() 331 */ 332 bool QScriptValue::isDate() const 333 { 334 return d_ptr->isDate(); 324 335 } 325 336 … … 486 497 { 487 498 return QScriptValuePrivate::get(d_ptr->toObject()); 499 } 500 501 /*! 502 Returns a QDateTime representation of this value, in local time. 503 If this QScriptValue is not a date, or the value of the date is 504 NaN (Not-a-Number), an invalid QDateTime is returned. 505 506 \sa isDate() 507 */ 508 QDateTime QScriptValue::toDateTime() const 509 { 510 return d_ptr->toDateTime(); 488 511 } 489 512 -
trunk/JavaScriptCore/qt/api/qscriptvalue.h
r62921 r68651 27 27 class QScriptEngine; 28 28 class QScriptValuePrivate; 29 class QDateTime; 29 30 30 31 class QScriptValue; … … 113 114 bool isError() const; 114 115 bool isArray() const; 116 bool isDate() const; 115 117 116 118 QString toString() const; … … 123 125 quint16 toUInt16() const; 124 126 QScriptValue toObject() const; 127 QDateTime toDateTime() const; 125 128 126 129 QScriptValue call(const QScriptValue& thisObject = QScriptValue(), -
trunk/JavaScriptCore/qt/api/qscriptvalue_p.h
r63980 r68651 27 27 #include <JavaScriptCore/JSRetainPtr.h> 28 28 #include <JSObjectRefPrivate.h> 29 #include <QtCore/qdatetime.h> 29 30 #include <QtCore/qmath.h> 30 31 #include <QtCore/qnumeric.h> … … 103 104 inline bool isFunction(); 104 105 inline bool isArray(); 106 inline bool isDate(); 105 107 106 108 inline QString toString() const; … … 114 116 inline QScriptValuePrivate* toObject(QScriptEnginePrivate* engine); 115 117 inline QScriptValuePrivate* toObject(); 118 inline QDateTime toDateTime(); 116 119 inline QScriptValuePrivate* prototype(); 117 120 inline void setPrototype(QScriptValuePrivate* prototype); … … 463 466 } 464 467 468 bool QScriptValuePrivate::isDate() 469 { 470 switch (m_state) { 471 case JSValue: 472 if (refinedJSValue() != JSObject) 473 return false; 474 // Fall-through. 475 case JSObject: 476 return m_engine->isDate(*this); 477 default: 478 return false; 479 } 480 } 481 465 482 QString QScriptValuePrivate::toString() const 466 483 { … … 661 678 // Without an engine there is not much we can do. 662 679 return new QScriptValuePrivate; 680 } 681 682 QDateTime QScriptValuePrivate::toDateTime() 683 { 684 if (!isDate()) 685 return QDateTime(); 686 687 JSValueRef exception = 0; 688 qsreal t = JSValueToNumber(*m_engine, *this, &exception); 689 690 if (exception) { 691 m_engine->setException(exception, QScriptEnginePrivate::NotNullException); 692 return QDateTime(); 693 } 694 695 QDateTime result; 696 result.setMSecsSinceEpoch(qint64(t)); 697 return result; 663 698 } 664 699 -
trunk/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp
r64130 r68651 22 22 #include "qscriptsyntaxcheckresult.h" 23 23 #include "qscriptvalue.h" 24 #include <QtCore/qnumeric.h> 24 25 #include <QtTest/qtest.h> 25 26 … … 51 52 void newArray(); 52 53 void uncaughtException(); 54 void newDate(); 53 55 }; 54 56 … … 682 684 } 683 685 686 void tst_QScriptEngine::newDate() 687 { 688 QScriptEngine eng; 689 { 690 QScriptValue date = eng.newDate(0); 691 QCOMPARE(date.isValid(), true); 692 QCOMPARE(date.isDate(), true); 693 QCOMPARE(date.isObject(), true); 694 QVERIFY(!date.isFunction()); 695 // prototype should be Date.prototype 696 QCOMPARE(date.prototype().isValid(), true); 697 QCOMPARE(date.prototype().isDate(), true); 698 QCOMPARE(date.prototype().strictlyEquals(eng.evaluate("Date.prototype")), true); 699 } 700 { 701 QDateTime dt = QDateTime(QDate(1, 2, 3), QTime(4, 5, 6, 7), Qt::LocalTime); 702 QScriptValue date = eng.newDate(dt); 703 QCOMPARE(date.isValid(), true); 704 QCOMPARE(date.isDate(), true); 705 QCOMPARE(date.isObject(), true); 706 // prototype should be Date.prototype 707 QCOMPARE(date.prototype().isValid(), true); 708 QCOMPARE(date.prototype().isDate(), true); 709 QCOMPARE(date.prototype().strictlyEquals(eng.evaluate("Date.prototype")), true); 710 711 QCOMPARE(date.toDateTime(), dt); 712 } 713 { 714 QDateTime dt = QDateTime(QDate(1, 2, 3), QTime(4, 5, 6, 7), Qt::UTC); 715 QScriptValue date = eng.newDate(dt); 716 // toDateTime() result should be in local time 717 QCOMPARE(date.toDateTime(), dt.toLocalTime()); 718 } 719 // Date.parse() should return NaN when it fails 720 { 721 QScriptValue ret = eng.evaluate("Date.parse()"); 722 QVERIFY(ret.isNumber()); 723 QVERIFY(qIsNaN(ret.toNumber())); 724 } 725 // Date.parse() should be able to parse the output of Date().toString() 726 { 727 QScriptValue ret = eng.evaluate("var x = new Date(); var s = x.toString(); s == new Date(Date.parse(s)).toString()"); 728 QVERIFY(ret.isBoolean()); 729 QCOMPARE(ret.toBoolean(), true); 730 } 731 } 732 684 733 QTEST_MAIN(tst_QScriptEngine) 685 734 #include "tst_qscriptengine.moc"
Note:
See TracChangeset
for help on using the changeset viewer.