/
lua_glue.c
247 lines (200 loc) · 7.15 KB
1
#include "universal.h"
2
#include "platform.h"
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
#include "fileio.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#define MOJOSETUP_NAMESPACE "MojoSetup"
static lua_State *luaState = NULL;
// Read data from a MojoInput when loading Lua code.
static const char *MojoLua_reader(lua_State *L, void *data, size_t *size)
{
MojoInput *in = (MojoInput *) data;
const char *retval = (const char *) scratchbuf_128k;
int64 br = in->read(in, scratchbuf_128k, sizeof (scratchbuf_128k));
if (br <= 0) // eof or error? (lua doesn't care which?!)
{
br = 0;
retval = NULL;
} // if
*size = (size_t) br;
return retval;
} // MojoLua_reader
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
boolean MojoLua_runFile(const char *basefname)
{
boolean retval = false;
size_t alloclen = strlen(basefname) + 16;
char *fname = (char *) xmalloc(alloclen);
int rc = 0;
MojoInput *io = NULL;
// !!! FIXME: change this API to accept multiple filenames to search for,
// !!! FIXME: since two full enumerations to discover the filename is
// !!! FIXME: missing is just wasteful.
STUBBED("See FIXME above.");
snprintf(fname, alloclen, "lua/%s.luac", basefname);
io = MojoInput_newFromArchivePath(GBaseArchive, fname);
#if !DISABLE_LUA_PARSER
if (io == NULL)
{
snprintf(fname, alloclen, "lua/%s.lua", basefname);
io = MojoInput_newFromArchivePath(GBaseArchive, fname);
} // if
#endif
if (io != NULL)
{
rc = lua_load(luaState, MojoLua_reader, io, fname);
io->close(io);
if (rc != 0)
lua_error(luaState);
else
{
// !!! FIXME: use pcall instead so we can get error backtraces and localize.
// Call new chunk on top of the stack (lua_call will pop it off).
lua_call(luaState, 0, 0); // return values are dumped.
retval = true; // if this didn't panic, we succeeded.
} // if
} // if
free(fname);
return retval;
} // MojoLua_runFile
73
74
75
void MojoLua_collectGarbage(void)
{
lua_State *L = luaState;
76
uint32 ticks = 0;
77
78
79
80
81
82
int pre = 0;
int post = 0;
STUBBED("logging!");
pre = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
printf("Collecting garbage (currently using %d bytes).\n", pre);
83
ticks = MojoPlatform_ticks();
84
lua_gc (L, LUA_GCCOLLECT, 0);
85
86
ticks = MojoPlatform_ticks() - ticks;
printf("Collection finished (took %d milliseconds).\n", (int) ticks);
87
88
89
90
91
post = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
printf("Now using %d bytes (%d bytes savings).\n", post, pre - post);
} // MojoLua_collectGarbage
92
93
94
95
96
97
// Since localization is kept in Lua tables, I stuck this in the Lua glue.
const char *translate(const char *str)
{
const char *retval = str;
int popcount = 0;
98
if (lua_checkstack(luaState, 3))
99
{
100
101
lua_getglobal(luaState, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(luaState, -1)) // namespace is sane?
102
{
103
104
lua_getfield(luaState, -1, "translations"); popcount++;
if (lua_istable(luaState, -1)) // translation table is sane?
105
{
106
107
108
109
110
111
112
113
const char *tr = NULL;
lua_getfield(luaState, -1, str); popcount++;
tr = lua_tostring(luaState, -1);
if (tr != NULL) // translated for this locale?
{
xstrncpy(scratchbuf_128k, tr, sizeof(scratchbuf_128k));
retval = scratchbuf_128k;
} // if
114
115
116
117
118
119
120
121
122
123
124
125
} // if
} // if
} // if
lua_pop(luaState, popcount); // remove our stack salsa.
return retval;
} // translate
// Hook into our error handling in case Lua throws a runtime error.
static int MojoLua_panic(lua_State *L)
{
126
127
128
const char *errstr = lua_tostring(L, 1);
if (errstr == NULL)
errstr = _("Unknown Lua error");
129
130
131
132
return fatal(errstr); // doesn't actually return.
} // MojoLua_panic
133
134
135
136
137
138
139
140
141
142
// Lua interface to MojoLua_runFile(). This is needed instead of Lua's
// require(), since it can access scripts inside an archive.
static int luahook_runfile(lua_State *L)
{
const char *fname = luaL_checkstring(L, 1);
lua_pushboolean(L, MojoLua_runFile(fname));
return 1;
} // luahook_runfile
143
144
145
146
147
148
149
150
151
// Lua interface to translate().
static int luahook_translate(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
lua_pushstring(L, translate(str));
return 1;
} // luahook_translate
152
153
154
155
156
157
158
159
// Sets t[sym]=f, where t is on the top of the Lua stack.
static inline void set_cfunc(lua_State *L, lua_CFunction f, const char *sym)
{
lua_pushcfunction(luaState, f);
lua_setfield(luaState, -2, sym);
} // set_cfunc
160
161
162
163
164
165
166
167
// Sets t[sym]=f, where t is on the top of the Lua stack.
static inline void set_string(lua_State *L, const char *str, const char *sym)
{
lua_pushstring(luaState, str);
lua_setfield(luaState, -2, sym);
} // set_string
168
169
boolean MojoLua_initLua(void)
{
170
char locale[16];
171
172
char ostype[64];
char osversion[64];
173
174
175
if (!MojoPlatform_locale(locale, sizeof (locale)))
xstrncpy(locale, "???", sizeof (locale));
176
177
178
179
180
181
if (!MojoPlatform_osType(ostype, sizeof (ostype)))
xstrncpy(ostype, "???", sizeof (ostype));
if (!MojoPlatform_osVersion(osversion, sizeof (osversion)))
xstrncpy(osversion, "???", sizeof (osversion));
assert(luaState == NULL);
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
luaState = luaL_newstate(); // !!! FIXME: define our own allocator?
if (luaState == NULL)
return false;
lua_atpanic(luaState, MojoLua_panic);
if (!lua_checkstack(luaState, 20)) // Just in case.
{
lua_close(luaState);
luaState = NULL;
return false;
} // if
luaL_openlibs(luaState);
// Build MojoSetup namespace for Lua to access and fill in C bridges...
lua_newtable(luaState);
200
// Set up initial C functions, etc we want to expose to Lua code...
201
set_cfunc(luaState, luahook_runfile, "runfile");
202
set_cfunc(luaState, luahook_translate, "translate");
203
set_string(luaState, locale, "locale");
204
205
206
207
set_string(luaState, PLATFORM_NAME, "platform");
set_string(luaState, PLATFORM_ARCH, "arch");
set_string(luaState, ostype, "ostype");
set_string(luaState, osversion, "osversion");
208
209
lua_setglobal(luaState, "MojoSetup");
210
// Set up localization table, if possible.
211
MojoLua_runFile("localization");
212
213
214
215
// Transfer control to Lua to setup some APIs and state...
if (!MojoLua_runFile("mojosetup_init"))
return false;
216
217
// ...and run the installer-specific config file.
218
219
220
if (!MojoLua_runFile("config"))
return false;
221
222
223
224
225
226
227
228
229
230
231
232
// We don't need the MojoSetup.schema namespace anymore. Make it
// eligible for garbage collection.
lua_getglobal(luaState, MOJOSETUP_NAMESPACE);
if (lua_istable(luaState, -1))
{
lua_pushnil(luaState);
lua_setfield(luaState, -2, "schema");
} // if
lua_pop(luaState, 1);
MojoLua_collectGarbage(); // get rid of old init crap we don't need.
233
234
235
236
237
238
239
240
241
242
243
244
245
246
return true;
} // MojoLua_initLua
void MojoLua_deinitLua(void)
{
if (luaState != NULL)
{
lua_close(luaState);
luaState = NULL;
} // if
} // MojoLua_deinitLua
// end of lua_glue.c ...