diff options
| author | Jesper K. Pedersen <jesper.pedersen@kdab.com> | 2013-04-25 16:47:17 +0200 |
|---|---|---|
| committer | Jesper K. Pedersen <jesper.pedersen@kdab.com> | 2013-04-30 06:53:17 +0200 |
| commit | 74ac0a518fcb79f14ab655ce68d424c034f6b846 (patch) | |
| tree | 2a0672f8ee2d56d01b69b6fafe7f36bbd7a544b7 /scriptrunner.cpp | |
| parent | 3d28db2eb6b6e33ab4ae0f7e976c576a19108dc4 (diff) | |
implemented include() to load scripts
Change-Id: I4e8a558fdb12f0a1e16551867274e35ac750fb04
Reviewed-by: Nicolas Arnaud-Cormos <nicolas@kdab.com>
Diffstat (limited to 'scriptrunner.cpp')
| -rw-r--r-- | scriptrunner.cpp | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/scriptrunner.cpp b/scriptrunner.cpp index bad389b..411da03 100644 --- a/scriptrunner.cpp +++ b/scriptrunner.cpp @@ -46,6 +46,11 @@ #include "utils/dialogs.h" #include "objects/cppfunction.h" #include "objects/cppargument.h" +#include <QTextStream> +#include <QFile> +#include <coreplugin/messagemanager.h> +#include <utils/outputformat.h> +#include <QFileInfo> using namespace Scripting; using namespace Scripting::Internal; @@ -61,14 +66,57 @@ ScriptRunner::~ScriptRunner() { } +// Path to the topmost script loaded. +static QString currentPath; -ErrorMessage ScriptRunner::runScript(const QString &sourceCode, const QString fileName) +static QScriptValue run(QScriptEngine* engine, const QString& fileName, bool recursive) { + QFile file(fileName); + if (file.open(QIODevice::ReadOnly)) { + + QTextStream stream(&file); + const QString sourceCode = stream.readAll(); + + if ( recursive ) { + // A script context is pushed by the include function call. + // Link that context to the one of the script that called include + // Only do that, however, when called from the scipt + QScriptContext *context = engine->currentContext(); + QScriptContext *parent=context->parentContext(); + context->setActivationObject(parent->activationObject()); + context->setThisObject(parent->thisObject()); + } + + QScriptValue result = engine->evaluate(sourceCode, fileName); + + return result; + } + else { + Core::MessageManager::instance()->printToOutputPane(QObject::tr("Error: %1 doesn't exist.\n").arg(fileName), + Utils::ErrorMessageFormat); + engine->abortEvaluation(); + return QScriptValue(); + } +} + +static QScriptValue load(QScriptContext *context, QScriptEngine *engine) +{ + QScriptValue callee = context->callee(); + if (context->argumentCount() == 1) + return run(engine, currentPath + QLatin1String("/") + context->argument(0).toString(), true); + else + context->throwError(QObject::tr("Wrong number of arguments given to import")); + return QScriptValue(); +} + +ErrorMessage ScriptRunner::runScript(const QString fileName) +{ + currentPath = QFileInfo(fileName).absolutePath(); ensureEngineInitialized(); + // Ensure no polution of environment between script runs m_engine->pushContext(); - QScriptValue result = m_engine->evaluate(sourceCode, fileName); - + QScriptValue result = run(m_engine.data(), fileName, false); const bool failed = m_engine->hasUncaughtException(); m_engine->popContext(); @@ -107,6 +155,8 @@ ScriptRunner::QScriptEnginePtr ScriptRunner::ensureEngineInitialized() registerGlobal(new Dialogs, QLatin1String("dialogs")); registerWrappers(m_engine.data()); registerEnums(m_engine.data()); + + m_engine->globalObject().setProperty(QLatin1String("include"), m_engine->newFunction(load)); return m_engine; } |
