1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qv8listwrapper_p.h"
43 #include "qv8engine_p.h"
44 #include <private/qqmllist_p.h>
48 class QV8ListResource : public QV8ObjectResource
50 V8_RESOURCE_TYPE(ListType);
52 QV8ListResource(QV8Engine *engine) : QV8ObjectResource(engine) {}
54 QQmlGuard<QObject> object;
55 QQmlListProperty<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, 0, 0, Enumerator);
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 | v8::DontEnum));
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 QQmlListProperty<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(QQmlListReferencePrivate::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);
178 v8::Handle<v8::Array> QV8ListWrapper::Enumerator(const v8::AccessorInfo &info)
180 QV8ListResource *resource = v8_resource_cast<QV8ListResource>(info.This());
182 if (!resource || resource->object.isNull()) return v8::Array::New();
184 quint32 count = resource->property.count?resource->property.count(&resource->property):0;
186 v8::Local<v8::Array> rv = v8::Array::New(count);
188 for (uint ii = 0; ii < count; ++ii)
189 rv->Set(ii, v8::Number::New(ii));