Lua 5.1.4: lauxlib.c


L0001    /*
L0002    ** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
L0003    ** Auxiliary functions for building Lua libraries
L0004    ** See Copyright Notice in lua.h
L0005    */
L0006    
L0007    
L0008    #include <ctype.h>
L0009    #include <errno.h>
L0010    #include <stdarg.h>
L0011    #include <stdio.h>
L0012    #include <stdlib.h>
L0013    #include <string.h>
L0014    
L0015    
L0016    /* This file uses only the official API of Lua.
L0017    ** Any function declared here could be written as an application function.
L0018    */
L0019    
L0020    #define lauxlib_c
L0021    #define LUA_LIB
L0022    
L0023    #include "lua.h"
L0024    
L0025    #include "lauxlib.h"
L0026    
L0027    
L0028    #define FREELIST_REF	0	/* free list of references */
L0029    
L0030    
L0031    /* convert a stack index to positive */
L0032    #define abs_index(L, i)		((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
L0033    					lua_gettop(L) + (i) + 1)
L0034    
L0035    
L0036    /*
L0037    ** {======================================================
L0038    ** Error-report functions
L0039    ** =======================================================
L0040    */
L0041    
L0042    
L0043    LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
manc:luaL_argerror
L0044 lua_Debug ar; L0045 if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ L0046 return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); L0047 lua_getinfo(L, "n", &ar); L0048 if (strcmp(ar.namewhat, "method") == 0) { L0049 narg--; /* do not count `self' */ L0050 if (narg == 0) /* error is in the self argument itself? */ L0051 return luaL_error(L, "calling " LUA_QS " on bad self (%s)", L0052 ar.name, extramsg); L0053 } L0054 if (ar.name == NULL) L0055 ar.name = "?"; L0056 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", L0057 narg, ar.name, extramsg); L0058 } L0059 L0060 L0061 LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
manc:luaL_typerror
L0062 const char *msg = lua_pushfstring(L, "%s expected, got %s", L0063 tname, luaL_typename(L, narg)); L0064 return luaL_argerror(L, narg, msg); L0065 } L0066 L0067 L0068 static void tag_error (lua_State *L, int narg, int tag) { L0069 luaL_typerror(L, narg, lua_typename(L, tag)); L0070 } L0071 L0072 L0073 LUALIB_API void luaL_where (lua_State *L, int level) {
manc:luaL_where
L0074 lua_Debug ar; L0075 if (lua_getstack(L, level, &ar)) { /* check function at level */ L0076 lua_getinfo(L, "Sl", &ar); /* get info about it */ L0077 if (ar.currentline > 0) { /* is there info? */ L0078 lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); L0079 return; L0080 } L0081 } L0082 lua_pushliteral(L, ""); /* else, no information available... */ L0083 } L0084 L0085 L0086 LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
manc:luaL_error
L0087 va_list argp; L0088 va_start(argp, fmt); L0089 luaL_where(L, 1); L0090 lua_pushvfstring(L, fmt, argp); L0091 va_end(argp); L0092 lua_concat(L, 2); L0093 return lua_error(L); L0094 } L0095 L0096 /* }====================================================== */ L0097 L0098 L0099 LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
manc:luaL_checkoption
L0100 const char *const lst[]) { L0101 const char *name = (def) ? luaL_optstring(L, narg, def) : L0102 luaL_checkstring(L, narg); L0103 int i; L0104 for (i=0; lst[i]; i++) L0105 if (strcmp(lst[i], name) == 0) L0106 return i; L0107 return luaL_argerror(L, narg, L0108 lua_pushfstring(L, "invalid option " LUA_QS, name)); L0109 } L0110 L0111 L0112 LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
manc:luaL_newmetatable
L0113 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ L0114 if (!lua_isnil(L, -1)) /* name already in use? */ L0115 return 0; /* leave previous value on top, but return 0 */ L0116 lua_pop(L, 1); L0117 lua_newtable(L); /* create metatable */ L0118 lua_pushvalue(L, -1); L0119 lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ L0120 return 1; L0121 } L0122 L0123 L0124 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
manc:luaL_checkudata
L0125 void *p = lua_touserdata(L, ud); L0126 if (p != NULL) { /* value is a userdata? */ L0127 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ L0128 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ L0129 if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ L0130 lua_pop(L, 2); /* remove both metatables */ L0131 return p; L0132 } L0133 } L0134 } L0135 luaL_typerror(L, ud, tname); /* else error */ L0136 return NULL; /* to avoid warnings */ L0137 } L0138 L0139 L0140 LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
manc:luaL_checkstack
L0141 if (!lua_checkstack(L, space)) L0142 luaL_error(L, "stack overflow (%s)", mes); L0143 } L0144 L0145 L0146 LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
manc:luaL_checktype
L0147 if (lua_type(L, narg) != t) L0148 tag_error(L, narg, t); L0149 } L0150 L0151 L0152 LUALIB_API void luaL_checkany (lua_State *L, int narg) {
manc:luaL_checkany
L0153 if (lua_type(L, narg) == LUA_TNONE) L0154 luaL_argerror(L, narg, "value expected"); L0155 } L0156 L0157 L0158 LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
manc:luaL_checklstring
L0159 const char *s = lua_tolstring(L, narg, len); L0160 if (!s) tag_error(L, narg, LUA_TSTRING); L0161 return s; L0162 } L0163 L0164 L0165 LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
manc:luaL_optlstring
L0166 const char *def, size_t *len) { L0167 if (lua_isnoneornil(L, narg)) { L0168 if (len) L0169 *len = (def ? strlen(def) : 0); L0170 return def; L0171 } L0172 else return luaL_checklstring(L, narg, len); L0173 } L0174 L0175 L0176 LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
manc:luaL_checknumber
L0177 lua_Number d = lua_tonumber(L, narg); L0178 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ L0179 tag_error(L, narg, LUA_TNUMBER); L0180 return d; L0181 } L0182 L0183 L0184 LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
manc:luaL_optnumber
L0185 return luaL_opt(L, luaL_checknumber, narg, def); L0186 } L0187 L0188 L0189 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
manc:luaL_checkinteger
L0190 lua_Integer d = lua_tointeger(L, narg); L0191 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ L0192 tag_error(L, narg, LUA_TNUMBER); L0193 return d; L0194 } L0195 L0196 L0197 LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
manc:luaL_optinteger
L0198 lua_Integer def) { L0199 return luaL_opt(L, luaL_checkinteger, narg, def); L0200 } L0201 L0202 L0203 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
manc:luaL_getmetafield
L0204 if (!lua_getmetatable(L, obj)) /* no metatable? */ L0205 return 0; L0206 lua_pushstring(L, event); L0207 lua_rawget(L, -2); L0208 if (lua_isnil(L, -1)) { L0209 lua_pop(L, 2); /* remove metatable and metafield */ L0210 return 0; L0211 } L0212 else { L0213 lua_remove(L, -2); /* remove only metatable */ L0214 return 1; L0215 } L0216 } L0217 L0218 L0219 LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
manc:luaL_callmeta
L0220 obj = abs_index(L, obj); L0221 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ L0222 return 0; L0223 lua_pushvalue(L, obj); L0224 lua_call(L, 1, 1); L0225 return 1; L0226 } L0227 L0228 L0229 LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
manc:luaL_register
L0230 const luaL_Reg *l) { L0231 luaI_openlib(L, libname, l, 0); L0232 } L0233 L0234 L0235 static int libsize (const luaL_Reg *l) { L0236 int size = 0; L0237 for (; l->name; l++) size++; L0238 return size; L0239 } L0240 L0241 L0242 LUALIB_API void luaI_openlib (lua_State *L, const char *libname, L0243 const luaL_Reg *l, int nup) { L0244 if (libname) { L0245 int size = libsize(l); L0246 /* check whether lib already exists */ L0247 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); L0248 lua_getfield(L, -1, libname); /* get _LOADED[libname] */ L0249 if (!lua_istable(L, -1)) { /* not found? */ L0250 lua_pop(L, 1); /* remove previous result */ L0251 /* try global variable (and create one if it does not exist) */ L0252 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) L0253 luaL_error(L, "name conflict for module " LUA_QS, libname); L0254 lua_pushvalue(L, -1); L0255 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ L0256 } L0257 lua_remove(L, -2); /* remove _LOADED table */ L0258 lua_insert(L, -(nup+1)); /* move library table to below upvalues */ L0259 } L0260 for (; l->name; l++) { L0261 int i; L0262 for (i=0; i<nup; i++) /* copy upvalues to the top */ L0263 lua_pushvalue(L, -nup); L0264 lua_pushcclosure(L, l->func, nup); L0265 lua_setfield(L, -(nup+2), l->name); L0266 } L0267 lua_pop(L, nup); /* remove upvalues */ L0268 } L0269 L0270 L0271 L0272 /* L0273 ** {====================================================== L0274 ** getn-setn: size for arrays L0275 ** ======================================================= L0276 */ L0277 L0278 #if defined(LUA_COMPAT_GETN) L0279 L0280 static int checkint (lua_State *L, int topop) { L0281 int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; L0282 lua_pop(L, topop); L0283 return n; L0284 } L0285 L0286 L0287 static void getsizes (lua_State *L) { L0288 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); L0289 if (lua_isnil(L, -1)) { /* no `size' table? */ L0290 lua_pop(L, 1); /* remove nil */ L0291 lua_newtable(L); /* create it */ L0292 lua_pushvalue(L, -1); /* `size' will be its own metatable */ L0293 lua_setmetatable(L, -2); L0294 lua_pushliteral(L, "kv"); L0295 lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ L0296 lua_pushvalue(L, -1); L0297 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ L0298 } L0299 } L0300 L0301 L0302 LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
manc:luaL_setn
L0303 t = abs_index(L, t); L0304 lua_pushliteral(L, "n"); L0305 lua_rawget(L, t); L0306 if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ L0307 lua_pushliteral(L, "n"); /* use it */ L0308 lua_pushinteger(L, n); L0309 lua_rawset(L, t); L0310 } L0311 else { /* use `sizes' */ L0312 getsizes(L); L0313 lua_pushvalue(L, t); L0314 lua_pushinteger(L, n); L0315 lua_rawset(L, -3); /* sizes[t] = n */ L0316 lua_pop(L, 1); /* remove `sizes' */ L0317 } L0318 } L0319 L0320 L0321 LUALIB_API int luaL_getn (lua_State *L, int t) {
manc:luaL_getn
L0322 int n; L0323 t = abs_index(L, t); L0324 lua_pushliteral(L, "n"); /* try t.n */ L0325 lua_rawget(L, t); L0326 if ((n = checkint(L, 1)) >= 0) return n; L0327 getsizes(L); /* else try sizes[t] */ L0328 lua_pushvalue(L, t); L0329 lua_rawget(L, -2); L0330 if ((n = checkint(L, 2)) >= 0) return n; L0331 return (int)lua_objlen(L, t); L0332 } L0333 L0334 #endif L0335 L0336 /* }====================================================== */ L0337 L0338 L0339 L0340 LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
manc:luaL_gsub
L0341 const char *r) { L0342 const char *wild; L0343 size_t l = strlen(p); L0344 luaL_Buffer b; L0345 luaL_buffinit(L, &b); L0346 while ((wild = strstr(s, p)) != NULL) { L0347 luaL_addlstring(&b, s, wild - s); /* push prefix */ L0348 luaL_addstring(&b, r); /* push replacement in place of pattern */ L0349 s = wild + l; /* continue after `p' */ L0350 } L0351 luaL_addstring(&b, s); /* push last suffix */ L0352 luaL_pushresult(&b); L0353 return lua_tostring(L, -1); L0354 } L0355 L0356 L0357 LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
manc:luaL_findtable
L0358 const char *fname, int szhint) { L0359 const char *e; L0360 lua_pushvalue(L, idx); L0361 do { L0362 e = strchr(fname, '.'); L0363 if (e == NULL) e = fname + strlen(fname); L0364 lua_pushlstring(L, fname, e - fname); L0365 lua_rawget(L, -2); L0366 if (lua_isnil(L, -1)) { /* no such field? */ L0367 lua_pop(L, 1); /* remove this nil */ L0368 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ L0369 lua_pushlstring(L, fname, e - fname); L0370 lua_pushvalue(L, -2); L0371 lua_settable(L, -4); /* set new table into field */ L0372 } L0373 else if (!lua_istable(L, -1)) { /* field has a non-table value? */ L0374 lua_pop(L, 2); /* remove table and value */ L0375 return fname; /* return problematic part of the name */ L0376 } L0377 lua_remove(L, -2); /* remove previous table */ L0378 fname = e + 1; L0379 } while (*e == '.'); L0380 return NULL; L0381 } L0382 L0383 L0384 L0385 /* L0386 ** {====================================================== L0387 ** Generic Buffer manipulation L0388 ** ======================================================= L0389 */ L0390 L0391 L0392 #define bufflen(B) ((B)->p - (B)->buffer) L0393 #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) L0394 L0395 #define LIMIT (LUA_MINSTACK/2) L0396 L0397 L0398 static int emptybuffer (luaL_Buffer *B) { L0399 size_t l = bufflen(B); L0400 if (l == 0) return 0; /* put nothing on stack */ L0401 else { L0402 lua_pushlstring(B->L, B->buffer, l); L0403 B->p = B->buffer; L0404 B->lvl++; L0405 return 1; L0406 } L0407 } L0408 L0409 L0410 static void adjuststack (luaL_Buffer *B) { L0411 if (B->lvl > 1) { L0412 lua_State *L = B->L; L0413 int toget = 1; /* number of levels to concat */ L0414 size_t toplen = lua_strlen(L, -1); L0415 do { L0416 size_t l = lua_strlen(L, -(toget+1)); L0417 if (B->lvl - toget + 1 >= LIMIT || toplen > l) { L0418 toplen += l; L0419 toget++; L0420 } L0421 else break; L0422 } while (toget < B->lvl); L0423 lua_concat(L, toget); L0424 B->lvl = B->lvl - toget + 1; L0425 } L0426 } L0427 L0428 L0429 LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { L0430 if (emptybuffer(B)) L0431 adjuststack(B); L0432 return B->buffer; L0433 } L0434 L0435 L0436 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { L0437 while (l--) L0438 luaL_addchar(B, *s++); L0439 } L0440 L0441 L0442 LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { L0443 luaL_addlstring(B, s, strlen(s)); L0444 } L0445 L0446 L0447 LUALIB_API void luaL_pushresult (luaL_Buffer *B) { L0448 emptybuffer(B); L0449 lua_concat(B->L, B->lvl); L0450 B->lvl = 1; L0451 } L0452 L0453 L0454 LUALIB_API void luaL_addvalue (luaL_Buffer *B) { L0455 lua_State *L = B->L; L0456 size_t vl; L0457 const char *s = lua_tolstring(L, -1, &vl); L0458 if (vl <= bufffree(B)) { /* fit into buffer? */ L0459 memcpy(B->p, s, vl); /* put it there */ L0460 B->p += vl; L0461 lua_pop(L, 1); /* remove from stack */ L0462 } L0463 else { L0464 if (emptybuffer(B)) L0465 lua_insert(L, -2); /* put buffer before new value */ L0466 B->lvl++; /* add new value into B stack */ L0467 adjuststack(B); L0468 } L0469 } L0470 L0471 L0472 LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
manc:luaL_buffinit
L0473 B->L = L; L0474 B->p = B->buffer; L0475 B->lvl = 0; L0476 } L0477 L0478 /* }====================================================== */ L0479 L0480 L0481 LUALIB_API int luaL_ref (lua_State *L, int t) {
manc:luaL_ref
L0482 int ref; L0483 t = abs_index(L, t); L0484 if (lua_isnil(L, -1)) { L0485 lua_pop(L, 1); /* remove from stack */ L0486 return LUA_REFNIL; /* `nil' has a unique fixed reference */ L0487 } L0488 lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ L0489 ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ L0490 lua_pop(L, 1); /* remove it from stack */ L0491 if (ref != 0) { /* any free element? */ L0492 lua_rawgeti(L, t, ref); /* remove it from list */ L0493 lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ L0494 } L0495 else { /* no free elements */ L0496 ref = (int)lua_objlen(L, t); L0497 ref++; /* create new reference */ L0498 } L0499 lua_rawseti(L, t, ref); L0500 return ref; L0501 } L0502 L0503 L0504 LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
manc:luaL_unref
L0505 if (ref >= 0) { L0506 t = abs_index(L, t); L0507 lua_rawgeti(L, t, FREELIST_REF); L0508 lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ L0509 lua_pushinteger(L, ref); L0510 lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ L0511 } L0512 } L0513 L0514 L0515 L0516 /* L0517 ** {====================================================== L0518 ** Load functions L0519 ** ======================================================= L0520 */ L0521 L0522 typedef struct LoadF { L0523 int extraline; L0524 FILE *f; L0525 char buff[LUAL_BUFFERSIZE]; L0526 } LoadF; L0527 L0528 L0529 static const char *getF (lua_State *L, void *ud, size_t *size) { L0530 LoadF *lf = (LoadF *)ud; L0531 (void)L; L0532 if (lf->extraline) { L0533 lf->extraline = 0; L0534 *size = 1; L0535 return "\n"; L0536 } L0537 if (feof(lf->f)) return NULL; L0538 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); L0539 return (*size > 0) ? lf->buff : NULL; L0540 } L0541 L0542 L0543 static int errfile (lua_State *L, const char *what, int fnameindex) { L0544 const char *serr = strerror(errno); L0545 const char *filename = lua_tostring(L, fnameindex) + 1; L0546 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); L0547 lua_remove(L, fnameindex); L0548 return LUA_ERRFILE; L0549 } L0550 L0551 L0552 LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
manc:luaL_loadfile
L0553 LoadF lf; L0554 int status, readstatus; L0555 int c; L0556 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ L0557 lf.extraline = 0; L0558 if (filename == NULL) { L0559 lua_pushliteral(L, "=stdin"); L0560 lf.f = stdin; L0561 } L0562 else { L0563 lua_pushfstring(L, "@%s", filename); L0564 lf.f = fopen(filename, "r"); L0565 if (lf.f == NULL) return errfile(L, "open", fnameindex); L0566 } L0567 c = getc(lf.f); L0568 if (c == '#') { /* Unix exec. file? */ L0569 lf.extraline = 1; L0570 while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ L0571 if (c == '\n') c = getc(lf.f); L0572 } L0573 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ L0574 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ L0575 if (lf.f == NULL) return errfile(L, "reopen", fnameindex); L0576 /* skip eventual `#!...' */ L0577 while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; L0578 lf.extraline = 0; L0579 } L0580 ungetc(c, lf.f); L0581 status = lua_load(L, getF, &lf, lua_tostring(L, -1)); L0582 readstatus = ferror(lf.f); L0583 if (filename) fclose(lf.f); /* close file (even in case of errors) */ L0584 if (readstatus) { L0585 lua_settop(L, fnameindex); /* ignore results from `lua_load' */ L0586 return errfile(L, "read", fnameindex); L0587 } L0588 lua_remove(L, fnameindex); L0589 return status; L0590 } L0591 L0592 L0593 typedef struct LoadS { L0594 const char *s; L0595 size_t size; L0596 } LoadS; L0597 L0598 L0599 static const char *getS (lua_State *L, void *ud, size_t *size) { L0600 LoadS *ls = (LoadS *)ud; L0601 (void)L; L0602 if (ls->size == 0) return NULL; L0603 *size = ls->size; L0604 ls->size = 0; L0605 return ls->s; L0606 } L0607 L0608 L0609 LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
manc:luaL_loadbuffer
L0610 const char *name) { L0611 LoadS ls; L0612 ls.s = buff; L0613 ls.size = size; L0614 return lua_load(L, getS, &ls, name); L0615 } L0616 L0617 L0618 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
manc:luaL_loadstring
L0619 return luaL_loadbuffer(L, s, strlen(s), s); L0620 } L0621 L0622 L0623 L0624 /* }====================================================== */ L0625 L0626 L0627 static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { L0628 (void)ud; L0629 (void)osize; L0630 if (nsize == 0) { L0631 free(ptr); L0632 return NULL; L0633 } L0634 else L0635 return realloc(ptr, nsize); L0636 } L0637 L0638 L0639 static int panic (lua_State *L) { L0640 (void)L; /* to avoid warnings */ L0641 fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", L0642 lua_tostring(L, -1)); L0643 return 0; L0644 } L0645 L0646 L0647 LUALIB_API lua_State *luaL_newstate (void) {
manc:luaL_newstate
L0648 lua_State *L = lua_newstate(l_alloc, NULL); L0649 if (L) lua_atpanic(L, &panic); L0650 return L; L0651 } L0652

Generated by pretty.lua