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 "qv8contextwrapper_p.h"
43 #include "qv8engine_p.h"
45 #include <private/qdeclarativeengine_p.h>
46 #include <private/qdeclarativecontext_p.h>
50 class QV8TypeResource : public QV8ObjectResource
52 V8_RESOURCE_TYPE(TypeType);
55 QV8TypeResource(QV8Engine *engine);
56 virtual ~QV8TypeResource();
58 QV8TypeWrapper::TypeNameMode mode;
60 QDeclarativeGuard<QObject> object;
61 QDeclarativeType *type;
62 QDeclarativeTypeNameCache *typeNamespace;
65 QV8TypeResource::QV8TypeResource(QV8Engine *engine)
66 : QV8ObjectResource(engine), mode(QV8TypeWrapper::IncludeEnums), type(0), typeNamespace(0)
70 QV8TypeResource::~QV8TypeResource()
72 if (typeNamespace) typeNamespace->release();
75 QV8TypeWrapper::QV8TypeWrapper()
80 QV8TypeWrapper::~QV8TypeWrapper()
84 void QV8TypeWrapper::destroy()
86 qPersistentDispose(m_constructor);
89 void QV8TypeWrapper::init(QV8Engine *engine)
92 v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
93 ft->InstanceTemplate()->SetNamedPropertyHandler(Getter, Setter);
94 ft->InstanceTemplate()->SetHasExternalResource(true);
95 m_constructor = qPersistentNew<v8::Function>(ft->GetFunction());
98 v8::Local<v8::Object> QV8TypeWrapper::newObject(QObject *o, QDeclarativeType *t, TypeNameMode mode)
101 // XXX aakenned - NewInstance() is slow for our case
102 v8::Local<v8::Object> rv = m_constructor->NewInstance();
103 QV8TypeResource *r = new QV8TypeResource(m_engine);
104 r->mode = mode; r->object = o; r->type = t;
105 rv->SetExternalResource(r);
109 v8::Local<v8::Object> QV8TypeWrapper::newObject(QObject *o, QDeclarativeTypeNameCache *t, TypeNameMode mode)
112 // XXX aakenned - NewInstance() is slow for our case
113 v8::Local<v8::Object> rv = m_constructor->NewInstance();
114 QV8TypeResource *r = new QV8TypeResource(m_engine);
116 r->mode = mode; r->object = o; r->typeNamespace = t;
117 rv->SetExternalResource(r);
121 v8::Handle<v8::Value> QV8TypeWrapper::Getter(v8::Local<v8::String> property,
122 const v8::AccessorInfo &info)
124 v8::Object::ExternalResource *r = info.This()->GetExternalResource();
125 QV8TypeResource *resource = v8_resource_cast<QV8TypeResource>(info.This());
128 return v8::Undefined();
130 QV8Engine *v8engine = resource->engine;
131 QObject *object = resource->object;
133 if (resource->type) {
134 QDeclarativeType *type = resource->type;
136 if (QV8Engine::startsWithUpper(property)) {
137 if (resource->mode == IncludeEnums) {
138 QString name = v8engine->toString(property);
141 QByteArray enumName = name.toUtf8();
142 const QMetaObject *metaObject = type->baseMetaObject();
143 for (int ii = metaObject->enumeratorCount() - 1; ii >= 0; --ii) {
144 QMetaEnum e = metaObject->enumerator(ii);
145 int value = e.keyToValue(enumName.constData());
147 return v8::Integer::New(value);
151 // Fall through to undefined
153 } else if (resource->object) {
154 QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(), object);
156 return v8engine->qobjectWrapper()->getProperty(ao, property, QV8QObjectWrapper::IgnoreRevision);
158 // Fall through to undefined
161 // Fall through to undefined
163 } else if (resource->typeNamespace) {
165 QDeclarativeTypeNameCache *typeNamespace = resource->typeNamespace;
166 QDeclarativeTypeNameCache::Data *d = typeNamespace->data(property);
167 Q_ASSERT(!d || !d->typeNamespace); // Nested namespaces not supported
170 return v8engine->typeWrapper()->newObject(object, d->type, resource->mode);
171 } else if (QDeclarativeMetaType::ModuleApiInstance *moduleApi = typeNamespace->moduleApi()) {
173 // XXX QtScript/JSC required
174 return v8::Undefined();
178 // Fall through to undefined
181 Q_ASSERT(!"Unreachable");
183 return v8::Undefined();
186 v8::Handle<v8::Value> QV8TypeWrapper::Setter(v8::Local<v8::String> property,
187 v8::Local<v8::Value> value,
188 const v8::AccessorInfo &info)
190 v8::Object::ExternalResource *r = info.This()->GetExternalResource();
191 QV8TypeResource *resource = v8_resource_cast<QV8TypeResource>(info.This());
196 QV8Engine *v8engine = resource->engine;
200 if (resource->type && resource->object) {
201 QDeclarativeType *type = resource->type;
202 QObject *object = resource->object;
203 QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(), object);
205 v8engine->qobjectWrapper()->setProperty(ao, property, value, QV8QObjectWrapper::IgnoreRevision);