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