1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qv8contextwrapper_p.h"
43 #include "qv8engine_p.h"
45 #include <private/qdeclarativeengine_p.h>
46 #include <private/qdeclarativecontext_p.h>
48 #include <private/qjsvalue_p.h>
49 #include <private/qscript_impl_p.h>
53 class QV8TypeResource : public QV8ObjectResource
55 V8_RESOURCE_TYPE(TypeType);
58 QV8TypeResource(QV8Engine *engine);
59 virtual ~QV8TypeResource();
61 QV8TypeWrapper::TypeNameMode mode;
63 QDeclarativeGuard<QObject> object;
65 QDeclarativeType *type;
66 QDeclarativeTypeNameCache *typeNamespace;
67 const void *importNamespace;
70 QV8TypeResource::QV8TypeResource(QV8Engine *engine)
71 : QV8ObjectResource(engine), mode(QV8TypeWrapper::IncludeEnums), type(0), typeNamespace(0), importNamespace(0)
75 QV8TypeResource::~QV8TypeResource()
77 if (typeNamespace) typeNamespace->release();
80 QV8TypeWrapper::QV8TypeWrapper()
85 QV8TypeWrapper::~QV8TypeWrapper()
89 void QV8TypeWrapper::destroy()
91 qPersistentDispose(m_constructor);
94 void QV8TypeWrapper::init(QV8Engine *engine)
97 v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
98 ft->InstanceTemplate()->SetNamedPropertyHandler(Getter, Setter);
99 ft->InstanceTemplate()->SetHasExternalResource(true);
100 m_constructor = qPersistentNew<v8::Function>(ft->GetFunction());
103 // Returns a type wrapper for type t on o. This allows access of enums, and attached properties.
104 v8::Local<v8::Object> QV8TypeWrapper::newObject(QObject *o, QDeclarativeType *t, TypeNameMode mode)
107 // XXX NewInstance() should be optimized
108 v8::Local<v8::Object> rv = m_constructor->NewInstance();
109 QV8TypeResource *r = new QV8TypeResource(m_engine);
110 r->mode = mode; r->object = o; r->type = t;
111 rv->SetExternalResource(r);
115 // Returns a type wrapper for importNamespace (of t) on o. This allows nested resolution of a type in a
117 v8::Local<v8::Object> QV8TypeWrapper::newObject(QObject *o, QDeclarativeTypeNameCache *t,
118 const void *importNamespace, TypeNameMode mode)
121 Q_ASSERT(importNamespace);
122 // XXX NewInstance() should be optimized
123 v8::Local<v8::Object> rv = m_constructor->NewInstance();
124 QV8TypeResource *r = new QV8TypeResource(m_engine);
126 r->mode = mode; r->object = o; r->typeNamespace = t; r->importNamespace = importNamespace;
127 rv->SetExternalResource(r);
131 QVariant QV8TypeWrapper::toVariant(QV8ObjectResource *r)
133 Q_ASSERT(r->resourceType() == QV8ObjectResource::TypeType);
134 QV8TypeResource *resource = static_cast<QV8TypeResource *>(r);
135 QV8Engine *v8engine = resource->engine;
137 if (resource->typeNamespace) {
138 if (QDeclarativeMetaType::ModuleApiInstance *moduleApi = resource->typeNamespace->moduleApi(resource->importNamespace)) {
139 if (moduleApi->scriptCallback) {
140 moduleApi->scriptApi = moduleApi->scriptCallback(v8engine->engine(), v8engine->engine());
141 moduleApi->scriptCallback = 0;
142 moduleApi->qobjectCallback = 0;
143 } else if (moduleApi->qobjectCallback) {
144 moduleApi->qobjectApi = moduleApi->qobjectCallback(v8engine->engine(), v8engine->engine());
145 moduleApi->scriptCallback = 0;
146 moduleApi->qobjectCallback = 0;
149 if (moduleApi->qobjectApi) {
150 return QVariant::fromValue<QObject*>(moduleApi->qobjectApi);
155 // only QObject Module API can be converted to a variant.
159 v8::Handle<v8::Value> QV8TypeWrapper::Getter(v8::Local<v8::String> property,
160 const v8::AccessorInfo &info)
162 QV8TypeResource *resource = v8_resource_cast<QV8TypeResource>(info.This());
165 return v8::Undefined();
167 QV8Engine *v8engine = resource->engine;
168 QObject *object = resource->object;
170 QHashedV8String propertystring(property);
172 if (resource->type) {
173 QDeclarativeType *type = resource->type;
175 if (QV8Engine::startsWithUpper(property)) {
176 int value = type->enumValue(propertystring);
178 return v8::Integer::New(value);
180 // Fall through to return empty handle
182 } else if (resource->object) {
183 QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(), object);
185 return v8engine->qobjectWrapper()->getProperty(ao, propertystring,
186 QV8QObjectWrapper::IgnoreRevision);
188 // Fall through to return empty handle
191 // Fall through to return empty handle
193 } else if (resource->typeNamespace) {
194 Q_ASSERT(resource->importNamespace);
195 QDeclarativeTypeNameCache::Result r = resource->typeNamespace->query(propertystring,
196 resource->importNamespace);
201 return v8engine->typeWrapper()->newObject(object, r.type, resource->mode);
202 } else if (QDeclarativeMetaType::ModuleApiInstance *moduleApi = resource->typeNamespace->moduleApi(resource->importNamespace)) {
204 if (moduleApi->scriptCallback) {
205 moduleApi->scriptApi = moduleApi->scriptCallback(v8engine->engine(), v8engine->engine());
206 moduleApi->scriptCallback = 0;
207 moduleApi->qobjectCallback = 0;
208 } else if (moduleApi->qobjectCallback) {
209 moduleApi->qobjectApi = moduleApi->qobjectCallback(v8engine->engine(), v8engine->engine());
210 moduleApi->scriptCallback = 0;
211 moduleApi->qobjectCallback = 0;
214 if (moduleApi->qobjectApi) {
215 // check for enum value
216 if (QV8Engine::startsWithUpper(property)) {
217 if (resource->mode == IncludeEnums) {
218 QString name = v8engine->toString(property);
221 QByteArray enumName = name.toUtf8();
222 const QMetaObject *metaObject = moduleApi->qobjectApi->metaObject();
223 for (int ii = metaObject->enumeratorCount() - 1; ii >= 0; --ii) {
224 QMetaEnum e = metaObject->enumerator(ii);
226 int value = e.keyToValue(enumName.constData(), &ok);
228 return v8::Integer::New(value);
233 // check for property.
234 v8::Handle<v8::Value> rv = v8engine->qobjectWrapper()->getProperty(moduleApi->qobjectApi, propertystring, QV8QObjectWrapper::IgnoreRevision);
236 } else if (moduleApi->scriptApi.isValid()) {
237 // NOTE: if used in a binding, changes will not trigger re-evaluation since non-NOTIFYable.
238 QJSValuePrivate *apiprivate = QJSValuePrivate::get(moduleApi->scriptApi);
239 QScopedPointer<QJSValuePrivate> propertyValue(apiprivate->property(property).give());
240 return propertyValue->asV8Value(v8engine);
242 return v8::Handle<v8::Value>();
246 // Fall through to return empty handle
249 Q_ASSERT(!"Unreachable");
252 return v8::Handle<v8::Value>();
255 v8::Handle<v8::Value> QV8TypeWrapper::Setter(v8::Local<v8::String> property,
256 v8::Local<v8::Value> value,
257 const v8::AccessorInfo &info)
259 QV8TypeResource *resource = v8_resource_cast<QV8TypeResource>(info.This());
264 QV8Engine *v8engine = resource->engine;
266 QHashedV8String propertystring(property);
268 if (resource->type && resource->object) {
269 QDeclarativeType *type = resource->type;
270 QObject *object = resource->object;
271 QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(), object);
273 v8engine->qobjectWrapper()->setProperty(ao, propertystring, value,
274 QV8QObjectWrapper::IgnoreRevision);
275 } else if (resource->typeNamespace) {
276 if (QDeclarativeMetaType::ModuleApiInstance *moduleApi = resource->typeNamespace->moduleApi(resource->importNamespace)) {
277 if (moduleApi->scriptCallback) {
278 moduleApi->scriptApi = moduleApi->scriptCallback(v8engine->engine(), v8engine->engine());
279 moduleApi->scriptCallback = 0;
280 moduleApi->qobjectCallback = 0;
281 } else if (moduleApi->qobjectCallback) {
282 moduleApi->qobjectApi = moduleApi->qobjectCallback(v8engine->engine(), v8engine->engine());
283 moduleApi->scriptCallback = 0;
284 moduleApi->qobjectCallback = 0;
287 if (moduleApi->qobjectApi) {
288 v8engine->qobjectWrapper()->setProperty(moduleApi->qobjectApi, propertystring, value,
289 QV8QObjectWrapper::IgnoreRevision);
290 } else if (moduleApi->scriptApi.isValid()) {
291 QScopedPointer<QJSValuePrivate> setvalp(new QJSValuePrivate(v8engine, value));
292 QJSValuePrivate *apiprivate = QJSValuePrivate::get(moduleApi->scriptApi);
293 if (apiprivate->propertyFlags(property) & QJSValue::ReadOnly) {
294 QString error = QLatin1String("Cannot assign to read-only property \"") +
295 v8engine->toString(property) + QLatin1Char('\"');
296 v8::ThrowException(v8::Exception::Error(v8engine->toString(error)));
298 apiprivate->setProperty(property, setvalp.data());