25 int n = lua_gettop(L);
27 for (i = 1; i <= n; i++) {
29 const char *s = luaL_tolstring(L, i, &l);
31 lua_writestring(
"\t", 1);
32 lua_writestring(s, l);
46 int n = lua_gettop(L);
48 luaL_checkstring(L, 1);
49 for (i = 2; i <= n; i++)
50 luaL_checkstring(L, i);
51 for (i = 1; i < n; i++)
52 lua_warning(L, lua_tostring(L, i), 1);
53 lua_warning(L, lua_tostring(L, n), 0);
58#define SPACECHARS " \f\n\r\t\v"
60static const char *b_str2int (
const char *s,
int base, lua_Integer *pn) {
63 s += strspn(s, SPACECHARS);
64 if (*s ==
'-') { s++; neg = 1; }
65 else if (*s ==
'+') s++;
66 if (!isalnum((
unsigned char)*s))
69 int digit = (isdigit((
unsigned char)*s)) ? *s -
'0'
70 : (toupper((unsigned char)*s) -
'A') + 10;
71 if (digit >= base)
return NULL;
74 }
while (isalnum((
unsigned char)*s));
75 s += strspn(s, SPACECHARS);
76 *pn = (lua_Integer)((neg) ? (0u - n) : n);
82 if (lua_isnoneornil(L, 2)) {
83 if (lua_type(L, 1) == LUA_TNUMBER) {
89 const char *s = lua_tolstring(L, 1, &l);
90 if (s != NULL && lua_stringtonumber(L, s) == l + 1)
100 lua_Integer base = luaL_checkinteger(L, 2);
101 luaL_checktype(L, 1, LUA_TSTRING);
102 s = lua_tolstring(L, 1, &l);
103 luaL_argcheck(L, 2 <= base && base <= 36, 2,
"base out of range");
104 if (b_str2int(s, (
int)base, &n) == s + l) {
105 lua_pushinteger(L, n);
115 int level = (int)luaL_optinteger(L, 2, 1);
117 if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
118 luaL_where(L, level);
126static int luaB_getmetatable (
lua_State *L) {
128 if (!lua_getmetatable(L, 1)) {
132 luaL_getmetafield(L, 1,
"__metatable");
137static int luaB_setmetatable (
lua_State *L) {
138 int t = lua_type(L, 2);
139 luaL_checktype(L, 1, LUA_TTABLE);
140 luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"nil or table");
141 if (l_unlikely(luaL_getmetafield(L, 1,
"__metatable") != LUA_TNIL))
142 return luaL_error(L,
"cannot change a protected metatable");
144 lua_setmetatable(L, 1);
152 lua_pushboolean(L, lua_rawequal(L, 1, 2));
158 int t = lua_type(L, 1);
159 luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
161 lua_pushinteger(L, lua_rawlen(L, 1));
167 luaL_checktype(L, 1, LUA_TTABLE);
175 luaL_checktype(L, 1, LUA_TTABLE);
184static int pushmode (
lua_State *L,
int oldmode) {
188 lua_pushstring(L, (oldmode == LUA_GCINC) ?
"incremental"
197#define checkvalres(res) { if (res == -1) break; }
199static int luaB_collectgarbage (
lua_State *L) {
200 static const char *
const opts[] = {
"stop",
"restart",
"collect",
201 "count",
"step",
"setpause",
"setstepmul",
202 "isrunning",
"generational",
"incremental", NULL};
203 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
204 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
205 LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
206 int o = optsnum[luaL_checkoption(L, 1,
"collect", opts)];
209 int k = lua_gc(L, o);
210 int b = lua_gc(L, LUA_GCCOUNTB);
212 lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
216 int step = (int)luaL_optinteger(L, 2, 0);
217 int res = lua_gc(L, o, step);
219 lua_pushboolean(L, res);
223 case LUA_GCSETSTEPMUL: {
224 int p = (int)luaL_optinteger(L, 2, 0);
225 int previous = lua_gc(L, o, p);
226 checkvalres(previous);
227 lua_pushinteger(L, previous);
230 case LUA_GCISRUNNING: {
231 int res = lua_gc(L, o);
233 lua_pushboolean(L, res);
237 int minormul = (int)luaL_optinteger(L, 2, 0);
238 int majormul = (int)luaL_optinteger(L, 3, 0);
239 return pushmode(L, lua_gc(L, o, minormul, majormul));
242 int pause = (int)luaL_optinteger(L, 2, 0);
243 int stepmul = (int)luaL_optinteger(L, 3, 0);
244 int stepsize = (int)luaL_optinteger(L, 4, 0);
245 return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
248 int res = lua_gc(L, o);
250 lua_pushinteger(L, res);
260 int t = lua_type(L, 1);
261 luaL_argcheck(L, t != LUA_TNONE, 1,
"value expected");
262 lua_pushstring(L, lua_typename(L, t));
268 luaL_checktype(L, 1, LUA_TTABLE);
279static int pairscont (
lua_State *L,
int status, lua_KContext k) {
280 (void)L; (void)status; (void)k;
286 if (luaL_getmetafield(L, 1,
"__pairs") == LUA_TNIL) {
287 lua_pushcfunction(L, luaB_next);
293 lua_callk(L, 1, 3, 0, pairscont);
303 lua_Integer i = luaL_checkinteger(L, 2);
304 i = luaL_intop(+, i, 1);
305 lua_pushinteger(L, i);
306 return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
316 lua_pushcfunction(L, ipairsaux);
318 lua_pushinteger(L, 0);
323static int load_aux (
lua_State *L,
int status,
int envidx) {
324 if (l_likely(status == LUA_OK)) {
326 lua_pushvalue(L, envidx);
327 if (!lua_setupvalue(L, -2, 1))
341 const char *fname = luaL_optstring(L, 1, NULL);
342 const char *mode = luaL_optstring(L, 2, NULL);
343 int env = (!lua_isnone(L, 3) ? 3 : 0);
344 int status = luaL_loadfilex(L, fname, mode);
345 return load_aux(L, status, env);
361#define RESERVEDSLOT 5
370static const char *generic_reader (
lua_State *L,
void *ud,
size_t *size) {
372 luaL_checkstack(L, 2,
"too many nested functions");
375 if (lua_isnil(L, -1)) {
380 else if (l_unlikely(!lua_isstring(L, -1)))
381 luaL_error(L,
"reader function must return a string");
382 lua_replace(L, RESERVEDSLOT);
383 return lua_tolstring(L, RESERVEDSLOT, size);
390 const char *s = lua_tolstring(L, 1, &l);
391 const char *mode = luaL_optstring(L, 3,
"bt");
392 int env = (!lua_isnone(L, 4) ? 4 : 0);
394 const char *chunkname = luaL_optstring(L, 2, s);
395 status = luaL_loadbufferx(L, s, l, chunkname, mode);
398 const char *chunkname = luaL_optstring(L, 2,
"=(load)");
399 luaL_checktype(L, 1, LUA_TFUNCTION);
400 lua_settop(L, RESERVEDSLOT);
401 status = lua_load(L, generic_reader, NULL, chunkname, mode);
403 return load_aux(L, status, env);
409static int dofilecont (
lua_State *L,
int d1, lua_KContext d2) {
411 return lua_gettop(L) - 1;
416 const char *fname = luaL_optstring(L, 1, NULL);
418 if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK))
420 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
421 return dofilecont(L, 0, 0);
426 if (l_likely(lua_toboolean(L, 1)))
427 return lua_gettop(L);
431 lua_pushliteral(L,
"assertion failed!");
433 return luaB_error(L);
439 int n = lua_gettop(L);
440 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) ==
'#') {
441 lua_pushinteger(L, n-1);
445 lua_Integer i = luaL_checkinteger(L, 1);
446 if (i < 0) i = n + i;
447 else if (i > n) i = n;
448 luaL_argcheck(L, 1 <= i, 1,
"index out of range");
461static int finishpcall (
lua_State *L,
int status, lua_KContext extra) {
462 if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) {
463 lua_pushboolean(L, 0);
464 lua_pushvalue(L, -2);
468 return lua_gettop(L) - (int)extra;
475 lua_pushboolean(L, 1);
477 status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
478 return finishpcall(L, status, 0);
489 int n = lua_gettop(L);
490 luaL_checktype(L, 2, LUA_TFUNCTION);
491 lua_pushboolean(L, 1);
494 status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
495 return finishpcall(L, status, 2);
501 luaL_tolstring(L, 1, NULL);
506static const luaL_Reg base_funcs[] = {
507 {
"assert", luaB_assert},
508 {
"collectgarbage", luaB_collectgarbage},
509 {
"dofile", luaB_dofile},
510 {
"error", luaB_error},
511 {
"getmetatable", luaB_getmetatable},
512 {
"ipairs", luaB_ipairs},
513 {
"loadfile", luaB_loadfile},
516 {
"pairs", luaB_pairs},
517 {
"pcall", luaB_pcall},
518 {
"print", luaB_print},
520 {
"rawequal", luaB_rawequal},
521 {
"rawlen", luaB_rawlen},
522 {
"rawget", luaB_rawget},
523 {
"rawset", luaB_rawset},
524 {
"select", luaB_select},
525 {
"setmetatable", luaB_setmetatable},
526 {
"tonumber", luaB_tonumber},
527 {
"tostring", luaB_tostring},
529 {
"xpcall", luaB_xpcall},
537LUAMOD_API
int luaopen_base (
lua_State *L) {
539 lua_pushglobaltable(L);
540 luaL_setfuncs(L, base_funcs, 0);
542 lua_pushvalue(L, -1);
543 lua_setfield(L, -2, LUA_GNAME);
545 lua_pushliteral(L, LUA_VERSION);
546 lua_setfield(L, -2,
"_VERSION");