eed97d41a9a88824aa9e90ab59c6df5ccdf82259
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qv8engine_impl_p.h
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-ONLY$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser
12 ** General Public License version 2.1 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.LGPL included in the
14 ** packaging of this file.  Please review the following information to
15 ** ensure the GNU Lesser General Public License version 2.1 requirements
16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** If you have questions regarding the use of this file, please contact
19 ** us via http://www.qt-project.org/.
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23
24 #ifndef QV8ENGINE_IMPL_P_H
25 #define QV8ENGINE_IMPL_P_H
26
27 //
28 //  W A R N I N G
29 //  -------------
30 //
31 // This file is not part of the Qt API.  It exists purely as an
32 // implementation detail.  This header file may change from version to
33 // version without notice, or even be removed.
34 //
35 // We mean it.
36 //
37
38 #include "qv8engine_p.h"
39 #include "qjsvalue_p.h"
40 #include "qjsconverter_p.h"
41 #include "qjsvalueiterator_p.h"
42
43 QT_BEGIN_NAMESPACE
44
45 inline v8::Handle<v8::Value> QV8Engine::makeJSValue(bool value)
46 {
47     return value ? v8::True() : v8::False();
48 }
49
50 inline v8::Local<v8::Value> QV8Engine::makeJSValue(int value)
51 {
52     return v8::Integer::New(value);
53 }
54
55 inline v8::Local<v8::Value> QV8Engine::makeJSValue(uint value)
56 {
57     return v8::Integer::NewFromUnsigned(value);
58 }
59
60 inline v8::Local<v8::Value> QV8Engine::makeJSValue(double value)
61 {
62     return v8::Number::New(value);
63 }
64
65 inline v8::Handle<v8::Value> QV8Engine::makeJSValue(QJSValue::SpecialValue value) {
66     if (value == QJSValue::NullValue)
67         return v8::Null();
68     return v8::Undefined();
69 }
70
71 inline v8::Local<v8::Value> QV8Engine::makeJSValue(const QString &value)
72 {
73     return QJSConverter::toString(value);
74 }
75
76 class QtScriptBagCleaner
77 {
78 public:
79     template<class T>
80     void operator () (T* value) const
81     {
82         value->reinitialize();
83     }
84     void operator () (QJSValueIteratorPrivate *iterator) const
85     {
86         iterator->invalidate();
87     }
88 };
89
90 inline void QV8Engine::registerValue(QJSValuePrivate *data)
91 {
92     m_values.insert(data);
93 }
94
95 inline void QV8Engine::unregisterValue(QJSValuePrivate *data)
96 {
97     m_values.remove(data);
98 }
99
100 inline void QV8Engine::invalidateAllValues()
101 {
102     ValueList::iterator it;
103     for (it = m_values.begin(); it != m_values.end(); it = it.erase())
104         (*it)->invalidate();
105     Q_ASSERT(m_values.isEmpty());
106 }
107
108 inline void QV8Engine::registerValueIterator(QJSValueIteratorPrivate *data)
109 {
110     m_valueIterators.insert(data);
111 }
112
113 inline void QV8Engine::unregisterValueIterator(QJSValueIteratorPrivate *data)
114 {
115     m_valueIterators.remove(data);
116 }
117
118 inline void QV8Engine::invalidateAllIterators()
119 {
120     ValueIteratorList::iterator it;
121     for (it = m_valueIterators.begin(); it != m_valueIterators.end(); it = it.erase())
122         (*it)->invalidate();
123     Q_ASSERT(m_valueIterators.isEmpty());
124 }
125
126 /*!
127   \internal
128   \note property can be index (v8::Integer) or a property (v8::String) name, according to ECMA script
129   property would be converted to a string.
130 */
131 inline QJSValue::PropertyFlags QV8Engine::getPropertyFlags(v8::Handle<v8::Object> object, v8::Handle<v8::Value> property)
132 {
133     QJSValue::PropertyFlags flags = m_originalGlobalObject.getPropertyFlags(object, property);
134     return flags;
135 }
136
137 QScriptPassPointer<QJSValuePrivate> QV8Engine::evaluate(const QString& program, const QString& fileName, int lineNumber)
138 {
139     v8::TryCatch tryCatch;
140     v8::ScriptOrigin scriptOrigin(QJSConverter::toString(fileName), v8::Integer::New(lineNumber - 1));
141     v8::Handle<v8::Script> script;
142     script = v8::Script::Compile(QJSConverter::toString(program), &scriptOrigin);
143     if (script.IsEmpty()) {
144         // TODO: Why don't we get the exception, as with Script::Compile()?
145         // Q_ASSERT(tryCatch.HasCaught());
146         v8::Handle<v8::Value> error = v8::Exception::SyntaxError(v8::String::New(""));
147         setException(error);
148         return new QJSValuePrivate(this, error);
149     }
150     return evaluate(script, tryCatch);
151 }
152
153 QT_END_NAMESPACE
154
155 #endif // QV8ENGINE_IMPL_P_H