ccbe1f74b44b2d26c180a913c03ea4f6b5b827d9
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qv8listwrapper.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qv8listwrapper_p.h"
43 #include "qv8engine_p.h"
44 #include <private/qdeclarativelist_p.h>
45
46 QT_BEGIN_NAMESPACE
47
48 class QV8ListResource : public QV8ObjectResource
49 {
50     V8_RESOURCE_TYPE(ListType);
51 public:
52     QV8ListResource(QV8Engine *engine) : QV8ObjectResource(engine) {}
53
54     QDeclarativeGuard<QObject> object;
55     QDeclarativeListProperty<QObject> property;
56     int propertyType;
57 };
58
59 QV8ListWrapper::QV8ListWrapper()
60 : m_engine(0)
61 {
62 }
63
64 QV8ListWrapper::~QV8ListWrapper()
65 {
66 }
67
68 void QV8ListWrapper::init(QV8Engine *engine)
69 {
70     m_engine = 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());
79 }
80
81 void QV8ListWrapper::destroy()
82 {
83     qPersistentDispose(m_constructor);
84 }
85
86 v8::Handle<v8::Value> QV8ListWrapper::newList(QObject *object, int propId, int propType)
87 {
88     if (!object || propId == -1)
89         return v8::Null();
90
91     // XXX NewInstance() should be optimized
92     v8::Local<v8::Object> rv = m_constructor->NewInstance(); 
93     QV8ListResource *r = new QV8ListResource(m_engine);
94     r->object = object;
95     r->propertyType = propType;
96     void *args[] = { &r->property, 0 };
97     QMetaObject::metacall(object, QMetaObject::ReadProperty, propId, args);
98     rv->SetExternalResource(r);
99     return rv;
100 }
101
102 v8::Handle<v8::Value> QV8ListWrapper::newList(const QDeclarativeListProperty<QObject> &prop, int propType)
103 {
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;
108     r->property = prop;
109     r->propertyType = propType;
110     rv->SetExternalResource(r);
111     return rv;
112 }
113
114 QVariant QV8ListWrapper::toVariant(v8::Handle<v8::Object> obj)
115 {
116     QV8ListResource *resource = v8_resource_cast<QV8ListResource>(obj);
117     if (resource) return toVariant(resource);
118     else return QVariant();
119 }
120
121 QVariant QV8ListWrapper::toVariant(QV8ObjectResource *r)
122 {
123     Q_ASSERT(r->resourceType() == QV8ObjectResource::ListType);
124     QV8ListResource *resource = static_cast<QV8ListResource *>(r);
125
126     if (!resource->object)
127         return QVariant();
128
129     return QVariant::fromValue(QDeclarativeListReferencePrivate::init(resource->property, resource->propertyType, 
130                                                                       m_engine->engine()));
131 }
132
133 v8::Handle<v8::Value> QV8ListWrapper::Getter(v8::Local<v8::String> property, 
134                                              const v8::AccessorInfo &info)
135 {
136     Q_UNUSED(property);
137     Q_UNUSED(info);
138     return v8::Handle<v8::Value>();
139 }
140
141 v8::Handle<v8::Value> QV8ListWrapper::Setter(v8::Local<v8::String> property, 
142                                              v8::Local<v8::Value> value,
143                                              const v8::AccessorInfo &info)
144 {
145     Q_UNUSED(property);
146     Q_UNUSED(info);
147     return value;
148 }
149
150 v8::Handle<v8::Value> QV8ListWrapper::IndexedGetter(uint32_t index, const v8::AccessorInfo &info)
151 {
152     QV8ListResource *resource = v8_resource_cast<QV8ListResource>(info.This());
153
154     if (!resource || resource->object.isNull()) return v8::Undefined();
155
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));
159     } else {
160         return v8::Undefined();
161     }
162 }
163
164 v8::Handle<v8::Value> QV8ListWrapper::LengthGetter(v8::Local<v8::String> property, 
165                                                    const v8::AccessorInfo &info)
166 {
167     Q_UNUSED(property);
168
169     QV8ListResource *resource = v8_resource_cast<QV8ListResource>(info.This());
170
171     if (!resource || resource->object.isNull()) return v8::Undefined();
172
173     quint32 count = resource->property.count?resource->property.count(&resource->property):0;
174
175     return v8::Integer::NewFromUnsigned(count);
176 }
177
178 v8::Handle<v8::Array> QV8ListWrapper::Enumerator(const v8::AccessorInfo &info)
179 {
180     QV8ListResource *resource = v8_resource_cast<QV8ListResource>(info.This());
181
182     if (!resource || resource->object.isNull()) return v8::Array::New();
183
184     quint32 count = resource->property.count?resource->property.count(&resource->property):0;
185
186     v8::Local<v8::Array> rv = v8::Array::New(count);
187
188     for (uint ii = 0; ii < count; ++ii)
189         rv->Set(ii, v8::Number::New(ii));
190
191     return rv;
192 }
193
194 QT_END_NAMESPACE