diff options
Diffstat (limited to 'src/declarative/qml/v8/qv8valuetypewrapper.cpp')
-rw-r--r-- | src/declarative/qml/v8/qv8valuetypewrapper.cpp | 105 |
1 files changed, 85 insertions, 20 deletions
diff --git a/src/declarative/qml/v8/qv8valuetypewrapper.cpp b/src/declarative/qml/v8/qv8valuetypewrapper.cpp index f3100cf7b5..e459fee71f 100644 --- a/src/declarative/qml/v8/qv8valuetypewrapper.cpp +++ b/src/declarative/qml/v8/qv8valuetypewrapper.cpp @@ -7,29 +7,29 @@ ** This file is part of the QtDeclarative module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** ** GNU Lesser General Public License Usage -** Alternatively, 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. +** 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. ** ** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception +** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. ** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. ** ** ** @@ -103,15 +103,21 @@ QV8ValueTypeWrapper::~QV8ValueTypeWrapper() void QV8ValueTypeWrapper::destroy() { + qPersistentDispose(m_toString); qPersistentDispose(m_constructor); } void QV8ValueTypeWrapper::init(QV8Engine *engine) { m_engine = engine; + m_toString = qPersistentNew<v8::Function>(v8::FunctionTemplate::New(ToString)->GetFunction()); v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New(); ft->InstanceTemplate()->SetNamedPropertyHandler(Getter, Setter); ft->InstanceTemplate()->SetHasExternalResource(true); + ft->InstanceTemplate()->MarkAsUseUserObjectComparison(); + ft->InstanceTemplate()->SetAccessor(v8::String::New("toString"), ToStringGetter, 0, + m_toString, v8::DEFAULT, + v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete)); m_constructor = qPersistentNew<v8::Function>(ft->GetFunction()); } @@ -166,26 +172,85 @@ QVariant QV8ValueTypeWrapper::toVariant(QV8ObjectResource *r) } } +bool QV8ValueTypeWrapper::isEqual(QV8ObjectResource *r, const QVariant& value) +{ + Q_ASSERT(r->resourceType() == QV8ObjectResource::ValueTypeType); + QV8ValueTypeResource *resource = static_cast<QV8ValueTypeResource *>(r); + + if (resource->objectType == QV8ValueTypeResource::Reference) { + QV8ValueTypeReferenceResource *reference = static_cast<QV8ValueTypeReferenceResource *>(resource); + if (reference->object) { + reference->type->read(reference->object, reference->property); + return reference->type->isEqual(value); + } else { + return false; + } + } else { + Q_ASSERT(resource->objectType == QV8ValueTypeResource::Copy); + QV8ValueTypeCopyResource *copy = static_cast<QV8ValueTypeCopyResource *>(resource); + return (value == copy->value); + } +} + +v8::Handle<v8::Value> QV8ValueTypeWrapper::ToStringGetter(v8::Local<v8::String> property, + const v8::AccessorInfo &info) +{ + Q_UNUSED(property); + return info.Data(); +} + +v8::Handle<v8::Value> QV8ValueTypeWrapper::ToString(const v8::Arguments &args) +{ + QV8ValueTypeResource *resource = v8_resource_cast<QV8ValueTypeResource>(args.This()); + if (resource) { + if (resource->objectType == QV8ValueTypeResource::Reference) { + QV8ValueTypeReferenceResource *reference = static_cast<QV8ValueTypeReferenceResource *>(resource); + if (reference->object) { + reference->type->read(reference->object, reference->property); + return resource->engine->toString(resource->type->toString()); + } else { + return v8::Undefined(); + } + } else { + Q_ASSERT(resource->objectType == QV8ValueTypeResource::Copy); + QV8ValueTypeCopyResource *copy = static_cast<QV8ValueTypeCopyResource *>(resource); + QString result = copy->value.toString(); + if (result.isEmpty() && !copy->value.canConvert(QVariant::String)) { + result = QString::fromLatin1("QVariant(%0)").arg(QString::fromLatin1(copy->value.typeName())); + } + return resource->engine->toString(result); + } + } else { + return v8::Undefined(); + } +} + v8::Handle<v8::Value> QV8ValueTypeWrapper::Getter(v8::Local<v8::String> property, const v8::AccessorInfo &info) { QV8ValueTypeResource *r = v8_resource_cast<QV8ValueTypeResource>(info.This()); - if (!r) return v8::Undefined(); + if (!r) return v8::Handle<v8::Value>(); // XXX This is horribly inefficient. Sadly people seem to have taken a liking to // value type properties, so we should probably try and optimize it a little. // We should probably just replace all value properties with dedicated accessors. QByteArray propName = r->engine->toString(property).toUtf8(); + if (propName == QByteArray("toString")) { + return r->engine->valueTypeWrapper()->m_toString; + } + int index = r->type->metaObject()->indexOfProperty(propName.constData()); if (index == -1) - return v8::Undefined(); + return v8::Handle<v8::Value>(); + if (r->objectType == QV8ValueTypeResource::Reference) { QV8ValueTypeReferenceResource *reference = static_cast<QV8ValueTypeReferenceResource *>(r); if (!reference->object) - return v8::Undefined(); + return v8::Handle<v8::Value>(); + r->type->read(reference->object, reference->property); } else { |