1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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));