1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtScript module of the Qt Toolkit.
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.
18 ** If you have questions regarding the use of this file, please contact
19 ** us via http://www.qt-project.org/.
22 ****************************************************************************/
27 #include <QtCore/qmetatype.h>
29 #include <QtCore/qvariant.h>
30 #include <QtCore/qsharedpointer.h>
31 #include <QtCore/qobject.h>
32 #include <QtDeclarative/qjsvalue.h>
45 inline T qjsvalue_cast(const QJSValue &);
47 class QJSEnginePrivate;
48 class Q_DECLARATIVE_EXPORT QJSEngine
54 enum ContextOwnership {
58 QT_DEPRECATED explicit QJSEngine(ContextOwnership ownership);
62 explicit QJSEngine(QObject *parent);
65 QJSValue globalObject() const;
67 QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1);
70 QJSValue newArray(uint length = 0);
72 QJSValue newQObject(QObject *object);
75 inline QJSValue toScriptValue(const T &value)
77 return create(qMetaTypeId<T>(), &value);
80 inline T fromScriptValue(const QJSValue &value)
82 return qjsvalue_cast<T>(value);
85 void collectGarbage();
87 QV8Engine *handle() const { return d; }
90 QT_DEPRECATED bool hasUncaughtException() const;
91 QT_DEPRECATED QJSValue uncaughtException() const;
92 QT_DEPRECATED void clearExceptions();
94 QT_DEPRECATED QJSValue nullValue();
95 QT_DEPRECATED QJSValue undefinedValue();
97 QT_DEPRECATED QJSValue newVariant(const QVariant &value);
99 QT_DEPRECATED QJSValue newRegExp(const QRegExp ®exp);
101 QT_DEPRECATED QJSValue newRegExp(const QString &pattern, const QString &flags);
102 QT_DEPRECATED QJSValue newDate(double value);
103 QT_DEPRECATED QJSValue newDate(const QDateTime &value);
105 QT_DEPRECATED QJSValue toObject(const QJSValue &value);
109 void signalHandlerException(const QJSValue &exception);
112 QJSValue create(int type, const void *ptr);
114 static bool convertV2(const QJSValue &value, int type, void *ptr);
116 friend inline bool qjsvalue_cast_helper(const QJSValue &, int, void *);
119 QJSEngine(QJSEnginePrivate &dd, QObject *parent = 0);
123 Q_DISABLE_COPY(QJSEngine)
124 Q_DECLARE_PRIVATE(QJSEngine)
125 friend class QV8Engine;
128 inline bool qjsvalue_cast_helper(const QJSValue &value, int type, void *ptr)
130 return QJSEngine::convertV2(value, type, ptr);
134 T qjsvalue_cast(const QJSValue &value)
137 const int id = qMetaTypeId<T>();
139 if (qjsvalue_cast_helper(value, id, &t))
141 else if (value.isVariant())
142 return qvariant_cast<T>(value.toVariant());
148 inline QVariant qjsvalue_cast<QVariant>(const QJSValue &value)
150 return value.toVariant();
157 #endif // QJSENGINE_H