xref: /freebsd/contrib/lua/src/lauxlib.c (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: lauxlib.c $
38e3e3a7aSWarner Losh ** Auxiliary functions for building Lua libraries
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #define lauxlib_c
88e3e3a7aSWarner Losh #define LUA_LIB
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh #include "lprefix.h"
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh 
138e3e3a7aSWarner Losh #include <errno.h>
148e3e3a7aSWarner Losh #include <stdarg.h>
158e3e3a7aSWarner Losh #include <stdio.h>
168e3e3a7aSWarner Losh #include <stdlib.h>
178e3e3a7aSWarner Losh #include <string.h>
188e3e3a7aSWarner Losh 
198e3e3a7aSWarner Losh 
208e3e3a7aSWarner Losh /*
218e3e3a7aSWarner Losh ** This file uses only the official API of Lua.
228e3e3a7aSWarner Losh ** Any function declared here could be written as an application function.
238e3e3a7aSWarner Losh */
248e3e3a7aSWarner Losh 
258e3e3a7aSWarner Losh #include "lua.h"
268e3e3a7aSWarner Losh 
278e3e3a7aSWarner Losh #include "lauxlib.h"
288e3e3a7aSWarner Losh 
298e3e3a7aSWarner Losh 
300495ed39SKyle Evans #if !defined(MAX_SIZET)
310495ed39SKyle Evans /* maximum value for size_t */
320495ed39SKyle Evans #define MAX_SIZET	((size_t)(~(size_t)0))
330495ed39SKyle Evans #endif
340495ed39SKyle Evans 
350495ed39SKyle Evans 
368e3e3a7aSWarner Losh /*
378e3e3a7aSWarner Losh ** {======================================================
388e3e3a7aSWarner Losh ** Traceback
398e3e3a7aSWarner Losh ** =======================================================
408e3e3a7aSWarner Losh */
418e3e3a7aSWarner Losh 
428e3e3a7aSWarner Losh 
438e3e3a7aSWarner Losh #define LEVELS1	10	/* size of the first part of the stack */
448e3e3a7aSWarner Losh #define LEVELS2	11	/* size of the second part of the stack */
458e3e3a7aSWarner Losh 
468e3e3a7aSWarner Losh 
478e3e3a7aSWarner Losh 
488e3e3a7aSWarner Losh /*
490495ed39SKyle Evans ** Search for 'objidx' in table at index -1. ('objidx' must be an
500495ed39SKyle Evans ** absolute index.) Return 1 + string at top if it found a good name.
518e3e3a7aSWarner Losh */
findfield(lua_State * L,int objidx,int level)528e3e3a7aSWarner Losh static int findfield (lua_State *L, int objidx, int level) {
538e3e3a7aSWarner Losh   if (level == 0 || !lua_istable(L, -1))
548e3e3a7aSWarner Losh     return 0;  /* not found */
558e3e3a7aSWarner Losh   lua_pushnil(L);  /* start 'next' loop */
568e3e3a7aSWarner Losh   while (lua_next(L, -2)) {  /* for each pair in table */
578e3e3a7aSWarner Losh     if (lua_type(L, -2) == LUA_TSTRING) {  /* ignore non-string keys */
588e3e3a7aSWarner Losh       if (lua_rawequal(L, objidx, -1)) {  /* found object? */
598e3e3a7aSWarner Losh         lua_pop(L, 1);  /* remove value (but keep name) */
608e3e3a7aSWarner Losh         return 1;
618e3e3a7aSWarner Losh       }
628e3e3a7aSWarner Losh       else if (findfield(L, objidx, level - 1)) {  /* try recursively */
630495ed39SKyle Evans         /* stack: lib_name, lib_table, field_name (top) */
640495ed39SKyle Evans         lua_pushliteral(L, ".");  /* place '.' between the two names */
650495ed39SKyle Evans         lua_replace(L, -3);  /* (in the slot occupied by table) */
660495ed39SKyle Evans         lua_concat(L, 3);  /* lib_name.field_name */
678e3e3a7aSWarner Losh         return 1;
688e3e3a7aSWarner Losh       }
698e3e3a7aSWarner Losh     }
708e3e3a7aSWarner Losh     lua_pop(L, 1);  /* remove value */
718e3e3a7aSWarner Losh   }
728e3e3a7aSWarner Losh   return 0;  /* not found */
738e3e3a7aSWarner Losh }
748e3e3a7aSWarner Losh 
758e3e3a7aSWarner Losh 
768e3e3a7aSWarner Losh /*
778e3e3a7aSWarner Losh ** Search for a name for a function in all loaded modules
788e3e3a7aSWarner Losh */
pushglobalfuncname(lua_State * L,lua_Debug * ar)798e3e3a7aSWarner Losh static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
808e3e3a7aSWarner Losh   int top = lua_gettop(L);
818e3e3a7aSWarner Losh   lua_getinfo(L, "f", ar);  /* push function */
828e3e3a7aSWarner Losh   lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
838e3e3a7aSWarner Losh   if (findfield(L, top + 1, 2)) {
848e3e3a7aSWarner Losh     const char *name = lua_tostring(L, -1);
850495ed39SKyle Evans     if (strncmp(name, LUA_GNAME ".", 3) == 0) {  /* name start with '_G.'? */
868e3e3a7aSWarner Losh       lua_pushstring(L, name + 3);  /* push name without prefix */
878e3e3a7aSWarner Losh       lua_remove(L, -2);  /* remove original name */
888e3e3a7aSWarner Losh     }
890495ed39SKyle Evans     lua_copy(L, -1, top + 1);  /* copy name to proper place */
900495ed39SKyle Evans     lua_settop(L, top + 1);  /* remove table "loaded" and name copy */
918e3e3a7aSWarner Losh     return 1;
928e3e3a7aSWarner Losh   }
938e3e3a7aSWarner Losh   else {
948e3e3a7aSWarner Losh     lua_settop(L, top);  /* remove function and global table */
958e3e3a7aSWarner Losh     return 0;
968e3e3a7aSWarner Losh   }
978e3e3a7aSWarner Losh }
988e3e3a7aSWarner Losh 
998e3e3a7aSWarner Losh 
pushfuncname(lua_State * L,lua_Debug * ar)1008e3e3a7aSWarner Losh static void pushfuncname (lua_State *L, lua_Debug *ar) {
1018e3e3a7aSWarner Losh   if (pushglobalfuncname(L, ar)) {  /* try first a global name */
1028e3e3a7aSWarner Losh     lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
1038e3e3a7aSWarner Losh     lua_remove(L, -2);  /* remove name */
1048e3e3a7aSWarner Losh   }
1058e3e3a7aSWarner Losh   else if (*ar->namewhat != '\0')  /* is there a name from code? */
1068e3e3a7aSWarner Losh     lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name);  /* use it */
1078e3e3a7aSWarner Losh   else if (*ar->what == 'm')  /* main? */
1088e3e3a7aSWarner Losh       lua_pushliteral(L, "main chunk");
1098e3e3a7aSWarner Losh   else if (*ar->what != 'C')  /* for Lua functions, use <file:line> */
1108e3e3a7aSWarner Losh     lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
1118e3e3a7aSWarner Losh   else  /* nothing left... */
1128e3e3a7aSWarner Losh     lua_pushliteral(L, "?");
1138e3e3a7aSWarner Losh }
1148e3e3a7aSWarner Losh 
1158e3e3a7aSWarner Losh 
lastlevel(lua_State * L)1168e3e3a7aSWarner Losh static int lastlevel (lua_State *L) {
1178e3e3a7aSWarner Losh   lua_Debug ar;
1188e3e3a7aSWarner Losh   int li = 1, le = 1;
1198e3e3a7aSWarner Losh   /* find an upper bound */
1208e3e3a7aSWarner Losh   while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
1218e3e3a7aSWarner Losh   /* do a binary search */
1228e3e3a7aSWarner Losh   while (li < le) {
1238e3e3a7aSWarner Losh     int m = (li + le)/2;
1248e3e3a7aSWarner Losh     if (lua_getstack(L, m, &ar)) li = m + 1;
1258e3e3a7aSWarner Losh     else le = m;
1268e3e3a7aSWarner Losh   }
1278e3e3a7aSWarner Losh   return le - 1;
1288e3e3a7aSWarner Losh }
1298e3e3a7aSWarner Losh 
1308e3e3a7aSWarner Losh 
luaL_traceback(lua_State * L,lua_State * L1,const char * msg,int level)1318e3e3a7aSWarner Losh LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
1328e3e3a7aSWarner Losh                                 const char *msg, int level) {
1330495ed39SKyle Evans   luaL_Buffer b;
1348e3e3a7aSWarner Losh   lua_Debug ar;
1358e3e3a7aSWarner Losh   int last = lastlevel(L1);
1360495ed39SKyle Evans   int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
1370495ed39SKyle Evans   luaL_buffinit(L, &b);
1380495ed39SKyle Evans   if (msg) {
1390495ed39SKyle Evans     luaL_addstring(&b, msg);
1400495ed39SKyle Evans     luaL_addchar(&b, '\n');
1410495ed39SKyle Evans   }
1420495ed39SKyle Evans   luaL_addstring(&b, "stack traceback:");
1438e3e3a7aSWarner Losh   while (lua_getstack(L1, level++, &ar)) {
1440495ed39SKyle Evans     if (limit2show-- == 0) {  /* too many levels? */
1450495ed39SKyle Evans       int n = last - level - LEVELS2 + 1;  /* number of levels to skip */
1460495ed39SKyle Evans       lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n);
1470495ed39SKyle Evans       luaL_addvalue(&b);  /* add warning about skip */
1480495ed39SKyle Evans       level += n;  /* and skip to last levels */
1498e3e3a7aSWarner Losh     }
1508e3e3a7aSWarner Losh     else {
1518e3e3a7aSWarner Losh       lua_getinfo(L1, "Slnt", &ar);
1520495ed39SKyle Evans       if (ar.currentline <= 0)
1530495ed39SKyle Evans         lua_pushfstring(L, "\n\t%s: in ", ar.short_src);
1540495ed39SKyle Evans       else
1550495ed39SKyle Evans         lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline);
1560495ed39SKyle Evans       luaL_addvalue(&b);
1578e3e3a7aSWarner Losh       pushfuncname(L, &ar);
1580495ed39SKyle Evans       luaL_addvalue(&b);
1598e3e3a7aSWarner Losh       if (ar.istailcall)
1600495ed39SKyle Evans         luaL_addstring(&b, "\n\t(...tail calls...)");
1618e3e3a7aSWarner Losh     }
1628e3e3a7aSWarner Losh   }
1630495ed39SKyle Evans   luaL_pushresult(&b);
1648e3e3a7aSWarner Losh }
1658e3e3a7aSWarner Losh 
1668e3e3a7aSWarner Losh /* }====================================================== */
1678e3e3a7aSWarner Losh 
1688e3e3a7aSWarner Losh 
1698e3e3a7aSWarner Losh /*
1708e3e3a7aSWarner Losh ** {======================================================
1718e3e3a7aSWarner Losh ** Error-report functions
1728e3e3a7aSWarner Losh ** =======================================================
1738e3e3a7aSWarner Losh */
1748e3e3a7aSWarner Losh 
luaL_argerror(lua_State * L,int arg,const char * extramsg)1758e3e3a7aSWarner Losh LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
1768e3e3a7aSWarner Losh   lua_Debug ar;
1778e3e3a7aSWarner Losh   if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
1788e3e3a7aSWarner Losh     return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
1798e3e3a7aSWarner Losh   lua_getinfo(L, "n", &ar);
1808e3e3a7aSWarner Losh   if (strcmp(ar.namewhat, "method") == 0) {
1818e3e3a7aSWarner Losh     arg--;  /* do not count 'self' */
1828e3e3a7aSWarner Losh     if (arg == 0)  /* error is in the self argument itself? */
1838e3e3a7aSWarner Losh       return luaL_error(L, "calling '%s' on bad self (%s)",
1848e3e3a7aSWarner Losh                            ar.name, extramsg);
1858e3e3a7aSWarner Losh   }
1868e3e3a7aSWarner Losh   if (ar.name == NULL)
1878e3e3a7aSWarner Losh     ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
1888e3e3a7aSWarner Losh   return luaL_error(L, "bad argument #%d to '%s' (%s)",
1898e3e3a7aSWarner Losh                         arg, ar.name, extramsg);
1908e3e3a7aSWarner Losh }
1918e3e3a7aSWarner Losh 
1928e3e3a7aSWarner Losh 
luaL_typeerror(lua_State * L,int arg,const char * tname)1938c784bb8SWarner Losh LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
1948e3e3a7aSWarner Losh   const char *msg;
1958e3e3a7aSWarner Losh   const char *typearg;  /* name for the type of the actual argument */
1968e3e3a7aSWarner Losh   if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
1978e3e3a7aSWarner Losh     typearg = lua_tostring(L, -1);  /* use the given type name */
1988e3e3a7aSWarner Losh   else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
1998e3e3a7aSWarner Losh     typearg = "light userdata";  /* special name for messages */
2008e3e3a7aSWarner Losh   else
2018e3e3a7aSWarner Losh     typearg = luaL_typename(L, arg);  /* standard name */
2028e3e3a7aSWarner Losh   msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg);
2038e3e3a7aSWarner Losh   return luaL_argerror(L, arg, msg);
2048e3e3a7aSWarner Losh }
2058e3e3a7aSWarner Losh 
2068e3e3a7aSWarner Losh 
tag_error(lua_State * L,int arg,int tag)2078e3e3a7aSWarner Losh static void tag_error (lua_State *L, int arg, int tag) {
2080495ed39SKyle Evans   luaL_typeerror(L, arg, lua_typename(L, tag));
2098e3e3a7aSWarner Losh }
2108e3e3a7aSWarner Losh 
2118e3e3a7aSWarner Losh 
2128e3e3a7aSWarner Losh /*
2138e3e3a7aSWarner Losh ** The use of 'lua_pushfstring' ensures this function does not
2148e3e3a7aSWarner Losh ** need reserved stack space when called.
2158e3e3a7aSWarner Losh */
luaL_where(lua_State * L,int level)2168e3e3a7aSWarner Losh LUALIB_API void luaL_where (lua_State *L, int level) {
2178e3e3a7aSWarner Losh   lua_Debug ar;
2188e3e3a7aSWarner Losh   if (lua_getstack(L, level, &ar)) {  /* check function at level */
2198e3e3a7aSWarner Losh     lua_getinfo(L, "Sl", &ar);  /* get info about it */
2208e3e3a7aSWarner Losh     if (ar.currentline > 0) {  /* is there info? */
2218e3e3a7aSWarner Losh       lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
2228e3e3a7aSWarner Losh       return;
2238e3e3a7aSWarner Losh     }
2248e3e3a7aSWarner Losh   }
2258e3e3a7aSWarner Losh   lua_pushfstring(L, "");  /* else, no information available... */
2268e3e3a7aSWarner Losh }
2278e3e3a7aSWarner Losh 
2288e3e3a7aSWarner Losh 
2298e3e3a7aSWarner Losh /*
2308e3e3a7aSWarner Losh ** Again, the use of 'lua_pushvfstring' ensures this function does
2318e3e3a7aSWarner Losh ** not need reserved stack space when called. (At worst, it generates
2328e3e3a7aSWarner Losh ** an error with "stack overflow" instead of the given message.)
2338e3e3a7aSWarner Losh */
luaL_error(lua_State * L,const char * fmt,...)2348e3e3a7aSWarner Losh LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
2358e3e3a7aSWarner Losh   va_list argp;
2368e3e3a7aSWarner Losh   va_start(argp, fmt);
2378e3e3a7aSWarner Losh   luaL_where(L, 1);
2388e3e3a7aSWarner Losh   lua_pushvfstring(L, fmt, argp);
2398e3e3a7aSWarner Losh   va_end(argp);
2408e3e3a7aSWarner Losh   lua_concat(L, 2);
2418e3e3a7aSWarner Losh   return lua_error(L);
2428e3e3a7aSWarner Losh }
2438e3e3a7aSWarner Losh 
2448e3e3a7aSWarner Losh 
luaL_fileresult(lua_State * L,int stat,const char * fname)2458e3e3a7aSWarner Losh LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
2468e3e3a7aSWarner Losh   int en = errno;  /* calls to Lua API may change this value */
2478e3e3a7aSWarner Losh   if (stat) {
2488e3e3a7aSWarner Losh     lua_pushboolean(L, 1);
2498e3e3a7aSWarner Losh     return 1;
2508e3e3a7aSWarner Losh   }
2518e3e3a7aSWarner Losh   else {
2520495ed39SKyle Evans     luaL_pushfail(L);
2538e3e3a7aSWarner Losh     if (fname)
2548e3e3a7aSWarner Losh       lua_pushfstring(L, "%s: %s", fname, strerror(en));
2558e3e3a7aSWarner Losh     else
2568e3e3a7aSWarner Losh       lua_pushstring(L, strerror(en));
2578e3e3a7aSWarner Losh     lua_pushinteger(L, en);
2588e3e3a7aSWarner Losh     return 3;
2598e3e3a7aSWarner Losh   }
2608e3e3a7aSWarner Losh }
2618e3e3a7aSWarner Losh 
2628e3e3a7aSWarner Losh 
2638e3e3a7aSWarner Losh #if !defined(l_inspectstat)	/* { */
2648e3e3a7aSWarner Losh 
2658e3e3a7aSWarner Losh #if defined(LUA_USE_POSIX)
2668e3e3a7aSWarner Losh 
2678e3e3a7aSWarner Losh #include <sys/wait.h>
2688e3e3a7aSWarner Losh 
2698e3e3a7aSWarner Losh /*
2708e3e3a7aSWarner Losh ** use appropriate macros to interpret 'pclose' return status
2718e3e3a7aSWarner Losh */
2728e3e3a7aSWarner Losh #define l_inspectstat(stat,what)  \
2738e3e3a7aSWarner Losh    if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
2748e3e3a7aSWarner Losh    else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
2758e3e3a7aSWarner Losh 
2768e3e3a7aSWarner Losh #else
2778e3e3a7aSWarner Losh 
2788e3e3a7aSWarner Losh #define l_inspectstat(stat,what)  /* no op */
2798e3e3a7aSWarner Losh 
2808e3e3a7aSWarner Losh #endif
2818e3e3a7aSWarner Losh 
2828e3e3a7aSWarner Losh #endif				/* } */
2838e3e3a7aSWarner Losh 
2848e3e3a7aSWarner Losh 
luaL_execresult(lua_State * L,int stat)2858e3e3a7aSWarner Losh LUALIB_API int luaL_execresult (lua_State *L, int stat) {
2860495ed39SKyle Evans   if (stat != 0 && errno != 0)  /* error with an 'errno'? */
2878e3e3a7aSWarner Losh     return luaL_fileresult(L, 0, NULL);
2888e3e3a7aSWarner Losh   else {
2890495ed39SKyle Evans     const char *what = "exit";  /* type of termination */
2908e3e3a7aSWarner Losh     l_inspectstat(stat, what);  /* interpret result */
2918e3e3a7aSWarner Losh     if (*what == 'e' && stat == 0)  /* successful termination? */
2928e3e3a7aSWarner Losh       lua_pushboolean(L, 1);
2938e3e3a7aSWarner Losh     else
2940495ed39SKyle Evans       luaL_pushfail(L);
2958e3e3a7aSWarner Losh     lua_pushstring(L, what);
2968e3e3a7aSWarner Losh     lua_pushinteger(L, stat);
2970495ed39SKyle Evans     return 3;  /* return true/fail,what,code */
2988e3e3a7aSWarner Losh   }
2998e3e3a7aSWarner Losh }
3008e3e3a7aSWarner Losh 
3018e3e3a7aSWarner Losh /* }====================================================== */
3028e3e3a7aSWarner Losh 
3038e3e3a7aSWarner Losh 
3040495ed39SKyle Evans 
3058e3e3a7aSWarner Losh /*
3068e3e3a7aSWarner Losh ** {======================================================
3078e3e3a7aSWarner Losh ** Userdata's metatable manipulation
3088e3e3a7aSWarner Losh ** =======================================================
3098e3e3a7aSWarner Losh */
3108e3e3a7aSWarner Losh 
luaL_newmetatable(lua_State * L,const char * tname)3118e3e3a7aSWarner Losh LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
3128e3e3a7aSWarner Losh   if (luaL_getmetatable(L, tname) != LUA_TNIL)  /* name already in use? */
3138e3e3a7aSWarner Losh     return 0;  /* leave previous value on top, but return 0 */
3148e3e3a7aSWarner Losh   lua_pop(L, 1);
3158e3e3a7aSWarner Losh   lua_createtable(L, 0, 2);  /* create metatable */
3168e3e3a7aSWarner Losh   lua_pushstring(L, tname);
3178e3e3a7aSWarner Losh   lua_setfield(L, -2, "__name");  /* metatable.__name = tname */
3188e3e3a7aSWarner Losh   lua_pushvalue(L, -1);
3198e3e3a7aSWarner Losh   lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
3208e3e3a7aSWarner Losh   return 1;
3218e3e3a7aSWarner Losh }
3228e3e3a7aSWarner Losh 
3238e3e3a7aSWarner Losh 
luaL_setmetatable(lua_State * L,const char * tname)3248e3e3a7aSWarner Losh LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
3258e3e3a7aSWarner Losh   luaL_getmetatable(L, tname);
3268e3e3a7aSWarner Losh   lua_setmetatable(L, -2);
3278e3e3a7aSWarner Losh }
3288e3e3a7aSWarner Losh 
3298e3e3a7aSWarner Losh 
luaL_testudata(lua_State * L,int ud,const char * tname)3308e3e3a7aSWarner Losh LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
3318e3e3a7aSWarner Losh   void *p = lua_touserdata(L, ud);
3328e3e3a7aSWarner Losh   if (p != NULL) {  /* value is a userdata? */
3338e3e3a7aSWarner Losh     if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */
3348e3e3a7aSWarner Losh       luaL_getmetatable(L, tname);  /* get correct metatable */
3358e3e3a7aSWarner Losh       if (!lua_rawequal(L, -1, -2))  /* not the same? */
3368e3e3a7aSWarner Losh         p = NULL;  /* value is a userdata with wrong metatable */
3378e3e3a7aSWarner Losh       lua_pop(L, 2);  /* remove both metatables */
3388e3e3a7aSWarner Losh       return p;
3398e3e3a7aSWarner Losh     }
3408e3e3a7aSWarner Losh   }
3418e3e3a7aSWarner Losh   return NULL;  /* value is not a userdata with a metatable */
3428e3e3a7aSWarner Losh }
3438e3e3a7aSWarner Losh 
3448e3e3a7aSWarner Losh 
luaL_checkudata(lua_State * L,int ud,const char * tname)3458e3e3a7aSWarner Losh LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
3468e3e3a7aSWarner Losh   void *p = luaL_testudata(L, ud, tname);
3470495ed39SKyle Evans   luaL_argexpected(L, p != NULL, ud, tname);
3488e3e3a7aSWarner Losh   return p;
3498e3e3a7aSWarner Losh }
3508e3e3a7aSWarner Losh 
3518e3e3a7aSWarner Losh /* }====================================================== */
3528e3e3a7aSWarner Losh 
3538e3e3a7aSWarner Losh 
3548e3e3a7aSWarner Losh /*
3558e3e3a7aSWarner Losh ** {======================================================
3568e3e3a7aSWarner Losh ** Argument check functions
3578e3e3a7aSWarner Losh ** =======================================================
3588e3e3a7aSWarner Losh */
3598e3e3a7aSWarner Losh 
luaL_checkoption(lua_State * L,int arg,const char * def,const char * const lst[])3608e3e3a7aSWarner Losh LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
3618e3e3a7aSWarner Losh                                  const char *const lst[]) {
3628e3e3a7aSWarner Losh   const char *name = (def) ? luaL_optstring(L, arg, def) :
3638e3e3a7aSWarner Losh                              luaL_checkstring(L, arg);
3648e3e3a7aSWarner Losh   int i;
3658e3e3a7aSWarner Losh   for (i=0; lst[i]; i++)
3668e3e3a7aSWarner Losh     if (strcmp(lst[i], name) == 0)
3678e3e3a7aSWarner Losh       return i;
3688e3e3a7aSWarner Losh   return luaL_argerror(L, arg,
3698e3e3a7aSWarner Losh                        lua_pushfstring(L, "invalid option '%s'", name));
3708e3e3a7aSWarner Losh }
3718e3e3a7aSWarner Losh 
3728e3e3a7aSWarner Losh 
3738e3e3a7aSWarner Losh /*
3748e3e3a7aSWarner Losh ** Ensures the stack has at least 'space' extra slots, raising an error
3758e3e3a7aSWarner Losh ** if it cannot fulfill the request. (The error handling needs a few
3768e3e3a7aSWarner Losh ** extra slots to format the error message. In case of an error without
3778e3e3a7aSWarner Losh ** this extra space, Lua will generate the same 'stack overflow' error,
3788e3e3a7aSWarner Losh ** but without 'msg'.)
3798e3e3a7aSWarner Losh */
luaL_checkstack(lua_State * L,int space,const char * msg)3808e3e3a7aSWarner Losh LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
3818c784bb8SWarner Losh   if (l_unlikely(!lua_checkstack(L, space))) {
3828e3e3a7aSWarner Losh     if (msg)
3838e3e3a7aSWarner Losh       luaL_error(L, "stack overflow (%s)", msg);
3848e3e3a7aSWarner Losh     else
3858e3e3a7aSWarner Losh       luaL_error(L, "stack overflow");
3868e3e3a7aSWarner Losh   }
3878e3e3a7aSWarner Losh }
3888e3e3a7aSWarner Losh 
3898e3e3a7aSWarner Losh 
luaL_checktype(lua_State * L,int arg,int t)3908e3e3a7aSWarner Losh LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
3918c784bb8SWarner Losh   if (l_unlikely(lua_type(L, arg) != t))
3928e3e3a7aSWarner Losh     tag_error(L, arg, t);
3938e3e3a7aSWarner Losh }
3948e3e3a7aSWarner Losh 
3958e3e3a7aSWarner Losh 
luaL_checkany(lua_State * L,int arg)3968e3e3a7aSWarner Losh LUALIB_API void luaL_checkany (lua_State *L, int arg) {
3978c784bb8SWarner Losh   if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
3988e3e3a7aSWarner Losh     luaL_argerror(L, arg, "value expected");
3998e3e3a7aSWarner Losh }
4008e3e3a7aSWarner Losh 
4018e3e3a7aSWarner Losh 
luaL_checklstring(lua_State * L,int arg,size_t * len)4028e3e3a7aSWarner Losh LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
4038e3e3a7aSWarner Losh   const char *s = lua_tolstring(L, arg, len);
4048c784bb8SWarner Losh   if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
4058e3e3a7aSWarner Losh   return s;
4068e3e3a7aSWarner Losh }
4078e3e3a7aSWarner Losh 
4088e3e3a7aSWarner Losh 
luaL_optlstring(lua_State * L,int arg,const char * def,size_t * len)4098e3e3a7aSWarner Losh LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
4108e3e3a7aSWarner Losh                                         const char *def, size_t *len) {
4118e3e3a7aSWarner Losh   if (lua_isnoneornil(L, arg)) {
4128e3e3a7aSWarner Losh     if (len)
4138e3e3a7aSWarner Losh       *len = (def ? strlen(def) : 0);
4148e3e3a7aSWarner Losh     return def;
4158e3e3a7aSWarner Losh   }
4168e3e3a7aSWarner Losh   else return luaL_checklstring(L, arg, len);
4178e3e3a7aSWarner Losh }
4188e3e3a7aSWarner Losh 
4198e3e3a7aSWarner Losh 
luaL_checknumber(lua_State * L,int arg)4208e3e3a7aSWarner Losh LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
4218e3e3a7aSWarner Losh   int isnum;
4228e3e3a7aSWarner Losh   lua_Number d = lua_tonumberx(L, arg, &isnum);
4238c784bb8SWarner Losh   if (l_unlikely(!isnum))
4248e3e3a7aSWarner Losh     tag_error(L, arg, LUA_TNUMBER);
4258e3e3a7aSWarner Losh   return d;
4268e3e3a7aSWarner Losh }
4278e3e3a7aSWarner Losh 
4288e3e3a7aSWarner Losh 
luaL_optnumber(lua_State * L,int arg,lua_Number def)4298e3e3a7aSWarner Losh LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
4308e3e3a7aSWarner Losh   return luaL_opt(L, luaL_checknumber, arg, def);
4318e3e3a7aSWarner Losh }
4328e3e3a7aSWarner Losh 
4338e3e3a7aSWarner Losh 
interror(lua_State * L,int arg)4348e3e3a7aSWarner Losh static void interror (lua_State *L, int arg) {
4358e3e3a7aSWarner Losh   if (lua_isnumber(L, arg))
4368e3e3a7aSWarner Losh     luaL_argerror(L, arg, "number has no integer representation");
4378e3e3a7aSWarner Losh   else
4388e3e3a7aSWarner Losh     tag_error(L, arg, LUA_TNUMBER);
4398e3e3a7aSWarner Losh }
4408e3e3a7aSWarner Losh 
4418e3e3a7aSWarner Losh 
luaL_checkinteger(lua_State * L,int arg)4428e3e3a7aSWarner Losh LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
4438e3e3a7aSWarner Losh   int isnum;
4448e3e3a7aSWarner Losh   lua_Integer d = lua_tointegerx(L, arg, &isnum);
4458c784bb8SWarner Losh   if (l_unlikely(!isnum)) {
4468e3e3a7aSWarner Losh     interror(L, arg);
4478e3e3a7aSWarner Losh   }
4488e3e3a7aSWarner Losh   return d;
4498e3e3a7aSWarner Losh }
4508e3e3a7aSWarner Losh 
4518e3e3a7aSWarner Losh 
luaL_optinteger(lua_State * L,int arg,lua_Integer def)4528e3e3a7aSWarner Losh LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
4538e3e3a7aSWarner Losh                                                       lua_Integer def) {
4548e3e3a7aSWarner Losh   return luaL_opt(L, luaL_checkinteger, arg, def);
4558e3e3a7aSWarner Losh }
4568e3e3a7aSWarner Losh 
4578e3e3a7aSWarner Losh /* }====================================================== */
4588e3e3a7aSWarner Losh 
4598e3e3a7aSWarner Losh 
4608e3e3a7aSWarner Losh /*
4618e3e3a7aSWarner Losh ** {======================================================
4628e3e3a7aSWarner Losh ** Generic Buffer manipulation
4638e3e3a7aSWarner Losh ** =======================================================
4648e3e3a7aSWarner Losh */
4658e3e3a7aSWarner Losh 
4668e3e3a7aSWarner Losh /* userdata to box arbitrary data */
4678e3e3a7aSWarner Losh typedef struct UBox {
4688e3e3a7aSWarner Losh   void *box;
4698e3e3a7aSWarner Losh   size_t bsize;
4708e3e3a7aSWarner Losh } UBox;
4718e3e3a7aSWarner Losh 
4728e3e3a7aSWarner Losh 
resizebox(lua_State * L,int idx,size_t newsize)4738e3e3a7aSWarner Losh static void *resizebox (lua_State *L, int idx, size_t newsize) {
4748e3e3a7aSWarner Losh   void *ud;
4758e3e3a7aSWarner Losh   lua_Alloc allocf = lua_getallocf(L, &ud);
4768e3e3a7aSWarner Losh   UBox *box = (UBox *)lua_touserdata(L, idx);
4778e3e3a7aSWarner Losh   void *temp = allocf(ud, box->box, box->bsize, newsize);
4788c784bb8SWarner Losh   if (l_unlikely(temp == NULL && newsize > 0)) {  /* allocation error? */
4790495ed39SKyle Evans     lua_pushliteral(L, "not enough memory");
4800495ed39SKyle Evans     lua_error(L);  /* raise a memory error */
4818e3e3a7aSWarner Losh   }
4828e3e3a7aSWarner Losh   box->box = temp;
4838e3e3a7aSWarner Losh   box->bsize = newsize;
4848e3e3a7aSWarner Losh   return temp;
4858e3e3a7aSWarner Losh }
4868e3e3a7aSWarner Losh 
4878e3e3a7aSWarner Losh 
boxgc(lua_State * L)4888e3e3a7aSWarner Losh static int boxgc (lua_State *L) {
4898e3e3a7aSWarner Losh   resizebox(L, 1, 0);
4908e3e3a7aSWarner Losh   return 0;
4918e3e3a7aSWarner Losh }
4928e3e3a7aSWarner Losh 
4938e3e3a7aSWarner Losh 
4940495ed39SKyle Evans static const luaL_Reg boxmt[] = {  /* box metamethods */
4950495ed39SKyle Evans   {"__gc", boxgc},
4960495ed39SKyle Evans   {"__close", boxgc},
4970495ed39SKyle Evans   {NULL, NULL}
4980495ed39SKyle Evans };
4990495ed39SKyle Evans 
5000495ed39SKyle Evans 
newbox(lua_State * L)5010495ed39SKyle Evans static void newbox (lua_State *L) {
5020495ed39SKyle Evans   UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0);
5038e3e3a7aSWarner Losh   box->box = NULL;
5048e3e3a7aSWarner Losh   box->bsize = 0;
5050495ed39SKyle Evans   if (luaL_newmetatable(L, "_UBOX*"))  /* creating metatable? */
5060495ed39SKyle Evans     luaL_setfuncs(L, boxmt, 0);  /* set its metamethods */
5078e3e3a7aSWarner Losh   lua_setmetatable(L, -2);
5088e3e3a7aSWarner Losh }
5098e3e3a7aSWarner Losh 
5108e3e3a7aSWarner Losh 
5118e3e3a7aSWarner Losh /*
5128e3e3a7aSWarner Losh ** check whether buffer is using a userdata on the stack as a temporary
5138e3e3a7aSWarner Losh ** buffer
5148e3e3a7aSWarner Losh */
5150495ed39SKyle Evans #define buffonstack(B)	((B)->b != (B)->init.b)
5168e3e3a7aSWarner Losh 
5178e3e3a7aSWarner Losh 
5188e3e3a7aSWarner Losh /*
5198c784bb8SWarner Losh ** Whenever buffer is accessed, slot 'idx' must either be a box (which
5208c784bb8SWarner Losh ** cannot be NULL) or it is a placeholder for the buffer.
5218c784bb8SWarner Losh */
5228c784bb8SWarner Losh #define checkbufferlevel(B,idx)  \
5238c784bb8SWarner Losh   lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL  \
5248c784bb8SWarner Losh                             : lua_touserdata(B->L, idx) == (void*)B)
5258c784bb8SWarner Losh 
5268c784bb8SWarner Losh 
5278c784bb8SWarner Losh /*
5280495ed39SKyle Evans ** Compute new size for buffer 'B', enough to accommodate extra 'sz'
529*a9490b81SWarner Losh ** bytes. (The test for "not big enough" also gets the case when the
530*a9490b81SWarner Losh ** computation of 'newsize' overflows.)
5310495ed39SKyle Evans */
newbuffsize(luaL_Buffer * B,size_t sz)5320495ed39SKyle Evans static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
533*a9490b81SWarner Losh   size_t newsize = (B->size / 2) * 3;  /* buffer size * 1.5 */
5348c784bb8SWarner Losh   if (l_unlikely(MAX_SIZET - sz < B->n))  /* overflow in (B->n + sz)? */
5350495ed39SKyle Evans     return luaL_error(B->L, "buffer too large");
536*a9490b81SWarner Losh   if (newsize < B->n + sz)  /* not big enough? */
5370495ed39SKyle Evans     newsize = B->n + sz;
5380495ed39SKyle Evans   return newsize;
5390495ed39SKyle Evans }
5400495ed39SKyle Evans 
5410495ed39SKyle Evans 
5420495ed39SKyle Evans /*
5430495ed39SKyle Evans ** Returns a pointer to a free area with at least 'sz' bytes in buffer
5448c784bb8SWarner Losh ** 'B'. 'boxidx' is the relative position in the stack where is the
5458c784bb8SWarner Losh ** buffer's box or its placeholder.
5460495ed39SKyle Evans */
prepbuffsize(luaL_Buffer * B,size_t sz,int boxidx)5470495ed39SKyle Evans static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
5488c784bb8SWarner Losh   checkbufferlevel(B, boxidx);
5490495ed39SKyle Evans   if (B->size - B->n >= sz)  /* enough space? */
5500495ed39SKyle Evans     return B->b + B->n;
5510495ed39SKyle Evans   else {
5520495ed39SKyle Evans     lua_State *L = B->L;
5530495ed39SKyle Evans     char *newbuff;
5540495ed39SKyle Evans     size_t newsize = newbuffsize(B, sz);
5550495ed39SKyle Evans     /* create larger buffer */
5560495ed39SKyle Evans     if (buffonstack(B))  /* buffer already has a box? */
5570495ed39SKyle Evans       newbuff = (char *)resizebox(L, boxidx, newsize);  /* resize it */
5580495ed39SKyle Evans     else {  /* no box yet */
5598c784bb8SWarner Losh       lua_remove(L, boxidx);  /* remove placeholder */
5600495ed39SKyle Evans       newbox(L);  /* create a new box */
5618c784bb8SWarner Losh       lua_insert(L, boxidx);  /* move box to its intended position */
5620495ed39SKyle Evans       lua_toclose(L, boxidx);
5630495ed39SKyle Evans       newbuff = (char *)resizebox(L, boxidx, newsize);
5640495ed39SKyle Evans       memcpy(newbuff, B->b, B->n * sizeof(char));  /* copy original content */
5650495ed39SKyle Evans     }
5660495ed39SKyle Evans     B->b = newbuff;
5670495ed39SKyle Evans     B->size = newsize;
5680495ed39SKyle Evans     return newbuff + B->n;
5690495ed39SKyle Evans   }
5700495ed39SKyle Evans }
5710495ed39SKyle Evans 
5720495ed39SKyle Evans /*
5738e3e3a7aSWarner Losh ** returns a pointer to a free area with at least 'sz' bytes
5748e3e3a7aSWarner Losh */
luaL_prepbuffsize(luaL_Buffer * B,size_t sz)5758e3e3a7aSWarner Losh LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
5760495ed39SKyle Evans   return prepbuffsize(B, sz, -1);
5778e3e3a7aSWarner Losh }
5788e3e3a7aSWarner Losh 
5798e3e3a7aSWarner Losh 
luaL_addlstring(luaL_Buffer * B,const char * s,size_t l)5808e3e3a7aSWarner Losh LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
5818e3e3a7aSWarner Losh   if (l > 0) {  /* avoid 'memcpy' when 's' can be NULL */
5820495ed39SKyle Evans     char *b = prepbuffsize(B, l, -1);
5838e3e3a7aSWarner Losh     memcpy(b, s, l * sizeof(char));
5848e3e3a7aSWarner Losh     luaL_addsize(B, l);
5858e3e3a7aSWarner Losh   }
5868e3e3a7aSWarner Losh }
5878e3e3a7aSWarner Losh 
5888e3e3a7aSWarner Losh 
luaL_addstring(luaL_Buffer * B,const char * s)5898e3e3a7aSWarner Losh LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
5908e3e3a7aSWarner Losh   luaL_addlstring(B, s, strlen(s));
5918e3e3a7aSWarner Losh }
5928e3e3a7aSWarner Losh 
5938e3e3a7aSWarner Losh 
luaL_pushresult(luaL_Buffer * B)5948e3e3a7aSWarner Losh LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
5958e3e3a7aSWarner Losh   lua_State *L = B->L;
5968c784bb8SWarner Losh   checkbufferlevel(B, -1);
5978e3e3a7aSWarner Losh   lua_pushlstring(L, B->b, B->n);
5988c784bb8SWarner Losh   if (buffonstack(B))
5998c784bb8SWarner Losh     lua_closeslot(L, -2);  /* close the box */
6008c784bb8SWarner Losh   lua_remove(L, -2);  /* remove box or placeholder from the stack */
6018e3e3a7aSWarner Losh }
6028e3e3a7aSWarner Losh 
6038e3e3a7aSWarner Losh 
luaL_pushresultsize(luaL_Buffer * B,size_t sz)6048e3e3a7aSWarner Losh LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
6058e3e3a7aSWarner Losh   luaL_addsize(B, sz);
6068e3e3a7aSWarner Losh   luaL_pushresult(B);
6078e3e3a7aSWarner Losh }
6088e3e3a7aSWarner Losh 
6098e3e3a7aSWarner Losh 
6100495ed39SKyle Evans /*
6110495ed39SKyle Evans ** 'luaL_addvalue' is the only function in the Buffer system where the
6120495ed39SKyle Evans ** box (if existent) is not on the top of the stack. So, instead of
6130495ed39SKyle Evans ** calling 'luaL_addlstring', it replicates the code using -2 as the
6140495ed39SKyle Evans ** last argument to 'prepbuffsize', signaling that the box is (or will
615*a9490b81SWarner Losh ** be) below the string being added to the buffer. (Box creation can
6160495ed39SKyle Evans ** trigger an emergency GC, so we should not remove the string from the
6170495ed39SKyle Evans ** stack before we have the space guaranteed.)
6180495ed39SKyle Evans */
luaL_addvalue(luaL_Buffer * B)6198e3e3a7aSWarner Losh LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
6208e3e3a7aSWarner Losh   lua_State *L = B->L;
6210495ed39SKyle Evans   size_t len;
6220495ed39SKyle Evans   const char *s = lua_tolstring(L, -1, &len);
6230495ed39SKyle Evans   char *b = prepbuffsize(B, len, -2);
6240495ed39SKyle Evans   memcpy(b, s, len * sizeof(char));
6250495ed39SKyle Evans   luaL_addsize(B, len);
6260495ed39SKyle Evans   lua_pop(L, 1);  /* pop string */
6278e3e3a7aSWarner Losh }
6288e3e3a7aSWarner Losh 
6298e3e3a7aSWarner Losh 
luaL_buffinit(lua_State * L,luaL_Buffer * B)6308e3e3a7aSWarner Losh LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
6318e3e3a7aSWarner Losh   B->L = L;
6320495ed39SKyle Evans   B->b = B->init.b;
6338e3e3a7aSWarner Losh   B->n = 0;
6348e3e3a7aSWarner Losh   B->size = LUAL_BUFFERSIZE;
6358c784bb8SWarner Losh   lua_pushlightuserdata(L, (void*)B);  /* push placeholder */
6368e3e3a7aSWarner Losh }
6378e3e3a7aSWarner Losh 
6388e3e3a7aSWarner Losh 
luaL_buffinitsize(lua_State * L,luaL_Buffer * B,size_t sz)6398e3e3a7aSWarner Losh LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
6408e3e3a7aSWarner Losh   luaL_buffinit(L, B);
6410495ed39SKyle Evans   return prepbuffsize(B, sz, -1);
6428e3e3a7aSWarner Losh }
6438e3e3a7aSWarner Losh 
6448e3e3a7aSWarner Losh /* }====================================================== */
6458e3e3a7aSWarner Losh 
6468e3e3a7aSWarner Losh 
6478e3e3a7aSWarner Losh /*
6488e3e3a7aSWarner Losh ** {======================================================
6498e3e3a7aSWarner Losh ** Reference system
6508e3e3a7aSWarner Losh ** =======================================================
6518e3e3a7aSWarner Losh */
6528e3e3a7aSWarner Losh 
6538c784bb8SWarner Losh /* index of free-list header (after the predefined values) */
6548c784bb8SWarner Losh #define freelist	(LUA_RIDX_LAST + 1)
6558e3e3a7aSWarner Losh 
6568c784bb8SWarner Losh /*
6578c784bb8SWarner Losh ** The previously freed references form a linked list:
6588c784bb8SWarner Losh ** t[freelist] is the index of a first free index, or zero if list is
6598c784bb8SWarner Losh ** empty; t[t[freelist]] is the index of the second element; etc.
6608c784bb8SWarner Losh */
luaL_ref(lua_State * L,int t)6618e3e3a7aSWarner Losh LUALIB_API int luaL_ref (lua_State *L, int t) {
6628e3e3a7aSWarner Losh   int ref;
6638e3e3a7aSWarner Losh   if (lua_isnil(L, -1)) {
6648e3e3a7aSWarner Losh     lua_pop(L, 1);  /* remove from stack */
6658e3e3a7aSWarner Losh     return LUA_REFNIL;  /* 'nil' has a unique fixed reference */
6668e3e3a7aSWarner Losh   }
6678e3e3a7aSWarner Losh   t = lua_absindex(L, t);
6688c784bb8SWarner Losh   if (lua_rawgeti(L, t, freelist) == LUA_TNIL) {  /* first access? */
6698c784bb8SWarner Losh     ref = 0;  /* list is empty */
6708c784bb8SWarner Losh     lua_pushinteger(L, 0);  /* initialize as an empty list */
6718c784bb8SWarner Losh     lua_rawseti(L, t, freelist);  /* ref = t[freelist] = 0 */
6728c784bb8SWarner Losh   }
6738c784bb8SWarner Losh   else {  /* already initialized */
6748c784bb8SWarner Losh     lua_assert(lua_isinteger(L, -1));
6758e3e3a7aSWarner Losh     ref = (int)lua_tointeger(L, -1);  /* ref = t[freelist] */
6768c784bb8SWarner Losh   }
6778c784bb8SWarner Losh   lua_pop(L, 1);  /* remove element from stack */
6788e3e3a7aSWarner Losh   if (ref != 0) {  /* any free element? */
6798e3e3a7aSWarner Losh     lua_rawgeti(L, t, ref);  /* remove it from list */
6808e3e3a7aSWarner Losh     lua_rawseti(L, t, freelist);  /* (t[freelist] = t[ref]) */
6818e3e3a7aSWarner Losh   }
6828e3e3a7aSWarner Losh   else  /* no free elements */
6838e3e3a7aSWarner Losh     ref = (int)lua_rawlen(L, t) + 1;  /* get a new reference */
6848e3e3a7aSWarner Losh   lua_rawseti(L, t, ref);
6858e3e3a7aSWarner Losh   return ref;
6868e3e3a7aSWarner Losh }
6878e3e3a7aSWarner Losh 
6888e3e3a7aSWarner Losh 
luaL_unref(lua_State * L,int t,int ref)6898e3e3a7aSWarner Losh LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
6908e3e3a7aSWarner Losh   if (ref >= 0) {
6918e3e3a7aSWarner Losh     t = lua_absindex(L, t);
6928e3e3a7aSWarner Losh     lua_rawgeti(L, t, freelist);
6938c784bb8SWarner Losh     lua_assert(lua_isinteger(L, -1));
6948e3e3a7aSWarner Losh     lua_rawseti(L, t, ref);  /* t[ref] = t[freelist] */
6958e3e3a7aSWarner Losh     lua_pushinteger(L, ref);
6968e3e3a7aSWarner Losh     lua_rawseti(L, t, freelist);  /* t[freelist] = ref */
6978e3e3a7aSWarner Losh   }
6988e3e3a7aSWarner Losh }
6998e3e3a7aSWarner Losh 
7008e3e3a7aSWarner Losh /* }====================================================== */
7018e3e3a7aSWarner Losh 
7028e3e3a7aSWarner Losh 
7038e3e3a7aSWarner Losh /*
7048e3e3a7aSWarner Losh ** {======================================================
7058e3e3a7aSWarner Losh ** Load functions
7068e3e3a7aSWarner Losh ** =======================================================
7078e3e3a7aSWarner Losh */
7088e3e3a7aSWarner Losh 
7098e3e3a7aSWarner Losh typedef struct LoadF {
7108e3e3a7aSWarner Losh   int n;  /* number of pre-read characters */
7118e3e3a7aSWarner Losh   FILE *f;  /* file being read */
7128e3e3a7aSWarner Losh   char buff[BUFSIZ];  /* area for reading file */
7138e3e3a7aSWarner Losh } LoadF;
7148e3e3a7aSWarner Losh 
7158e3e3a7aSWarner Losh 
getF(lua_State * L,void * ud,size_t * size)7168e3e3a7aSWarner Losh static const char *getF (lua_State *L, void *ud, size_t *size) {
7178e3e3a7aSWarner Losh   LoadF *lf = (LoadF *)ud;
7188e3e3a7aSWarner Losh   (void)L;  /* not used */
7198e3e3a7aSWarner Losh   if (lf->n > 0) {  /* are there pre-read characters to be read? */
7208e3e3a7aSWarner Losh     *size = lf->n;  /* return them (chars already in buffer) */
7218e3e3a7aSWarner Losh     lf->n = 0;  /* no more pre-read characters */
7228e3e3a7aSWarner Losh   }
7238e3e3a7aSWarner Losh   else {  /* read a block from file */
7248e3e3a7aSWarner Losh     /* 'fread' can return > 0 *and* set the EOF flag. If next call to
7258e3e3a7aSWarner Losh        'getF' called 'fread', it might still wait for user input.
7268e3e3a7aSWarner Losh        The next check avoids this problem. */
7278e3e3a7aSWarner Losh     if (feof(lf->f)) return NULL;
7288e3e3a7aSWarner Losh     *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);  /* read block */
7298e3e3a7aSWarner Losh   }
7308e3e3a7aSWarner Losh   return lf->buff;
7318e3e3a7aSWarner Losh }
7328e3e3a7aSWarner Losh 
7338e3e3a7aSWarner Losh 
errfile(lua_State * L,const char * what,int fnameindex)7348e3e3a7aSWarner Losh static int errfile (lua_State *L, const char *what, int fnameindex) {
7358e3e3a7aSWarner Losh   const char *serr = strerror(errno);
7368e3e3a7aSWarner Losh   const char *filename = lua_tostring(L, fnameindex) + 1;
7378e3e3a7aSWarner Losh   lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
7388e3e3a7aSWarner Losh   lua_remove(L, fnameindex);
7398e3e3a7aSWarner Losh   return LUA_ERRFILE;
7408e3e3a7aSWarner Losh }
7418e3e3a7aSWarner Losh 
7428e3e3a7aSWarner Losh 
743*a9490b81SWarner Losh /*
744*a9490b81SWarner Losh ** Skip an optional BOM at the start of a stream. If there is an
745*a9490b81SWarner Losh ** incomplete BOM (the first character is correct but the rest is
746*a9490b81SWarner Losh ** not), returns the first character anyway to force an error
747*a9490b81SWarner Losh ** (as no chunk can start with 0xEF).
748*a9490b81SWarner Losh */
skipBOM(FILE * f)749*a9490b81SWarner Losh static int skipBOM (FILE *f) {
750*a9490b81SWarner Losh   int c = getc(f);  /* read first character */
751*a9490b81SWarner Losh   if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF)  /* correct BOM? */
752*a9490b81SWarner Losh     return getc(f);  /* ignore BOM and return next char */
753*a9490b81SWarner Losh   else  /* no (valid) BOM */
754*a9490b81SWarner Losh     return c;  /* return first character */
7558e3e3a7aSWarner Losh }
7568e3e3a7aSWarner Losh 
7578e3e3a7aSWarner Losh 
7588e3e3a7aSWarner Losh /*
7598e3e3a7aSWarner Losh ** reads the first character of file 'f' and skips an optional BOM mark
7608e3e3a7aSWarner Losh ** in its beginning plus its first line if it starts with '#'. Returns
7618e3e3a7aSWarner Losh ** true if it skipped the first line.  In any case, '*cp' has the
7628e3e3a7aSWarner Losh ** first "valid" character of the file (after the optional BOM and
7638e3e3a7aSWarner Losh ** a first-line comment).
7648e3e3a7aSWarner Losh */
skipcomment(FILE * f,int * cp)765*a9490b81SWarner Losh static int skipcomment (FILE *f, int *cp) {
766*a9490b81SWarner Losh   int c = *cp = skipBOM(f);
7678e3e3a7aSWarner Losh   if (c == '#') {  /* first line is a comment (Unix exec. file)? */
7688e3e3a7aSWarner Losh     do {  /* skip first line */
769*a9490b81SWarner Losh       c = getc(f);
7708e3e3a7aSWarner Losh     } while (c != EOF && c != '\n');
771*a9490b81SWarner Losh     *cp = getc(f);  /* next character after comment, if present */
7728e3e3a7aSWarner Losh     return 1;  /* there was a comment */
7738e3e3a7aSWarner Losh   }
7748e3e3a7aSWarner Losh   else return 0;  /* no comment */
7758e3e3a7aSWarner Losh }
7768e3e3a7aSWarner Losh 
7778e3e3a7aSWarner Losh 
luaL_loadfilex(lua_State * L,const char * filename,const char * mode)7788e3e3a7aSWarner Losh LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
7798e3e3a7aSWarner Losh                                              const char *mode) {
7808e3e3a7aSWarner Losh   LoadF lf;
7818e3e3a7aSWarner Losh   int status, readstatus;
7828e3e3a7aSWarner Losh   int c;
7838e3e3a7aSWarner Losh   int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
7848e3e3a7aSWarner Losh   if (filename == NULL) {
7858e3e3a7aSWarner Losh     lua_pushliteral(L, "=stdin");
7868e3e3a7aSWarner Losh     lf.f = stdin;
7878e3e3a7aSWarner Losh   }
7888e3e3a7aSWarner Losh   else {
7898e3e3a7aSWarner Losh     lua_pushfstring(L, "@%s", filename);
7908e3e3a7aSWarner Losh     lf.f = fopen(filename, "r");
7918e3e3a7aSWarner Losh     if (lf.f == NULL) return errfile(L, "open", fnameindex);
7928e3e3a7aSWarner Losh   }
793*a9490b81SWarner Losh   lf.n = 0;
794*a9490b81SWarner Losh   if (skipcomment(lf.f, &c))  /* read initial portion */
795*a9490b81SWarner Losh     lf.buff[lf.n++] = '\n';  /* add newline to correct line numbers */
796*a9490b81SWarner Losh   if (c == LUA_SIGNATURE[0]) {  /* binary file? */
797*a9490b81SWarner Losh     lf.n = 0;  /* remove possible newline */
798*a9490b81SWarner Losh     if (filename) {  /* "real" file? */
7998e3e3a7aSWarner Losh       lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
8008e3e3a7aSWarner Losh       if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
801*a9490b81SWarner Losh       skipcomment(lf.f, &c);  /* re-read initial portion */
802*a9490b81SWarner Losh     }
8038e3e3a7aSWarner Losh   }
8048e3e3a7aSWarner Losh   if (c != EOF)
8058e3e3a7aSWarner Losh     lf.buff[lf.n++] = c;  /* 'c' is the first character of the stream */
8068e3e3a7aSWarner Losh   status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
8078e3e3a7aSWarner Losh   readstatus = ferror(lf.f);
8088e3e3a7aSWarner Losh   if (filename) fclose(lf.f);  /* close file (even in case of errors) */
8098e3e3a7aSWarner Losh   if (readstatus) {
8108e3e3a7aSWarner Losh     lua_settop(L, fnameindex);  /* ignore results from 'lua_load' */
8118e3e3a7aSWarner Losh     return errfile(L, "read", fnameindex);
8128e3e3a7aSWarner Losh   }
8138e3e3a7aSWarner Losh   lua_remove(L, fnameindex);
8148e3e3a7aSWarner Losh   return status;
8158e3e3a7aSWarner Losh }
8168e3e3a7aSWarner Losh 
8178e3e3a7aSWarner Losh 
8188e3e3a7aSWarner Losh typedef struct LoadS {
8198e3e3a7aSWarner Losh   const char *s;
8208e3e3a7aSWarner Losh   size_t size;
8218e3e3a7aSWarner Losh } LoadS;
8228e3e3a7aSWarner Losh 
8238e3e3a7aSWarner Losh 
getS(lua_State * L,void * ud,size_t * size)8248e3e3a7aSWarner Losh static const char *getS (lua_State *L, void *ud, size_t *size) {
8258e3e3a7aSWarner Losh   LoadS *ls = (LoadS *)ud;
8268e3e3a7aSWarner Losh   (void)L;  /* not used */
8278e3e3a7aSWarner Losh   if (ls->size == 0) return NULL;
8288e3e3a7aSWarner Losh   *size = ls->size;
8298e3e3a7aSWarner Losh   ls->size = 0;
8308e3e3a7aSWarner Losh   return ls->s;
8318e3e3a7aSWarner Losh }
8328e3e3a7aSWarner Losh 
8338e3e3a7aSWarner Losh 
luaL_loadbufferx(lua_State * L,const char * buff,size_t size,const char * name,const char * mode)8348e3e3a7aSWarner Losh LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
8358e3e3a7aSWarner Losh                                  const char *name, const char *mode) {
8368e3e3a7aSWarner Losh   LoadS ls;
8378e3e3a7aSWarner Losh   ls.s = buff;
8388e3e3a7aSWarner Losh   ls.size = size;
8398e3e3a7aSWarner Losh   return lua_load(L, getS, &ls, name, mode);
8408e3e3a7aSWarner Losh }
8418e3e3a7aSWarner Losh 
8428e3e3a7aSWarner Losh 
luaL_loadstring(lua_State * L,const char * s)8438e3e3a7aSWarner Losh LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
8448e3e3a7aSWarner Losh   return luaL_loadbuffer(L, s, strlen(s), s);
8458e3e3a7aSWarner Losh }
8468e3e3a7aSWarner Losh 
8478e3e3a7aSWarner Losh /* }====================================================== */
8488e3e3a7aSWarner Losh 
8498e3e3a7aSWarner Losh 
8508e3e3a7aSWarner Losh 
luaL_getmetafield(lua_State * L,int obj,const char * event)8518e3e3a7aSWarner Losh LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
8528e3e3a7aSWarner Losh   if (!lua_getmetatable(L, obj))  /* no metatable? */
8538e3e3a7aSWarner Losh     return LUA_TNIL;
8548e3e3a7aSWarner Losh   else {
8558e3e3a7aSWarner Losh     int tt;
8568e3e3a7aSWarner Losh     lua_pushstring(L, event);
8578e3e3a7aSWarner Losh     tt = lua_rawget(L, -2);
8588e3e3a7aSWarner Losh     if (tt == LUA_TNIL)  /* is metafield nil? */
8598e3e3a7aSWarner Losh       lua_pop(L, 2);  /* remove metatable and metafield */
8608e3e3a7aSWarner Losh     else
8618e3e3a7aSWarner Losh       lua_remove(L, -2);  /* remove only metatable */
8628e3e3a7aSWarner Losh     return tt;  /* return metafield type */
8638e3e3a7aSWarner Losh   }
8648e3e3a7aSWarner Losh }
8658e3e3a7aSWarner Losh 
8668e3e3a7aSWarner Losh 
luaL_callmeta(lua_State * L,int obj,const char * event)8678e3e3a7aSWarner Losh LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
8688e3e3a7aSWarner Losh   obj = lua_absindex(L, obj);
8698e3e3a7aSWarner Losh   if (luaL_getmetafield(L, obj, event) == LUA_TNIL)  /* no metafield? */
8708e3e3a7aSWarner Losh     return 0;
8718e3e3a7aSWarner Losh   lua_pushvalue(L, obj);
8728e3e3a7aSWarner Losh   lua_call(L, 1, 1);
8738e3e3a7aSWarner Losh   return 1;
8748e3e3a7aSWarner Losh }
8758e3e3a7aSWarner Losh 
8768e3e3a7aSWarner Losh 
luaL_len(lua_State * L,int idx)8778e3e3a7aSWarner Losh LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
8788e3e3a7aSWarner Losh   lua_Integer l;
8798e3e3a7aSWarner Losh   int isnum;
8808e3e3a7aSWarner Losh   lua_len(L, idx);
8818e3e3a7aSWarner Losh   l = lua_tointegerx(L, -1, &isnum);
8828c784bb8SWarner Losh   if (l_unlikely(!isnum))
8838e3e3a7aSWarner Losh     luaL_error(L, "object length is not an integer");
8848e3e3a7aSWarner Losh   lua_pop(L, 1);  /* remove object */
8858e3e3a7aSWarner Losh   return l;
8868e3e3a7aSWarner Losh }
8878e3e3a7aSWarner Losh 
8888e3e3a7aSWarner Losh 
luaL_tolstring(lua_State * L,int idx,size_t * len)8898e3e3a7aSWarner Losh LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
8908c784bb8SWarner Losh   idx = lua_absindex(L,idx);
8918e3e3a7aSWarner Losh   if (luaL_callmeta(L, idx, "__tostring")) {  /* metafield? */
8928e3e3a7aSWarner Losh     if (!lua_isstring(L, -1))
8938e3e3a7aSWarner Losh       luaL_error(L, "'__tostring' must return a string");
8948e3e3a7aSWarner Losh   }
8958e3e3a7aSWarner Losh   else {
8968e3e3a7aSWarner Losh     switch (lua_type(L, idx)) {
8978e3e3a7aSWarner Losh       case LUA_TNUMBER: {
8988e3e3a7aSWarner Losh         if (lua_isinteger(L, idx))
8998e3e3a7aSWarner Losh           lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx));
9008e3e3a7aSWarner Losh         else
9018e3e3a7aSWarner Losh           lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
9028e3e3a7aSWarner Losh         break;
9038e3e3a7aSWarner Losh       }
9048e3e3a7aSWarner Losh       case LUA_TSTRING:
9058e3e3a7aSWarner Losh         lua_pushvalue(L, idx);
9068e3e3a7aSWarner Losh         break;
9078e3e3a7aSWarner Losh       case LUA_TBOOLEAN:
9088e3e3a7aSWarner Losh         lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
9098e3e3a7aSWarner Losh         break;
9108e3e3a7aSWarner Losh       case LUA_TNIL:
9118e3e3a7aSWarner Losh         lua_pushliteral(L, "nil");
9128e3e3a7aSWarner Losh         break;
9138e3e3a7aSWarner Losh       default: {
9148e3e3a7aSWarner Losh         int tt = luaL_getmetafield(L, idx, "__name");  /* try name */
9158e3e3a7aSWarner Losh         const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
9168e3e3a7aSWarner Losh                                                  luaL_typename(L, idx);
9178e3e3a7aSWarner Losh         lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
9188e3e3a7aSWarner Losh         if (tt != LUA_TNIL)
9198e3e3a7aSWarner Losh           lua_remove(L, -2);  /* remove '__name' */
9208e3e3a7aSWarner Losh         break;
9218e3e3a7aSWarner Losh       }
9228e3e3a7aSWarner Losh     }
9238e3e3a7aSWarner Losh   }
9248e3e3a7aSWarner Losh   return lua_tolstring(L, -1, len);
9258e3e3a7aSWarner Losh }
9268e3e3a7aSWarner Losh 
9278e3e3a7aSWarner Losh 
9288e3e3a7aSWarner Losh /*
9298e3e3a7aSWarner Losh ** set functions from list 'l' into table at top - 'nup'; each
9308e3e3a7aSWarner Losh ** function gets the 'nup' elements at the top as upvalues.
9318e3e3a7aSWarner Losh ** Returns with only the table at the stack.
9328e3e3a7aSWarner Losh */
luaL_setfuncs(lua_State * L,const luaL_Reg * l,int nup)9338e3e3a7aSWarner Losh LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
9348e3e3a7aSWarner Losh   luaL_checkstack(L, nup, "too many upvalues");
9358e3e3a7aSWarner Losh   for (; l->name != NULL; l++) {  /* fill the table with given functions */
9360495ed39SKyle Evans     if (l->func == NULL)  /* place holder? */
9370495ed39SKyle Evans       lua_pushboolean(L, 0);
9380495ed39SKyle Evans     else {
9398e3e3a7aSWarner Losh       int i;
9408e3e3a7aSWarner Losh       for (i = 0; i < nup; i++)  /* copy upvalues to the top */
9418e3e3a7aSWarner Losh         lua_pushvalue(L, -nup);
9428e3e3a7aSWarner Losh       lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
9430495ed39SKyle Evans     }
9448e3e3a7aSWarner Losh     lua_setfield(L, -(nup + 2), l->name);
9458e3e3a7aSWarner Losh   }
9468e3e3a7aSWarner Losh   lua_pop(L, nup);  /* remove upvalues */
9478e3e3a7aSWarner Losh }
9488e3e3a7aSWarner Losh 
9498e3e3a7aSWarner Losh 
9508e3e3a7aSWarner Losh /*
9518e3e3a7aSWarner Losh ** ensure that stack[idx][fname] has a table and push that table
9528e3e3a7aSWarner Losh ** into the stack
9538e3e3a7aSWarner Losh */
luaL_getsubtable(lua_State * L,int idx,const char * fname)9548e3e3a7aSWarner Losh LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
9558e3e3a7aSWarner Losh   if (lua_getfield(L, idx, fname) == LUA_TTABLE)
9568e3e3a7aSWarner Losh     return 1;  /* table already there */
9578e3e3a7aSWarner Losh   else {
9588e3e3a7aSWarner Losh     lua_pop(L, 1);  /* remove previous result */
9598e3e3a7aSWarner Losh     idx = lua_absindex(L, idx);
9608e3e3a7aSWarner Losh     lua_newtable(L);
9618e3e3a7aSWarner Losh     lua_pushvalue(L, -1);  /* copy to be left at top */
9628e3e3a7aSWarner Losh     lua_setfield(L, idx, fname);  /* assign new table to field */
9638e3e3a7aSWarner Losh     return 0;  /* false, because did not find table there */
9648e3e3a7aSWarner Losh   }
9658e3e3a7aSWarner Losh }
9668e3e3a7aSWarner Losh 
9678e3e3a7aSWarner Losh 
9688e3e3a7aSWarner Losh /*
9698e3e3a7aSWarner Losh ** Stripped-down 'require': After checking "loaded" table, calls 'openf'
9708e3e3a7aSWarner Losh ** to open a module, registers the result in 'package.loaded' table and,
9718e3e3a7aSWarner Losh ** if 'glb' is true, also registers the result in the global table.
9728e3e3a7aSWarner Losh ** Leaves resulting module on the top.
9738e3e3a7aSWarner Losh */
luaL_requiref(lua_State * L,const char * modname,lua_CFunction openf,int glb)9748e3e3a7aSWarner Losh LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
9758e3e3a7aSWarner Losh                                lua_CFunction openf, int glb) {
9768e3e3a7aSWarner Losh   luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
9778e3e3a7aSWarner Losh   lua_getfield(L, -1, modname);  /* LOADED[modname] */
9788e3e3a7aSWarner Losh   if (!lua_toboolean(L, -1)) {  /* package not already loaded? */
9798e3e3a7aSWarner Losh     lua_pop(L, 1);  /* remove field */
9808e3e3a7aSWarner Losh     lua_pushcfunction(L, openf);
9818e3e3a7aSWarner Losh     lua_pushstring(L, modname);  /* argument to open function */
9828e3e3a7aSWarner Losh     lua_call(L, 1, 1);  /* call 'openf' to open module */
9838e3e3a7aSWarner Losh     lua_pushvalue(L, -1);  /* make copy of module (call result) */
9848e3e3a7aSWarner Losh     lua_setfield(L, -3, modname);  /* LOADED[modname] = module */
9858e3e3a7aSWarner Losh   }
9868e3e3a7aSWarner Losh   lua_remove(L, -2);  /* remove LOADED table */
9878e3e3a7aSWarner Losh   if (glb) {
9888e3e3a7aSWarner Losh     lua_pushvalue(L, -1);  /* copy of module */
9898e3e3a7aSWarner Losh     lua_setglobal(L, modname);  /* _G[modname] = module */
9908e3e3a7aSWarner Losh   }
9918e3e3a7aSWarner Losh }
9928e3e3a7aSWarner Losh 
9938e3e3a7aSWarner Losh 
luaL_addgsub(luaL_Buffer * b,const char * s,const char * p,const char * r)9940495ed39SKyle Evans LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
9950495ed39SKyle Evans                                      const char *p, const char *r) {
9968e3e3a7aSWarner Losh   const char *wild;
9978e3e3a7aSWarner Losh   size_t l = strlen(p);
9988e3e3a7aSWarner Losh   while ((wild = strstr(s, p)) != NULL) {
9990495ed39SKyle Evans     luaL_addlstring(b, s, wild - s);  /* push prefix */
10000495ed39SKyle Evans     luaL_addstring(b, r);  /* push replacement in place of pattern */
10018e3e3a7aSWarner Losh     s = wild + l;  /* continue after 'p' */
10028e3e3a7aSWarner Losh   }
10030495ed39SKyle Evans   luaL_addstring(b, s);  /* push last suffix */
10040495ed39SKyle Evans }
10050495ed39SKyle Evans 
10060495ed39SKyle Evans 
luaL_gsub(lua_State * L,const char * s,const char * p,const char * r)10070495ed39SKyle Evans LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
10080495ed39SKyle Evans                                   const char *p, const char *r) {
10090495ed39SKyle Evans   luaL_Buffer b;
10100495ed39SKyle Evans   luaL_buffinit(L, &b);
10110495ed39SKyle Evans   luaL_addgsub(&b, s, p, r);
10128e3e3a7aSWarner Losh   luaL_pushresult(&b);
10138e3e3a7aSWarner Losh   return lua_tostring(L, -1);
10148e3e3a7aSWarner Losh }
10158e3e3a7aSWarner Losh 
10168e3e3a7aSWarner Losh 
l_alloc(void * ud,void * ptr,size_t osize,size_t nsize)10178e3e3a7aSWarner Losh static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
10188e3e3a7aSWarner Losh   (void)ud; (void)osize;  /* not used */
10198e3e3a7aSWarner Losh   if (nsize == 0) {
10208e3e3a7aSWarner Losh     free(ptr);
10218e3e3a7aSWarner Losh     return NULL;
10228e3e3a7aSWarner Losh   }
10230495ed39SKyle Evans   else
10240495ed39SKyle Evans     return realloc(ptr, nsize);
10258e3e3a7aSWarner Losh }
10268e3e3a7aSWarner Losh 
10278e3e3a7aSWarner Losh 
panic(lua_State * L)10288e3e3a7aSWarner Losh static int panic (lua_State *L) {
10290495ed39SKyle Evans   const char *msg = lua_tostring(L, -1);
10300495ed39SKyle Evans   if (msg == NULL) msg = "error object is not a string";
10318e3e3a7aSWarner Losh   lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
10320495ed39SKyle Evans                         msg);
10338e3e3a7aSWarner Losh   return 0;  /* return to Lua to abort */
10348e3e3a7aSWarner Losh }
10358e3e3a7aSWarner Losh 
10368e3e3a7aSWarner Losh 
10370495ed39SKyle Evans /*
10380495ed39SKyle Evans ** Warning functions:
10390495ed39SKyle Evans ** warnfoff: warning system is off
10400495ed39SKyle Evans ** warnfon: ready to start a new message
10410495ed39SKyle Evans ** warnfcont: previous message is to be continued
10420495ed39SKyle Evans */
10430495ed39SKyle Evans static void warnfoff (void *ud, const char *message, int tocont);
10440495ed39SKyle Evans static void warnfon (void *ud, const char *message, int tocont);
10450495ed39SKyle Evans static void warnfcont (void *ud, const char *message, int tocont);
10460495ed39SKyle Evans 
10470495ed39SKyle Evans 
10480495ed39SKyle Evans /*
10490495ed39SKyle Evans ** Check whether message is a control message. If so, execute the
10500495ed39SKyle Evans ** control or ignore it if unknown.
10510495ed39SKyle Evans */
checkcontrol(lua_State * L,const char * message,int tocont)10520495ed39SKyle Evans static int checkcontrol (lua_State *L, const char *message, int tocont) {
10530495ed39SKyle Evans   if (tocont || *(message++) != '@')  /* not a control message? */
10540495ed39SKyle Evans     return 0;
10550495ed39SKyle Evans   else {
10560495ed39SKyle Evans     if (strcmp(message, "off") == 0)
10570495ed39SKyle Evans       lua_setwarnf(L, warnfoff, L);  /* turn warnings off */
10580495ed39SKyle Evans     else if (strcmp(message, "on") == 0)
10590495ed39SKyle Evans       lua_setwarnf(L, warnfon, L);   /* turn warnings on */
10600495ed39SKyle Evans     return 1;  /* it was a control message */
10610495ed39SKyle Evans   }
10620495ed39SKyle Evans }
10630495ed39SKyle Evans 
10640495ed39SKyle Evans 
warnfoff(void * ud,const char * message,int tocont)10650495ed39SKyle Evans static void warnfoff (void *ud, const char *message, int tocont) {
10660495ed39SKyle Evans   checkcontrol((lua_State *)ud, message, tocont);
10670495ed39SKyle Evans }
10680495ed39SKyle Evans 
10690495ed39SKyle Evans 
10700495ed39SKyle Evans /*
10710495ed39SKyle Evans ** Writes the message and handle 'tocont', finishing the message
10720495ed39SKyle Evans ** if needed and setting the next warn function.
10730495ed39SKyle Evans */
warnfcont(void * ud,const char * message,int tocont)10740495ed39SKyle Evans static void warnfcont (void *ud, const char *message, int tocont) {
10750495ed39SKyle Evans   lua_State *L = (lua_State *)ud;
10760495ed39SKyle Evans   lua_writestringerror("%s", message);  /* write message */
10770495ed39SKyle Evans   if (tocont)  /* not the last part? */
10780495ed39SKyle Evans     lua_setwarnf(L, warnfcont, L);  /* to be continued */
10790495ed39SKyle Evans   else {  /* last part */
10800495ed39SKyle Evans     lua_writestringerror("%s", "\n");  /* finish message with end-of-line */
10810495ed39SKyle Evans     lua_setwarnf(L, warnfon, L);  /* next call is a new message */
10820495ed39SKyle Evans   }
10830495ed39SKyle Evans }
10840495ed39SKyle Evans 
10850495ed39SKyle Evans 
warnfon(void * ud,const char * message,int tocont)10860495ed39SKyle Evans static void warnfon (void *ud, const char *message, int tocont) {
10870495ed39SKyle Evans   if (checkcontrol((lua_State *)ud, message, tocont))  /* control message? */
10880495ed39SKyle Evans     return;  /* nothing else to be done */
10890495ed39SKyle Evans   lua_writestringerror("%s", "Lua warning: ");  /* start a new warning */
10900495ed39SKyle Evans   warnfcont(ud, message, tocont);  /* finish processing */
10910495ed39SKyle Evans }
10920495ed39SKyle Evans 
10930495ed39SKyle Evans 
luaL_newstate(void)10948e3e3a7aSWarner Losh LUALIB_API lua_State *luaL_newstate (void) {
10958e3e3a7aSWarner Losh   lua_State *L = lua_newstate(l_alloc, NULL);
10968c784bb8SWarner Losh   if (l_likely(L)) {
10970495ed39SKyle Evans     lua_atpanic(L, &panic);
10980495ed39SKyle Evans     lua_setwarnf(L, warnfoff, L);  /* default is warnings off */
10990495ed39SKyle Evans   }
11008e3e3a7aSWarner Losh   return L;
11018e3e3a7aSWarner Losh }
11028e3e3a7aSWarner Losh 
11038e3e3a7aSWarner Losh 
luaL_checkversion_(lua_State * L,lua_Number ver,size_t sz)11048e3e3a7aSWarner Losh LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
11050495ed39SKyle Evans   lua_Number v = lua_version(L);
11068e3e3a7aSWarner Losh   if (sz != LUAL_NUMSIZES)  /* check numeric types */
11078e3e3a7aSWarner Losh     luaL_error(L, "core and library have incompatible numeric types");
11080495ed39SKyle Evans   else if (v != ver)
11098e3e3a7aSWarner Losh     luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
11100495ed39SKyle Evans                   (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v);
11118e3e3a7aSWarner Losh }
11128e3e3a7aSWarner Losh 
1113