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 "LuaEngineOS.h"
#include <QApplication>
#include <QTextStream>
#include <QFont>
#include <QFile>
@ -44,11 +45,22 @@ int main(int argc, char *argv[])
if (arguments.length() >= 2) {
arguments.removeFirst();
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;
LuaEngineIO::pushClass(&luaEngineGui);
LuaEngineOS::pushClass(&luaEngineGui);
luaEngineGui.executeLuaScript(&luaScript);
luaEngineGui.executeLuaScript(luaScript.readAll());
if (luaEngineGui.executeLuaFunction("main", arguments, true)) {
QVariant variant = luaEngineGui.returnVariant();

View file

@ -22,6 +22,7 @@
#include <QTextStream>
#include <QSaveFile>
#include <QFile>
#include <QDir>
int main(int argc, char *argv[])
{
@ -31,7 +32,7 @@ int main(int argc, char *argv[])
QCommandLineParser parser;
QCommandLineOption pLuaEngineOption(QStringList() << "le" << "luaengine", "Location of portable Lua Engine executor.", "luaengine");
QCommandLineOption pLuaEngineOption(QStringList() << "le" << "luaengine", "Location of portable LuaEngine executor.", "luaengine");
parser.addOption(pLuaEngineOption);
parser.addPositionalArgument("source", "Source file to build.");
@ -42,19 +43,45 @@ int main(int argc, char *argv[])
parser.process(a);
QString inputFile;
QString outputFile;
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));
LuaEngine luaEngine;
if (!luaEngine.loadLuaScript(&luaScript)) {
QTextStream(stderr) << "Failed to load \"" << args.at(0) << "\"." << endl;
QFile luaScript(inputFile);
if (!luaScript.open(QIODevice::ReadOnly)) {
QTextStream(stderr) << "Error: Failed to open \"" << inputFile << "\"." << endl;
return 1;
}
QSaveFile outputFile(args.at(1));
if (!outputFile.open(QIODevice::WriteOnly)) {
QTextStream(stderr) << "Failed to open \"" << args.at(1) << "\"." << endl;
if (QString::fromUtf8(luaScript.read(2)) == "#!") {
luaScript.readLine();
}
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;
}
@ -62,24 +89,24 @@ int main(int argc, char *argv[])
if (!pLuaEngine.isEmpty()) {
QFile portableLuaEngine(pLuaEngine);
if (!portableLuaEngine.open(QIODevice::ReadOnly)) {
outputFile.cancelWriting();
outputFile.commit();
QTextStream(stderr) << "Failed to open \"" << pLuaEngine << "\"." << endl;
outputSaveFile.cancelWriting();
outputSaveFile.commit();
QTextStream(stderr) << "Error: Failed to open \"" << pLuaEngine << "\"." << endl;
return 1;
}
outputFile.write(portableLuaEngine.readAll());
outputSaveFile.write(portableLuaEngine.readAll());
}
const QByteArray luaDump = luaEngine.dumpLuaScript();
int dumpSize = luaDump.size();
outputFile.write(luaDump);
outputSaveFile.write(luaDump);
if (!pLuaEngine.isEmpty()) {
QByteArray lengthArray = QByteArray::number(dumpSize, 16);
if (lengthArray.size() > 8) {
outputFile.cancelWriting();
outputFile.commit();
QTextStream(stderr) << "Lua Engine script is too large." << endl;
outputSaveFile.cancelWriting();
outputSaveFile.commit();
QTextStream(stderr) << "Error: Lua Engine script is too large." << endl;
return 1;
}
else {
@ -87,12 +114,12 @@ int main(int argc, char *argv[])
lengthArray.insert(0, "0");
}
}
outputFile.write(QByteArray::fromHex(lengthArray));
outputFile.write("\xb4\x00", 2);
outputSaveFile.write(QByteArray::fromHex(lengthArray));
outputSaveFile.write("\xb4\x00", 2);
}
if (!outputFile.commit()) {
QTextStream(stderr) << "Failed to write \"" << args.at(1) << "\"." << endl;
if (!outputSaveFile.commit()) {
QTextStream(stderr) << "Error: Failed to write \"" << args.at(1) << "\"." << endl;
return 1;
}