forked from qt-creator/qt-creator
Core: Replace usage of QScriptEngine with QJSEngine
This replaces the dependency of Core on QtScript with the dependency on QtQml. Changes: QJSEngine manages the onwership of injected QObjects. The Exception information is part of QJSValue instead of the engine. Change-Id: I54e40a7f2f7795424c4f3e37d962ac59fc2b1a09 Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
committed by
Tobias Hunger
parent
0a294eebe4
commit
fb9cea18a8
@@ -33,7 +33,6 @@
|
|||||||
|
|
||||||
#include <utils/stringutils.h>
|
#include <utils/stringutils.h>
|
||||||
|
|
||||||
#include <QScriptEngine>
|
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
@@ -2,7 +2,7 @@ DEFINES += CORE_LIBRARY
|
|||||||
QT += help \
|
QT += help \
|
||||||
network \
|
network \
|
||||||
printsupport \
|
printsupport \
|
||||||
script \
|
qml \
|
||||||
sql
|
sql
|
||||||
|
|
||||||
# embedding build time information prevents repeatedly binary exact versions from same source code
|
# embedding build time information prevents repeatedly binary exact versions from same source code
|
||||||
|
@@ -37,7 +37,7 @@
|
|||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QScriptEngine>
|
#include <QJSEngine>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
@@ -45,10 +45,7 @@ namespace Internal {
|
|||||||
|
|
||||||
class JsExpanderPrivate {
|
class JsExpanderPrivate {
|
||||||
public:
|
public:
|
||||||
~JsExpanderPrivate() { qDeleteAll(m_registeredObjects); }
|
QJSEngine m_engine;
|
||||||
|
|
||||||
QScriptEngine m_engine;
|
|
||||||
QList<QObject *> m_registeredObjects;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
@@ -57,26 +54,23 @@ static Internal::JsExpanderPrivate *d;
|
|||||||
|
|
||||||
void JsExpander::registerQObjectForJs(const QString &name, QObject *obj)
|
void JsExpander::registerQObjectForJs(const QString &name, QObject *obj)
|
||||||
{
|
{
|
||||||
obj->setParent(0); // take ownership!
|
QJSValue jsObj = d->m_engine.newQObject(obj);
|
||||||
d->m_registeredObjects.append(obj);
|
|
||||||
QScriptValue jsObj = d->m_engine.newQObject(obj, QScriptEngine::QtOwnership);
|
|
||||||
d->m_engine.globalObject().setProperty(name, jsObj);
|
d->m_engine.globalObject().setProperty(name, jsObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString JsExpander::evaluate(const QString &expression, QString *errorMessage)
|
QString JsExpander::evaluate(const QString &expression, QString *errorMessage)
|
||||||
{
|
{
|
||||||
d->m_engine.clearExceptions();
|
QJSValue value = d->m_engine.evaluate(expression);
|
||||||
QScriptValue value = d->m_engine.evaluate(expression);
|
if (value.isError()) {
|
||||||
if (d->m_engine.hasUncaughtException()) {
|
|
||||||
const QString msg = QCoreApplication::translate("Core::JsExpander", "Error in \"%1\": %2")
|
const QString msg = QCoreApplication::translate("Core::JsExpander", "Error in \"%1\": %2")
|
||||||
.arg(expression, d->m_engine.uncaughtException().toString());
|
.arg(expression, value.toString());
|
||||||
if (errorMessage)
|
if (errorMessage)
|
||||||
*errorMessage = msg;
|
*errorMessage = msg;
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
// Try to convert to bool, be that an int or whatever.
|
// Try to convert to bool, be that an int or whatever.
|
||||||
if (value.isBool())
|
if (value.isBool())
|
||||||
return value.toBool() ? QStringLiteral("true") : QStringLiteral("false");
|
return value.toString();
|
||||||
if (value.isNumber())
|
if (value.isNumber())
|
||||||
return QString::number(value.toNumber());
|
return QString::number(value.toNumber());
|
||||||
if (value.isString())
|
if (value.isString())
|
||||||
|
Reference in New Issue
Block a user