-
Notifications
You must be signed in to change notification settings - Fork 1
/
lextlib.c
347 lines (250 loc) · 8.45 KB
/
lextlib.c
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
337
338
339
340
341
342
343
344
345
346
347
#include "lextlib.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <lauxlib.h>
static int luax_traceback_function = 0;
bool luaX_havetraceback (lua_State *L) {
lua_pushlightuserdata(L, &luax_traceback_function); // [-0,+1,-]
lua_rawget(L, LUA_REGISTRYINDEX); // [-1,+1,-]
bool result = lua_isfunction(L, -1); // [-0,+0,-]
lua_pop(L, 1); // [-1,+0,-]
return result;
}
int luaX_settraceback (lua_State *L) {
luaX_checktype(L, 1, "traceback function", LUA_TFUNCTION); // [-0,+0,e]
lua_pushlightuserdata(L, &luax_traceback_function); // [-0,+1,-]
lua_pushvalue(L, 1); // [-0,+1,-]
lua_rawset(L, LUA_REGISTRYINDEX); // [-1,+1,-]
return 0;
}
int luaX_traceback (lua_State *L) {
lua_pushlightuserdata(L, &luax_traceback_function); // [-0,+1,-]
lua_rawget(L, LUA_REGISTRYINDEX); // [-1,+1,-]
if (!lua_isfunction(L, -1)) { // [-0,+0,-]
lua_pop(L, 1); // [-1,+0,-]
return 1;
}
lua_insert(L, -2); /* shift function below error message */ // [-1,+1,-]
lua_pushinteger(L, 2); /* skip this function in the trace */ // [-0,+1,-]
lua_call(L, 2, 1); /* call traceback function */ // [-3,+1,e]
return 1;
}
int luaX_panic (lua_State *L) {
int nresults = luaX_traceback(L);
if (nresults != 1) {
fprintf(stderr, "Unable to get backtrace: Unexpected number of results (%d)\n", nresults);
return 0;
}
if (!lua_isstring(L, -1)) {
fprintf(stderr, "Unable to get backtrace: Result not a string\n");
return 0;
}
const char *err = lua_tostring(L, -1);
if (err == NULL) {
fprintf(stderr, "Unable to get backtrace: Cannot convert result to string\n");
return 0;
}
fprintf(stderr, "PANIC: %s\n", err);
return 0;
}
void luaX_showstack (lua_State *L) { //> [-0,+0,e]
lua_pushcfunction(L, luaX_traceback); /* push traceback function */
lua_pushnil(L);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
fprintf(stderr, "Unable to get backtrace: Executing traceback function failed\n");
return;
}
if (!lua_isstring(L, -1)) {
fprintf(stderr, "Unable to get backtrace: Result not a string\n");
return;
}
const char *err = lua_tostring(L, -1);
if (err == NULL) {
fprintf(stderr, "Unable to get backtrace: Cannot convert result to string\n");
return;
}
fprintf(stderr, "DEBUG: %s\n", err);
lua_pop(L, 1);
}
void luaX_error (lua_State *L, const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
lua_close(L);
exit (EXIT_FAILURE);
}
void luaX_preload(lua_State *L, const char *name, lua_CFunction init_function) {
lua_getglobal(L, "package"); // [-0,+1,e]
lua_getfield(L, -1, "preload"); // [-0,+1,e]
lua_pushcfunction(L, init_function); // [-0,+1,-]
lua_setfield(L, -2, name); // package.preload[name] = init_function // [-1,+0,e]
lua_pop(L, 2); // pop (package.preload, package) // [-2,+0,-]
}
void luaX_restrict(lua_State *L) {
lua_getglobal(L, "package"); // [-0,+1,e]
lua_getfield(L, -1, "searchers"); // [-0,+1,e]
lua_newtable(L); // push ($newtable) // [-0,+1,e]
lua_rawgeti(L, -2, 1); // push (package.searchers[1]) // [-0,+1,-]
lua_rawseti(L, -2, 1); // $newtable[1] = top, pop // [-1,+0,e]
lua_setfield(L, -3, "searchers"); // package.searchers = top, pop // [-1,+0,e]
lua_pop(L, 2); // pop ($package.searchers, package) // [-2,+0,-]
}
const char *luaX_typename(lua_State *L, int narg) {
if (luaL_getmetafield(L, narg, LUAX_STR_TYPENAME)) {
const char *cname = lua_tostring(L, -1);
lua_pop(L, 1); // FIXME might free the string
return cname;
}
if (luaL_getmetafield(L, narg, LUAX_STR_CLASS)) {
if (luaL_getmetafield(L, -1, LUAX_STR_TYPENAME)) {
const char *cname = lua_tostring(L, -1);
lua_pop(L, 2); // FIXME might free the string
return cname;
}
lua_pop(L, 1);
}
return luaL_typename(L, narg);
}
const char* luaX_pushargerror (lua_State *L, int narg, const char *argname, const char *extramsg) {
const char *msg = NULL;
if (argname != NULL) {
msg = lua_pushfstring(L, LUA_QS ": %s", argname, extramsg);
}
else {
#if LUA_VERSION_NUM >= 502
msg = lua_pushstring(L, extramsg);
#else
msg = lua_pushfstring(L, "%s", extramsg);
#endif
}
return msg;
}
int luaX_argerror (lua_State *L, int narg, const char *argname, const char *extramsg) {
const char *msg = luaX_pushargerror(L, narg, argname, extramsg);
return luaL_argerror(L, narg, msg);
}
const char* luaX_pushtypeerror (lua_State *L, int narg, const char *argname, const char *tname) {
return lua_pushfstring(L, "%s expected, got %s", tname, luaX_typename(L, narg));
}
int luaX_typeerror (lua_State *L, int narg, const char *argname, const char *tname) {
const char *msg = luaX_pushtypeerror(L, narg, argname, tname);
return luaX_argerror(L, narg, argname, msg);
}
static int tag_error (lua_State *L, int narg, const char *argname, int tag) {
return luaX_typeerror(L, narg, argname, lua_typename(L, tag));
}
void luaX_checktype (lua_State *L, int narg, const char *argname, int t) {
if (lua_type(L, narg) != t)
tag_error(L, narg, argname, t);
}
lua_Number luaX_checknumber (lua_State *L, int narg, const char *argname) {
int isnum = false;
lua_Number d = lua_tonumberx(L, narg, &isnum);
if (!isnum) {
tag_error(L, narg, argname, LUA_TNUMBER);
}
return d;
}
lua_Number luaX_optnumber (lua_State *L, int narg, const char *argname, lua_Number def) {
return luaX_opt(L, luaX_checknumber, narg, argname, def);
}
lua_Integer luaX_checkinteger (lua_State *L, int narg, const char *argname) {
int isnum = false;
lua_Integer d = lua_tointegerx(L, narg, &isnum);
if (!isnum) {
tag_error(L, narg, argname, LUA_TNUMBER);
}
return d;
}
lua_Integer luaX_optinteger (lua_State *L, int narg, const char *argname, lua_Integer def) {
return luaX_opt(L, luaX_checkinteger, narg, argname, def);
}
const char* luaX_checklstring (lua_State *L, int narg, const char *argname, size_t *len) {
const char *s = lua_tolstring(L, narg, len);
if (s == NULL) {
tag_error(L, narg, argname, LUA_TSTRING);
}
return s;
}
const char* luaX_optlstring (lua_State *L, int narg, const char *argname, size_t *len, const char *def) {
if (lua_isnoneornil(L, narg)) {
if (len != NULL) {
*len = def ? strlen(def) : 0;
}
return def;
}
return luaX_checklstring(L, narg, argname, len);
}
void* luaX_checkudata (lua_State *L, int narg, const char *argname, const char *tname) {
void *d = luaL_testudata(L, narg, tname);
if (d == NULL) {
luaX_typeerror(L, narg, argname, tname);
}
return d;
}
void* luaX_optudata (lua_State *L, int narg, const char *argname, const char *tname, void* def) {
if (lua_isnoneornil(L, narg)) {
return def;
}
return luaX_checkudata(L, narg, argname, tname);
}
bool luaX_isclass (lua_State *L, int narg, const char *cname) {
narg = lua_absindex(L, narg);
lua_getmetatable(L, narg);
luaL_getmetatable(L, cname);
if (lua_rawequal(L, -1, -2)) {
lua_pop(L, 2);
return true;
}
if (!luaL_getmetafield(L, narg, LUAX_STR_CLASS)) {
lua_pop(L, 2);
return false;
}
if (lua_rawequal(L, -1, -2)) {
lua_pop(L, 3);
return true;
}
lua_pop(L, 3);
return false;
}
void* luaX_testclass (lua_State *L, int narg, const char *cname) {
void *p = lua_touserdata(L, narg);
if (p == NULL) {
return NULL;
}
if (luaX_isclass(L, narg, cname)) {
return p;
}
return NULL;
}
void* luaX_checkclass (lua_State *L, int narg, const char *cname, const char *argname) {
void *p = luaX_testclass(L, narg, cname);
if (p == NULL) {
luaX_typeerror(L, narg, argname, cname);
}
return p;
}
const char* luaX_status2str (int status) {
switch (status) {
case LUA_OK:
return "no error";
case LUA_YIELD:
return "yield";
case LUA_ERRRUN:
return "runtime error";
case LUA_ERRSYNTAX:
return "syntax error";
case LUA_ERRMEM:
return "memory allocation error";
case LUA_ERRGCMM:
return "garbage collector error";
case LUA_ERRERR:
return "message handler error";
default:
return "unknown error";
}
}