This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathluaengine_p.cpp
213 lines (182 loc) · 6.15 KB
/
luaengine_p.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifdef HAS_LUAJIT
#include "luaengine_p.h"
#include <QFile>
#include <QByteArray>
#include <QDebug>
#include <QApplication>
#include <QSharedPointer>
#include "luaapi.h"
#include <QDir>
#include <QDebug>
static QStringList pluginsDirectoryPrefix;
static int QtResourceSearcher(lua_State *lua)
{
// check module name prefix
QString moduleName(luaL_checkstring(lua, 1));
if (moduleName == "lclass" ||
moduleName == "json") {
// lclass is an external library, so no prefix needed
moduleName.prepend("terravox.");
}
// try loading corresponding file
QString path = moduleName;
bool isApiFile = moduleName == "terravox.api";
path.replace('.', '/');
path += isApiFile ? ".h" : ".lua";
if (moduleName.startsWith("terravox.")) {
path = ":/TerravoxLua/" + path.mid(9);
} else {
auto origPath = path;
foreach (const QString &prefix, pluginsDirectoryPrefix) {
path = prefix + "/" + origPath;
if (QFile(path).exists()) {
break;
}
}
}
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
lua_pushstring(lua, ("\n" + path + ": " + file.errorString()).toUtf8().data());
return 1;
}
QByteArray source = file.readAll();
if (isApiFile) {
// translate to LuaJIT FFI definition code
QString str(source);
auto lines = str.split('\n');
lines = lines.filter(QRegExp("^[^#].*$"));
str = lines.join('\n');
source = str.toUtf8();
source.prepend("require(\"ffi\").cdef[[");
source.append("]]");
}
luaL_loadbuffer(lua, source.data(), source.size(), moduleName.toUtf8().data());
return 1;
}
LuaEnginePrivate::LuaEnginePrivate(LuaEngine *e) :
q_ptr(e),
isDestroyed(new bool(false))
{
}
void LuaEnginePrivate::initialize(LuaInterface *i)
{
Q_Q(LuaEngine);
lua = lua_open();
if (!lua) {
emit q->error(tr("Fail to start LuaJIT."));
return;
}
static LuaEnginePrivate *priv = this; // quick hack (assuming LuaEnginePrivate is singleton)
lua_atpanic(lua, [](lua_State *lua) {
const char *msg = luaL_checkstring(lua, -1);
qDebug() << msg;
emit priv->q_func()->error(msg);
return 0;
});
luaL_openlibs(lua);
// add searcher
pluginsDirectoryPrefix = q->pluginDirectories(false);
lua_pushcfunction(lua, &QtResourceSearcher);
lua_setglobal(lua, "__qsearcher");
auto isDestroyed = this->isDestroyed;
sinf.reset(new LuaScriptInterface(i, [=](std::function<void()> f){
if (*isDestroyed) { // always fail the call after Lua engine is destroyed
qDebug() << "FIXME: Lua code was called after Lua engine was destroyed.";
return false;
}
return callProtected(f);
}));
lua_pushinteger(lua, reinterpret_cast<lua_Integer>(sinf->api()));
lua_setglobal(lua, "__terravoxApi");
// load main script
QFile mainFile(":/TerravoxLua/main.lua");
if (!mainFile.open(QIODevice::ReadOnly)) {
emit q->error(tr("Fail to start LuaJIT main script."));
return;
}
QByteArray mainSource = mainFile.readAll();
Q_ASSERT(mainSource.length() > 0);
int ret = luaL_loadbuffer(lua, mainSource.data(), mainSource.size(), "terravox.main");
if (ret) {
const char *msg = luaL_checkstring(lua, -1);
emit q->error(tr("Fail to start LuaJIT main script.") + "\n\n" +
(msg ? QString(msg) : tr("(no information available)")));
return;
}
if (!callProtected(0, 0)) {
return;
}
// load plugins
QStringList pluginsDirs = q->pluginDirectories(false);
foreach (const QString &pluginsDirPath, pluginsDirs) {
qDebug() << "Scanning: " << pluginsDirPath;
QDir pluginsDir(pluginsDirPath);
if (pluginsDir.exists()) {
QStringList errors;
foreach (const QString& ent, pluginsDir.entryList(QDir::Dirs|QDir::Readable)) {
if (ent.startsWith('.')) {
continue;
}
if (ent.contains('.')) {
qDebug() << "Invalid plugin name " << ent;
continue;
}
QDir pluginDir(pluginsDir.path() + "/" + ent);
QString mainFilePath = pluginDir.path() + "/main.lua";
QFile mainFile(mainFilePath);
if (!mainFile.exists()) {
qDebug() << "Plugin main file " << mainFilePath << " does not exist.";
continue;
}
QString mainModule = ent + ".main";
lua_getglobal(lua, "require");
lua_pushstring(lua, mainModule.toUtf8().data());
int ret = lua_pcall(lua, 1, 0, 0);
if (ret) {
// fail
const char *msg = luaL_checkstring(lua, -1);
errors.push_back(ent + " : " + (msg ? msg : tr("(unknown)")));
}
}
if (!errors.empty()) {
emit q->error(tr("One or more plugins failed to load.\n\n%1").arg(errors.join("\n\n")));
}
}
}
}
bool LuaEnginePrivate::callProtected(int nargs, int nresults)
{
Q_Q(LuaEngine);
int ret = lua_pcall(lua, nargs, nresults, 0);
if (ret) {
// fail
const char *msg = luaL_checkstring(lua, -1);
emit q->error(msg ? msg : tr("(unknown)"));
return false;
}
return true;
}
bool LuaEnginePrivate::callProtected(std::function<void ()> fn)
{
Q_Q(LuaEngine);
int ret = lua_cpcall(lua, [](lua_State *lua) {
// careful not to use RAII here because Lua might do longjmp...
auto &fn = *reinterpret_cast<std::function<void ()> *>(lua_touserdata(lua, -1));
lua_pop(lua, 1);
fn();
return 0;
}, reinterpret_cast<void *>(&fn));
if (ret) {
// fail
const char *msg = luaL_checkstring(lua, -1);
emit q->error(msg ? msg : tr("(unknown)"));
return false;
}
return true;
}
LuaEnginePrivate::~LuaEnginePrivate()
{
*isDestroyed = true;
// lua_close(lua); // crashes...
}
#endif