diff options
Diffstat (limited to 'src/lua/lauxlib.c')
-rw-r--r-- | src/lua/lauxlib.c | 1057 |
1 files changed, 1057 insertions, 0 deletions
diff --git a/src/lua/lauxlib.c b/src/lua/lauxlib.c new file mode 100644 index 0000000..e3d9be3 --- /dev/null +++ b/src/lua/lauxlib.c | |||
@@ -0,0 +1,1057 @@ | |||
1 | /* | ||
2 | ** $Id: lauxlib.c $ | ||
3 | ** Auxiliary functions for building Lua libraries | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | #define lauxlib_c | ||
8 | #define LUA_LIB | ||
9 | |||
10 | #include "lprefix.h" | ||
11 | |||
12 | |||
13 | #include <errno.h> | ||
14 | #include <stdarg.h> | ||
15 | #include <stdio.h> | ||
16 | #include <stdlib.h> | ||
17 | #include <string.h> | ||
18 | |||
19 | |||
20 | /* | ||
21 | ** This file uses only the official API of Lua. | ||
22 | ** Any function declared here could be written as an application function. | ||
23 | */ | ||
24 | |||
25 | #include "lua.h" | ||
26 | |||
27 | #include "lauxlib.h" | ||
28 | |||
29 | |||
30 | #if !defined(MAX_SIZET) | ||
31 | /* maximum value for size_t */ | ||
32 | #define MAX_SIZET ((size_t)(~(size_t)0)) | ||
33 | #endif | ||
34 | |||
35 | |||
36 | /* | ||
37 | ** {====================================================== | ||
38 | ** Traceback | ||
39 | ** ======================================================= | ||
40 | */ | ||
41 | |||
42 | |||
43 | #define LEVELS1 10 /* size of the first part of the stack */ | ||
44 | #define LEVELS2 11 /* size of the second part of the stack */ | ||
45 | |||
46 | |||
47 | |||
48 | /* | ||
49 | ** Search for 'objidx' in table at index -1. ('objidx' must be an | ||
50 | ** absolute index.) Return 1 + string at top if it found a good name. | ||
51 | */ | ||
52 | static int findfield (lua_State *L, int objidx, int level) { | ||
53 | if (level == 0 || !lua_istable(L, -1)) | ||
54 | return 0; /* not found */ | ||
55 | lua_pushnil(L); /* start 'next' loop */ | ||
56 | while (lua_next(L, -2)) { /* for each pair in table */ | ||
57 | if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ | ||
58 | if (lua_rawequal(L, objidx, -1)) { /* found object? */ | ||
59 | lua_pop(L, 1); /* remove value (but keep name) */ | ||
60 | return 1; | ||
61 | } | ||
62 | else if (findfield(L, objidx, level - 1)) { /* try recursively */ | ||
63 | /* stack: lib_name, lib_table, field_name (top) */ | ||
64 | lua_pushliteral(L, "."); /* place '.' between the two names */ | ||
65 | lua_replace(L, -3); /* (in the slot occupied by table) */ | ||
66 | lua_concat(L, 3); /* lib_name.field_name */ | ||
67 | return 1; | ||
68 | } | ||
69 | } | ||
70 | lua_pop(L, 1); /* remove value */ | ||
71 | } | ||
72 | return 0; /* not found */ | ||
73 | } | ||
74 | |||
75 | |||
76 | /* | ||
77 | ** Search for a name for a function in all loaded modules | ||
78 | */ | ||
79 | static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { | ||
80 | int top = lua_gettop(L); | ||
81 | lua_getinfo(L, "f", ar); /* push function */ | ||
82 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); | ||
83 | if (findfield(L, top + 1, 2)) { | ||
84 | const char *name = lua_tostring(L, -1); | ||
85 | if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ | ||
86 | lua_pushstring(L, name + 3); /* push name without prefix */ | ||
87 | lua_remove(L, -2); /* remove original name */ | ||
88 | } | ||
89 | lua_copy(L, -1, top + 1); /* copy name to proper place */ | ||
90 | lua_settop(L, top + 1); /* remove table "loaded" and name copy */ | ||
91 | return 1; | ||
92 | } | ||
93 | else { | ||
94 | lua_settop(L, top); /* remove function and global table */ | ||
95 | return 0; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | |||
100 | static void pushfuncname (lua_State *L, lua_Debug *ar) { | ||
101 | if (pushglobalfuncname(L, ar)) { /* try first a global name */ | ||
102 | lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); | ||
103 | lua_remove(L, -2); /* remove name */ | ||
104 | } | ||
105 | else if (*ar->namewhat != '\0') /* is there a name from code? */ | ||
106 | lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ | ||
107 | else if (*ar->what == 'm') /* main? */ | ||
108 | lua_pushliteral(L, "main chunk"); | ||
109 | else if (*ar->what != 'C') /* for Lua functions, use <file:line> */ | ||
110 | lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); | ||
111 | else /* nothing left... */ | ||
112 | lua_pushliteral(L, "?"); | ||
113 | } | ||
114 | |||
115 | |||
116 | static int lastlevel (lua_State *L) { | ||
117 | lua_Debug ar; | ||
118 | int li = 1, le = 1; | ||
119 | /* find an upper bound */ | ||
120 | while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } | ||
121 | /* do a binary search */ | ||
122 | while (li < le) { | ||
123 | int m = (li + le)/2; | ||
124 | if (lua_getstack(L, m, &ar)) li = m + 1; | ||
125 | else le = m; | ||
126 | } | ||
127 | return le - 1; | ||
128 | } | ||
129 | |||
130 | |||
131 | LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, | ||
132 | const char *msg, int level) { | ||
133 | luaL_Buffer b; | ||
134 | lua_Debug ar; | ||
135 | int last = lastlevel(L1); | ||
136 | int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; | ||
137 | luaL_buffinit(L, &b); | ||
138 | if (msg) { | ||
139 | luaL_addstring(&b, msg); | ||
140 | luaL_addchar(&b, '\n'); | ||
141 | } | ||
142 | luaL_addstring(&b, "stack traceback:"); | ||
143 | while (lua_getstack(L1, level++, &ar)) { | ||
144 | if (limit2show-- == 0) { /* too many levels? */ | ||
145 | int n = last - level - LEVELS2 + 1; /* number of levels to skip */ | ||
146 | lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); | ||
147 | luaL_addvalue(&b); /* add warning about skip */ | ||
148 | level += n; /* and skip to last levels */ | ||
149 | } | ||
150 | else { | ||
151 | lua_getinfo(L1, "Slnt", &ar); | ||
152 | if (ar.currentline <= 0) | ||
153 | lua_pushfstring(L, "\n\t%s: in ", ar.short_src); | ||
154 | else | ||
155 | lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); | ||
156 | luaL_addvalue(&b); | ||
157 | pushfuncname(L, &ar); | ||
158 | luaL_addvalue(&b); | ||
159 | if (ar.istailcall) | ||
160 | luaL_addstring(&b, "\n\t(...tail calls...)"); | ||
161 | } | ||
162 | } | ||
163 | luaL_pushresult(&b); | ||
164 | } | ||
165 | |||
166 | /* }====================================================== */ | ||
167 | |||
168 | |||
169 | /* | ||
170 | ** {====================================================== | ||
171 | ** Error-report functions | ||
172 | ** ======================================================= | ||
173 | */ | ||
174 | |||
175 | LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { | ||
176 | lua_Debug ar; | ||
177 | if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ | ||
178 | return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); | ||
179 | lua_getinfo(L, "n", &ar); | ||
180 | if (strcmp(ar.namewhat, "method") == 0) { | ||
181 | arg--; /* do not count 'self' */ | ||
182 | if (arg == 0) /* error is in the self argument itself? */ | ||
183 | return luaL_error(L, "calling '%s' on bad self (%s)", | ||
184 | ar.name, extramsg); | ||
185 | } | ||
186 | if (ar.name == NULL) | ||
187 | ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; | ||
188 | return luaL_error(L, "bad argument #%d to '%s' (%s)", | ||
189 | arg, ar.name, extramsg); | ||
190 | } | ||
191 | |||
192 | |||
193 | int luaL_typeerror (lua_State *L, int arg, const char *tname) { | ||
194 | const char *msg; | ||
195 | const char *typearg; /* name for the type of the actual argument */ | ||
196 | if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) | ||
197 | typearg = lua_tostring(L, -1); /* use the given type name */ | ||
198 | else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) | ||
199 | typearg = "light userdata"; /* special name for messages */ | ||
200 | else | ||
201 | typearg = luaL_typename(L, arg); /* standard name */ | ||
202 | msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); | ||
203 | return luaL_argerror(L, arg, msg); | ||
204 | } | ||
205 | |||
206 | |||
207 | static void tag_error (lua_State *L, int arg, int tag) { | ||
208 | luaL_typeerror(L, arg, lua_typename(L, tag)); | ||
209 | } | ||
210 | |||
211 | |||
212 | /* | ||
213 | ** The use of 'lua_pushfstring' ensures this function does not | ||
214 | ** need reserved stack space when called. | ||
215 | */ | ||
216 | LUALIB_API void luaL_where (lua_State *L, int level) { | ||
217 | lua_Debug ar; | ||
218 | if (lua_getstack(L, level, &ar)) { /* check function at level */ | ||
219 | lua_getinfo(L, "Sl", &ar); /* get info about it */ | ||
220 | if (ar.currentline > 0) { /* is there info? */ | ||
221 | lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); | ||
222 | return; | ||
223 | } | ||
224 | } | ||
225 | lua_pushfstring(L, ""); /* else, no information available... */ | ||
226 | } | ||
227 | |||
228 | |||
229 | /* | ||
230 | ** Again, the use of 'lua_pushvfstring' ensures this function does | ||
231 | ** not need reserved stack space when called. (At worst, it generates | ||
232 | ** an error with "stack overflow" instead of the given message.) | ||
233 | */ | ||
234 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { | ||
235 | va_list argp; | ||
236 | va_start(argp, fmt); | ||
237 | luaL_where(L, 1); | ||
238 | lua_pushvfstring(L, fmt, argp); | ||
239 | va_end(argp); | ||
240 | lua_concat(L, 2); | ||
241 | return lua_error(L); | ||
242 | } | ||
243 | |||
244 | |||
245 | LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { | ||
246 | int en = errno; /* calls to Lua API may change this value */ | ||
247 | if (stat) { | ||
248 | lua_pushboolean(L, 1); | ||
249 | return 1; | ||
250 | } | ||
251 | else { | ||
252 | luaL_pushfail(L); | ||
253 | if (fname) | ||
254 | lua_pushfstring(L, "%s: %s", fname, strerror(en)); | ||
255 | else | ||
256 | lua_pushstring(L, strerror(en)); | ||
257 | lua_pushinteger(L, en); | ||
258 | return 3; | ||
259 | } | ||
260 | } | ||
261 | |||
262 | |||
263 | #if !defined(l_inspectstat) /* { */ | ||
264 | |||
265 | #if defined(LUA_USE_POSIX) | ||
266 | |||
267 | #include <sys/wait.h> | ||
268 | |||
269 | /* | ||
270 | ** use appropriate macros to interpret 'pclose' return status | ||
271 | */ | ||
272 | #define l_inspectstat(stat,what) \ | ||
273 | if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ | ||
274 | else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } | ||
275 | |||
276 | #else | ||
277 | |||
278 | #define l_inspectstat(stat,what) /* no op */ | ||
279 | |||
280 | #endif | ||
281 | |||
282 | #endif /* } */ | ||
283 | |||
284 | |||
285 | LUALIB_API int luaL_execresult (lua_State *L, int stat) { | ||
286 | const char *what = "exit"; /* type of termination */ | ||
287 | if (stat != 0 && errno != 0) /* error with an 'errno'? */ | ||
288 | return luaL_fileresult(L, 0, NULL); | ||
289 | else { | ||
290 | l_inspectstat(stat, what); /* interpret result */ | ||
291 | if (*what == 'e' && stat == 0) /* successful termination? */ | ||
292 | lua_pushboolean(L, 1); | ||
293 | else | ||
294 | luaL_pushfail(L); | ||
295 | lua_pushstring(L, what); | ||
296 | lua_pushinteger(L, stat); | ||
297 | return 3; /* return true/fail,what,code */ | ||
298 | } | ||
299 | } | ||
300 | |||
301 | /* }====================================================== */ | ||
302 | |||
303 | |||
304 | |||
305 | /* | ||
306 | ** {====================================================== | ||
307 | ** Userdata's metatable manipulation | ||
308 | ** ======================================================= | ||
309 | */ | ||
310 | |||
311 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { | ||
312 | if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ | ||
313 | return 0; /* leave previous value on top, but return 0 */ | ||
314 | lua_pop(L, 1); | ||
315 | lua_createtable(L, 0, 2); /* create metatable */ | ||
316 | lua_pushstring(L, tname); | ||
317 | lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ | ||
318 | lua_pushvalue(L, -1); | ||
319 | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ | ||
320 | return 1; | ||
321 | } | ||
322 | |||
323 | |||
324 | LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { | ||
325 | luaL_getmetatable(L, tname); | ||
326 | lua_setmetatable(L, -2); | ||
327 | } | ||
328 | |||
329 | |||
330 | LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { | ||
331 | void *p = lua_touserdata(L, ud); | ||
332 | if (p != NULL) { /* value is a userdata? */ | ||
333 | if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ | ||
334 | luaL_getmetatable(L, tname); /* get correct metatable */ | ||
335 | if (!lua_rawequal(L, -1, -2)) /* not the same? */ | ||
336 | p = NULL; /* value is a userdata with wrong metatable */ | ||
337 | lua_pop(L, 2); /* remove both metatables */ | ||
338 | return p; | ||
339 | } | ||
340 | } | ||
341 | return NULL; /* value is not a userdata with a metatable */ | ||
342 | } | ||
343 | |||
344 | |||
345 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { | ||
346 | void *p = luaL_testudata(L, ud, tname); | ||
347 | luaL_argexpected(L, p != NULL, ud, tname); | ||
348 | return p; | ||
349 | } | ||
350 | |||
351 | /* }====================================================== */ | ||
352 | |||
353 | |||
354 | /* | ||
355 | ** {====================================================== | ||
356 | ** Argument check functions | ||
357 | ** ======================================================= | ||
358 | */ | ||
359 | |||
360 | LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, | ||
361 | const char *const lst[]) { | ||
362 | const char *name = (def) ? luaL_optstring(L, arg, def) : | ||
363 | luaL_checkstring(L, arg); | ||
364 | int i; | ||
365 | for (i=0; lst[i]; i++) | ||
366 | if (strcmp(lst[i], name) == 0) | ||
367 | return i; | ||
368 | return luaL_argerror(L, arg, | ||
369 | lua_pushfstring(L, "invalid option '%s'", name)); | ||
370 | } | ||
371 | |||
372 | |||
373 | /* | ||
374 | ** Ensures the stack has at least 'space' extra slots, raising an error | ||
375 | ** if it cannot fulfill the request. (The error handling needs a few | ||
376 | ** extra slots to format the error message. In case of an error without | ||
377 | ** this extra space, Lua will generate the same 'stack overflow' error, | ||
378 | ** but without 'msg'.) | ||
379 | */ | ||
380 | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { | ||
381 | if (!lua_checkstack(L, space)) { | ||
382 | if (msg) | ||
383 | luaL_error(L, "stack overflow (%s)", msg); | ||
384 | else | ||
385 | luaL_error(L, "stack overflow"); | ||
386 | } | ||
387 | } | ||
388 | |||
389 | |||
390 | LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { | ||
391 | if (lua_type(L, arg) != t) | ||
392 | tag_error(L, arg, t); | ||
393 | } | ||
394 | |||
395 | |||
396 | LUALIB_API void luaL_checkany (lua_State *L, int arg) { | ||
397 | if (lua_type(L, arg) == LUA_TNONE) | ||
398 | luaL_argerror(L, arg, "value expected"); | ||
399 | } | ||
400 | |||
401 | |||
402 | LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { | ||
403 | const char *s = lua_tolstring(L, arg, len); | ||
404 | if (!s) tag_error(L, arg, LUA_TSTRING); | ||
405 | return s; | ||
406 | } | ||
407 | |||
408 | |||
409 | LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, | ||
410 | const char *def, size_t *len) { | ||
411 | if (lua_isnoneornil(L, arg)) { | ||
412 | if (len) | ||
413 | *len = (def ? strlen(def) : 0); | ||
414 | return def; | ||
415 | } | ||
416 | else return luaL_checklstring(L, arg, len); | ||
417 | } | ||
418 | |||
419 | |||
420 | LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { | ||
421 | int isnum; | ||
422 | lua_Number d = lua_tonumberx(L, arg, &isnum); | ||
423 | if (!isnum) | ||
424 | tag_error(L, arg, LUA_TNUMBER); | ||
425 | return d; | ||
426 | } | ||
427 | |||
428 | |||
429 | LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { | ||
430 | return luaL_opt(L, luaL_checknumber, arg, def); | ||
431 | } | ||
432 | |||
433 | |||
434 | static void interror (lua_State *L, int arg) { | ||
435 | if (lua_isnumber(L, arg)) | ||
436 | luaL_argerror(L, arg, "number has no integer representation"); | ||
437 | else | ||
438 | tag_error(L, arg, LUA_TNUMBER); | ||
439 | } | ||
440 | |||
441 | |||
442 | LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { | ||
443 | int isnum; | ||
444 | lua_Integer d = lua_tointegerx(L, arg, &isnum); | ||
445 | if (!isnum) { | ||
446 | interror(L, arg); | ||
447 | } | ||
448 | return d; | ||
449 | } | ||
450 | |||
451 | |||
452 | LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, | ||
453 | lua_Integer def) { | ||
454 | return luaL_opt(L, luaL_checkinteger, arg, def); | ||
455 | } | ||
456 | |||
457 | /* }====================================================== */ | ||
458 | |||
459 | |||
460 | /* | ||
461 | ** {====================================================== | ||
462 | ** Generic Buffer manipulation | ||
463 | ** ======================================================= | ||
464 | */ | ||
465 | |||
466 | /* userdata to box arbitrary data */ | ||
467 | typedef struct UBox { | ||
468 | void *box; | ||
469 | size_t bsize; | ||
470 | } UBox; | ||
471 | |||
472 | |||
473 | static void *resizebox (lua_State *L, int idx, size_t newsize) { | ||
474 | void *ud; | ||
475 | lua_Alloc allocf = lua_getallocf(L, &ud); | ||
476 | UBox *box = (UBox *)lua_touserdata(L, idx); | ||
477 | void *temp = allocf(ud, box->box, box->bsize, newsize); | ||
478 | if (temp == NULL && newsize > 0) /* allocation error? */ | ||
479 | luaL_error(L, "not enough memory"); | ||
480 | box->box = temp; | ||
481 | box->bsize = newsize; | ||
482 | return temp; | ||
483 | } | ||
484 | |||
485 | |||
486 | static int boxgc (lua_State *L) { | ||
487 | resizebox(L, 1, 0); | ||
488 | return 0; | ||
489 | } | ||
490 | |||
491 | |||
492 | static const luaL_Reg boxmt[] = { /* box metamethods */ | ||
493 | {"__gc", boxgc}, | ||
494 | {"__close", boxgc}, | ||
495 | {NULL, NULL} | ||
496 | }; | ||
497 | |||
498 | |||
499 | static void newbox (lua_State *L) { | ||
500 | UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); | ||
501 | box->box = NULL; | ||
502 | box->bsize = 0; | ||
503 | if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ | ||
504 | luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ | ||
505 | lua_setmetatable(L, -2); | ||
506 | } | ||
507 | |||
508 | |||
509 | /* | ||
510 | ** check whether buffer is using a userdata on the stack as a temporary | ||
511 | ** buffer | ||
512 | */ | ||
513 | #define buffonstack(B) ((B)->b != (B)->init.b) | ||
514 | |||
515 | |||
516 | /* | ||
517 | ** Compute new size for buffer 'B', enough to accommodate extra 'sz' | ||
518 | ** bytes. | ||
519 | */ | ||
520 | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { | ||
521 | size_t newsize = B->size * 2; /* double buffer size */ | ||
522 | if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ | ||
523 | return luaL_error(B->L, "buffer too large"); | ||
524 | if (newsize < B->n + sz) /* double is not big enough? */ | ||
525 | newsize = B->n + sz; | ||
526 | return newsize; | ||
527 | } | ||
528 | |||
529 | |||
530 | /* | ||
531 | ** Returns a pointer to a free area with at least 'sz' bytes in buffer | ||
532 | ** 'B'. 'boxidx' is the relative position in the stack where the | ||
533 | ** buffer's box is or should be. | ||
534 | */ | ||
535 | static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { | ||
536 | if (B->size - B->n >= sz) /* enough space? */ | ||
537 | return B->b + B->n; | ||
538 | else { | ||
539 | lua_State *L = B->L; | ||
540 | char *newbuff; | ||
541 | size_t newsize = newbuffsize(B, sz); | ||
542 | /* create larger buffer */ | ||
543 | if (buffonstack(B)) /* buffer already has a box? */ | ||
544 | newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ | ||
545 | else { /* no box yet */ | ||
546 | lua_pushnil(L); /* reserve slot for final result */ | ||
547 | newbox(L); /* create a new box */ | ||
548 | /* move box (and slot) to its intended position */ | ||
549 | lua_rotate(L, boxidx - 1, 2); | ||
550 | lua_toclose(L, boxidx); | ||
551 | newbuff = (char *)resizebox(L, boxidx, newsize); | ||
552 | memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ | ||
553 | } | ||
554 | B->b = newbuff; | ||
555 | B->size = newsize; | ||
556 | return newbuff + B->n; | ||
557 | } | ||
558 | } | ||
559 | |||
560 | /* | ||
561 | ** returns a pointer to a free area with at least 'sz' bytes | ||
562 | */ | ||
563 | LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { | ||
564 | return prepbuffsize(B, sz, -1); | ||
565 | } | ||
566 | |||
567 | |||
568 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { | ||
569 | if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ | ||
570 | char *b = prepbuffsize(B, l, -1); | ||
571 | memcpy(b, s, l * sizeof(char)); | ||
572 | luaL_addsize(B, l); | ||
573 | } | ||
574 | } | ||
575 | |||
576 | |||
577 | LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { | ||
578 | luaL_addlstring(B, s, strlen(s)); | ||
579 | } | ||
580 | |||
581 | |||
582 | LUALIB_API void luaL_pushresult (luaL_Buffer *B) { | ||
583 | lua_State *L = B->L; | ||
584 | lua_pushlstring(L, B->b, B->n); | ||
585 | if (buffonstack(B)) { | ||
586 | lua_copy(L, -1, -3); /* move string to reserved slot */ | ||
587 | lua_pop(L, 2); /* pop string and box (closing the box) */ | ||
588 | } | ||
589 | } | ||
590 | |||
591 | |||
592 | LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { | ||
593 | luaL_addsize(B, sz); | ||
594 | luaL_pushresult(B); | ||
595 | } | ||
596 | |||
597 | |||
598 | /* | ||
599 | ** 'luaL_addvalue' is the only function in the Buffer system where the | ||
600 | ** box (if existent) is not on the top of the stack. So, instead of | ||
601 | ** calling 'luaL_addlstring', it replicates the code using -2 as the | ||
602 | ** last argument to 'prepbuffsize', signaling that the box is (or will | ||
603 | ** be) bellow the string being added to the buffer. (Box creation can | ||
604 | ** trigger an emergency GC, so we should not remove the string from the | ||
605 | ** stack before we have the space guaranteed.) | ||
606 | */ | ||
607 | LUALIB_API void luaL_addvalue (luaL_Buffer *B) { | ||
608 | lua_State *L = B->L; | ||
609 | size_t len; | ||
610 | const char *s = lua_tolstring(L, -1, &len); | ||
611 | char *b = prepbuffsize(B, len, -2); | ||
612 | memcpy(b, s, len * sizeof(char)); | ||
613 | luaL_addsize(B, len); | ||
614 | lua_pop(L, 1); /* pop string */ | ||
615 | } | ||
616 | |||
617 | |||
618 | LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { | ||
619 | B->L = L; | ||
620 | B->b = B->init.b; | ||
621 | B->n = 0; | ||
622 | B->size = LUAL_BUFFERSIZE; | ||
623 | } | ||
624 | |||
625 | |||
626 | LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { | ||
627 | luaL_buffinit(L, B); | ||
628 | return prepbuffsize(B, sz, -1); | ||
629 | } | ||
630 | |||
631 | /* }====================================================== */ | ||
632 | |||
633 | |||
634 | /* | ||
635 | ** {====================================================== | ||
636 | ** Reference system | ||
637 | ** ======================================================= | ||
638 | */ | ||
639 | |||
640 | /* index of free-list header */ | ||
641 | #define freelist 0 | ||
642 | |||
643 | |||
644 | LUALIB_API int luaL_ref (lua_State *L, int t) { | ||
645 | int ref; | ||
646 | if (lua_isnil(L, -1)) { | ||
647 | lua_pop(L, 1); /* remove from stack */ | ||
648 | return LUA_REFNIL; /* 'nil' has a unique fixed reference */ | ||
649 | } | ||
650 | t = lua_absindex(L, t); | ||
651 | lua_rawgeti(L, t, freelist); /* get first free element */ | ||
652 | ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ | ||
653 | lua_pop(L, 1); /* remove it from stack */ | ||
654 | if (ref != 0) { /* any free element? */ | ||
655 | lua_rawgeti(L, t, ref); /* remove it from list */ | ||
656 | lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ | ||
657 | } | ||
658 | else /* no free elements */ | ||
659 | ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ | ||
660 | lua_rawseti(L, t, ref); | ||
661 | return ref; | ||
662 | } | ||
663 | |||
664 | |||
665 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { | ||
666 | if (ref >= 0) { | ||
667 | t = lua_absindex(L, t); | ||
668 | lua_rawgeti(L, t, freelist); | ||
669 | lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ | ||
670 | lua_pushinteger(L, ref); | ||
671 | lua_rawseti(L, t, freelist); /* t[freelist] = ref */ | ||
672 | } | ||
673 | } | ||
674 | |||
675 | /* }====================================================== */ | ||
676 | |||
677 | |||
678 | /* | ||
679 | ** {====================================================== | ||
680 | ** Load functions | ||
681 | ** ======================================================= | ||
682 | */ | ||
683 | |||
684 | typedef struct LoadF { | ||
685 | int n; /* number of pre-read characters */ | ||
686 | FILE *f; /* file being read */ | ||
687 | char buff[BUFSIZ]; /* area for reading file */ | ||
688 | } LoadF; | ||
689 | |||
690 | |||
691 | static const char *getF (lua_State *L, void *ud, size_t *size) { | ||
692 | LoadF *lf = (LoadF *)ud; | ||
693 | (void)L; /* not used */ | ||
694 | if (lf->n > 0) { /* are there pre-read characters to be read? */ | ||
695 | *size = lf->n; /* return them (chars already in buffer) */ | ||
696 | lf->n = 0; /* no more pre-read characters */ | ||
697 | } | ||
698 | else { /* read a block from file */ | ||
699 | /* 'fread' can return > 0 *and* set the EOF flag. If next call to | ||
700 | 'getF' called 'fread', it might still wait for user input. | ||
701 | The next check avoids this problem. */ | ||
702 | if (feof(lf->f)) return NULL; | ||
703 | *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ | ||
704 | } | ||
705 | return lf->buff; | ||
706 | } | ||
707 | |||
708 | |||
709 | static int errfile (lua_State *L, const char *what, int fnameindex) { | ||
710 | const char *serr = strerror(errno); | ||
711 | const char *filename = lua_tostring(L, fnameindex) + 1; | ||
712 | lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); | ||
713 | lua_remove(L, fnameindex); | ||
714 | return LUA_ERRFILE; | ||
715 | } | ||
716 | |||
717 | |||
718 | static int skipBOM (LoadF *lf) { | ||
719 | const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ | ||
720 | int c; | ||
721 | lf->n = 0; | ||
722 | do { | ||
723 | c = getc(lf->f); | ||
724 | if (c == EOF || c != *(const unsigned char *)p++) return c; | ||
725 | lf->buff[lf->n++] = c; /* to be read by the parser */ | ||
726 | } while (*p != '\0'); | ||
727 | lf->n = 0; /* prefix matched; discard it */ | ||
728 | return getc(lf->f); /* return next character */ | ||
729 | } | ||
730 | |||
731 | |||
732 | /* | ||
733 | ** reads the first character of file 'f' and skips an optional BOM mark | ||
734 | ** in its beginning plus its first line if it starts with '#'. Returns | ||
735 | ** true if it skipped the first line. In any case, '*cp' has the | ||
736 | ** first "valid" character of the file (after the optional BOM and | ||
737 | ** a first-line comment). | ||
738 | */ | ||
739 | static int skipcomment (LoadF *lf, int *cp) { | ||
740 | int c = *cp = skipBOM(lf); | ||
741 | if (c == '#') { /* first line is a comment (Unix exec. file)? */ | ||
742 | do { /* skip first line */ | ||
743 | c = getc(lf->f); | ||
744 | } while (c != EOF && c != '\n'); | ||
745 | *cp = getc(lf->f); /* skip end-of-line, if present */ | ||
746 | return 1; /* there was a comment */ | ||
747 | } | ||
748 | else return 0; /* no comment */ | ||
749 | } | ||
750 | |||
751 | |||
752 | LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, | ||
753 | const char *mode) { | ||
754 | LoadF lf; | ||
755 | int status, readstatus; | ||
756 | int c; | ||
757 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ | ||
758 | if (filename == NULL) { | ||
759 | lua_pushliteral(L, "=stdin"); | ||
760 | lf.f = stdin; | ||
761 | } | ||
762 | else { | ||
763 | lua_pushfstring(L, "@%s", filename); | ||
764 | lf.f = fopen(filename, "r"); | ||
765 | if (lf.f == NULL) return errfile(L, "open", fnameindex); | ||
766 | } | ||
767 | if (skipcomment(&lf, &c)) /* read initial portion */ | ||
768 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ | ||
769 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ | ||
770 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ | ||
771 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); | ||
772 | skipcomment(&lf, &c); /* re-read initial portion */ | ||
773 | } | ||
774 | if (c != EOF) | ||
775 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ | ||
776 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); | ||
777 | readstatus = ferror(lf.f); | ||
778 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ | ||
779 | if (readstatus) { | ||
780 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ | ||
781 | return errfile(L, "read", fnameindex); | ||
782 | } | ||
783 | lua_remove(L, fnameindex); | ||
784 | return status; | ||
785 | } | ||
786 | |||
787 | |||
788 | typedef struct LoadS { | ||
789 | const char *s; | ||
790 | size_t size; | ||
791 | } LoadS; | ||
792 | |||
793 | |||
794 | static const char *getS (lua_State *L, void *ud, size_t *size) { | ||
795 | LoadS *ls = (LoadS *)ud; | ||
796 | (void)L; /* not used */ | ||
797 | if (ls->size == 0) return NULL; | ||
798 | *size = ls->size; | ||
799 | ls->size = 0; | ||
800 | return ls->s; | ||
801 | } | ||
802 | |||
803 | |||
804 | LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, | ||
805 | const char *name, const char *mode) { | ||
806 | LoadS ls; | ||
807 | ls.s = buff; | ||
808 | ls.size = size; | ||
809 | return lua_load(L, getS, &ls, name, mode); | ||
810 | } | ||
811 | |||
812 | |||
813 | LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { | ||
814 | return luaL_loadbuffer(L, s, strlen(s), s); | ||
815 | } | ||
816 | |||
817 | /* }====================================================== */ | ||
818 | |||
819 | |||
820 | |||
821 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { | ||
822 | if (!lua_getmetatable(L, obj)) /* no metatable? */ | ||
823 | return LUA_TNIL; | ||
824 | else { | ||
825 | int tt; | ||
826 | lua_pushstring(L, event); | ||
827 | tt = lua_rawget(L, -2); | ||
828 | if (tt == LUA_TNIL) /* is metafield nil? */ | ||
829 | lua_pop(L, 2); /* remove metatable and metafield */ | ||
830 | else | ||
831 | lua_remove(L, -2); /* remove only metatable */ | ||
832 | return tt; /* return metafield type */ | ||
833 | } | ||
834 | } | ||
835 | |||
836 | |||
837 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { | ||
838 | obj = lua_absindex(L, obj); | ||
839 | if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ | ||
840 | return 0; | ||
841 | lua_pushvalue(L, obj); | ||
842 | lua_call(L, 1, 1); | ||
843 | return 1; | ||
844 | } | ||
845 | |||
846 | |||
847 | LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { | ||
848 | lua_Integer l; | ||
849 | int isnum; | ||
850 | lua_len(L, idx); | ||
851 | l = lua_tointegerx(L, -1, &isnum); | ||
852 | if (!isnum) | ||
853 | luaL_error(L, "object length is not an integer"); | ||
854 | lua_pop(L, 1); /* remove object */ | ||
855 | return l; | ||
856 | } | ||
857 | |||
858 | |||
859 | LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { | ||
860 | if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ | ||
861 | if (!lua_isstring(L, -1)) | ||
862 | luaL_error(L, "'__tostring' must return a string"); | ||
863 | } | ||
864 | else { | ||
865 | switch (lua_type(L, idx)) { | ||
866 | case LUA_TNUMBER: { | ||
867 | if (lua_isinteger(L, idx)) | ||
868 | lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); | ||
869 | else | ||
870 | lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); | ||
871 | break; | ||
872 | } | ||
873 | case LUA_TSTRING: | ||
874 | lua_pushvalue(L, idx); | ||
875 | break; | ||
876 | case LUA_TBOOLEAN: | ||
877 | lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); | ||
878 | break; | ||
879 | case LUA_TNIL: | ||
880 | lua_pushliteral(L, "nil"); | ||
881 | break; | ||
882 | default: { | ||
883 | int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ | ||
884 | const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : | ||
885 | luaL_typename(L, idx); | ||
886 | lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); | ||
887 | if (tt != LUA_TNIL) | ||
888 | lua_remove(L, -2); /* remove '__name' */ | ||
889 | break; | ||
890 | } | ||
891 | } | ||
892 | } | ||
893 | return lua_tolstring(L, -1, len); | ||
894 | } | ||
895 | |||
896 | |||
897 | /* | ||
898 | ** set functions from list 'l' into table at top - 'nup'; each | ||
899 | ** function gets the 'nup' elements at the top as upvalues. | ||
900 | ** Returns with only the table at the stack. | ||
901 | */ | ||
902 | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { | ||
903 | luaL_checkstack(L, nup, "too many upvalues"); | ||
904 | for (; l->name != NULL; l++) { /* fill the table with given functions */ | ||
905 | if (l->func == NULL) /* place holder? */ | ||
906 | lua_pushboolean(L, 0); | ||
907 | else { | ||
908 | int i; | ||
909 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | ||
910 | lua_pushvalue(L, -nup); | ||
911 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ | ||
912 | } | ||
913 | lua_setfield(L, -(nup + 2), l->name); | ||
914 | } | ||
915 | lua_pop(L, nup); /* remove upvalues */ | ||
916 | } | ||
917 | |||
918 | |||
919 | /* | ||
920 | ** ensure that stack[idx][fname] has a table and push that table | ||
921 | ** into the stack | ||
922 | */ | ||
923 | LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { | ||
924 | if (lua_getfield(L, idx, fname) == LUA_TTABLE) | ||
925 | return 1; /* table already there */ | ||
926 | else { | ||
927 | lua_pop(L, 1); /* remove previous result */ | ||
928 | idx = lua_absindex(L, idx); | ||
929 | lua_newtable(L); | ||
930 | lua_pushvalue(L, -1); /* copy to be left at top */ | ||
931 | lua_setfield(L, idx, fname); /* assign new table to field */ | ||
932 | return 0; /* false, because did not find table there */ | ||
933 | } | ||
934 | } | ||
935 | |||
936 | |||
937 | /* | ||
938 | ** Stripped-down 'require': After checking "loaded" table, calls 'openf' | ||
939 | ** to open a module, registers the result in 'package.loaded' table and, | ||
940 | ** if 'glb' is true, also registers the result in the global table. | ||
941 | ** Leaves resulting module on the top. | ||
942 | */ | ||
943 | LUALIB_API void luaL_requiref (lua_State *L, const char *modname, | ||
944 | lua_CFunction openf, int glb) { | ||
945 | luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); | ||
946 | lua_getfield(L, -1, modname); /* LOADED[modname] */ | ||
947 | if (!lua_toboolean(L, -1)) { /* package not already loaded? */ | ||
948 | lua_pop(L, 1); /* remove field */ | ||
949 | lua_pushcfunction(L, openf); | ||
950 | lua_pushstring(L, modname); /* argument to open function */ | ||
951 | lua_call(L, 1, 1); /* call 'openf' to open module */ | ||
952 | lua_pushvalue(L, -1); /* make copy of module (call result) */ | ||
953 | lua_setfield(L, -3, modname); /* LOADED[modname] = module */ | ||
954 | } | ||
955 | lua_remove(L, -2); /* remove LOADED table */ | ||
956 | if (glb) { | ||
957 | lua_pushvalue(L, -1); /* copy of module */ | ||
958 | lua_setglobal(L, modname); /* _G[modname] = module */ | ||
959 | } | ||
960 | } | ||
961 | |||
962 | |||
963 | LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, | ||
964 | const char *p, const char *r) { | ||
965 | const char *wild; | ||
966 | size_t l = strlen(p); | ||
967 | while ((wild = strstr(s, p)) != NULL) { | ||
968 | luaL_addlstring(b, s, wild - s); /* push prefix */ | ||
969 | luaL_addstring(b, r); /* push replacement in place of pattern */ | ||
970 | s = wild + l; /* continue after 'p' */ | ||
971 | } | ||
972 | luaL_addstring(b, s); /* push last suffix */ | ||
973 | } | ||
974 | |||
975 | |||
976 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, | ||
977 | const char *p, const char *r) { | ||
978 | luaL_Buffer b; | ||
979 | luaL_buffinit(L, &b); | ||
980 | luaL_addgsub(&b, s, p, r); | ||
981 | luaL_pushresult(&b); | ||
982 | return lua_tostring(L, -1); | ||
983 | } | ||
984 | |||
985 | |||
986 | static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { | ||
987 | (void)ud; (void)osize; /* not used */ | ||
988 | if (nsize == 0) { | ||
989 | free(ptr); | ||
990 | return NULL; | ||
991 | } | ||
992 | else | ||
993 | return realloc(ptr, nsize); | ||
994 | } | ||
995 | |||
996 | |||
997 | static int panic (lua_State *L) { | ||
998 | const char *msg = lua_tostring(L, -1); | ||
999 | if (msg == NULL) msg = "error object is not a string"; | ||
1000 | lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", | ||
1001 | msg); | ||
1002 | return 0; /* return to Lua to abort */ | ||
1003 | } | ||
1004 | |||
1005 | |||
1006 | /* | ||
1007 | ** Emit a warning. '*warnstate' means: | ||
1008 | ** 0 - warning system is off; | ||
1009 | ** 1 - ready to start a new message; | ||
1010 | ** 2 - previous message is to be continued. | ||
1011 | */ | ||
1012 | static void warnf (void *ud, const char *message, int tocont) { | ||
1013 | int *warnstate = (int *)ud; | ||
1014 | if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */ | ||
1015 | if (strcmp(message, "@off") == 0) | ||
1016 | *warnstate = 0; | ||
1017 | else if (strcmp(message, "@on") == 0) | ||
1018 | *warnstate = 1; | ||
1019 | return; | ||
1020 | } | ||
1021 | else if (*warnstate == 0) /* warnings off? */ | ||
1022 | return; | ||
1023 | if (*warnstate == 1) /* previous message was the last? */ | ||
1024 | lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ | ||
1025 | lua_writestringerror("%s", message); /* write message */ | ||
1026 | if (tocont) /* not the last part? */ | ||
1027 | *warnstate = 2; /* to be continued */ | ||
1028 | else { /* last part */ | ||
1029 | lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ | ||
1030 | *warnstate = 1; /* ready to start a new message */ | ||
1031 | } | ||
1032 | } | ||
1033 | |||
1034 | |||
1035 | LUALIB_API lua_State *luaL_newstate (void) { | ||
1036 | lua_State *L = lua_newstate(l_alloc, NULL); | ||
1037 | if (L) { | ||
1038 | int *warnstate; /* space for warning state */ | ||
1039 | lua_atpanic(L, &panic); | ||
1040 | warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0); | ||
1041 | luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ | ||
1042 | *warnstate = 0; /* default is warnings off */ | ||
1043 | lua_setwarnf(L, warnf, warnstate); | ||
1044 | } | ||
1045 | return L; | ||
1046 | } | ||
1047 | |||
1048 | |||
1049 | LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { | ||
1050 | lua_Number v = lua_version(L); | ||
1051 | if (sz != LUAL_NUMSIZES) /* check numeric types */ | ||
1052 | luaL_error(L, "core and library have incompatible numeric types"); | ||
1053 | else if (v != ver) | ||
1054 | luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", | ||
1055 | (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v); | ||
1056 | } | ||
1057 | |||