improve interpreter support

This commit is contained in:
Syping 2019-10-17 14:25:49 +02:00
parent d68f279f96
commit a509408b05
2 changed files with 61 additions and 22 deletions

View file

@ -19,6 +19,7 @@
#include "LuaEngineIO.h" #include "LuaEngineIO.h"
#include "LuaEngineOS.h" #include "LuaEngineOS.h"
#include <QApplication> #include <QApplication>
#include <QTextStream>
#include <QFont> #include <QFont>
#include <QFile> #include <QFile>
@ -44,11 +45,22 @@ int main(int argc, char *argv[])
if (arguments.length() >= 2) { if (arguments.length() >= 2) {
arguments.removeFirst(); arguments.removeFirst();
QFile luaScript(arguments.first().toString()); QFile luaScript(arguments.first().toString());
if (!luaScript.open(QIODevice::ReadOnly)) {
QTextStream(stderr) << "Error: Failed to open \"" << arguments.first().toString() << "\"." << endl;
return 1;
}
if (QString::fromUtf8(luaScript.read(2)) == "#!") {
luaScript.readLine();
}
else {
luaScript.reset();
}
LuaEngineGui luaEngineGui; LuaEngineGui luaEngineGui;
LuaEngineIO::pushClass(&luaEngineGui); LuaEngineIO::pushClass(&luaEngineGui);
LuaEngineOS::pushClass(&luaEngineGui); LuaEngineOS::pushClass(&luaEngineGui);
luaEngineGui.executeLuaScript(&luaScript); luaEngineGui.executeLuaScript(luaScript.readAll());
if (luaEngineGui.executeLuaFunction("main", arguments, true)) { if (luaEngineGui.executeLuaFunction("main", arguments, true)) {
QVariant variant = luaEngineGui.returnVariant(); QVariant variant = luaEngineGui.returnVariant();

View file

@ -22,6 +22,7 @@
#include <QTextStream> #include <QTextStream>
#include <QSaveFile> #include <QSaveFile>
#include <QFile> #include <QFile>
#include <QDir>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -42,19 +43,45 @@ int main(int argc, char *argv[])
parser.process(a); parser.process(a);
QString inputFile;
QString outputFile;
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
if (args.length() < 1) {
QTextStream(stderr) << "Error: No input files." << endl;
return 1;
}
else {
inputFile = args.at(0);
}
if (args.length() < 2) {
outputFile = QDir::current().absolutePath() + "/luaenginec.out";
}
else {
outputFile = args.at(1);
}
QFile luaScript(args.at(0)); QFile luaScript(inputFile);
if (!luaScript.open(QIODevice::ReadOnly)) {
LuaEngine luaEngine; QTextStream(stderr) << "Error: Failed to open \"" << inputFile << "\"." << endl;
if (!luaEngine.loadLuaScript(&luaScript)) {
QTextStream(stderr) << "Failed to load \"" << args.at(0) << "\"." << endl;
return 1; return 1;
} }
QSaveFile outputFile(args.at(1)); if (QString::fromUtf8(luaScript.read(2)) == "#!") {
if (!outputFile.open(QIODevice::WriteOnly)) { luaScript.readLine();
QTextStream(stderr) << "Failed to open \"" << args.at(1) << "\"." << endl; }
else {
luaScript.reset();
}
LuaEngine luaEngine;
if (!luaEngine.loadLuaScript(luaScript.readAll())) {
QTextStream(stderr) << "Error: Failed to load \"" << inputFile << "\"." << endl;
return 1;
}
QSaveFile outputSaveFile(outputFile);
if (!outputSaveFile.open(QIODevice::WriteOnly)) {
QTextStream(stderr) << "Error: Failed to open \"" << outputFile << "\"." << endl;
return 1; return 1;
} }
@ -62,24 +89,24 @@ int main(int argc, char *argv[])
if (!pLuaEngine.isEmpty()) { if (!pLuaEngine.isEmpty()) {
QFile portableLuaEngine(pLuaEngine); QFile portableLuaEngine(pLuaEngine);
if (!portableLuaEngine.open(QIODevice::ReadOnly)) { if (!portableLuaEngine.open(QIODevice::ReadOnly)) {
outputFile.cancelWriting(); outputSaveFile.cancelWriting();
outputFile.commit(); outputSaveFile.commit();
QTextStream(stderr) << "Failed to open \"" << pLuaEngine << "\"." << endl; QTextStream(stderr) << "Error: Failed to open \"" << pLuaEngine << "\"." << endl;
return 1; return 1;
} }
outputFile.write(portableLuaEngine.readAll()); outputSaveFile.write(portableLuaEngine.readAll());
} }
const QByteArray luaDump = luaEngine.dumpLuaScript(); const QByteArray luaDump = luaEngine.dumpLuaScript();
int dumpSize = luaDump.size(); int dumpSize = luaDump.size();
outputFile.write(luaDump); outputSaveFile.write(luaDump);
if (!pLuaEngine.isEmpty()) { if (!pLuaEngine.isEmpty()) {
QByteArray lengthArray = QByteArray::number(dumpSize, 16); QByteArray lengthArray = QByteArray::number(dumpSize, 16);
if (lengthArray.size() > 8) { if (lengthArray.size() > 8) {
outputFile.cancelWriting(); outputSaveFile.cancelWriting();
outputFile.commit(); outputSaveFile.commit();
QTextStream(stderr) << "Lua Engine script is too large." << endl; QTextStream(stderr) << "Error: Lua Engine script is too large." << endl;
return 1; return 1;
} }
else { else {
@ -87,12 +114,12 @@ int main(int argc, char *argv[])
lengthArray.insert(0, "0"); lengthArray.insert(0, "0");
} }
} }
outputFile.write(QByteArray::fromHex(lengthArray)); outputSaveFile.write(QByteArray::fromHex(lengthArray));
outputFile.write("\xb4\x00", 2); outputSaveFile.write("\xb4\x00", 2);
} }
if (!outputFile.commit()) { if (!outputSaveFile.commit()) {
QTextStream(stderr) << "Failed to write \"" << args.at(1) << "\"." << endl; QTextStream(stderr) << "Error: Failed to write \"" << args.at(1) << "\"." << endl;
return 1; return 1;
} }