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
|
#include <QtCore>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <lua.hpp>
#include <optional>
#include <qvariant.h>
extern "C" {
#include <luv/luv.h>
}
#include "AsyncEventLoop.hpp"
#include "LuaRuntime.hpp"
#include "LuaRuntimeApi.hpp"
LuaRuntime::LuaRuntime() {
state = luaL_newstate();
luaL_openlibs(state);
init_builtins_package_path();
init_web_api();
}
int luv_callback(lua_State *state, int nargs, int nresults, int flags) {
return luv_cfpcall(state, nargs, nresults, flags | LUVF_CALLBACK_NOEXIT);
}
void LuaRuntime::start_event_loop() {
if (event_loop != nullptr)
stop_event_loop();
// Init event loop
event_loop = new AsyncEventLoop();
// Load `uv` (luv)
luv_set_loop(state, event_loop->get_uv_loop());
luv_set_callback(state, &luv_callback);
luaopen_luv(state);
lua_setglobal(state, "uv");
luaL_dostring(state, "web.uv = uv");
}
void LuaRuntime::stop_event_loop() {
if (event_loop != nullptr) {
delete event_loop;
event_loop = nullptr;
}
// Clear the uv global
lua_pushnil(state);
lua_setglobal(state, "uv");
lua_gc(state, LUA_GCCOLLECT, 0);
}
void LuaRuntime::init_web_api() {
luaL_newlib(state, internals_api);
lua_setglobal(state, "__internals");
require_module("null-browser.api");
}
void LuaRuntime::require_module(const QString &module_name) {
preserve_top(state, {
lua_getglobal(state, "require");
lua_pushstring(state, module_name.toStdString().c_str());
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
qCritical() << "Unable to require module" << module_name << ":" << lua_tostring(state, -1);
}
});
}
void LuaRuntime::init_builtins_package_path() {
auto builtins_lua_path = QString(PROJECT_LUA_PATH);
append_package_path(builtins_lua_path);
}
void LuaRuntime::evaluate(const QString &code) {
queue_task([this, code]() {
preserve_top(state, {
if (luaL_dostring(state, code.toStdString().c_str()) != LUA_OK) {
const char *value = lua_tostring(state, -1);
qDebug() << "Lua Error: " << value;
emit evaluation_failed(value);
} else {
const QVariant value = get_lua_value(state, -1);
emit evaluation_completed(value);
}
})
});
}
void LuaRuntime::append_package_path(const QString &path) {
preserve_top(state, {
lua_getglobal(state, "package");
lua_getfield(state, -1, "path");
auto pkg_path = QString(lua_tostring(state, -1)) + ";" + path;
lua_pop(state, 1);
lua_pushstring(state, pkg_path.toStdString().c_str());
lua_setfield(state, -2, "path");
});
}
void LuaRuntime::load_file_sync(const QString &path) {
preserve_top(state, {
if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) {
lua_error(state);
}
});
}
LuaRuntime::~LuaRuntime() {
stop_event_loop();
lua_close(state);
state = nullptr;
}
std::vector<QString> LuaRuntime::lua_tostringlist(lua_State *state) {
std::vector<QString> values;
if (!lua_istable(state, -1))
return values;
lua_pushnil(state); // First key for lua_next()
while (lua_next(state, -2) != 0) {
if (lua_isstring(state, -1))
values.emplace_back(lua_tostring(state, -1));
lua_pop(state, 1);
}
lua_pop(state, 1);
return values;
}
void LuaRuntime::inspect_lua_stack(lua_State *state) {
int top = lua_gettop(state);
qDebug() << "--- Lua Stack (top: " << top << ") ---\n";
for (int i = 1; i <= top; i++) {
int type = lua_type(state, i);
qDebug() << " " << i << ": " << lua_typename(state, type);
}
qDebug() << "---------------------------\n";
lua_settop(state, top);
}
QVariant LuaRuntime::get_lua_value(lua_State *state, int idx, QVariant default_value) {
if (lua_isnoneornil(state, idx))
return default_value;
if (lua_isstring(state, idx))
return lua_tostring(state, idx);
if (lua_isboolean(state, idx))
return lua_toboolean(state, idx);
if (lua_isnumber(state, idx))
return lua_tonumber(state, idx);
return lua_tostring(state, idx);
}
void LuaRuntime::push_qvariant(lua_State *state, std::optional<QVariant> opt) {
if (!opt.has_value()) {
lua_pushnil(state);
return;
}
auto type = QString(opt.value().typeName());
// qDebug() << type << opt.value();
// TODO: QVariantMap
// TODO: QVariantList
if (type == "") {
lua_pushnil(state);
return;
}
if (type == "bool") {
lua_pushboolean(state, opt.value().toBool());
return;
}
if (type == "int") {
lua_pushinteger(state, opt.value().toInt());
return;
}
if (type == "double") {
lua_pushnumber(state, opt.value().toDouble());
return;
}
// String default
lua_pushstring(state, opt.value().toString().toStdString().c_str());
}
|