1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QV8ENGINE_IMPL_P_H
43 #define QV8ENGINE_IMPL_P_H
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.
56 #include "qv8engine_p.h"
57 #include "qjsvalue_p.h"
58 #include "qjsconverter_p.h"
59 #include "qjsvalueiterator_p.h"
63 inline v8::Handle<v8::Value> QV8Engine::makeJSValue(bool value)
65 return value ? v8::True() : v8::False();
68 inline v8::Local<v8::Value> QV8Engine::makeJSValue(int value)
70 return v8::Integer::New(value);
73 inline v8::Local<v8::Value> QV8Engine::makeJSValue(uint value)
75 return v8::Integer::NewFromUnsigned(value);
78 inline v8::Local<v8::Value> QV8Engine::makeJSValue(double value)
80 return v8::Number::New(value);
83 inline v8::Handle<v8::Value> QV8Engine::makeJSValue(QJSValue::SpecialValue value) {
84 if (value == QJSValue::NullValue)
86 return v8::Undefined();
89 inline v8::Local<v8::Value> QV8Engine::makeJSValue(const QString &value)
91 return QJSConverter::toString(value);
94 class QtScriptBagCleaner
98 void operator () (T* value) const
100 value->reinitialize();
102 void operator () (QJSValueIteratorPrivate *iterator) const
104 iterator->invalidate();
108 inline void QV8Engine::registerValue(QJSValuePrivate *data)
110 m_values.insert(data);
113 inline void QV8Engine::unregisterValue(QJSValuePrivate *data)
115 m_values.remove(data);
118 inline void QV8Engine::invalidateAllValues()
120 ValueList::iterator it;
121 for (it = m_values.begin(); it != m_values.end(); it = it.erase())
123 Q_ASSERT(m_values.isEmpty());
126 inline void QV8Engine::registerValueIterator(QJSValueIteratorPrivate *data)
128 m_valueIterators.insert(data);
131 inline void QV8Engine::unregisterValueIterator(QJSValueIteratorPrivate *data)
133 m_valueIterators.remove(data);
136 inline void QV8Engine::invalidateAllIterators()
138 ValueIteratorList::iterator it;
139 for (it = m_valueIterators.begin(); it != m_valueIterators.end(); it = it.erase())
141 Q_ASSERT(m_valueIterators.isEmpty());
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.
149 inline QJSValuePrivate::PropertyFlags QV8Engine::getPropertyFlags(v8::Handle<v8::Object> object, v8::Handle<v8::Value> property)
151 QJSValuePrivate::PropertyFlags flags = m_originalGlobalObject.getPropertyFlags(object, property);
155 QScriptPassPointer<QJSValuePrivate> QV8Engine::evaluate(const QString& program, const QString& fileName, quint16 lineNumber)
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);
167 return evaluate(script, tryCatch);
172 #endif // QV8ENGINE_IMPL_P_H