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