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