30#if !defined(MAX_SIZET)
32#define MAX_SIZET ((size_t)(~(size_t)0))
52static int findfield (
lua_State *L,
int objidx,
int level) {
53 if (level == 0 || !lua_istable(L, -1))
56 while (lua_next(L, -2)) {
57 if (lua_type(L, -2) == LUA_TSTRING) {
58 if (lua_rawequal(L, objidx, -1)) {
62 else if (findfield(L, objidx, level - 1)) {
64 lua_pushliteral(L,
".");
80 int top = lua_gettop(L);
81 lua_getinfo(L,
"f", ar);
82 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
83 if (findfield(L, top + 1, 2)) {
84 const char *name = lua_tostring(L, -1);
85 if (strncmp(name, LUA_GNAME
".", 3) == 0) {
86 lua_pushstring(L, name + 3);
89 lua_copy(L, -1, top + 1);
90 lua_settop(L, top + 1);
101 if (pushglobalfuncname(L, ar)) {
102 lua_pushfstring(L,
"function '%s'", lua_tostring(L, -1));
105 else if (*ar->namewhat !=
'\0')
106 lua_pushfstring(L,
"%s '%s'", ar->namewhat, ar->name);
107 else if (*ar->what ==
'm')
108 lua_pushliteral(L,
"main chunk");
109 else if (*ar->what !=
'C')
110 lua_pushfstring(L,
"function <%s:%d>", ar->short_src, ar->linedefined);
112 lua_pushliteral(L,
"?");
120 while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
124 if (lua_getstack(L, m, &ar)) li = m + 1;
132 const char *msg,
int level) {
135 int last = lastlevel(L1);
136 int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
137 luaL_buffinit(L, &b);
139 luaL_addstring(&b, msg);
140 luaL_addchar(&b,
'\n');
142 luaL_addstring(&b,
"stack traceback:");
143 while (lua_getstack(L1, level++, &ar)) {
144 if (limit2show-- == 0) {
145 int n = last - level - LEVELS2 + 1;
146 lua_pushfstring(L,
"\n\t...\t(skipping %d levels)", n);
151 lua_getinfo(L1,
"Slnt", &ar);
152 if (ar.currentline <= 0)
153 lua_pushfstring(L,
"\n\t%s: in ", ar.short_src);
155 lua_pushfstring(L,
"\n\t%s:%d: in ", ar.short_src, ar.currentline);
157 pushfuncname(L, &ar);
160 luaL_addstring(&b,
"\n\t(...tail calls...)");
175LUALIB_API
int luaL_argerror (
lua_State *L,
int arg,
const char *extramsg) {
177 if (!lua_getstack(L, 0, &ar))
178 return luaL_error(L,
"bad argument #%d (%s)", arg, extramsg);
179 lua_getinfo(L,
"n", &ar);
180 if (strcmp(ar.namewhat,
"method") == 0) {
183 return luaL_error(L,
"calling '%s' on bad self (%s)",
187 ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) :
"?";
188 return luaL_error(L,
"bad argument #%d to '%s' (%s)",
189 arg, ar.name, extramsg);
193LUALIB_API
int luaL_typeerror (
lua_State *L,
int arg,
const char *tname) {
196 if (luaL_getmetafield(L, arg,
"__name") == LUA_TSTRING)
197 typearg = lua_tostring(L, -1);
198 else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
199 typearg =
"light userdata";
201 typearg = luaL_typename(L, arg);
202 msg = lua_pushfstring(L,
"%s expected, got %s", tname, typearg);
203 return luaL_argerror(L, arg, msg);
207static void tag_error (
lua_State *L,
int arg,
int tag) {
208 luaL_typeerror(L, arg, lua_typename(L, tag));
216LUALIB_API
void luaL_where (
lua_State *L,
int level) {
218 if (lua_getstack(L, level, &ar)) {
219 lua_getinfo(L,
"Sl", &ar);
220 if (ar.currentline > 0) {
221 lua_pushfstring(L,
"%s:%d: ", ar.short_src, ar.currentline);
225 lua_pushfstring(L,
"");
234LUALIB_API
int luaL_error (
lua_State *L,
const char *fmt, ...) {
238 lua_pushvfstring(L, fmt, argp);
245LUALIB_API
int luaL_fileresult (
lua_State *L,
int stat,
const char *fname) {
248 lua_pushboolean(L, 1);
254 lua_pushfstring(L,
"%s: %s", fname, strerror(en));
256 lua_pushstring(L, strerror(en));
257 lua_pushinteger(L, en);
263#if !defined(l_inspectstat)
265#if defined(LUA_USE_POSIX)
272#define l_inspectstat(stat,what) \
273 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
274 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
278#define l_inspectstat(stat,what)
285LUALIB_API
int luaL_execresult (
lua_State *L,
int stat) {
286 if (stat != 0 && errno != 0)
287 return luaL_fileresult(L, 0, NULL);
289 const char *what =
"exit";
290 l_inspectstat(stat, what);
291 if (*what ==
'e' && stat == 0)
292 lua_pushboolean(L, 1);
295 lua_pushstring(L, what);
296 lua_pushinteger(L, stat);
311LUALIB_API
int luaL_newmetatable (
lua_State *L,
const char *tname) {
312 if (luaL_getmetatable(L, tname) != LUA_TNIL)
315 lua_createtable(L, 0, 2);
316 lua_pushstring(L, tname);
317 lua_setfield(L, -2,
"__name");
318 lua_pushvalue(L, -1);
319 lua_setfield(L, LUA_REGISTRYINDEX, tname);
324LUALIB_API
void luaL_setmetatable (
lua_State *L,
const char *tname) {
325 luaL_getmetatable(L, tname);
326 lua_setmetatable(L, -2);
330LUALIB_API
void *luaL_testudata (
lua_State *L,
int ud,
const char *tname) {
331 void *p = lua_touserdata(L, ud);
333 if (lua_getmetatable(L, ud)) {
334 luaL_getmetatable(L, tname);
335 if (!lua_rawequal(L, -1, -2))
345LUALIB_API
void *luaL_checkudata (
lua_State *L,
int ud,
const char *tname) {
346 void *p = luaL_testudata(L, ud, tname);
347 luaL_argexpected(L, p != NULL, ud, tname);
360LUALIB_API
int luaL_checkoption (
lua_State *L,
int arg,
const char *def,
361 const char *
const lst[]) {
362 const char *name = (def) ? luaL_optstring(L, arg, def) :
363 luaL_checkstring(L, arg);
365 for (i=0; lst[i]; i++)
366 if (strcmp(lst[i], name) == 0)
368 return luaL_argerror(L, arg,
369 lua_pushfstring(L,
"invalid option '%s'", name));
380LUALIB_API
void luaL_checkstack (
lua_State *L,
int space,
const char *msg) {
381 if (l_unlikely(!lua_checkstack(L, space))) {
383 luaL_error(L,
"stack overflow (%s)", msg);
385 luaL_error(L,
"stack overflow");
390LUALIB_API
void luaL_checktype (
lua_State *L,
int arg,
int t) {
391 if (l_unlikely(lua_type(L, arg) != t))
392 tag_error(L, arg, t);
396LUALIB_API
void luaL_checkany (
lua_State *L,
int arg) {
397 if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
398 luaL_argerror(L, arg,
"value expected");
402LUALIB_API
const char *luaL_checklstring (
lua_State *L,
int arg,
size_t *len) {
403 const char *s = lua_tolstring(L, arg, len);
404 if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
409LUALIB_API
const char *luaL_optlstring (
lua_State *L,
int arg,
410 const char *def,
size_t *len) {
411 if (lua_isnoneornil(L, arg)) {
413 *len = (def ? strlen(def) : 0);
416 else return luaL_checklstring(L, arg, len);
420LUALIB_API lua_Number luaL_checknumber (
lua_State *L,
int arg) {
422 lua_Number d = lua_tonumberx(L, arg, &isnum);
423 if (l_unlikely(!isnum))
424 tag_error(L, arg, LUA_TNUMBER);
429LUALIB_API lua_Number luaL_optnumber (
lua_State *L,
int arg, lua_Number def) {
430 return luaL_opt(L, luaL_checknumber, arg, def);
434static void interror (
lua_State *L,
int arg) {
435 if (lua_isnumber(L, arg))
436 luaL_argerror(L, arg,
"number has no integer representation");
438 tag_error(L, arg, LUA_TNUMBER);
442LUALIB_API lua_Integer luaL_checkinteger (
lua_State *L,
int arg) {
444 lua_Integer d = lua_tointegerx(L, arg, &isnum);
445 if (l_unlikely(!isnum)) {
452LUALIB_API lua_Integer luaL_optinteger (
lua_State *L,
int arg,
454 return luaL_opt(L, luaL_checkinteger, arg, def);
473static void *resizebox (
lua_State *L,
int idx,
size_t newsize) {
475 lua_Alloc allocf = lua_getallocf(L, &ud);
476 UBox *box = (
UBox *)lua_touserdata(L, idx);
477 void *temp = allocf(ud, box->box, box->bsize, newsize);
478 if (l_unlikely(temp == NULL && newsize > 0)) {
479 lua_pushliteral(L,
"not enough memory");
483 box->bsize = newsize;
502 UBox *box = (
UBox *)lua_newuserdatauv(L,
sizeof(
UBox), 0);
505 if (luaL_newmetatable(L,
"_UBOX*"))
506 luaL_setfuncs(L, boxmt, 0);
507 lua_setmetatable(L, -2);
515#define buffonstack(B) ((B)->b != (B)->init.b)
522#define checkbufferlevel(B,idx) \
523 lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \
524 : lua_touserdata(B->L, idx) == (void*)B)
532static size_t newbuffsize (
luaL_Buffer *B,
size_t sz) {
533 size_t newsize = (B->size / 2) * 3;
534 if (l_unlikely(MAX_SIZET - sz < B->n))
535 return luaL_error(B->L,
"buffer too large");
536 if (newsize < B->n + sz)
547static char *prepbuffsize (
luaL_Buffer *B,
size_t sz,
int boxidx) {
548 checkbufferlevel(B, boxidx);
549 if (B->size - B->n >= sz)
554 size_t newsize = newbuffsize(B, sz);
557 newbuff = (
char *)resizebox(L, boxidx, newsize);
559 lua_remove(L, boxidx);
561 lua_insert(L, boxidx);
562 lua_toclose(L, boxidx);
563 newbuff = (
char *)resizebox(L, boxidx, newsize);
564 memcpy(newbuff, B->b, B->n *
sizeof(
char));
568 return newbuff + B->n;
575LUALIB_API
char *luaL_prepbuffsize (
luaL_Buffer *B,
size_t sz) {
576 return prepbuffsize(B, sz, -1);
580LUALIB_API
void luaL_addlstring (
luaL_Buffer *B,
const char *s,
size_t l) {
582 char *b = prepbuffsize(B, l, -1);
583 memcpy(b, s, l *
sizeof(
char));
589LUALIB_API
void luaL_addstring (
luaL_Buffer *B,
const char *s) {
590 luaL_addlstring(B, s, strlen(s));
596 checkbufferlevel(B, -1);
597 lua_pushlstring(L, B->b, B->n);
599 lua_closeslot(L, -2);
604LUALIB_API
void luaL_pushresultsize (
luaL_Buffer *B,
size_t sz) {
622 const char *s = lua_tolstring(L, -1, &len);
623 char *b = prepbuffsize(B, len, -2);
624 memcpy(b, s, len *
sizeof(
char));
625 luaL_addsize(B, len);
634 B->size = LUAL_BUFFERSIZE;
635 lua_pushlightuserdata(L, (
void*)B);
641 return prepbuffsize(B, sz, -1);
654#define freelist (LUA_RIDX_LAST + 1)
661LUALIB_API
int luaL_ref (
lua_State *L,
int t) {
663 if (lua_isnil(L, -1)) {
667 t = lua_absindex(L, t);
668 if (lua_rawgeti(L, t, freelist) == LUA_TNIL) {
670 lua_pushinteger(L, 0);
671 lua_rawseti(L, t, freelist);
674 lua_assert(lua_isinteger(L, -1));
675 ref = (int)lua_tointeger(L, -1);
679 lua_rawgeti(L, t, ref);
680 lua_rawseti(L, t, freelist);
683 ref = (int)lua_rawlen(L, t) + 1;
684 lua_rawseti(L, t, ref);
689LUALIB_API
void luaL_unref (
lua_State *L,
int t,
int ref) {
691 t = lua_absindex(L, t);
692 lua_rawgeti(L, t, freelist);
693 lua_assert(lua_isinteger(L, -1));
694 lua_rawseti(L, t, ref);
695 lua_pushinteger(L, ref);
696 lua_rawseti(L, t, freelist);
716static const char *getF (
lua_State *L,
void *ud,
size_t *size) {
727 if (feof(lf->f))
return NULL;
728 *size = fread(lf->buff, 1,
sizeof(lf->buff), lf->f);
734static int errfile (
lua_State *L,
const char *what,
int fnameindex) {
735 const char *serr = strerror(errno);
736 const char *filename = lua_tostring(L, fnameindex) + 1;
737 lua_pushfstring(L,
"cannot %s %s: %s", what, filename, serr);
738 lua_remove(L, fnameindex);
749static int skipBOM (FILE *f) {
751 if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF)
765static int skipcomment (FILE *f,
int *cp) {
766 int c = *cp = skipBOM(f);
770 }
while (c != EOF && c !=
'\n');
778LUALIB_API
int luaL_loadfilex (
lua_State *L,
const char *filename,
781 int status, readstatus;
783 int fnameindex = lua_gettop(L) + 1;
784 if (filename == NULL) {
785 lua_pushliteral(L,
"=stdin");
789 lua_pushfstring(L,
"@%s", filename);
790 lf.f = fopen(filename,
"r");
791 if (lf.f == NULL)
return errfile(L,
"open", fnameindex);
794 if (skipcomment(lf.f, &c))
795 lf.buff[lf.n++] =
'\n';
796 if (c == LUA_SIGNATURE[0]) {
799 lf.f = freopen(filename,
"rb", lf.f);
800 if (lf.f == NULL)
return errfile(L,
"reopen", fnameindex);
801 skipcomment(lf.f, &c);
806 status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
807 readstatus = ferror(lf.f);
808 if (filename) fclose(lf.f);
810 lua_settop(L, fnameindex);
811 return errfile(L,
"read", fnameindex);
813 lua_remove(L, fnameindex);
824static const char *getS (
lua_State *L,
void *ud,
size_t *size) {
827 if (ls->size == 0)
return NULL;
834LUALIB_API
int luaL_loadbufferx (
lua_State *L,
const char *buff,
size_t size,
835 const char *name,
const char *mode) {
839 return lua_load(L, getS, &ls, name, mode);
843LUALIB_API
int luaL_loadstring (
lua_State *L,
const char *s) {
844 return luaL_loadbuffer(L, s, strlen(s), s);
851LUALIB_API
int luaL_getmetafield (
lua_State *L,
int obj,
const char *event) {
852 if (!lua_getmetatable(L, obj))
856 lua_pushstring(L, event);
857 tt = lua_rawget(L, -2);
867LUALIB_API
int luaL_callmeta (
lua_State *L,
int obj,
const char *event) {
868 obj = lua_absindex(L, obj);
869 if (luaL_getmetafield(L, obj, event) == LUA_TNIL)
871 lua_pushvalue(L, obj);
877LUALIB_API lua_Integer luaL_len (
lua_State *L,
int idx) {
881 l = lua_tointegerx(L, -1, &isnum);
882 if (l_unlikely(!isnum))
883 luaL_error(L,
"object length is not an integer");
889LUALIB_API
const char *luaL_tolstring (
lua_State *L,
int idx,
size_t *len) {
890 idx = lua_absindex(L,idx);
891 if (luaL_callmeta(L, idx,
"__tostring")) {
892 if (!lua_isstring(L, -1))
893 luaL_error(L,
"'__tostring' must return a string");
896 switch (lua_type(L, idx)) {
898 if (lua_isinteger(L, idx))
899 lua_pushfstring(L,
"%I", (LUAI_UACINT)lua_tointeger(L, idx));
901 lua_pushfstring(L,
"%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
905 lua_pushvalue(L, idx);
908 lua_pushstring(L, (lua_toboolean(L, idx) ?
"true" :
"false"));
911 lua_pushliteral(L,
"nil");
914 int tt = luaL_getmetafield(L, idx,
"__name");
915 const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
916 luaL_typename(L, idx);
917 lua_pushfstring(L,
"%s: %p", kind, lua_topointer(L, idx));
924 return lua_tolstring(L, -1, len);
934 luaL_checkstack(L, nup,
"too many upvalues");
935 for (; l->name != NULL; l++) {
937 lua_pushboolean(L, 0);
940 for (i = 0; i < nup; i++)
941 lua_pushvalue(L, -nup);
942 lua_pushcclosure(L, l->func, nup);
944 lua_setfield(L, -(nup + 2), l->name);
954LUALIB_API
int luaL_getsubtable (
lua_State *L,
int idx,
const char *fname) {
955 if (lua_getfield(L, idx, fname) == LUA_TTABLE)
959 idx = lua_absindex(L, idx);
961 lua_pushvalue(L, -1);
962 lua_setfield(L, idx, fname);
974LUALIB_API
void luaL_requiref (
lua_State *L,
const char *modname,
975 lua_CFunction openf,
int glb) {
976 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
977 lua_getfield(L, -1, modname);
978 if (!lua_toboolean(L, -1)) {
980 lua_pushcfunction(L, openf);
981 lua_pushstring(L, modname);
983 lua_pushvalue(L, -1);
984 lua_setfield(L, -3, modname);
988 lua_pushvalue(L, -1);
989 lua_setglobal(L, modname);
994LUALIB_API
void luaL_addgsub (
luaL_Buffer *b,
const char *s,
995 const char *p,
const char *r) {
997 size_t l = strlen(p);
998 while ((wild = strstr(s, p)) != NULL) {
999 luaL_addlstring(b, s, wild - s);
1000 luaL_addstring(b, r);
1003 luaL_addstring(b, s);
1007LUALIB_API
const char *luaL_gsub (
lua_State *L,
const char *s,
1008 const char *p,
const char *r) {
1010 luaL_buffinit(L, &b);
1011 luaL_addgsub(&b, s, p, r);
1012 luaL_pushresult(&b);
1013 return lua_tostring(L, -1);
1017static void *l_alloc (
void *ud,
void *ptr,
size_t osize,
size_t nsize) {
1018 (void)ud; (void)osize;
1024 return realloc(ptr, nsize);
1029 const char *msg = lua_tostring(L, -1);
1030 if (msg == NULL) msg =
"error object is not a string";
1031 lua_writestringerror(
"PANIC: unprotected error in call to Lua API (%s)\n",
1043static void warnfoff (
void *ud,
const char *message,
int tocont);
1044static void warnfon (
void *ud,
const char *message,
int tocont);
1045static void warnfcont (
void *ud,
const char *message,
int tocont);
1052static int checkcontrol (
lua_State *L,
const char *message,
int tocont) {
1053 if (tocont || *(message++) !=
'@')
1056 if (strcmp(message,
"off") == 0)
1057 lua_setwarnf(L, warnfoff, L);
1058 else if (strcmp(message,
"on") == 0)
1059 lua_setwarnf(L, warnfon, L);
1065static void warnfoff (
void *ud,
const char *message,
int tocont) {
1066 checkcontrol((
lua_State *)ud, message, tocont);
1074static void warnfcont (
void *ud,
const char *message,
int tocont) {
1076 lua_writestringerror(
"%s", message);
1078 lua_setwarnf(L, warnfcont, L);
1080 lua_writestringerror(
"%s",
"\n");
1081 lua_setwarnf(L, warnfon, L);
1086static void warnfon (
void *ud,
const char *message,
int tocont) {
1087 if (checkcontrol((
lua_State *)ud, message, tocont))
1089 lua_writestringerror(
"%s",
"Lua warning: ");
1090 warnfcont(ud, message, tocont);
1094LUALIB_API
lua_State *luaL_newstate (
void) {
1095 lua_State *L = lua_newstate(l_alloc, NULL);
1097 lua_atpanic(L, &panic);
1098 lua_setwarnf(L, warnfoff, L);
1104LUALIB_API
void luaL_checkversion_ (
lua_State *L, lua_Number ver,
size_t sz) {
1105 lua_Number v = lua_version(L);
1106 if (sz != LUAL_NUMSIZES)
1107 luaL_error(L,
"core and library have incompatible numeric types");
1109 luaL_error(L,
"version mismatch: app. needs %f, Lua core provides %f",
1110 (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v);