Repository navigation
Expand file tree
/
Copy pathLgInterpreter.cpp
More file actions
336 lines (319 loc) · 9.3 KB
/
Copy pathLgInterpreter.cpp
File metadata and controls
336 lines (319 loc) · 9.3 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/******************************************************************************
* ScriptInterpreter.cpp
*
* This file is part of LgScript
* Copyright (C) 2011 Tom N Harris <telliamed@whoopdedo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*****************************************************************************/
#define LUAX_INLINE
#include "luax.h"
#include "LgInterpreter.h"
#include "LgScript.h"
#include "LgMessage.h"
#include "LgLinkset.h"
#include "LgLink.h"
#include "LgObject.h"
#include "LgServices.h"
#include "ScriptModule.h"
using namespace Lgs;
using namespace luax;
const char ScriptInterpreter::s_ScriptRegistry[] = "LGSRegistry";
const char ScriptInterpreter::s_ScriptCache[] = "LGSCache";
int ScriptInterpreter::Print(Handle L)
{
char buf[1024];
State S(L);
int num = S.getTop();
S.getGlobal("tostring");
for (int i=1; i<=num; i++)
{
S.copy().copy(i).call(1,1);
size_t l;
const char* s = S.asString(-1, &l);
if (s == NULL)
return S.error(LUA_QL("tostring") " must return a string to " LUA_QL("print"));
if (i > 1)
ScriptModule::MPrintf("\t");
while (l >= 1024)
{
strncpy(buf, s, 1023);
buf[1023] = '\0';
s += 1023;
l -= 1023;
ScriptModule::MPrintf("%s", buf);
}
ScriptModule::MPrintf("%s", s);
S.pop();
}
ScriptModule::MPrintf("\n");
return 0;
}
int ScriptInterpreter::NoPrint(Handle L)
{
State S(L);
S.setTop(0);
return 0;
}
int ScriptInterpreter::Traceback(Handle L)
{
State S(L);
int err = 1;
LgScript* pScript = NULL;
if (!S.isNoneOrNil(Upvalue(1)))
pScript = S.toUserdata(Userdata<LgScript>(),Upvalue(1));
if (!S.isString(1)) /* 'message' not a string? */
{
err = S.push("Unknown error").getTop();
}
if (!S.getGlobal("debug").isTable())
{
S.setTop(1);
return 1;
}
if (!S.getField("traceback").isFunction())
{
S.setTop(1);
return 1;
}
S.copy(err) // pass error message
.push(2) // skip this function and traceback
.call(2,1); // call debug.traceback
if (pScript)
ScriptModule::MPrintf("!!! [%s:%d] %s\n",
pScript->Name(), pScript->ObjId(),
S.asString());
else
ScriptModule::MPrintf("!!! %s\n", S.asString());
if (err != 1)
S.setTop(1);
return 1;
}
int ScriptInterpreter::NoTraceback(Handle L)
{
State S(L);
#if 0
// Why bother? There's no monolog in the retail game.
int err = 1;
LgScript* pScript = NULL;
if (!S.isNoneOrNil(Upvalue(1)))
pScript = S.toUserdata(Userdata<LgScript>(),Upvalue(1));
if (!S.isString(1)) /* 'message' not a string? */
{
err = S.push("Unknown error").getTop();
}
if (pScript)
ScriptModule::MPrintf("!!! [%s:%d] %s\n",
pScript->Name(), pScript->ObjId(),
S.asString());
else
ScriptModule::MPrintf("!!! %s\n", S.asString());
if (err != 1)
#endif
S.setTop(1);
return 1;
}
ScriptInterpreter::~ScriptInterpreter()
{
#ifdef DEBUG
m_pLua->gcCollect();
Cache();
ScriptModule::MPrintf("*** CACHE %d (should be 0)\n", m_pLua->objLen());
Instance();
ScriptModule::MPrintf("*** INSTANCE %d (should be 0)\n", m_pLua->objLen());
ScriptModule::MPrintf("*** GCCOUNT %dKb\n", m_pLua->gcCount());
#endif
delete static_cast<MainState*>(m_pLua);
}
ScriptInterpreter::ScriptInterpreter(bool bEditor)
: m_pLua(NULL), m_bEditor(bEditor)
{
ScriptModule::MPrintf(
"=== This is LgScript 1.0 Copyright (C) 2011 Tom N Harris\n"
"=== Using " LUA_RELEASE " " LUA_COPYRIGHT "\n"
"=== LgScript comes with ABSOLUTELY NO WARRANTY; see the GNU General\n"
"=== Public License for more details.\n"
"=== This is free software, and you are welcome to redistribute it\n"
"=== under certain conditions; for details see the copyright file\n"
"=== that is distributed with this script module.\n\n"
);
m_pLua = new MainState();
m_pLua->gcStop();
static_cast<MainState*>(m_pLua)->openLibs();
Init();
LgScript::Init(*m_pLua);
ScriptMessage::Init(*m_pLua);
LinkSet::Init(*m_pLua);
LgLink::Init(*m_pLua);
LgObject::Init(*m_pLua);
ScriptServices::Init(*m_pLua);
m_pLua->gcRestart();
#ifdef DEBUG
m_pLua->gcCollect();
ScriptModule::MPrintf("*** GCCOUNT %dKb\n", m_pLua->gcCount());
#endif
}
void ScriptInterpreter::Init(void)
{
// Create chunk cache
m_pLua->createTable(0,0).setField(s_ScriptCache, LUA_REGISTRYINDEX);
// Create script instance cache
m_pLua->createTable(0,0).copy().setMetatable();
//m_pLua->push("k").setField("__mode");
m_pLua->setField(s_ScriptRegistry, LUA_REGISTRYINDEX);
// Replace `print'
m_pLua->push(m_bEditor ? Print : NoPrint).setField("print", LUA_GLOBALSINDEX);
}
int ScriptInterpreter::Registry(State& S, const char* pszKey)
{
return S.getFieldI(pszKey, LUA_REGISTRYINDEX);
}
IScript* ScriptInterpreter::LoadScript(const char* pszName, int iObjId)
{
IScript* pScript = NULL;
Frame S(*m_pLua);
S->checkStack(LUA_MINSTACK);
S->push(pszName); // name
S->copy().push(iObjId).concat(); // name nameobjid
int iCache = Cache(); // name nameobjid LGS
if (!S->rawGet(iCache, -2).isNil()) // name nameobjid LGS script
{
#ifdef DEBUG
S->getGlobal("print").push("*** CACHE > ").copy(-5).copy(-4).call(3,0);
#endif
pScript = S->toUserdata(Userdata<IScript>());
pScript->AddRef();
return pScript;
}
S->pop(); // name nameobjid LGS
S->copy(-3).push(0).concat(); // name nameobjid LGS name0
if (S->rawGet(iCache, -1).isNil()) // name nameobjid LGS name0 chunk
{
S->pop().copy(-4); // name nameobjid LGS name0 name
if (0 != Chunk()) // name nameobjid LGS name0 chunk
{
ScriptModule::MPrintf("!!! Script not found [%s]\n", pszName);
ScriptModule::MPrintf("!!! %s\n", S->asString());
return NULL;
}
#ifdef DEBUG
S->getGlobal("print").push("*** CACHE < ").copy(-4).copy(-4).call(3,0);
#endif
S->rawSet(iCache, -2, -1); // cache[name0] = chunk
}
S->remove(-2); // name nameobjid LGS chunk
pScript = new(*m_pLua) LgScript(this, pszName, iObjId);
S->insert(-2).copy(-2); // name nameobjid LGS script chunk script
if (0 != Environment()) // name nameobjid LGS script screnv
{
// no need to delete script, GC will clean it up
return NULL;
}
#ifdef DEBUG
S->getGlobal("print").push("*** CACHE < ").copy(-6).copy(-5).call(3,0);
#endif
S->rawSet(iCache, -4, -2); // cache[nameobjid] = script
int iScripts = Instance(); // name nameobjid LGS script screnv Registry
S->push(Userdata<IScript>(pScript)); // name nameobjid LGS script screnv Registry pointer
#ifdef DEBUG
S->getGlobal("print").push("*** INSTANCE < ").copy(-3).copy(-6).call(3,0);
#endif
S->copy(-3).rawSet(iScripts); // registry[pointer] = screnv
return pScript;
}
void ScriptInterpreter::UnloadScript(const char* pszName, int iObjId)
{
Frame S(*m_pLua);
S->push(pszName); // name
S->copy().push(iObjId).concat(); // name nameobjid
#ifdef DEBUG
S->getGlobal("print").push("*** CACHE X ").copy(-3).call(2,0);
#endif
int iCache = Cache(); // name nameobjid LGS
if (S->rawGet(iCache, -2).isNil()) // name nameobjid LGS script
{
ScriptModule::MPrintf("!!! Script not found. Possible memory leak. [%s:%d]\n",
pszName, iObjId);
return;
}
IScript* pScript = S->toUserdata(Userdata<IScript>());
S->pop(); // name nameobjid LGS
S->copy(-2).push(Nil()).rawSet(iCache); // cache[nameobjid] = nil
#ifdef DEBUG
S->getGlobal("print").push("*** CACHE X ").push(Userdata<IScript>(pScript)).call(2,0);
#endif
int iScripts = Instance(); // name nameobjid LGS Registry
S->push(Userdata<IScript>(pScript)).push(Nil()).rawSet(iScripts); // scripts[pointer] = nil
#ifdef DEBUG
S->gcCollect();
ScriptModule::MPrintf("*** GCCOUNT %dKb\n", S->gcCount());
#endif
}
// Load script chunk. Name is passed on the stack.
int ScriptInterpreter::Chunk(void)
{
State S(*m_pLua);
int iName = S.getTop();
int iLoaders = S.getGlobal("package").getFieldI("loaders");
if (!S.isTable(iLoaders))
S.error(LUA_QL("package.loaders") " must be a table");
S.push("");
for (int i=1; !S.rawGetN(i, iLoaders).isNil(); i++)
{
S.copy(iName).call(1, 1);
if (S.isFunction())
{
S.insert(iName).setTop(iName);
return 0;
}
if (S.isString())
S.concat();
else
S.pop();
}
S.pop().insert(iName).setTop(iName);
return 1;
}
// Create environment for script. Stack is [chunk script]
int ScriptInterpreter::Environment(void)
{
State S(*m_pLua);
int iChunk = S.getTop() - 1;
S.createTable(0, 2);
LgScript::Environment(S);
// chunk script env
S.push("script").copy(-3).rawSet(-3).remove(-2);
//S.insert(-2).setField("script"); // env['script'] = script
S.copy().setFEnv(iChunk);
try
{
S.push(m_bEditor ? Traceback : NoTraceback) // chunk env traceback chunk
.copy(iChunk).pCall(0, 0, -2);
}
catch (Exception&)
{
S.copy(LUA_GLOBALSINDEX).setFEnv(iChunk);
S.setTop(iChunk-1);
return 1;
}
S.copy(LUA_GLOBALSINDEX).setFEnv(iChunk);
S.remove(iChunk).setTop(iChunk); // env
return 0;
}
void ScriptInterpreter::PushTraceback(State& S, IScript* pScript)
{
S.push(Userdata<IScript>(pScript))
.push(m_bEditor ? Traceback : NoTraceback, 1);
}