Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / src / qml / qml / v8 / qv8contextwrapper.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qv8contextwrapper_p.h"
43 #include "qv8engine_p.h"
44
45 #include <private/qqmlengine_p.h>
46 #include <private/qqmlcontext_p.h>
47
48 QT_BEGIN_NAMESPACE
49
50 static QString internal(QLatin1String("You've stumbled onto an internal implementation detail "
51                                       "that should never have been exposed."));
52
53 class QV8ContextResource : public QV8ObjectResource
54 {
55     V8_RESOURCE_TYPE(ContextType);
56
57 public:
58     QV8ContextResource(QV8Engine *engine, QQmlContextData *context, QObject *scopeObject);
59     ~QV8ContextResource();
60
61     inline QQmlContextData *getContext() const;
62     inline QObject *getScopeObject() const;
63
64     quint32 isSharedContext:1;
65     quint32 hasSubContexts:1;
66     quint32 readOnly:1;
67     quint32 dummy:29;
68
69     // This is a pretty horrible hack, and an abuse of external strings.  When we create a 
70     // sub-context (a context created by a Qt.include() in an external javascript file),
71     // we pass a specially crafted SubContext external string as the v8::Script::Data() to
72     // the script, which contains a pointer to the context.  We can then access the 
73     // v8::Script::Data() later on to resolve names and URLs against the sub-context instead
74     // of the main outer context.
75     struct SubContext : public v8::String::ExternalStringResource {
76         SubContext(QQmlContextData *context) : context(context) {}
77         QQmlGuardedContextData context;
78
79         virtual const uint16_t* data() const { return (const uint16_t *)internal.constData(); }
80         virtual size_t length() const { return internal.length(); }
81     };
82
83 private:
84     QQmlGuardedContextData context;
85     QQmlGuard<QObject> scopeObject;
86
87 };
88
89 QV8ContextResource::QV8ContextResource(QV8Engine *engine, QQmlContextData *context, QObject *scopeObject)
90 : QV8ObjectResource(engine), isSharedContext(false), hasSubContexts(false), readOnly(true), 
91   context(context), scopeObject(scopeObject)
92 {
93 }
94
95 QV8ContextResource::~QV8ContextResource()
96 {
97     if (context && context->isJSContext)
98         context->destroy();
99 }
100
101 // Returns the scope object
102 QObject *QV8ContextResource::getScopeObject() const
103 {
104     if (isSharedContext)
105         return QQmlEnginePrivate::get(engine->engine())->sharedScope;
106     else
107         return scopeObject;
108 }
109
110 // Returns the context, including resolving a subcontext
111 QQmlContextData *QV8ContextResource::getContext() const
112 {
113     if (isSharedContext)
114         return QQmlEnginePrivate::get(engine->engine())->sharedContext;
115     
116     if (!hasSubContexts)
117         return context;
118
119     v8::Local<v8::Value> callingdata = v8::Context::GetCallingScriptData();
120     if (callingdata.IsEmpty() || !callingdata->IsString())
121         return context;
122
123     v8::Local<v8::String> callingstring = callingdata->ToString();
124     Q_ASSERT(callingstring->IsExternal());
125     Q_ASSERT(callingstring->GetExternalStringResource());
126
127     SubContext *sc = static_cast<SubContext *>(callingstring->GetExternalStringResource());
128     return sc->context;
129 }
130
131 QV8ContextWrapper::QV8ContextWrapper()
132 : m_engine(0)
133 {
134 }
135
136 QV8ContextWrapper::~QV8ContextWrapper()
137 {
138 }
139
140 void QV8ContextWrapper::destroy()
141 {
142     qPersistentDispose(m_sharedContext);
143     qPersistentDispose(m_urlConstructor);
144     qPersistentDispose(m_constructor);
145 }
146
147 void QV8ContextWrapper::init(QV8Engine *engine)
148 {
149     m_engine = engine;
150     {
151     v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
152     ft->InstanceTemplate()->SetHasExternalResource(true);
153     ft->InstanceTemplate()->SetFallbackPropertyHandler(Getter, Setter);
154     m_constructor = qPersistentNew<v8::Function>(ft->GetFunction());
155     }
156     {
157     v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
158     ft->InstanceTemplate()->SetHasExternalResource(true);
159     ft->InstanceTemplate()->SetFallbackPropertyHandler(NullGetter, NullSetter);
160     m_urlConstructor = qPersistentNew<v8::Function>(ft->GetFunction());
161     }
162     {
163     v8::Local<v8::Object> sharedContext = m_constructor->NewInstance();
164     QV8ContextResource *r = new QV8ContextResource(engine, 0, 0);
165     r->isSharedContext = true;
166     sharedContext->SetExternalResource(r);
167     m_sharedContext = qPersistentNew<v8::Object>(sharedContext);
168     }
169 }
170
171 v8::Local<v8::Object> QV8ContextWrapper::qmlScope(QQmlContextData *ctxt, QObject *scope)
172 {
173     // XXX NewInstance() should be optimized
174     v8::Local<v8::Object> rv = m_constructor->NewInstance(); 
175     QV8ContextResource *r = new QV8ContextResource(m_engine, ctxt, scope);
176     rv->SetExternalResource(r);
177     return rv;
178 }
179
180 v8::Local<v8::Object> QV8ContextWrapper::urlScope(const QUrl &url)
181 {
182     QQmlContextData *context = new QQmlContextData;
183     context->url = url;
184     context->isInternal = true;
185     context->isJSContext = true;
186
187     // XXX NewInstance() should be optimized
188     v8::Local<v8::Object> rv = m_urlConstructor->NewInstance(); 
189     QV8ContextResource *r = new QV8ContextResource(m_engine, context, 0);
190     rv->SetExternalResource(r);
191     return rv;
192 }
193
194 void QV8ContextWrapper::setReadOnly(v8::Handle<v8::Object> qmlglobal, bool readOnly)
195 {
196     QV8ContextResource *resource = v8_resource_cast<QV8ContextResource>(qmlglobal);
197     Q_ASSERT(resource);
198     resource->readOnly = readOnly;
199 }
200
201 void QV8ContextWrapper::addSubContext(v8::Handle<v8::Object> qmlglobal, v8::Handle<v8::Script> script, 
202                                       QQmlContextData *ctxt)
203 {
204     QV8ContextResource *resource = v8_resource_cast<QV8ContextResource>(qmlglobal);
205     Q_ASSERT(resource);
206     resource->hasSubContexts = true;
207     script->SetData(v8::String::NewExternal(new QV8ContextResource::SubContext(ctxt)));
208 }
209
210 QQmlContextData *QV8ContextWrapper::callingContext()
211 {
212     v8::Local<v8::Object> qmlglobal = v8::Context::GetCallingQmlGlobal();
213     if (qmlglobal.IsEmpty()) return 0;
214
215     QV8ContextResource *r = v8_resource_cast<QV8ContextResource>(qmlglobal);
216     return r?r->getContext():0;
217 }
218
219 QQmlContextData *QV8ContextWrapper::context(v8::Handle<v8::Value> value)
220 {
221     if (!value->IsObject())
222         return 0;
223
224     v8::Handle<v8::Object> qmlglobal = v8::Handle<v8::Object>::Cast(value);
225     QV8ContextResource *r = v8_resource_cast<QV8ContextResource>(qmlglobal);
226     return r?r->getContext():0;
227 }
228
229 v8::Handle<v8::Value> QV8ContextWrapper::NullGetter(v8::Local<v8::String>,
230                                                     const v8::AccessorInfo &)
231 {
232     // V8 will throw a ReferenceError if appropriate ("typeof" should not throw)
233     return v8::Handle<v8::Value>();
234 }
235
236 v8::Handle<v8::Value> QV8ContextWrapper::Getter(v8::Local<v8::String> property, 
237                                                 const v8::AccessorInfo &info)
238 {
239     QV8ContextResource *resource = v8_resource_check<QV8ContextResource>(info.This());
240
241     // Its possible we could delay the calculation of the "actual" context (in the case
242     // of sub contexts) until it is definately needed.
243     QQmlContextData *context = resource->getContext();
244     QQmlContextData *expressionContext = context;
245
246     if (!context)
247         return v8::Undefined();
248
249     if (v8::Context::GetCallingQmlGlobal() != info.This())
250         return v8::Handle<v8::Value>();
251
252     // Search type (attached property/enum/imported scripts) names
253     // while (context) {
254     //     Search context properties
255     //     Search scope object
256     //     Search context object
257     //     context = context->parent
258     // }
259
260     QV8Engine *engine = resource->engine;
261
262     QObject *scopeObject = resource->getScopeObject();
263
264     QHashedV8String propertystring(property);
265
266     if (context->imports && QV8Engine::startsWithUpper(property)) {
267         // Search for attached properties, enums and imported scripts
268         QQmlTypeNameCache::Result r = context->imports->query(propertystring);
269         
270         if (r.isValid()) { 
271             if (r.scriptIndex != -1) {
272                 int index = r.scriptIndex;
273                 if (index < context->importedScripts.count())
274                     return context->importedScripts.at(index);
275                 else
276                     return v8::Undefined();
277             } else if (r.type) {
278                 return engine->typeWrapper()->newObject(scopeObject, r.type);
279             } else if (r.importNamespace) {
280                 return engine->typeWrapper()->newObject(scopeObject, context->imports, r.importNamespace);
281             }
282             Q_ASSERT(!"Unreachable");
283         }
284
285         // Fall through
286     }
287
288     QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine->engine());
289     QV8QObjectWrapper *qobjectWrapper = engine->qobjectWrapper();
290
291     while (context) {
292         // Search context properties
293         if (context->propertyNames) {
294             int propertyIdx = context->propertyNames->value(propertystring);
295
296             if (propertyIdx != -1) {
297
298                 if (propertyIdx < context->idValueCount) {
299
300                     ep->captureProperty(&context->idValues[propertyIdx].bindings);
301                     return engine->newQObject(context->idValues[propertyIdx]);
302                 } else {
303
304                     QQmlContextPrivate *cp = context->asQQmlContextPrivate();
305
306                     ep->captureProperty(context->asQQmlContext(), -1,
307                                         propertyIdx + cp->notifyIndex);
308
309                     const QVariant &value = cp->propertyValues.at(propertyIdx);
310                     if (value.userType() == qMetaTypeId<QList<QObject*> >()) {
311                         QQmlListProperty<QObject> prop(context->asQQmlContext(), (void*) qintptr(propertyIdx),
312                                                                0,
313                                                                QQmlContextPrivate::context_count,
314                                                                QQmlContextPrivate::context_at);
315                         return engine->listWrapper()->newList(prop, qMetaTypeId<QQmlListProperty<QObject> >());
316                     } else {
317                         return engine->fromVariant(cp->propertyValues.at(propertyIdx));
318                     }
319                 }
320             }
321         }
322
323         // Search scope object
324         if (scopeObject) {
325             v8::Handle<v8::Value> result = qobjectWrapper->getProperty(scopeObject, propertystring,
326                                                                        context, QV8QObjectWrapper::CheckRevision);
327             if (!result.IsEmpty()) return result;
328         }
329         scopeObject = 0;
330
331
332         // Search context object
333         if (context->contextObject) {
334             v8::Handle<v8::Value> result = qobjectWrapper->getProperty(context->contextObject, propertystring,
335                                                                        context, QV8QObjectWrapper::CheckRevision);
336             if (!result.IsEmpty()) return result;
337         }
338
339         context = context->parent;
340     }
341
342     expressionContext->unresolvedNames = true;
343
344     // V8 will throw a ReferenceError if appropriate ("typeof" should not throw)
345     return v8::Handle<v8::Value>();
346 }
347
348 v8::Handle<v8::Value> QV8ContextWrapper::NullSetter(v8::Local<v8::String> property, 
349                                                     v8::Local<v8::Value>,
350                                                     const v8::AccessorInfo &info)
351 {
352     QV8ContextResource *resource = v8_resource_check<QV8ContextResource>(info.This());
353
354     QV8Engine *engine = resource->engine;
355
356     if (!resource->readOnly) {
357         return v8::Handle<v8::Value>();
358     } else {
359         QString error = QLatin1String("Invalid write to global property \"") + engine->toString(property) + 
360                         QLatin1Char('"');
361         v8::ThrowException(v8::Exception::Error(engine->toString(error)));
362         return v8::Handle<v8::Value>();
363     }
364 }
365
366 v8::Handle<v8::Value> QV8ContextWrapper::Setter(v8::Local<v8::String> property, 
367                                                 v8::Local<v8::Value> value,
368                                                 const v8::AccessorInfo &info)
369 {
370     QV8ContextResource *resource = v8_resource_check<QV8ContextResource>(info.This());
371
372     // Its possible we could delay the calculation of the "actual" context (in the case
373     // of sub contexts) until it is definately needed.
374     QQmlContextData *context = resource->getContext();
375     QQmlContextData *expressionContext = context;
376
377     if (!context)
378         return v8::Undefined();
379
380     if (v8::Context::GetCallingQmlGlobal() != info.This())
381         return v8::Handle<v8::Value>();
382
383     // See QV8ContextWrapper::Getter for resolution order
384     
385     QV8Engine *engine = resource->engine;
386     QObject *scopeObject = resource->getScopeObject();
387
388     QHashedV8String propertystring(property);
389
390     QV8QObjectWrapper *qobjectWrapper = engine->qobjectWrapper();
391
392     while (context) {
393         // Search context properties
394         if (context->propertyNames && -1 != context->propertyNames->value(propertystring))
395             return value;
396
397         // Search scope object
398         if (scopeObject && 
399             qobjectWrapper->setProperty(scopeObject, propertystring, context, value, QV8QObjectWrapper::CheckRevision))
400             return value;
401         scopeObject = 0;
402
403         // Search context object
404         if (context->contextObject &&
405             qobjectWrapper->setProperty(context->contextObject, propertystring, context, value,
406                                         QV8QObjectWrapper::CheckRevision))
407             return value;
408
409         context = context->parent;
410     }
411
412     expressionContext->unresolvedNames = true;
413
414     if (!resource->readOnly) {
415         return v8::Handle<v8::Value>();
416     } else {
417         QString error = QLatin1String("Invalid write to global property \"") + engine->toString(property) + 
418                         QLatin1Char('"');
419         v8::ThrowException(v8::Exception::Error(engine->toString(error)));
420         return v8::Undefined();
421     }
422 }
423
424 QT_END_NAMESPACE