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