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-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file. Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
22 ****************************************************************************/
24 #ifndef QV8ENGINE_IMPL_P_H
25 #define QV8ENGINE_IMPL_P_H
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.
38 #include "qv8engine_p.h"
39 #include "qjsvalue_p.h"
40 #include "qjsconverter_p.h"
41 #include "qjsvalueiterator_p.h"
45 inline v8::Handle<v8::Value> QV8Engine::makeJSValue(bool value)
47 return value ? v8::True() : v8::False();
50 inline v8::Local<v8::Value> QV8Engine::makeJSValue(int value)
52 return v8::Integer::New(value);
55 inline v8::Local<v8::Value> QV8Engine::makeJSValue(uint value)
57 return v8::Integer::NewFromUnsigned(value);
60 inline v8::Local<v8::Value> QV8Engine::makeJSValue(double value)
62 return v8::Number::New(value);
65 inline v8::Handle<v8::Value> QV8Engine::makeJSValue(QJSValue::SpecialValue value) {
66 if (value == QJSValue::NullValue)
68 return v8::Undefined();
71 inline v8::Local<v8::Value> QV8Engine::makeJSValue(const QString &value)
73 return QJSConverter::toString(value);
76 class QtScriptBagCleaner
80 void operator () (T* value) const
82 value->reinitialize();
84 void operator () (QJSValueIteratorPrivate *iterator) const
86 iterator->invalidate();
90 inline void QV8Engine::registerValue(QJSValuePrivate *data)
92 m_values.insert(data);
95 inline void QV8Engine::unregisterValue(QJSValuePrivate *data)
97 m_values.remove(data);
100 inline void QV8Engine::invalidateAllValues()
102 ValueList::iterator it;
103 for (it = m_values.begin(); it != m_values.end(); it = it.erase())
105 Q_ASSERT(m_values.isEmpty());
108 inline void QV8Engine::registerValueIterator(QJSValueIteratorPrivate *data)
110 m_valueIterators.insert(data);
113 inline void QV8Engine::unregisterValueIterator(QJSValueIteratorPrivate *data)
115 m_valueIterators.remove(data);
118 inline void QV8Engine::invalidateAllIterators()
120 ValueIteratorList::iterator it;
121 for (it = m_valueIterators.begin(); it != m_valueIterators.end(); it = it.erase())
123 Q_ASSERT(m_valueIterators.isEmpty());
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.
131 inline QJSValuePrivate::PropertyFlags QV8Engine::getPropertyFlags(v8::Handle<v8::Object> object, v8::Handle<v8::Value> property)
133 QJSValuePrivate::PropertyFlags flags = m_originalGlobalObject.getPropertyFlags(object, property);
137 QScriptPassPointer<QJSValuePrivate> QV8Engine::evaluate(const QString& program, const QString& fileName, int lineNumber)
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(""));
148 return new QJSValuePrivate(this, error);
150 return evaluate(script, tryCatch);
155 #endif // QV8ENGINE_IMPL_P_H