forked from Dushistov/qt_monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_runner.cpp
79 lines (62 loc) · 2.17 KB
/
script_runner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "script_runner.hpp"
#include <cassert>
#include "common.hpp"
#include "script.hpp"
#include "script_api.hpp"
using qt_monkey_agent::PopulateScriptContext;
using qt_monkey_agent::ScriptAPI;
using qt_monkey_agent::Private::Script;
using qt_monkey_agent::Private::ScriptRunner;
static int extractLineNumFromBacktraceLine(const QString &line)
{
const int ln = line.indexOf(':');
assert(ln != -1);
return line.right(line.size() - ln - 1).toUInt();
}
ScriptRunner::ScriptRunner(ScriptAPI &api,
const PopulateScriptContext &onInitCb)
{
QScriptValue testCtrl = scriptEngine_.newQObject(&api);
QScriptValue global = scriptEngine_.globalObject();
global.setProperty(QLatin1String("Test"), testCtrl);
if (onInitCb != nullptr)
onInitCb(scriptEngine_);
}
void ScriptRunner::runScript(const Script &script, QString &errMsg)
{
scriptEngine_.evaluate(script.code(), "script", 1);
if (scriptEngine_.hasUncaughtException()) {
QString expd;
expd += QStringLiteral("Backtrace:\n");
const QStringList backtrace
= scriptEngine_.uncaughtExceptionBacktrace();
expd += backtrace.join("\n") + '\n';
int elino = 0;
if (!backtrace.empty()) {
elino = extractLineNumFromBacktraceLine(backtrace.back());
} else {
elino = scriptEngine_.uncaughtExceptionLineNumber();
}
const QStringList slines = script.code().split('\n');
if (elino <= slines.size())
expd += QString("Line which throw exception: %1\n")
.arg(slines[elino - 1]);
expd += QString("Exception: %1")
.arg(scriptEngine_.uncaughtException().toString());
errMsg = expd;
}
}
int ScriptRunner::currentLineNum() const
{
auto ctx = scriptEngine_.currentContext();
assert(ctx != nullptr);
assert(!ctx->backtrace().isEmpty());
const QStringList backtrace = ctx->backtrace();
return extractLineNumFromBacktraceLine(backtrace.back());
}
void ScriptRunner::throwError(QString errMsg)
{
auto ctx = scriptEngine_.currentContext();
assert(ctx != nullptr);
ctx->throwError(errMsg);
}