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