Files
qt-creator/src/plugins/coreplugin/jsexpander.cpp
hjk 0d7f33aa71 Core: Work around upstream introduction of std::hash<QString>
Change-Id: I51d7e97011d03b1b1af921e55ccc40a61d8822f7
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
2019-07-01 16:05:23 +00:00

153 lines
4.7 KiB
C++

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "jsexpander.h"
#include "corejsextensions.h"
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
#include <QDebug>
#include <QJSEngine>
#include <unordered_map>
#if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
namespace std {
template<> struct hash<QString>
{
using argument_type = QString;
using result_type = size_t;
result_type operator()(const argument_type &v) const
{
return hash<string>()(v.toStdString());
}
};
} // namespace std
#endif
using ExtensionMap = std::unordered_map<QString, Core::JsExpander::ObjectFactory>;
Q_GLOBAL_STATIC(ExtensionMap, globalJsExtensions);
static Core::JsExpander *globalExpander = nullptr;
namespace Core {
namespace Internal {
class JsExpanderPrivate {
public:
QJSEngine m_engine;
};
} // namespace Internal
void JsExpander::registerGlobalObject(const QString &name, const ObjectFactory &factory)
{
globalJsExtensions->insert({name, factory});
if (globalExpander)
globalExpander->registerObject(name, factory());
}
void JsExpander::registerObject(const QString &name, QObject *obj)
{
QJSValue jsObj = d->m_engine.newQObject(obj);
d->m_engine.globalObject().setProperty(name, jsObj);
}
QString JsExpander::evaluate(const QString &expression, QString *errorMessage)
{
QJSValue value = d->m_engine.evaluate(expression);
if (value.isError()) {
const QString msg = QCoreApplication::translate("Core::JsExpander", "Error in \"%1\": %2")
.arg(expression, value.toString());
if (errorMessage)
*errorMessage = msg;
return QString();
}
// Try to convert to bool, be that an int or whatever.
if (value.isBool())
return value.toString();
if (value.isNumber())
return QString::number(value.toNumber());
if (value.isString())
return value.toString();
QString msg = QCoreApplication::translate("Core::JsExpander",
"Cannot convert result of \"%1\" to string.").arg(expression);
if (errorMessage)
*errorMessage = msg;
return QString();
}
QJSEngine &JsExpander::engine()
{
return d->m_engine;
}
void JsExpander::registerForExpander(Utils::MacroExpander *macroExpander)
{
macroExpander->registerPrefix(
"JS",
QCoreApplication::translate("Core::JsExpander",
"Evaluate simple JavaScript statements.<br>"
"Literal '}' characters must be escaped as \"\\}\", "
"'\\' characters must be escaped as \"\\\\\", "
"and \"%{\" must be escaped as \"%\\{\"."),
[this](QString in) -> QString {
QString errorMessage;
QString result = evaluate(in, &errorMessage);
if (!errorMessage.isEmpty()) {
qWarning() << errorMessage;
return errorMessage;
} else {
return result;
}
});
}
JsExpander *JsExpander::createGlobalJsExpander()
{
globalExpander = new JsExpander();
registerGlobalObject<Internal::UtilsJsExtension>("Util");
globalExpander->registerForExpander(Utils::globalMacroExpander());
return globalExpander;
}
JsExpander::JsExpander()
{
d = new Internal::JsExpanderPrivate;
for (const std::pair<const QString, ObjectFactory> &obj : *globalJsExtensions)
registerObject(obj.first, obj.second());
}
JsExpander::~JsExpander()
{
delete d;
d = nullptr;
}
} // namespace Core