This repository was archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLThread.cpp
More file actions
265 lines (234 loc) · 5.51 KB
/
LThread.cpp
File metadata and controls
265 lines (234 loc) · 5.51 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "LThread.h"
extern "C" {
#include "Lua/lstate.h"
}
QString LThread::destoryId;
void LThread::set_destory(QString destoryId)
{
LThread::destoryId = destoryId;
}
void LThread::hookFunction(lua_State* L, lua_Debug* ar)
{
QMutex* mutex = (QMutex*)L->thread_mutex;
QString* str = (QString*)L->thread_id;
mutex->lock();
QString tName = (*str);
mutex->unlock();
if (tName == LThread::destoryId) {
lua_pushstring(L, "This thread is destoried!");
lua_error(L);
}
}
LThread::LThread(QObject *parent)
: QThread(parent)
{
this->lstate = luaL_newstate();
this->lstate->thread_id = (void*)(&(this->Id));
this->lstate->thread_mutex = (void*)(&(this->idMutex));
lua_sethook(this->lstate, LThread::hookFunction, LUA_MASKLINE, 0);
luaL_openlibs(this->lstate);
bool error1 = luaL_dostring(this->lstate, QString("package.path = '" + QCoreApplication::applicationDirPath() + "/scripts/?.lua;;"+ QCoreApplication::applicationDirPath() + "/scripts/?/?.lua;'").toStdString().c_str());
if (error1) {
qDebug("Lua Error:%s", qPrintable(lua_tostring(this->lstate, -1)));
lua_pop(this->lstate, 1);
}
bool error2 = luaL_dostring(this->lstate, QString("package.cpath = '" + QCoreApplication::applicationDirPath() + "/scripts/libs/?.dll;'").toStdString().c_str());
if (error2) {
qDebug("Lua Error:%s", qPrintable(lua_tostring(this->lstate, -1)));
lua_pop(this->lstate, 1);
}
}
LThread::~LThread()
{
lua_close(this->lstate);
for (auto& p : this->shareData) {
free(p.second);
}
this->shareData.clear();
}
void LThread::run()
{
emit this->tStarted(this->Id);
if (this->tType == LType::DoFile) {
bool error = luaL_dofile(this->lstate, this->lFileName.toStdString().c_str());
if (error) {
emit this->errorMessage(QString::fromStdString(lua_tostring(this->lstate, -1)));
lua_pop(this->lstate, 1);
emit this->tEnded(this->Id);
return;
}
}
while (this->strList.size() > 0) {
bool error = luaL_dostring(this->lstate, this->strList.dequeue().toStdString().c_str());
if (error) {
emit this->errorMessage(QString::fromStdString(lua_tostring(this->lstate, -1)));
lua_pop(this->lstate, 1);
}
}
emit this->tEnded(this->Id);
quit();
}
bool LThread::doFile(QString name)
{
if (!this->Id.isEmpty()) {
if (!this->isRunning()) {
this->tType = LType::DoFile;
this->lFileName = name;
this->start();
return true;
}
}
return false;
}
bool LThread::doString(QString str)
{
if (!this->Id.isEmpty()) {
this->tType = LType::DoString;
this->strList.enqueue(str);
if (!this->isRunning()) {
this->start();
}
else {
emit this->normalMessage("The VM is running.Command will wait in queue.");
}
return true;
}
return false;
}
bool LThread::setId(QString id)
{
if (!id.isEmpty()) {
if (this->Id.isEmpty()) {
this->Id = id;
return true;
}
}
return false;
}
QString LThread::getId()
{
return this->Id;
}
void LThread::beginGlobalTable()
{
lua_newtable(this->lstate);
}
void LThread::endGlobalTable(QString name)
{
lua_setglobal(this->lstate, name.toStdString().c_str());
}
void LThread::beginTable(QString name)
{
lua_pushstring(this->lstate, name.toStdString().c_str());
lua_newtable(this->lstate);
}
void LThread::endTable()
{
lua_settable(this->lstate, -3);
}
void LThread::addFunction(QString name, lua_CFunction function)
{
lua_pushstring(this->lstate, name.toStdString().c_str());
lua_pushcfunction(this->lstate, function);
lua_settable(this->lstate, -3);
}
void LThread::loadUtils()
{
std::string comm("IUtils = require(\"utils\")");
bool error = luaL_dostring(this->lstate, comm.c_str());
if (error) {
emit this->errorMessage(QString::fromStdString(lua_tostring(this->lstate, -1)));
lua_pop(this->lstate, 1);
}
error = luaL_dostring(this->lstate, "Infinity.Console.print = print");
if (error) {
emit this->errorMessage(QString::fromStdString(lua_tostring(this->lstate, -1)));
lua_pop(this->lstate, 1);
}
}
bool LThread::destory(QString id)
{
if (this->isRunning()) {
this->idMutex.lock();
this->Id = id;
this->idMutex.unlock();
return true;
}
return false;
}
bool LThread::checkShare(QString key)
{
this->shareMutex.lock();
bool result = this->shareData.contains(key);
this->shareMutex.unlock();
return result;
}
void* LThread::newShare(QString key, size_t size)
{
if (checkShare(key)) {
return nullptr;
}
this->shareMutex.lock();
void* result = malloc(size);
this->shareData.insert(key, qMakePair(size, result));
this->shareMutex.unlock();
return result;
}
bool LThread::removeShare(QString key)
{
if (!checkShare(key)) {
return false;
}
this->shareMutex.lock();
free(this->shareData.value(key).second);
this->shareData.remove(key);
this->shareMutex.unlock();
return true;
}
void* LThread::getShare(QString key)
{
if (!checkShare(key)) {
return nullptr;
}
this->shareMutex.lock();
void* result = this->shareData.value(key).second;
this->shareMutex.unlock();
return result;
}
size_t LThread::sizeShare(QString key)
{
if (!checkShare(key)) {
return 0;
}
this->shareMutex.lock();
size_t result = this->shareData.value(key).first;
this->shareMutex.unlock();
return result;
}
bool LThread::clearShare()
{
this->shareMutex.lock();
for (auto& p : this->shareData) {
if (p.second != nullptr) {
free(p.second);
}
}
this->shareData.clear();
this->shareMutex.unlock();
return true;
}
QStringList LThread::listShare()
{
this->shareMutex.lock();
QStringList result = this->shareData.keys();
this->shareMutex.unlock();
return result;
}
void LThread::lockShare()
{
this->shareMutex.lock();
}
void LThread::unlockShare()
{
this->shareMutex.unlock();
}