1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qv8listwrapper_p.h"
43 #include "qv8engine_p.h"
44 #include <private/qdeclarativelist_p.h>
48 class QV8ListResource : public QV8ObjectResource
50 V8_RESOURCE_TYPE(ListType);
52 QV8ListResource(QV8Engine *engine) : QV8ObjectResource(engine) {}
54 QDeclarativeGuard<QObject> object;
55 QDeclarativeListProperty<QObject> property;
59 QV8ListWrapper::QV8ListWrapper()
64 QV8ListWrapper::~QV8ListWrapper()
68 void QV8ListWrapper::init(QV8Engine *engine)
71 v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
72 ft->InstanceTemplate()->SetFallbackPropertyHandler(Getter, Setter);
73 ft->InstanceTemplate()->SetIndexedPropertyHandler(IndexedGetter);
74 ft->InstanceTemplate()->SetAccessor(v8::String::New("length"), LengthGetter, 0,
75 v8::Handle<v8::Value>(), v8::DEFAULT,
76 v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
77 ft->InstanceTemplate()->SetHasExternalResource(true);
78 m_constructor = qPersistentNew<v8::Function>(ft->GetFunction());
81 void QV8ListWrapper::destroy()
83 qPersistentDispose(m_constructor);
86 v8::Handle<v8::Value> QV8ListWrapper::newList(QObject *object, int propId, int propType)
88 if (!object || propId == -1)
91 // XXX NewInstance() should be optimized
92 v8::Local<v8::Object> rv = m_constructor->NewInstance();
93 QV8ListResource *r = new QV8ListResource(m_engine);
95 r->propertyType = propType;
96 void *args[] = { &r->property, 0 };
97 QMetaObject::metacall(object, QMetaObject::ReadProperty, propId, args);
98 rv->SetExternalResource(r);
102 v8::Handle<v8::Value> QV8ListWrapper::newList(const QDeclarativeListProperty<QObject> &prop, int propType)
104 // XXX NewInstance() should be optimized
105 v8::Local<v8::Object> rv = m_constructor->NewInstance();
106 QV8ListResource *r = new QV8ListResource(m_engine);
107 r->object = prop.object;
109 r->propertyType = propType;
110 rv->SetExternalResource(r);
114 QVariant QV8ListWrapper::toVariant(v8::Handle<v8::Object> obj)
116 QV8ListResource *resource = v8_resource_cast<QV8ListResource>(obj);
117 if (resource) return toVariant(resource);
118 else return QVariant();
121 QVariant QV8ListWrapper::toVariant(QV8ObjectResource *r)
123 Q_ASSERT(r->resourceType() == QV8ObjectResource::ListType);
124 QV8ListResource *resource = static_cast<QV8ListResource *>(r);
126 if (!resource->object)
129 return QVariant::fromValue(QDeclarativeListReferencePrivate::init(resource->property, resource->propertyType,
130 m_engine->engine()));
133 v8::Handle<v8::Value> QV8ListWrapper::Getter(v8::Local<v8::String> property,
134 const v8::AccessorInfo &info)
138 return v8::Handle<v8::Value>();
141 v8::Handle<v8::Value> QV8ListWrapper::Setter(v8::Local<v8::String> property,
142 v8::Local<v8::Value> value,
143 const v8::AccessorInfo &info)
150 v8::Handle<v8::Value> QV8ListWrapper::IndexedGetter(uint32_t index, const v8::AccessorInfo &info)
152 QV8ListResource *resource = v8_resource_cast<QV8ListResource>(info.This());
154 if (!resource || resource->object.isNull()) return v8::Undefined();
156 quint32 count = resource->property.count?resource->property.count(&resource->property):0;
157 if (index < count && resource->property.at) {
158 return resource->engine->newQObject(resource->property.at(&resource->property, index));
160 return v8::Undefined();
164 v8::Handle<v8::Value> QV8ListWrapper::LengthGetter(v8::Local<v8::String> property,
165 const v8::AccessorInfo &info)
169 QV8ListResource *resource = v8_resource_cast<QV8ListResource>(info.This());
171 if (!resource || resource->object.isNull()) return v8::Undefined();
173 quint32 count = resource->property.count?resource->property.count(&resource->property):0;
175 return v8::Integer::NewFromUnsigned(count);