diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2010-07-01 17:24:01 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2010-07-01 17:24:01 (GMT) |
commit | 745ecfd8925716d962c97a4415881377faf6bdd5 (patch) | |
tree | c4f78b3bd143af52372064cd3ba5c0ae440813c5 /src/script | |
parent | bda164303570629e44185e8baa52908ced6da301 (diff) | |
parent | 8968c79c575755cdb52d5e615ed19e4529047464 (diff) | |
download | Qt-745ecfd8925716d962c97a4415881377faf6bdd5.zip Qt-745ecfd8925716d962c97a4415881377faf6bdd5.tar.gz Qt-745ecfd8925716d962c97a4415881377faf6bdd5.tar.bz2 |
Merge remote branch 'origin/4.7' into qt-master-from-4.7
Conflicts:
bin/syncqt
src/gui/text/qtextlayout.cpp
tools/assistant/tools/assistant/helpviewer_qwv.cpp
tools/assistant/tools/assistant/helpviewer_qwv.h
tools/configure/configureapp.cpp
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/api/qscriptengine.cpp | 93 | ||||
-rw-r--r-- | src/script/api/qscriptengine_p.h | 31 | ||||
-rw-r--r-- | src/script/api/qscriptvalue.cpp | 4 | ||||
-rw-r--r-- | src/script/bridge/bridge.pri | 2 | ||||
-rw-r--r-- | src/script/bridge/qscriptdeclarativeclass.cpp | 36 | ||||
-rw-r--r-- | src/script/bridge/qscriptdeclarativeclass_p.h | 5 | ||||
-rw-r--r-- | src/script/bridge/qscriptstaticscopeobject.cpp | 157 | ||||
-rw-r--r-- | src/script/bridge/qscriptstaticscopeobject_p.h | 103 | ||||
-rw-r--r-- | src/script/parser/qscriptlexer.cpp | 2 | ||||
-rw-r--r-- | src/script/script.pri | 1 | ||||
-rw-r--r-- | src/script/utils/qscriptdate.cpp | 365 | ||||
-rw-r--r-- | src/script/utils/qscriptdate_p.h | 52 | ||||
-rw-r--r-- | src/script/utils/utils.pri | 5 |
13 files changed, 399 insertions, 457 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 4c92246..6894585 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -64,6 +64,7 @@ #include "bridge/qscriptqobject_p.h" #include "bridge/qscriptglobalobject_p.h" #include "bridge/qscriptactivationobject_p.h" +#include "bridge/qscriptstaticscopeobject_p.h" #ifndef QT_NO_QOBJECT #include <QtCore/qcoreapplication.h> @@ -437,6 +438,53 @@ qsreal ToNumber(const QString &value) #endif +static const qsreal MsPerSecond = 1000.0; + +static inline int MsFromTime(qsreal t) +{ + int r = int(::fmod(t, MsPerSecond)); + return (r >= 0) ? r : r + int(MsPerSecond); +} + +/*! + \internal + Converts a JS date value (milliseconds) to a QDateTime (local time). +*/ +QDateTime MsToDateTime(JSC::ExecState *exec, qsreal t) +{ + if (qIsNaN(t)) + return QDateTime(); + JSC::GregorianDateTime tm; + JSC::msToGregorianDateTime(exec, t, /*output UTC=*/true, tm); + int ms = MsFromTime(t); + QDateTime convertedUTC = QDateTime(QDate(tm.year + 1900, tm.month + 1, tm.monthDay), + QTime(tm.hour, tm.minute, tm.second, ms), Qt::UTC); + return convertedUTC.toLocalTime(); +} + +/*! + \internal + Converts a QDateTime to a JS date value (milliseconds). +*/ +qsreal DateTimeToMs(JSC::ExecState *exec, const QDateTime &dt) +{ + if (!dt.isValid()) + return qSNaN(); + QDateTime utc = dt.toUTC(); + QDate date = utc.date(); + QTime time = utc.time(); + JSC::GregorianDateTime tm; + tm.year = date.year() - 1900; + tm.month = date.month() - 1; + tm.monthDay = date.day(); + tm.weekDay = date.dayOfWeek(); + tm.yearDay = date.dayOfYear(); + tm.hour = time.hour(); + tm.minute = time.minute(); + tm.second = time.second(); + return JSC::gregorianDateTimeToMS(exec, tm, time.msec(), /*inputIsUTC=*/true); +} + void GlobalClientData::mark(JSC::MarkStack& markStack) { engine->mark(markStack); @@ -905,6 +953,7 @@ QScriptEnginePrivate::QScriptEnginePrivate() JSC::ExecState* exec = globalObject->globalExec(); scriptObjectStructure = QScriptObject::createStructure(globalObject->objectPrototype()); + staticScopeObjectStructure = QScriptStaticScopeObject::createStructure(JSC::jsNull()); qobjectPrototype = new (exec) QScript::QObjectPrototype(exec, QScript::QObjectPrototype::createStructure(globalObject->objectPrototype()), globalObject->prototypeFunctionStructure()); qobjectWrapperObjectStructure = QScriptObject::createStructure(qobjectPrototype); @@ -1011,12 +1060,17 @@ JSC::JSValue QScriptEnginePrivate::arrayFromVariantList(JSC::ExecState *exec, co return arr; } -QVariantList QScriptEnginePrivate::variantListFromArray(JSC::ExecState *exec, JSC::JSValue arr) +QVariantList QScriptEnginePrivate::variantListFromArray(JSC::ExecState *exec, JSC::JSArray *arr) { + QScriptEnginePrivate *eng = QScript::scriptEngineFromExec(exec); + if (eng->visitedConversionObjects.contains(arr)) + return QVariantList(); // Avoid recursion. + eng->visitedConversionObjects.insert(arr); QVariantList lst; uint len = toUInt32(exec, property(exec, arr, exec->propertyNames().length)); for (uint i = 0; i < len; ++i) lst.append(toVariant(exec, property(exec, arr, i))); + eng->visitedConversionObjects.remove(arr); return lst; } @@ -1029,14 +1083,19 @@ JSC::JSValue QScriptEnginePrivate::objectFromVariantMap(JSC::ExecState *exec, co return obj; } -QVariantMap QScriptEnginePrivate::variantMapFromObject(JSC::ExecState *exec, JSC::JSValue obj) +QVariantMap QScriptEnginePrivate::variantMapFromObject(JSC::ExecState *exec, JSC::JSObject *obj) { + QScriptEnginePrivate *eng = QScript::scriptEngineFromExec(exec); + if (eng->visitedConversionObjects.contains(obj)) + return QVariantMap(); // Avoid recursion. + eng->visitedConversionObjects.insert(obj); JSC::PropertyNameArray propertyNames(exec); - JSC::asObject(obj)->getOwnPropertyNames(exec, propertyNames, JSC::IncludeDontEnumProperties); + obj->getOwnPropertyNames(exec, propertyNames, JSC::IncludeDontEnumProperties); QVariantMap vmap; JSC::PropertyNameArray::const_iterator it = propertyNames.begin(); for( ; it != propertyNames.end(); ++it) vmap.insert(it->ustring(), toVariant(exec, property(exec, obj, *it))); + eng->visitedConversionObjects.remove(obj); return vmap; } @@ -1525,7 +1584,7 @@ void QScriptEnginePrivate::detachAllRegisteredScriptStrings() #ifndef QT_NO_REGEXP -Q_DECL_IMPORT extern QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax); +Q_CORE_EXPORT QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax); JSC::JSValue QScriptEnginePrivate::newRegExp(JSC::ExecState *exec, const QRegExp ®exp) { @@ -1661,16 +1720,10 @@ QVariant QScriptEnginePrivate::toVariant(JSC::ExecState *exec, JSC::JSValue valu return QVariant(toRegExp(exec, value)); #endif else if (isArray(value)) - return variantListFromArray(exec, value); + return variantListFromArray(exec, JSC::asArray(value)); else if (QScriptDeclarativeClass *dc = declarativeClass(value)) return dc->toVariant(declarativeObject(value)); - // try to convert to primitive - JSC::JSValue savedException; - saveException(exec, &savedException); - JSC::JSValue prim = value.toPrimitive(exec); - restoreException(exec, savedException); - if (!prim.isObject()) - return toVariant(exec, prim); + return variantMapFromObject(exec, JSC::asObject(value)); } else if (value.isNumber()) { return QVariant(toNumber(exec, value)); } else if (value.isString()) { @@ -1766,15 +1819,7 @@ void QScriptEnginePrivate::setProperty(JSC::ExecState *exec, JSC::JSValue object } else if (flags != QScriptValue::KeepExistingFlags) { if (thisObject->hasOwnProperty(exec, id)) thisObject->deleteProperty(exec, id); // ### hmmm - can't we just update the attributes? - unsigned attribs = 0; - if (flags & QScriptValue::ReadOnly) - attribs |= JSC::ReadOnly; - if (flags & QScriptValue::SkipInEnumeration) - attribs |= JSC::DontEnum; - if (flags & QScriptValue::Undeletable) - attribs |= JSC::DontDelete; - attribs |= flags & QScriptValue::UserRange; - thisObject->putWithAttributes(exec, id, value, attribs); + thisObject->putWithAttributes(exec, id, value, propertyFlagsToJSCAttributes(flags)); } else { JSC::PutPropertySlot slot; thisObject->put(exec, id, value, slot); @@ -2020,8 +2065,6 @@ QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun, #ifndef QT_NO_REGEXP -Q_DECL_IMPORT extern QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax); - /*! Creates a QtScript object of class RegExp with the given \a regexp. @@ -3131,12 +3174,12 @@ bool QScriptEnginePrivate::convertValue(JSC::ExecState *exec, JSC::JSValue value } break; case QMetaType::QVariantList: if (isArray(value)) { - *reinterpret_cast<QVariantList *>(ptr) = variantListFromArray(exec, value); + *reinterpret_cast<QVariantList *>(ptr) = variantListFromArray(exec, JSC::asArray(value)); return true; } break; case QMetaType::QVariantMap: if (isObject(value)) { - *reinterpret_cast<QVariantMap *>(ptr) = variantMapFromObject(exec, value); + *reinterpret_cast<QVariantMap *>(ptr) = variantMapFromObject(exec, JSC::asObject(value)); return true; } break; case QMetaType::QVariant: diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h index fd47208..c71465d 100644 --- a/src/script/api/qscriptengine_p.h +++ b/src/script/api/qscriptengine_p.h @@ -50,7 +50,6 @@ #include "bridge/qscriptobject_p.h" #include "bridge/qscriptqobject_p.h" #include "bridge/qscriptvariant_p.h" -#include "utils/qscriptdate_p.h" #include "DateConstructor.h" #include "DateInstance.h" @@ -119,6 +118,9 @@ namespace QScript inline QString ToString(qsreal); #endif + QDateTime MsToDateTime(JSC::ExecState *, qsreal); + qsreal DateTimeToMs(JSC::ExecState *, const QDateTime &); + //some conversion helper functions inline QScriptEnginePrivate *scriptEngineFromExec(const JSC::ExecState *exec); bool isFunction(JSC::JSValue value); @@ -205,6 +207,7 @@ public: inline QScriptValue scriptValueFromJSCValue(JSC::JSValue value); inline JSC::JSValue scriptValueToJSCValue(const QScriptValue &value); + static inline unsigned propertyFlagsToJSCAttributes(const QScriptValue::PropertyFlags &flags); static inline JSC::JSValue jscValueFromVariant(JSC::ExecState*, const QVariant &value); static QVariant jscValueToVariant(JSC::ExecState*, JSC::JSValue value, int targetType); @@ -215,10 +218,10 @@ public: static QStringList stringListFromArray(JSC::ExecState*, JSC::JSValue arr); static JSC::JSValue arrayFromVariantList(JSC::ExecState*, const QVariantList &lst); - static QVariantList variantListFromArray(JSC::ExecState*, JSC::JSValue arr); + static QVariantList variantListFromArray(JSC::ExecState*, JSC::JSArray *arr); static JSC::JSValue objectFromVariantMap(JSC::ExecState*, const QVariantMap &vmap); - static QVariantMap variantMapFromObject(JSC::ExecState*, JSC::JSValue obj); + static QVariantMap variantMapFromObject(JSC::ExecState*, JSC::JSObject *obj); JSC::JSValue defaultPrototype(int metaTypeId) const; void setDefaultPrototype(int metaTypeId, JSC::JSValue prototype); @@ -346,6 +349,7 @@ public: JSC::ExecState *currentFrame; WTF::RefPtr<JSC::Structure> scriptObjectStructure; + WTF::RefPtr<JSC::Structure> staticScopeObjectStructure; QScript::QObjectPrototype *qobjectPrototype; WTF::RefPtr<JSC::Structure> qobjectWrapperObjectStructure; @@ -378,6 +382,8 @@ public: QHash<intptr_t, QScript::UStringSourceProviderWithFeedback*> loadedScripts; QScriptValue m_currentException; + QSet<JSC::JSObject*> visitedConversionObjects; + #ifndef QT_NO_QOBJECT QHash<QObject*, QScript::QObjectData*> m_qobjectData; #endif @@ -637,6 +643,19 @@ inline JSC::JSValue QScriptEnginePrivate::scriptValueToJSCValue(const QScriptVal return vv->jscValue; } +inline unsigned QScriptEnginePrivate::propertyFlagsToJSCAttributes(const QScriptValue::PropertyFlags &flags) +{ + unsigned attribs = 0; + if (flags & QScriptValue::ReadOnly) + attribs |= JSC::ReadOnly; + if (flags & QScriptValue::SkipInEnumeration) + attribs |= JSC::DontEnum; + if (flags & QScriptValue::Undeletable) + attribs |= JSC::DontDelete; + attribs |= flags & QScriptValue::UserRange; + return attribs; +} + inline QScriptValuePrivate::~QScriptValuePrivate() { if (engine) @@ -844,7 +863,7 @@ inline JSC::JSValue QScriptEnginePrivate::newDate(JSC::ExecState *exec, qsreal v inline JSC::JSValue QScriptEnginePrivate::newDate(JSC::ExecState *exec, const QDateTime &value) { - return newDate(exec, QScript::FromDateTime(value)); + return newDate(exec, QScript::DateTimeToMs(exec, value)); } inline JSC::JSValue QScriptEnginePrivate::newObject() @@ -977,12 +996,12 @@ inline JSC::UString QScriptEnginePrivate::toString(JSC::ExecState *exec, JSC::JS return str; } -inline QDateTime QScriptEnginePrivate::toDateTime(JSC::ExecState *, JSC::JSValue value) +inline QDateTime QScriptEnginePrivate::toDateTime(JSC::ExecState *exec, JSC::JSValue value) { if (!isDate(value)) return QDateTime(); qsreal t = static_cast<JSC::DateInstance*>(JSC::asObject(value))->internalNumber(); - return QScript::ToDateTime(t, Qt::LocalTime); + return QScript::MsToDateTime(exec, t); } inline QObject *QScriptEnginePrivate::toQObject(JSC::ExecState *exec, JSC::JSValue value) diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp index 1310c8c..451d1b0 100644 --- a/src/script/api/qscriptvalue.cpp +++ b/src/script/api/qscriptvalue.cpp @@ -1215,8 +1215,8 @@ qsreal QScriptValue::toInteger() const \row \o QObject Object \o A QVariant containing a pointer to the QObject. \row \o Date Object \o A QVariant containing the date value (toDateTime()). \row \o RegExp Object \o A QVariant containing the regular expression value (toRegExp()). - \row \o Array Object \o The array is converted to a QVariantList. - \row \o Object \o If the value is primitive, then the result is converted to a QVariant according to the above rules; otherwise, an invalid QVariant is returned. + \row \o Array Object \o The array is converted to a QVariantList. Each element is converted to a QVariant, recursively; cyclic references are not followed. + \row \o Object \o The object is converted to a QVariantMap. Each property is converted to a QVariant, recursively; cyclic references are not followed. \endtable \sa isVariant() diff --git a/src/script/bridge/bridge.pri b/src/script/bridge/bridge.pri index 09e2dfb..ab0a322 100644 --- a/src/script/bridge/bridge.pri +++ b/src/script/bridge/bridge.pri @@ -6,6 +6,7 @@ SOURCES += \ $$PWD/qscriptqobject.cpp \ $$PWD/qscriptglobalobject.cpp \ $$PWD/qscriptactivationobject.cpp \ + $$PWD/qscriptstaticscopeobject.cpp \ $$PWD/qscriptdeclarativeobject.cpp \ $$PWD/qscriptdeclarativeclass.cpp @@ -17,5 +18,6 @@ HEADERS += \ $$PWD/qscriptqobject_p.h \ $$PWD/qscriptglobalobject_p.h \ $$PWD/qscriptactivationobject_p.h \ + $$PWD/qscriptstaticscopeobject_p.h \ $$PWD/qscriptdeclarativeobject_p.h \ $$PWD/qscriptdeclarativeclass_p.h diff --git a/src/script/bridge/qscriptdeclarativeclass.cpp b/src/script/bridge/qscriptdeclarativeclass.cpp index 1093448..8080b9f 100644 --- a/src/script/bridge/qscriptdeclarativeclass.cpp +++ b/src/script/bridge/qscriptdeclarativeclass.cpp @@ -24,6 +24,7 @@ #include "qscriptdeclarativeclass_p.h" #include "qscriptdeclarativeobject_p.h" #include "qscriptobject_p.h" +#include "qscriptstaticscopeobject_p.h" #include <QtScript/qscriptstring.h> #include <QtScript/qscriptengine.h> #include <QtScript/qscriptengineagent.h> @@ -549,4 +550,39 @@ QScriptContext *QScriptDeclarativeClass::context() const return d_ptr->context; } +/*! + Creates a scope object with a fixed set of undeletable properties. +*/ +QScriptValue QScriptDeclarativeClass::newStaticScopeObject( + QScriptEngine *engine, int propertyCount, const QString *names, + const QScriptValue *values, const QScriptValue::PropertyFlags *flags) +{ + QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine); + QScript::APIShim shim(eng_p); + JSC::ExecState *exec = eng_p->currentFrame; + QScriptStaticScopeObject::PropertyInfo *props = new QScriptStaticScopeObject::PropertyInfo[propertyCount]; + for (int i = 0; i < propertyCount; ++i) { + unsigned attribs = QScriptEnginePrivate::propertyFlagsToJSCAttributes(flags[i]); + Q_ASSERT_X(attribs & JSC::DontDelete, Q_FUNC_INFO, "All properties must be undeletable"); + JSC::Identifier id = JSC::Identifier(exec, names[i]); + JSC::JSValue jsval = eng_p->scriptValueToJSCValue(values[i]); + props[i] = QScriptStaticScopeObject::PropertyInfo(id, jsval, attribs); + } + QScriptValue result = eng_p->scriptValueFromJSCValue(new (exec)QScriptStaticScopeObject(eng_p->staticScopeObjectStructure, + propertyCount, props)); + delete[] props; + return result; +} + +/*! + Creates a static scope object that's initially empty, but to which new + properties can be added. +*/ +QScriptValue QScriptDeclarativeClass::newStaticScopeObject(QScriptEngine *engine) +{ + QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine); + QScript::APIShim shim(eng_p); + return eng_p->scriptValueFromJSCValue(new (eng_p->currentFrame)QScriptStaticScopeObject(eng_p->staticScopeObjectStructure)); +} + QT_END_NAMESPACE diff --git a/src/script/bridge/qscriptdeclarativeclass_p.h b/src/script/bridge/qscriptdeclarativeclass_p.h index 714a67c..420b133 100644 --- a/src/script/bridge/qscriptdeclarativeclass_p.h +++ b/src/script/bridge/qscriptdeclarativeclass_p.h @@ -92,6 +92,11 @@ public: static QScriptValue scopeChainValue(QScriptContext *, int index); static QScriptContext *pushCleanContext(QScriptEngine *); + static QScriptValue newStaticScopeObject( + QScriptEngine *, int propertyCount, const QString *names, + const QScriptValue *values, const QScriptValue::PropertyFlags *flags); + static QScriptValue newStaticScopeObject(QScriptEngine *); + class Q_SCRIPT_EXPORT PersistentIdentifier { public: diff --git a/src/script/bridge/qscriptstaticscopeobject.cpp b/src/script/bridge/qscriptstaticscopeobject.cpp new file mode 100644 index 0000000..44548a4 --- /dev/null +++ b/src/script/bridge/qscriptstaticscopeobject.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtScript module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL-ONLY$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "config.h" +#include "qscriptstaticscopeobject_p.h" + +namespace JSC +{ + ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScriptStaticScopeObject)); +} + +QT_BEGIN_NAMESPACE + +/*! + \class QScriptStaticScopeObject + \internal + + Represents a static scope object. + + This class allows the VM to determine at JS script compile time whether + the object has a given property or not. If the object has the property, + a fast, index-based read/write operation will be used. If the object + doesn't have the property, the compiler knows it can safely skip this + object when dynamically resolving the property. Either way, this can + greatly improve performance. + + \sa QScriptContext::pushScope() +*/ + +const JSC::ClassInfo QScriptStaticScopeObject::info = { "QScriptStaticScopeObject", 0, 0, 0 }; + +/*! + Creates a static scope object with a fixed set of undeletable properties. + + It's not possible to add new properties to the object after construction. +*/ +QScriptStaticScopeObject::QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure, + int propertyCount, const PropertyInfo* props) + : JSC::JSVariableObject(structure, new Data(/*canGrow=*/false)) +{ + int index = growRegisterArray(propertyCount); + for (int i = 0; i < propertyCount; ++i, --index) { + const PropertyInfo& prop = props[i]; + JSC::SymbolTableEntry entry(index, prop.attributes); + symbolTable().add(prop.identifier.ustring().rep(), entry); + registerAt(index) = prop.value; + } +} + +/*! + Creates an empty static scope object. + + Properties can be added to the object after construction, either by + calling QScriptValue::setProperty(), or by pushing the object on the + scope chain; variable declarations ("var" statements) and function + declarations in JavaScript will create properties on the scope object. + + Note that once the scope object has been used in a closure and the + resulting function has been compiled, it's no longer safe to add + properties to the scope object (because the VM will bypass this + object the next time the function is executed). +*/ +QScriptStaticScopeObject::QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure) + : JSC::JSVariableObject(structure, new Data(/*canGrow=*/true)) +{ +} + +QScriptStaticScopeObject::~QScriptStaticScopeObject() +{ + delete d; +} + +bool QScriptStaticScopeObject::getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot& slot) +{ + return symbolTableGet(propertyName, slot); +} + +bool QScriptStaticScopeObject::getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor& descriptor) +{ + return symbolTableGet(propertyName, descriptor); +} + +void QScriptStaticScopeObject::putWithAttributes(JSC::ExecState* exec, const JSC::Identifier &propertyName, JSC::JSValue value, unsigned attributes) +{ + if (symbolTablePutWithAttributes(propertyName, value, attributes)) + return; + Q_ASSERT(d_ptr()->canGrow); + addSymbolTableProperty(propertyName, value, attributes); +} + +void QScriptStaticScopeObject::put(JSC::ExecState* exec, const JSC::Identifier& propertyName, JSC::JSValue value, JSC::PutPropertySlot&) +{ + if (symbolTablePut(propertyName, value)) + return; + Q_ASSERT(d_ptr()->canGrow); + addSymbolTableProperty(propertyName, value, /*attributes=*/0); +} + +bool QScriptStaticScopeObject::deleteProperty(JSC::ExecState*, const JSC::Identifier&) +{ + return false; +} + +void QScriptStaticScopeObject::markChildren(JSC::MarkStack& markStack) +{ + JSC::Register* registerArray = d_ptr()->registerArray.get(); + if (!registerArray) + return; + markStack.appendValues(reinterpret_cast<JSC::JSValue*>(registerArray), d_ptr()->registerArraySize); +} + +void QScriptStaticScopeObject::addSymbolTableProperty(const JSC::Identifier& name, JSC::JSValue value, unsigned attributes) +{ + int index = growRegisterArray(1); + JSC::SymbolTableEntry newEntry(index, attributes | JSC::DontDelete); + symbolTable().add(name.ustring().rep(), newEntry); + registerAt(index) = value; +} + +/*! + Grows the register array by \a count elements, and returns the offset of + the newly added elements (note that the register file grows downwards, + starting at index -1). +*/ +int QScriptStaticScopeObject::growRegisterArray(int count) +{ + size_t oldSize = d_ptr()->registerArraySize; + size_t newSize = oldSize + count; + JSC::Register* registerArray = new JSC::Register[newSize]; + if (d_ptr()->registerArray) + memcpy(registerArray + count, d_ptr()->registerArray.get(), oldSize * sizeof(JSC::Register)); + setRegisters(registerArray + newSize, registerArray); + d_ptr()->registerArraySize = newSize; + return -oldSize - 1; +} + +QT_END_NAMESPACE diff --git a/src/script/bridge/qscriptstaticscopeobject_p.h b/src/script/bridge/qscriptstaticscopeobject_p.h new file mode 100644 index 0000000..0a0e7ef --- /dev/null +++ b/src/script/bridge/qscriptstaticscopeobject_p.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtScript module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL-ONLY$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSCRIPTSTATICSCOPEOBJECT_P_H +#define QSCRIPTSTATICSCOPEOBJECT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qobjectdefs.h> + +#include "JSVariableObject.h" + +QT_BEGIN_NAMESPACE + +class QScriptStaticScopeObject : public JSC::JSVariableObject { +public: + struct PropertyInfo { + PropertyInfo(const JSC::Identifier& i, JSC::JSValue v, unsigned a) + : identifier(i), value(v), attributes(a) + { } + PropertyInfo() {} + + JSC::Identifier identifier; + JSC::JSValue value; + unsigned attributes; + }; + + QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure, + int propertyCount, const PropertyInfo*); + QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure); + virtual ~QScriptStaticScopeObject(); + + virtual bool isDynamicScope() const { return false; } + + virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&); + virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&); + + virtual void putWithAttributes(JSC::ExecState *exec, const JSC::Identifier &propertyName, JSC::JSValue value, unsigned attributes); + virtual void put(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue value, JSC::PutPropertySlot&); + + virtual bool deleteProperty(JSC::ExecState*, const JSC::Identifier& propertyName); + + virtual void markChildren(JSC::MarkStack&); + + virtual const JSC::ClassInfo* classInfo() const { return &info; } + static const JSC::ClassInfo info; + + static WTF::PassRefPtr<JSC::Structure> createStructure(JSC::JSValue proto) { + return JSC::Structure::create(proto, JSC::TypeInfo(JSC::ObjectType, StructureFlags)); + } + +protected: + static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | JSC::NeedsThisConversion | JSC::OverridesMarkChildren | JSC::OverridesGetPropertyNames | JSC::JSVariableObject::StructureFlags; + + struct Data : public JSVariableObjectData { + Data(bool canGrow_) + : JSVariableObjectData(&symbolTable, /*registers=*/0), + canGrow(canGrow_), registerArraySize(0) + { } + bool canGrow; + int registerArraySize; + JSC::SymbolTable symbolTable; + }; + + Data* d_ptr() const { return static_cast<Data*>(JSVariableObject::d); } + +private: + void addSymbolTableProperty(const JSC::Identifier&, JSC::JSValue, unsigned attributes); + int growRegisterArray(int); +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/script/parser/qscriptlexer.cpp b/src/script/parser/qscriptlexer.cpp index ca64776..3ddc3aa 100644 --- a/src/script/parser/qscriptlexer.cpp +++ b/src/script/parser/qscriptlexer.cpp @@ -31,7 +31,7 @@ QT_BEGIN_NAMESPACE -Q_DECL_IMPORT extern double qstrtod(const char *s00, char const **se, bool *ok); +Q_CORE_EXPORT double qstrtod(const char *s00, char const **se, bool *ok); #define shiftWindowsLineBreak() \ do { \ diff --git a/src/script/script.pri b/src/script/script.pri index 2ee1a82..9cd71d3 100644 --- a/src/script/script.pri +++ b/src/script/script.pri @@ -1,4 +1,3 @@ include($$PWD/api/api.pri) include($$PWD/bridge/bridge.pri) include($$PWD/parser/parser.pri) -include($$PWD/utils/utils.pri) diff --git a/src/script/utils/qscriptdate.cpp b/src/script/utils/qscriptdate.cpp deleted file mode 100644 index 5980256..0000000 --- a/src/script/utils/qscriptdate.cpp +++ /dev/null @@ -1,365 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtScript module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL-ONLY$ -** GNU Lesser General Public License Usage -** This file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qscriptdate_p.h" - -#include <QtCore/qnumeric.h> -#include <QtCore/qstringlist.h> - -#include <math.h> - -#ifndef Q_WS_WIN -# include <time.h> -# include <sys/time.h> -#else -# include <windows.h> -#endif - -QT_BEGIN_NAMESPACE - -namespace QScript { - -qsreal ToInteger(qsreal n); - -static const qsreal HoursPerDay = 24.0; -static const qsreal MinutesPerHour = 60.0; -static const qsreal SecondsPerMinute = 60.0; -static const qsreal msPerSecond = 1000.0; -static const qsreal msPerMinute = 60000.0; -static const qsreal msPerHour = 3600000.0; -static const qsreal msPerDay = 86400000.0; - -static qsreal LocalTZA = 0.0; // initialized at startup - -static inline qsreal TimeWithinDay(qsreal t) -{ - qsreal r = ::fmod(t, msPerDay); - return (r >= 0) ? r : r + msPerDay; -} - -static inline int HourFromTime(qsreal t) -{ - int r = int(::fmod(::floor(t / msPerHour), HoursPerDay)); - return (r >= 0) ? r : r + int(HoursPerDay); -} - -static inline int MinFromTime(qsreal t) -{ - int r = int(::fmod(::floor(t / msPerMinute), MinutesPerHour)); - return (r >= 0) ? r : r + int(MinutesPerHour); -} - -static inline int SecFromTime(qsreal t) -{ - int r = int(::fmod(::floor(t / msPerSecond), SecondsPerMinute)); - return (r >= 0) ? r : r + int(SecondsPerMinute); -} - -static inline int msFromTime(qsreal t) -{ - int r = int(::fmod(t, msPerSecond)); - return (r >= 0) ? r : r + int(msPerSecond); -} - -static inline qsreal Day(qsreal t) -{ - return ::floor(t / msPerDay); -} - -static inline qsreal DaysInYear(qsreal y) -{ - if (::fmod(y, 4)) - return 365; - - else if (::fmod(y, 100)) - return 366; - - else if (::fmod(y, 400)) - return 365; - - return 366; -} - -static inline qsreal DayFromYear(qsreal y) -{ - return 365 * (y - 1970) - + ::floor((y - 1969) / 4) - - ::floor((y - 1901) / 100) - + ::floor((y - 1601) / 400); -} - -static inline qsreal TimeFromYear(qsreal y) -{ - return msPerDay * DayFromYear(y); -} - -static inline qsreal YearFromTime(qsreal t) -{ - int y = 1970; - y += (int) ::floor(t / (msPerDay * 365.2425)); - - qsreal t2 = TimeFromYear(y); - return (t2 > t) ? y - 1 : ((t2 + msPerDay * DaysInYear(y)) <= t) ? y + 1 : y; -} - -static inline bool InLeapYear(qsreal t) -{ - qsreal x = DaysInYear(YearFromTime(t)); - if (x == 365) - return 0; - - Q_ASSERT (x == 366); - return 1; -} - -static inline qsreal DayWithinYear(qsreal t) -{ - return Day(t) - DayFromYear(YearFromTime(t)); -} - -static inline qsreal MonthFromTime(qsreal t) -{ - qsreal d = DayWithinYear(t); - qsreal l = InLeapYear(t); - - if (d < 31.0) - return 0; - - else if (d < 59.0 + l) - return 1; - - else if (d < 90.0 + l) - return 2; - - else if (d < 120.0 + l) - return 3; - - else if (d < 151.0 + l) - return 4; - - else if (d < 181.0 + l) - return 5; - - else if (d < 212.0 + l) - return 6; - - else if (d < 243.0 + l) - return 7; - - else if (d < 273.0 + l) - return 8; - - else if (d < 304.0 + l) - return 9; - - else if (d < 334.0 + l) - return 10; - - else if (d < 365.0 + l) - return 11; - - return qSNaN(); // ### assert? -} - -static inline qsreal DateFromTime(qsreal t) -{ - int m = (int) ToInteger(MonthFromTime(t)); - qsreal d = DayWithinYear(t); - qsreal l = InLeapYear(t); - - switch (m) { - case 0: return d + 1.0; - case 1: return d - 30.0; - case 2: return d - 58.0 - l; - case 3: return d - 89.0 - l; - case 4: return d - 119.0 - l; - case 5: return d - 150.0 - l; - case 6: return d - 180.0 - l; - case 7: return d - 211.0 - l; - case 8: return d - 242.0 - l; - case 9: return d - 272.0 - l; - case 10: return d - 303.0 - l; - case 11: return d - 333.0 - l; - } - - return qSNaN(); // ### assert -} - -static inline qsreal WeekDay(qsreal t) -{ - qsreal r = ::fmod (Day(t) + 4.0, 7.0); - return (r >= 0) ? r : r + 7.0; -} - - -static inline qsreal MakeTime(qsreal hour, qsreal min, qsreal sec, qsreal ms) -{ - return ((hour * MinutesPerHour + min) * SecondsPerMinute + sec) * msPerSecond + ms; -} - -static inline qsreal DayFromMonth(qsreal month, qsreal leap) -{ - switch ((int) month) { - case 0: return 0; - case 1: return 31.0; - case 2: return 59.0 + leap; - case 3: return 90.0 + leap; - case 4: return 120.0 + leap; - case 5: return 151.0 + leap; - case 6: return 181.0 + leap; - case 7: return 212.0 + leap; - case 8: return 243.0 + leap; - case 9: return 273.0 + leap; - case 10: return 304.0 + leap; - case 11: return 334.0 + leap; - } - - return qSNaN(); // ### assert? -} - -static qsreal MakeDay(qsreal year, qsreal month, qsreal day) -{ - year += ::floor(month / 12.0); - - month = ::fmod(month, 12.0); - if (month < 0) - month += 12.0; - - qsreal t = TimeFromYear(year); - qsreal leap = InLeapYear(t); - - day += ::floor(t / msPerDay); - day += DayFromMonth(month, leap); - - return day - 1; -} - -static inline qsreal MakeDate(qsreal day, qsreal time) -{ - return day * msPerDay + time; -} - -static inline qsreal DaylightSavingTA(double t) -{ -#ifndef Q_WS_WIN - long int tt = (long int)(t / msPerSecond); - struct tm *tmtm = localtime((const time_t*)&tt); - if (! tmtm) - return 0; - return (tmtm->tm_isdst > 0) ? msPerHour : 0; -#else - Q_UNUSED(t); - /// ### implement me - return 0; -#endif -} - -static inline qsreal LocalTime(qsreal t) -{ - return t + LocalTZA + DaylightSavingTA(t); -} - -static inline qsreal UTC(qsreal t) -{ - return t - LocalTZA - DaylightSavingTA(t - LocalTZA); -} - -static inline qsreal TimeClip(qsreal t) -{ - if (! qIsFinite(t) || fabs(t) > 8.64e15) - return qSNaN(); - return ToInteger(t); -} - -static qsreal getLocalTZA() -{ -#ifndef Q_WS_WIN - struct tm* t; - time_t curr; - time(&curr); - t = localtime(&curr); - time_t locl = mktime(t); - t = gmtime(&curr); - time_t globl = mktime(t); - return double(locl - globl) * 1000.0; -#else - TIME_ZONE_INFORMATION tzInfo; - GetTimeZoneInformation(&tzInfo); - return -tzInfo.Bias * 60.0 * 1000.0; -#endif -} - -/*! - \internal - - Converts the QDateTime \a dt to an ECMA Date value (in UTC form). -*/ -qsreal FromDateTime(const QDateTime &dt) -{ - if (!dt.isValid()) - return qSNaN(); - if (!LocalTZA) // ### move - LocalTZA = getLocalTZA(); - QDate date = dt.date(); - QTime taim = dt.time(); - int year = date.year(); - int month = date.month() - 1; - int day = date.day(); - int hours = taim.hour(); - int mins = taim.minute(); - int secs = taim.second(); - int ms = taim.msec(); - double t = MakeDate(MakeDay(year, month, day), - MakeTime(hours, mins, secs, ms)); - if (dt.timeSpec() == Qt::LocalTime) - t = UTC(t); - return TimeClip(t); -} - -/*! - \internal - - Converts the ECMA Date value \tt (in UTC form) to QDateTime - according to \a spec. -*/ -QDateTime ToDateTime(qsreal t, Qt::TimeSpec spec) -{ - if (qIsNaN(t)) - return QDateTime(); - if (!LocalTZA) // ### move - LocalTZA = getLocalTZA(); - if (spec == Qt::LocalTime) - t = LocalTime(t); - int year = int(YearFromTime(t)); - int month = int(MonthFromTime(t) + 1); - int day = int(DateFromTime(t)); - int hours = HourFromTime(t); - int mins = MinFromTime(t); - int secs = SecFromTime(t); - int ms = msFromTime(t); - return QDateTime(QDate(year, month, day), QTime(hours, mins, secs, ms), spec); -} - -} // namespace QScript - -QT_END_NAMESPACE diff --git a/src/script/utils/qscriptdate_p.h b/src/script/utils/qscriptdate_p.h deleted file mode 100644 index b9c9fd4..0000000 --- a/src/script/utils/qscriptdate_p.h +++ /dev/null @@ -1,52 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtScript module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL-ONLY$ -** GNU Lesser General Public License Usage -** This file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSCRIPTDATE_P_H -#define QSCRIPTDATE_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include <QtCore/qdatetime.h> - -QT_BEGIN_NAMESPACE - -typedef double qsreal; - -namespace QScript -{ - qsreal FromDateTime(const QDateTime &dt); - QDateTime ToDateTime(qsreal t, Qt::TimeSpec spec); -} - -QT_END_NAMESPACE - -#endif diff --git a/src/script/utils/utils.pri b/src/script/utils/utils.pri deleted file mode 100644 index d8302d5..0000000 --- a/src/script/utils/utils.pri +++ /dev/null @@ -1,5 +0,0 @@ -SOURCES += \ - $$PWD/qscriptdate.cpp - -HEADERS += \ - $$PWD/qscriptdate_p.h |