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 "qv8variantwrapper_p.h"
43 #include "qv8engine_p.h"
44 #include "qdeclarativeengine_p.h"
48 class QV8VariantResource : public QV8ObjectResource,
49 public QDeclarativeEnginePrivate::ScarceResourceData
51 V8_RESOURCE_TYPE(VariantType);
53 QV8VariantResource(QV8Engine *engine, const QVariant &data);
56 QV8VariantResource::QV8VariantResource(QV8Engine *engine, const QVariant &data)
57 : QV8ObjectResource(engine), QDeclarativeEnginePrivate::ScarceResourceData(data)
61 QV8VariantWrapper::QV8VariantWrapper()
66 QV8VariantWrapper::~QV8VariantWrapper()
70 void QV8VariantWrapper::init(QV8Engine *engine)
73 m_toString = qPersistentNew<v8::Function>(v8::FunctionTemplate::New(ToString)->GetFunction());
74 m_valueOf = qPersistentNew<v8::Function>(v8::FunctionTemplate::New(ValueOf)->GetFunction());
77 v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
78 ft->InstanceTemplate()->SetFallbackPropertyHandler(Getter, Setter);
79 ft->InstanceTemplate()->SetHasExternalResource(true);
80 ft->InstanceTemplate()->MarkAsUseUserObjectComparison();
81 ft->InstanceTemplate()->SetAccessor(v8::String::New("toString"), ToStringGetter, 0,
82 m_toString, v8::DEFAULT,
83 v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
84 ft->InstanceTemplate()->SetAccessor(v8::String::New("valueOf"), ValueOfGetter, 0,
85 m_valueOf, v8::DEFAULT,
86 v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
87 m_constructor = qPersistentNew<v8::Function>(ft->GetFunction());
90 m_preserve = qPersistentNew<v8::Function>(v8::FunctionTemplate::New(Preserve)->GetFunction());
91 m_destroy = qPersistentNew<v8::Function>(v8::FunctionTemplate::New(Destroy)->GetFunction());
92 v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
93 ft->InstanceTemplate()->SetFallbackPropertyHandler(Getter, Setter);
94 ft->InstanceTemplate()->SetHasExternalResource(true);
95 ft->InstanceTemplate()->MarkAsUseUserObjectComparison();
96 ft->InstanceTemplate()->SetAccessor(v8::String::New("preserve"), PreserveGetter, 0,
97 m_preserve, v8::DEFAULT,
98 v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
99 ft->InstanceTemplate()->SetAccessor(v8::String::New("destroy"), DestroyGetter, 0,
100 m_destroy, v8::DEFAULT,
101 v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
102 ft->InstanceTemplate()->SetAccessor(v8::String::New("toString"), ToStringGetter, 0,
103 m_toString, v8::DEFAULT,
104 v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
105 ft->InstanceTemplate()->SetAccessor(v8::String::New("valueOf"), ValueOfGetter, 0,
106 m_valueOf, v8::DEFAULT,
107 v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
108 m_scarceConstructor = qPersistentNew<v8::Function>(ft->GetFunction());
113 void QV8VariantWrapper::destroy()
115 qPersistentDispose(m_valueOf);
116 qPersistentDispose(m_toString);
117 qPersistentDispose(m_destroy);
118 qPersistentDispose(m_preserve);
119 qPersistentDispose(m_scarceConstructor);
120 qPersistentDispose(m_constructor);
123 v8::Local<v8::Object> QV8VariantWrapper::newVariant(const QVariant &value)
125 bool scarceResource = value.type() == QVariant::Pixmap ||
126 value.type() == QVariant::Image;
128 // XXX NewInstance() should be optimized
129 v8::Local<v8::Object> rv;
130 QV8VariantResource *r = new QV8VariantResource(m_engine, value);
132 if (scarceResource) {
133 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(m_engine->engine());
134 Q_ASSERT(ep->scarceResourcesRefCount);
135 rv = m_scarceConstructor->NewInstance();
136 ep->scarceResources.insert(r);
138 rv = m_constructor->NewInstance();
141 rv->SetExternalResource(r);
145 bool QV8VariantWrapper::isVariant(v8::Handle<v8::Value> value)
147 return value->IsObject() && v8_resource_cast<QV8VariantResource>(value->ToObject());
150 QVariant QV8VariantWrapper::toVariant(v8::Handle<v8::Object> obj)
152 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(obj);
153 return r?r->data:QVariant();
156 QVariant QV8VariantWrapper::toVariant(QV8ObjectResource *r)
158 Q_ASSERT(r->resourceType() == QV8ObjectResource::VariantType);
159 return static_cast<QV8VariantResource *>(r)->data;
162 v8::Handle<v8::Value> QV8VariantWrapper::Getter(v8::Local<v8::String> property,
163 const v8::AccessorInfo &info)
165 return v8::Undefined();
168 v8::Handle<v8::Value> QV8VariantWrapper::Setter(v8::Local<v8::String> property,
169 v8::Local<v8::Value> value,
170 const v8::AccessorInfo &info)
172 return v8::Undefined();
175 v8::Handle<v8::Value> QV8VariantWrapper::PreserveGetter(v8::Local<v8::String> property,
176 const v8::AccessorInfo &info)
182 v8::Handle<v8::Value> QV8VariantWrapper::DestroyGetter(v8::Local<v8::String> property,
183 const v8::AccessorInfo &info)
189 v8::Handle<v8::Value> QV8VariantWrapper::ToStringGetter(v8::Local<v8::String> property,
190 const v8::AccessorInfo &info)
196 v8::Handle<v8::Value> QV8VariantWrapper::ValueOfGetter(v8::Local<v8::String> property,
197 const v8::AccessorInfo &info)
203 v8::Handle<v8::Value> QV8VariantWrapper::Preserve(const v8::Arguments &args)
205 QV8VariantResource *resource = v8_resource_cast<QV8VariantResource>(args.This());
207 resource->node.remove();
209 return v8::Undefined();
212 v8::Handle<v8::Value> QV8VariantWrapper::Destroy(const v8::Arguments &args)
214 QV8VariantResource *resource = v8_resource_cast<QV8VariantResource>(args.This());
216 resource->data = QVariant();
217 resource->node.remove();
219 return v8::Undefined();
222 v8::Handle<v8::Value> QV8VariantWrapper::ToString(const v8::Arguments &args)
224 QV8VariantResource *resource = v8_resource_cast<QV8VariantResource>(args.This());
226 QString result = resource->data.toString();
227 if (result.isEmpty() && !resource->data.canConvert(QVariant::String))
228 result = QString::fromLatin1("QVariant(%0)").arg(QString::fromLatin1(resource->data.typeName()));
229 return resource->engine->toString(result);
231 return v8::Undefined();
235 v8::Handle<v8::Value> QV8VariantWrapper::ValueOf(const v8::Arguments &args)
237 QV8VariantResource *resource = v8_resource_cast<QV8VariantResource>(args.This());
239 QVariant v = resource->data;
241 case QVariant::Invalid:
242 return v8::Undefined();
243 case QVariant::String:
244 return resource->engine->toString(v.toString());
246 case QVariant::Double:
248 return v8::Number::New(v.toDouble());
250 return v8::Boolean::New(v.toBool());