/
lua_glue.c
237 lines (191 loc) · 6.84 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
76
77
78
79
80
81
82
83
84
85
86
87
88
void MojoLua_collectGarbage(void)
{
lua_State *L = luaState;
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);
lua_gc (L, LUA_GCCOLLECT, 0);
printf("Collection finished (took ??? milliseconds).\n"); // !!! FIXME
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
89
90
// !!! FIXME: God, I'm so torn about whether we should call Lua or lua should
// !!! FIXME: call us for this...
91
92
93
94
95
96
// 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;
97
98
99
100
101
if (!lua_checkstack(luaState, 5))
return str;
lua_getglobal(luaState, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(luaState, -1)) // namespace is sane?
102
{
103
104
lua_getfield(luaState, -1, "translate"); popcount++;
if (lua_isfunction(luaState, -1))
105
{
106
107
108
109
110
const char *tr = NULL;
lua_pushstring(luaState, str);
lua_call(luaState, 1, 1); // popcount ends up the same...
tr = lua_tostring(luaState, -1);
if (tr != NULL)
111
{
112
113
xstrncpy(scratchbuf_128k, tr, sizeof(scratchbuf_128k));
retval = scratchbuf_128k;
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
// 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
151
152
153
154
155
156
157
158
// 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
159
160
boolean MojoLua_initLua(void)
{
161
char locale[16];
162
163
char ostype[64];
char osversion[64];
164
165
166
if (!MojoPlatform_locale(locale, sizeof (locale)))
xstrncpy(locale, "???", sizeof (locale));
167
168
169
170
171
172
if (!MojoPlatform_osType(ostype, sizeof (ostype)))
xstrncpy(ostype, "???", sizeof (ostype));
if (!MojoPlatform_osVersion(osversion, sizeof (osversion)))
xstrncpy(osversion, "???", sizeof (osversion));
assert(luaState == NULL);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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);
191
// Set up initial C functions, etc we want to expose to Lua code...
192
set_cfunc(luaState, luahook_runfile, "runfile");
193
set_string(luaState, locale, "locale");
194
195
196
197
set_string(luaState, PLATFORM_NAME, "platform");
set_string(luaState, PLATFORM_ARCH, "arch");
set_string(luaState, ostype, "ostype");
set_string(luaState, osversion, "osversion");
198
199
lua_setglobal(luaState, "MojoSetup");
200
// Set up localization table, if possible.
201
MojoLua_runFile("localization");
202
203
204
205
// Transfer control to Lua to setup some APIs and state...
if (!MojoLua_runFile("mojosetup_init"))
return false;
206
207
// ...and run the installer-specific config file.
208
209
210
if (!MojoLua_runFile("config"))
return false;
211
212
213
214
215
216
217
218
219
220
221
222
// 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.
223
224
225
226
227
228
229
230
231
232
233
234
235
236
return true;
} // MojoLua_initLua
void MojoLua_deinitLua(void)
{
if (luaState != NULL)
{
lua_close(luaState);
luaState = NULL;
} // if
} // MojoLua_deinitLua
// end of lua_glue.c ...