/
main.cpp
67 lines (56 loc) · 2.01 KB
/
main.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
#include <QtCore/QCoreApplication>
#include <QtScript/QScriptEngine>
#include <QFile>
#include <QFileInfo>
#include <qtextstream.h>
#include <QDebug>
#include "scriptfunctions.h"
static QScriptValue loadScripts(QScriptContext *context, QScriptEngine *engine)
{
for (int i = 0; i < context->argumentCount(); ++i) {
QString fileName = context->argument(0).toString();
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return context->throwError(QString::fromLatin1("could not open %0 for reading").arg(fileName));
QTextStream ts(&file);
QString contents = ts.readAll();
file.close();
QScriptContext *pc = context->parentContext();
context->setActivationObject(pc->activationObject());
context->setThisObject(pc->thisObject());
QScriptValue ret = engine->evaluate(contents);
if (engine->hasUncaughtException())
return ret;
}
return engine->undefinedValue();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString fileName = "load.qs";
QFile scriptFile(fileName);
if (!scriptFile.open(QIODevice::ReadOnly))
{
// handle error
qDebug()<<"File open error";
}
else
{
QFileInfo info(scriptFile);
qDebug()<< "File Path" + info.absoluteFilePath();
}
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
scriptFile.close();
ScriptFunctions functions;
QScriptEngine myEngine;
QScriptValue scriptFunctions = myEngine.newQObject(&functions);
myEngine.globalObject().setProperty("nativeFunctions", scriptFunctions);
myEngine.globalObject().setProperty("load", myEngine.newFunction(loadScripts, /*length=*/1));
QScriptValue result = myEngine.evaluate(contents, fileName);
if (myEngine.hasUncaughtException()) {
int line = myEngine.uncaughtExceptionLineNumber();
qDebug() << "uncaught exception at line" << line << ":" << result.toString();
}
return a.exec();
}