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