Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lbaselib.c File Reference
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define lbaselib_c
 
#define LUA_LIB
 
#define CO_RUN   0 /* running */
 
#define CO_SUS   1 /* suspended */
 
#define CO_NOR   2 /* 'normal' (it resumed another coroutine) */
 
#define CO_DEAD   3
 

Functions

static int luaB_print (lua_State *L)
 
static int luaB_tonumber (lua_State *L)
 
static int luaB_error (lua_State *L)
 
static int luaB_getmetatable (lua_State *L)
 
static int luaB_setmetatable (lua_State *L)
 
static void getfunc (lua_State *L, int opt)
 
static int luaB_getfenv (lua_State *L)
 
static int luaB_setfenv (lua_State *L)
 
static int luaB_rawequal (lua_State *L)
 
static int luaB_rawget (lua_State *L)
 
static int luaB_rawset (lua_State *L)
 
static int luaB_gcinfo (lua_State *L)
 
static int luaB_collectgarbage (lua_State *L)
 
static int luaB_type (lua_State *L)
 
static int luaB_next (lua_State *L)
 
static int luaB_pairs (lua_State *L)
 
static int ipairsaux (lua_State *L)
 
static int luaB_ipairs (lua_State *L)
 
static int load_aux (lua_State *L, int status)
 
static int luaB_loadstring (lua_State *L)
 
static int luaB_loadfile (lua_State *L)
 
static const char * generic_reader (lua_State *L, void *ud, size_t *size)
 
static int luaB_load (lua_State *L)
 
static int luaB_dofile (lua_State *L)
 
static int luaB_assert (lua_State *L)
 
static int luaB_unpack (lua_State *L)
 
static int luaB_select (lua_State *L)
 
static int luaB_pcall (lua_State *L)
 
static int luaB_xpcall (lua_State *L)
 
static int luaB_tostring (lua_State *L)
 
static int luaB_newproxy (lua_State *L)
 
static int costatus (lua_State *L, lua_State *co)
 
static int luaB_costatus (lua_State *L)
 
static int auxresume (lua_State *L, lua_State *co, int narg)
 
static int luaB_coresume (lua_State *L)
 
static int luaB_auxwrap (lua_State *L)
 
static int luaB_cocreate (lua_State *L)
 
static int luaB_cowrap (lua_State *L)
 
static int luaB_yield (lua_State *L)
 
static int luaB_corunning (lua_State *L)
 
static void auxopen (lua_State *L, const char *name, lua_CFunction f, lua_CFunction u)
 
static void base_open (lua_State *L)
 
LUALIB_API int luaopen_base (lua_State *L)
 

Variables

static const luaL_Reg base_funcs []
 
static const char *const statnames []
 
static const luaL_Reg co_funcs []
 

Macro Definition Documentation

◆ CO_DEAD

#define CO_DEAD   3

Definition at line 485 of file lua-5.1.5/src/lbaselib.c.

Referenced by costatus().

◆ CO_NOR

#define CO_NOR   2 /* 'normal' (it resumed another coroutine) */

Definition at line 484 of file lua-5.1.5/src/lbaselib.c.

Referenced by costatus().

◆ CO_RUN

#define CO_RUN   0 /* running */

Definition at line 482 of file lua-5.1.5/src/lbaselib.c.

Referenced by costatus().

◆ CO_SUS

#define CO_SUS   1 /* suspended */

Definition at line 483 of file lua-5.1.5/src/lbaselib.c.

Referenced by auxresume(), and costatus().

◆ lbaselib_c

#define lbaselib_c

Definition at line 14 of file lua-5.1.5/src/lbaselib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 15 of file lua-5.1.5/src/lbaselib.c.

Function Documentation

◆ auxopen()

static void auxopen ( lua_State * L,
const char * name,
lua_CFunction f,
lua_CFunction u )
static

Definition at line 618 of file lua-5.1.5/src/lbaselib.c.

619 {
620 lua_pushcfunction(L, u);
621 lua_pushcclosure(L, f, 1);
622 lua_setfield(L, -2, name);
623}
const char * name
Definition lsqlite3.c:2154
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
#define lua_pushcfunction(L, f)

References lua_pushcclosure(), lua_pushcfunction, lua_setfield(), and name.

Referenced by base_open().

◆ auxresume()

static int auxresume ( lua_State * L,
lua_State * co,
int narg )
static

Definition at line 518 of file lua-5.1.5/src/lbaselib.c.

518 {
519 int status = costatus(L, co);
520 if (!lua_checkstack(co, narg))
521 luaL_error(L, "too many arguments to resume");
522 if (status != CO_SUS) {
523 lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
524 return -1; /* error flag */
525 }
526 lua_xmove(L, co, narg);
527 lua_setlevel(L, co);
528 status = lua_resume(co, narg);
529 if (status == 0 || status == LUA_YIELD) {
530 int nres = lua_gettop(co);
531 if (!lua_checkstack(L, nres + 1))
532 luaL_error(L, "too many results to resume");
533 lua_xmove(co, L, nres); /* move yielded values */
534 return nres;
535 }
536 else {
537 lua_xmove(co, L, 1); /* move error message */
538 return -1; /* error flag */
539 }
540}
LUA_API void lua_setlevel(lua_State *from, lua_State *to)
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_API int lua_checkstack(lua_State *L, int size)
LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
LUA_API int lua_gettop(lua_State *L)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
static const char *const statnames[]
#define CO_SUS
static int costatus(lua_State *L, lua_State *co)
LUA_API int lua_resume(lua_State *L, int nargs)
#define LUA_YIELD

References CO_SUS, costatus(), lua_checkstack(), lua_gettop(), lua_pushfstring(), lua_resume(), lua_setlevel(), lua_xmove(), LUA_YIELD, luaL_error(), and statnames.

Referenced by luaB_auxwrap(), and luaB_coresume().

◆ base_open()

static void base_open ( lua_State * L)
static

Definition at line 626 of file lua-5.1.5/src/lbaselib.c.

626 {
627 /* set global _G */
629 lua_setglobal(L, "_G");
630 /* open lib into global table */
631 luaL_register(L, "_G", base_funcs);
633 lua_setglobal(L, "_VERSION"); /* set global _VERSION */
634 /* `ipairs' and `pairs' need auxiliary functions as upvalues */
635 auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
636 auxopen(L, "pairs", luaB_pairs, luaB_next);
637 /* `newproxy' needs a weaktable as upvalue */
638 lua_createtable(L, 0, 1); /* new table `w' */
639 lua_pushvalue(L, -1); /* `w' will be its own metatable */
640 lua_setmetatable(L, -2);
641 lua_pushliteral(L, "kv");
642 lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
644 lua_setglobal(L, "newproxy"); /* set global `newproxy' */
645}
LUA_API int lua_setmetatable(lua_State *L, int objindex)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUALIB_API void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
static const luaL_Reg base_funcs[]
static int luaB_pairs(lua_State *L)
static int luaB_ipairs(lua_State *L)
static int luaB_newproxy(lua_State *L)
static int ipairsaux(lua_State *L)
static int luaB_next(lua_State *L)
static void auxopen(lua_State *L, const char *name, lua_CFunction f, lua_CFunction u)
#define LUA_VERSION
#define lua_pushliteral(L, s)
#define lua_setglobal(L, s)
#define LUA_GLOBALSINDEX

References auxopen(), base_funcs, ipairsaux(), lua_createtable(), LUA_GLOBALSINDEX, lua_pushcclosure(), lua_pushliteral, lua_pushvalue(), lua_setfield(), lua_setglobal, lua_setmetatable(), LUA_VERSION, luaB_ipairs(), luaB_newproxy(), luaB_next(), luaB_pairs(), and luaL_register().

Referenced by luaopen_base().

◆ costatus()

static int costatus ( lua_State * L,
lua_State * co )
static

Definition at line 490 of file lua-5.1.5/src/lbaselib.c.

490 {
491 if (L == co) return CO_RUN;
492 switch (lua_status(co)) {
493 case LUA_YIELD:
494 return CO_SUS;
495 case 0: {
496 lua_Debug ar;
497 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
498 return CO_NOR; /* it is running */
499 else if (lua_gettop(co) == 0)
500 return CO_DEAD;
501 else
502 return CO_SUS; /* initial state */
503 }
504 default: /* some error occured */
505 return CO_DEAD;
506 }
507}
LUA_API int lua_status(lua_State *L)
#define CO_RUN
#define CO_DEAD
#define CO_NOR
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)

References CO_DEAD, CO_NOR, CO_RUN, CO_SUS, lua_getstack(), lua_gettop(), lua_status(), and LUA_YIELD.

Referenced by auxresume(), and luaB_costatus().

◆ generic_reader()

static const char * generic_reader ( lua_State * L,
void * ud,
size_t * size )
static

Definition at line 297 of file lua-5.1.5/src/lbaselib.c.

297 {
298 (void)ud; /* to avoid warnings */
299 luaL_checkstack(L, 2, "too many nested functions");
300 lua_pushvalue(L, 1); /* get function */
301 lua_call(L, 0, 1); /* call it */
302 if (lua_isnil(L, -1)) {
303 *size = 0;
304 return NULL;
305 }
306 else if (lua_isstring(L, -1)) {
307 lua_replace(L, 3); /* save string in a reserved stack slot */
308 return lua_tolstring(L, 3, size);
309 }
310 else luaL_error(L, "reader function must return a string");
311 return NULL; /* to avoid warnings */
312}
#define NULL
Definition gmacros.h:924
LUA_API const char * lua_tolstring(lua_State *L, int idx, size_t *len)
LUA_API int lua_isstring(lua_State *L, int idx)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
#define lua_isnil(L, n)
#define lua_call(L, n, r)
#define lua_replace(L, idx)

References lua_call, lua_isnil, lua_isstring(), lua_pushvalue(), lua_replace, lua_tolstring(), luaL_checkstack(), luaL_error(), and NULL.

Referenced by luaB_load().

◆ getfunc()

static void getfunc ( lua_State * L,
int opt )
static

Definition at line 117 of file lua-5.1.5/src/lbaselib.c.

117 {
118 if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
119 else {
120 lua_Debug ar;
121 int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
122 luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
123 if (lua_getstack(L, level, &ar) == 0)
124 luaL_argerror(L, 1, "invalid level");
125 lua_getinfo(L, "f", &ar);
126 if (lua_isnil(L, -1))
127 luaL_error(L, "no function environment for tail call at level %d",
128 level);
129 }
130}
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
#define luaL_optint(L, n, d)
#define luaL_checkint(L, n)
#define luaL_argcheck(L, cond, numarg, extramsg)
LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
#define lua_isfunction(L, n)

References lua_getinfo(), lua_getstack(), lua_isfunction, lua_isnil, lua_pushvalue(), luaL_argcheck, luaL_argerror(), luaL_checkint, luaL_error(), and luaL_optint.

Referenced by luaB_getfenv(), and luaB_setfenv().

◆ ipairsaux()

static int ipairsaux ( lua_State * L)
static

Definition at line 247 of file lua-5.1.5/src/lbaselib.c.

247 {
248 int i = luaL_checkint(L, 2);
250 i++; /* next value */
251 lua_pushinteger(L, i);
252 lua_rawgeti(L, 1, i);
253 return (lua_isnil(L, -1)) ? 0 : 2;
254}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
#define LUA_TTABLE

References lua_isnil, lua_pushinteger(), lua_rawgeti(), LUA_TTABLE, luaL_checkint, and luaL_checktype().

Referenced by base_open().

◆ load_aux()

static int load_aux ( lua_State * L,
int status )
static

Definition at line 266 of file lua-5.1.5/src/lbaselib.c.

266 {
267 if (status == 0) /* OK? */
268 return 1;
269 else {
270 lua_pushnil(L);
271 lua_insert(L, -2); /* put before error message */
272 return 2; /* return nil plus error message */
273 }
274}
LUA_API void lua_pushnil(lua_State *L)
#define lua_insert(L, idx)

References lua_insert, and lua_pushnil().

Referenced by luaB_load(), luaB_loadfile(), and luaB_loadstring().

◆ luaB_assert()

static int luaB_assert ( lua_State * L)
static

Definition at line 334 of file lua-5.1.5/src/lbaselib.c.

334 {
335 luaL_checkany(L, 1);
336 if (!lua_toboolean(L, 1))
337 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
338 return lua_gettop(L);
339}
LUA_API int lua_toboolean(lua_State *L, int idx)
LUALIB_API void luaL_checkany(lua_State *L, int narg)
#define luaL_optstring(L, n, d)

References lua_gettop(), lua_toboolean(), luaL_checkany(), luaL_error(), and luaL_optstring.

◆ luaB_auxwrap()

static int luaB_auxwrap ( lua_State * L)
static

Definition at line 561 of file lua-5.1.5/src/lbaselib.c.

561 {
563 int r = auxresume(L, co, lua_gettop(L));
564 if (r < 0) {
565 if (lua_isstring(L, -1)) { /* error object is a string? */
566 luaL_where(L, 1); /* add extra info */
567 lua_insert(L, -2);
568 lua_concat(L, 2);
569 }
570 lua_error(L); /* propagate error */
571 }
572 return r;
573}
LUA_API void lua_concat(lua_State *L, int n)
LUA_API int lua_error(lua_State *L)
LUA_API lua_State * lua_tothread(lua_State *L, int idx)
LUALIB_API void luaL_where(lua_State *L, int level)
static int auxresume(lua_State *L, lua_State *co, int narg)
#define lua_upvalueindex(i)

References auxresume(), lua_concat(), lua_error(), lua_gettop(), lua_insert, lua_isstring(), lua_tothread(), lua_upvalueindex, and luaL_where().

Referenced by luaB_cowrap().

◆ luaB_cocreate()

static int luaB_cocreate ( lua_State * L)
static

Definition at line 576 of file lua-5.1.5/src/lbaselib.c.

576 {
577 lua_State *NL = lua_newthread(L);
578 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
579 "Lua function expected");
580 lua_pushvalue(L, 1); /* move function to top */
581 lua_xmove(L, NL, 1); /* move function from L to NL */
582 return 1;
583}
LUA_API int lua_iscfunction(lua_State *L, int idx)
LUA_API lua_State * lua_newthread(lua_State *L)

References lua_iscfunction(), lua_isfunction, lua_newthread(), lua_pushvalue(), lua_xmove(), and luaL_argcheck.

Referenced by luaB_cowrap().

◆ luaB_collectgarbage()

static int luaB_collectgarbage ( lua_State * L)
static

Definition at line 193 of file lua-5.1.5/src/lbaselib.c.

193 {
194 static const char *const opts[] = {"stop", "restart", "collect",
195 "count", "step", "setpause", "setstepmul", NULL};
196 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
198 int o = luaL_checkoption(L, 1, "collect", opts);
199 int ex = luaL_optint(L, 2, 0);
200 int res = lua_gc(L, optsnum[o], ex);
201 switch (optsnum[o]) {
202 case LUA_GCCOUNT: {
203 int b = lua_gc(L, LUA_GCCOUNTB, 0);
204 lua_pushnumber(L, res + ((lua_Number)b/1024));
205 return 1;
206 }
207 case LUA_GCSTEP: {
208 lua_pushboolean(L, res);
209 return 1;
210 }
211 default: {
212 lua_pushnumber(L, res);
213 return 1;
214 }
215 }
216}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API void lua_pushboolean(lua_State *L, int b)
LUA_API int lua_gc(lua_State *L, int what, int data)
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def, const char *const lst[])
#define LUA_GCSETPAUSE
#define LUA_GCCOUNT
#define LUA_GCCOLLECT
#define LUA_GCSTOP
#define LUA_GCRESTART
#define LUA_GCCOUNTB
#define LUA_GCSTEP
#define LUA_GCSETSTEPMUL
LUA_NUMBER lua_Number

References lua_gc(), LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCCOUNTB, LUA_GCRESTART, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, LUA_GCSTEP, LUA_GCSTOP, lua_pushboolean(), lua_pushnumber(), luaL_checkoption(), luaL_optint, and NULL.

◆ luaB_coresume()

static int luaB_coresume ( lua_State * L)
static

Definition at line 543 of file lua-5.1.5/src/lbaselib.c.

543 {
544 lua_State *co = lua_tothread(L, 1);
545 int r;
546 luaL_argcheck(L, co, 1, "coroutine expected");
547 r = auxresume(L, co, lua_gettop(L) - 1);
548 if (r < 0) {
549 lua_pushboolean(L, 0);
550 lua_insert(L, -2);
551 return 2; /* return false + error message */
552 }
553 else {
554 lua_pushboolean(L, 1);
555 lua_insert(L, -(r + 1));
556 return r + 1; /* return true + `resume' returns */
557 }
558}

References auxresume(), lua_gettop(), lua_insert, lua_pushboolean(), lua_tothread(), and luaL_argcheck.

◆ luaB_corunning()

static int luaB_corunning ( lua_State * L)
static

Definition at line 598 of file lua-5.1.5/src/lbaselib.c.

598 {
599 if (lua_pushthread(L))
600 lua_pushnil(L); /* main thread is not a coroutine */
601 return 1;
602}
LUA_API int lua_pushthread(lua_State *L)

References lua_pushnil(), and lua_pushthread().

◆ luaB_costatus()

static int luaB_costatus ( lua_State * L)
static

Definition at line 510 of file lua-5.1.5/src/lbaselib.c.

510 {
511 lua_State *co = lua_tothread(L, 1);
512 luaL_argcheck(L, co, 1, "coroutine expected");
514 return 1;
515}
LUA_API void lua_pushstring(lua_State *L, const char *s)

References costatus(), lua_pushstring(), lua_tothread(), luaL_argcheck, and statnames.

◆ luaB_cowrap()

static int luaB_cowrap ( lua_State * L)
static

Definition at line 586 of file lua-5.1.5/src/lbaselib.c.

586 {
587 luaB_cocreate(L);
589 return 1;
590}
static int luaB_auxwrap(lua_State *L)
static int luaB_cocreate(lua_State *L)

References lua_pushcclosure(), luaB_auxwrap(), and luaB_cocreate().

◆ luaB_dofile()

static int luaB_dofile ( lua_State * L)
static

Definition at line 325 of file lua-5.1.5/src/lbaselib.c.

325 {
326 const char *fname = luaL_optstring(L, 1, NULL);
327 int n = lua_gettop(L);
328 if (luaL_loadfile(L, fname) != 0) lua_error(L);
329 lua_call(L, 0, LUA_MULTRET);
330 return lua_gettop(L) - n;
331}
#define LUA_MULTRET
#define luaL_loadfile(L, f)

References lua_call, lua_error(), lua_gettop(), LUA_MULTRET, luaL_loadfile, luaL_optstring, and NULL.

◆ luaB_error()

static int luaB_error ( lua_State * L)
static

Definition at line 81 of file lua-5.1.5/src/lbaselib.c.

81 {
82 int level = luaL_optint(L, 2, 1);
83 lua_settop(L, 1);
84 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
85 luaL_where(L, level);
86 lua_pushvalue(L, 1);
87 lua_concat(L, 2);
88 }
89 return lua_error(L);
90}
LUA_API void lua_settop(lua_State *L, int idx)

References lua_concat(), lua_error(), lua_isstring(), lua_pushvalue(), lua_settop(), luaL_optint, and luaL_where().

◆ luaB_gcinfo()

static int luaB_gcinfo ( lua_State * L)
static

Definition at line 187 of file lua-5.1.5/src/lbaselib.c.

187 {
189 return 1;
190}
#define lua_getgccount(L)

References lua_getgccount, and lua_pushinteger().

◆ luaB_getfenv()

static int luaB_getfenv ( lua_State * L)
static

Definition at line 133 of file lua-5.1.5/src/lbaselib.c.

133 {
134 getfunc(L, 1);
135 if (lua_iscfunction(L, -1)) /* is a C function? */
136 lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
137 else
138 lua_getfenv(L, -1);
139 return 1;
140}
LUA_API void lua_getfenv(lua_State *L, int idx)
static void getfunc(lua_State *L, int opt)

References getfunc(), lua_getfenv(), LUA_GLOBALSINDEX, lua_iscfunction(), and lua_pushvalue().

◆ luaB_getmetatable()

static int luaB_getmetatable ( lua_State * L)
static

Definition at line 93 of file lua-5.1.5/src/lbaselib.c.

93 {
94 luaL_checkany(L, 1);
95 if (!lua_getmetatable(L, 1)) {
96 lua_pushnil(L);
97 return 1; /* no metatable */
98 }
99 luaL_getmetafield(L, 1, "__metatable");
100 return 1; /* returns either __metatable field (if present) or metatable */
101}
LUA_API int lua_getmetatable(lua_State *L, int objindex)
LUALIB_API int luaL_getmetafield(lua_State *L, int obj, const char *event)

References lua_getmetatable(), lua_pushnil(), luaL_checkany(), and luaL_getmetafield().

◆ luaB_ipairs()

static int luaB_ipairs ( lua_State * L)
static

Definition at line 257 of file lua-5.1.5/src/lbaselib.c.

257 {
259 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
260 lua_pushvalue(L, 1); /* state, */
261 lua_pushinteger(L, 0); /* and initial value */
262 return 3;
263}

References lua_pushinteger(), lua_pushvalue(), LUA_TTABLE, lua_upvalueindex, and luaL_checktype().

Referenced by base_open().

◆ luaB_load()

static int luaB_load ( lua_State * L)
static

Definition at line 315 of file lua-5.1.5/src/lbaselib.c.

315 {
316 int status;
317 const char *cname = luaL_optstring(L, 2, "=(load)");
319 lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
320 status = lua_load(L, generic_reader, NULL, cname);
321 return load_aux(L, status);
322}
LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char *chunkname)
static int load_aux(lua_State *L, int status)
static const char * generic_reader(lua_State *L, void *ud, size_t *size)
#define LUA_TFUNCTION

References generic_reader(), load_aux(), lua_load(), lua_settop(), LUA_TFUNCTION, luaL_checktype(), luaL_optstring, and NULL.

◆ luaB_loadfile()

static int luaB_loadfile ( lua_State * L)
static

Definition at line 285 of file lua-5.1.5/src/lbaselib.c.

285 {
286 const char *fname = luaL_optstring(L, 1, NULL);
287 return load_aux(L, luaL_loadfile(L, fname));
288}

References load_aux(), luaL_loadfile, luaL_optstring, and NULL.

◆ luaB_loadstring()

static int luaB_loadstring ( lua_State * L)
static

Definition at line 277 of file lua-5.1.5/src/lbaselib.c.

277 {
278 size_t l;
279 const char *s = luaL_checklstring(L, 1, &l);
280 const char *chunkname = luaL_optstring(L, 2, s);
281 return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
282}
LUALIB_API const char * luaL_checklstring(lua_State *L, int narg, size_t *len)
#define luaL_loadbuffer(L, s, sz, n)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

References load_aux(), luaL_checklstring(), luaL_loadbuffer, luaL_optstring, and s.

◆ luaB_newproxy()

static int luaB_newproxy ( lua_State * L)
static

Definition at line 421 of file lua-5.1.5/src/lbaselib.c.

421 {
422 lua_settop(L, 1);
423 lua_newuserdata(L, 0); /* create proxy */
424 if (lua_toboolean(L, 1) == 0)
425 return 1; /* no metatable */
426 else if (lua_isboolean(L, 1)) {
427 lua_newtable(L); /* create a new metatable `m' ... */
428 lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
429 lua_pushboolean(L, 1);
430 lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
431 }
432 else {
433 int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
434 if (lua_getmetatable(L, 1)) {
436 validproxy = lua_toboolean(L, -1);
437 lua_pop(L, 1); /* remove value */
438 }
439 luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
440 lua_getmetatable(L, 1); /* metatable is valid; get it */
441 }
442 lua_setmetatable(L, 2);
443 return 1;
444}
LUA_API void lua_rawget(lua_State *L, int idx)
LUA_API void lua_rawset(lua_State *L, int idx)
#define lua_newtable(L)
#define lua_pop(L, n)
#define lua_isboolean(L, n)
#define lua_newuserdata(L, s)

References lua_getmetatable(), lua_isboolean, lua_newtable, lua_newuserdata, lua_pop, lua_pushboolean(), lua_pushvalue(), lua_rawget(), lua_rawset(), lua_setmetatable(), lua_settop(), lua_toboolean(), lua_upvalueindex, and luaL_argcheck.

Referenced by base_open().

◆ luaB_next()

static int luaB_next ( lua_State * L)
static

Definition at line 226 of file lua-5.1.5/src/lbaselib.c.

226 {
228 lua_settop(L, 2); /* create a 2nd argument if there isn't one */
229 if (lua_next(L, 1))
230 return 2;
231 else {
232 lua_pushnil(L);
233 return 1;
234 }
235}
LUA_API int lua_next(lua_State *L, int idx)

References lua_next(), lua_pushnil(), lua_settop(), LUA_TTABLE, and luaL_checktype().

Referenced by base_open().

◆ luaB_pairs()

static int luaB_pairs ( lua_State * L)
static

Definition at line 238 of file lua-5.1.5/src/lbaselib.c.

238 {
240 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
241 lua_pushvalue(L, 1); /* state, */
242 lua_pushnil(L); /* and initial value */
243 return 3;
244}

References lua_pushnil(), lua_pushvalue(), LUA_TTABLE, lua_upvalueindex, and luaL_checktype().

Referenced by base_open().

◆ luaB_pcall()

static int luaB_pcall ( lua_State * L)
static

Definition at line 374 of file lua-5.1.5/src/lbaselib.c.

374 {
375 int status;
376 luaL_checkany(L, 1);
377 status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
378 lua_pushboolean(L, (status == 0));
379 lua_insert(L, 1);
380 return lua_gettop(L); /* return status + all results */
381}
#define lua_pcall(L, n, r, f)

References lua_gettop(), lua_insert, LUA_MULTRET, lua_pcall, lua_pushboolean(), and luaL_checkany().

◆ luaB_print()

static int luaB_print ( lua_State * L)
static

Definition at line 31 of file lua-5.1.5/src/lbaselib.c.

31 {
32 int n = lua_gettop(L); /* number of arguments */
33 int i;
34 lua_getglobal(L, "tostring");
35 for (i=1; i<=n; i++) {
36 const char *s;
37 lua_pushvalue(L, -1); /* function to be called */
38 lua_pushvalue(L, i); /* value to print */
39 lua_call(L, 1, 1);
40 s = lua_tostring(L, -1); /* get result */
41 if (s == NULL)
42 return luaL_error(L, LUA_QL("tostring") " must return a string to "
43 LUA_QL("print"));
44 if (i>1) fputs("\t", stdout);
45 fputs(s, stdout);
46 lua_pop(L, 1); /* pop result */
47 }
48 fputs("\n", stdout);
49 return 0;
50}
#define lua_getglobal(L, s)
#define lua_tostring(L, i)
#define LUA_QL(x)

References lua_call, lua_getglobal, lua_gettop(), lua_pop, lua_pushvalue(), LUA_QL, lua_tostring, luaL_error(), NULL, and s.

◆ luaB_rawequal()

static int luaB_rawequal ( lua_State * L)
static

Definition at line 161 of file lua-5.1.5/src/lbaselib.c.

161 {
162 luaL_checkany(L, 1);
163 luaL_checkany(L, 2);
164 lua_pushboolean(L, lua_rawequal(L, 1, 2));
165 return 1;
166}
LUA_API int lua_rawequal(lua_State *L, int index1, int index2)

References lua_pushboolean(), lua_rawequal(), and luaL_checkany().

◆ luaB_rawget()

static int luaB_rawget ( lua_State * L)
static

Definition at line 169 of file lua-5.1.5/src/lbaselib.c.

169 {
171 luaL_checkany(L, 2);
172 lua_settop(L, 2);
173 lua_rawget(L, 1);
174 return 1;
175}

References lua_rawget(), lua_settop(), LUA_TTABLE, luaL_checkany(), and luaL_checktype().

◆ luaB_rawset()

static int luaB_rawset ( lua_State * L)
static

Definition at line 177 of file lua-5.1.5/src/lbaselib.c.

177 {
179 luaL_checkany(L, 2);
180 luaL_checkany(L, 3);
181 lua_settop(L, 3);
182 lua_rawset(L, 1);
183 return 1;
184}

References lua_rawset(), lua_settop(), LUA_TTABLE, luaL_checkany(), and luaL_checktype().

◆ luaB_select()

static int luaB_select ( lua_State * L)
static

Definition at line 358 of file lua-5.1.5/src/lbaselib.c.

358 {
359 int n = lua_gettop(L);
360 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
361 lua_pushinteger(L, n-1);
362 return 1;
363 }
364 else {
365 int i = luaL_checkint(L, 1);
366 if (i < 0) i = n + i;
367 else if (i > n) i = n;
368 luaL_argcheck(L, 1 <= i, 1, "index out of range");
369 return n - i;
370 }
371}
LUA_API int lua_type(lua_State *L, int idx)
#define LUA_TSTRING

References lua_gettop(), lua_pushinteger(), lua_tostring, LUA_TSTRING, lua_type(), luaL_argcheck, and luaL_checkint.

◆ luaB_setfenv()

static int luaB_setfenv ( lua_State * L)
static

Definition at line 143 of file lua-5.1.5/src/lbaselib.c.

143 {
145 getfunc(L, 0);
146 lua_pushvalue(L, 2);
147 if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
148 /* change environment of current thread */
150 lua_insert(L, -2);
151 lua_setfenv(L, -2);
152 return 0;
153 }
154 else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
155 luaL_error(L,
156 LUA_QL("setfenv") " cannot change environment of given object");
157 return 1;
158}
LUA_API int lua_isnumber(lua_State *L, int idx)
LUA_API int lua_setfenv(lua_State *L, int idx)
#define lua_tonumber(L, i)

References getfunc(), lua_insert, lua_iscfunction(), lua_isnumber(), lua_pushthread(), lua_pushvalue(), LUA_QL, lua_setfenv(), lua_tonumber, LUA_TTABLE, luaL_checktype(), and luaL_error().

◆ luaB_setmetatable()

static int luaB_setmetatable ( lua_State * L)
static

Definition at line 104 of file lua-5.1.5/src/lbaselib.c.

104 {
105 int t = lua_type(L, 2);
107 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
108 "nil or table expected");
109 if (luaL_getmetafield(L, 1, "__metatable"))
110 luaL_error(L, "cannot change a protected metatable");
111 lua_settop(L, 2);
112 lua_setmetatable(L, 1);
113 return 1;
114}
#define LUA_TNIL

References lua_setmetatable(), lua_settop(), LUA_TNIL, LUA_TTABLE, lua_type(), luaL_argcheck, luaL_checktype(), luaL_error(), and luaL_getmetafield().

◆ luaB_tonumber()

static int luaB_tonumber ( lua_State * L)
static

Definition at line 53 of file lua-5.1.5/src/lbaselib.c.

53 {
54 int base = luaL_optint(L, 2, 10);
55 if (base == 10) { /* standard conversion */
56 luaL_checkany(L, 1);
57 if (lua_isnumber(L, 1)) {
59 return 1;
60 }
61 }
62 else {
63 const char *s1 = luaL_checkstring(L, 1);
64 char *s2;
65 unsigned long n;
66 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
67 n = strtoul(s1, &s2, base);
68 if (s1 != s2) { /* at least one valid digit? */
69 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
70 if (*s2 == '\0') { /* no invalid trailing characters? */
72 return 1;
73 }
74 }
75 }
76 lua_pushnil(L); /* else not a number */
77 return 1;
78}
#define luaL_checkstring(L, n)

References lua_isnumber(), lua_pushnil(), lua_pushnumber(), lua_tonumber, luaL_argcheck, luaL_checkany(), luaL_checkstring, and luaL_optint.

◆ luaB_tostring()

static int luaB_tostring ( lua_State * L)
static

Definition at line 396 of file lua-5.1.5/src/lbaselib.c.

396 {
397 luaL_checkany(L, 1);
398 if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
399 return 1; /* use its value */
400 switch (lua_type(L, 1)) {
401 case LUA_TNUMBER:
403 break;
404 case LUA_TSTRING:
405 lua_pushvalue(L, 1);
406 break;
407 case LUA_TBOOLEAN:
408 lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
409 break;
410 case LUA_TNIL:
411 lua_pushliteral(L, "nil");
412 break;
413 default:
414 lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
415 break;
416 }
417 return 1;
418}
LUA_API const void * lua_topointer(lua_State *L, int idx)
LUALIB_API int luaL_callmeta(lua_State *L, int obj, const char *event)
#define luaL_typename(L, i)
#define LUA_TBOOLEAN
#define LUA_TNUMBER

References lua_pushfstring(), lua_pushliteral, lua_pushstring(), lua_pushvalue(), LUA_TBOOLEAN, LUA_TNIL, LUA_TNUMBER, lua_toboolean(), lua_topointer(), lua_tostring, LUA_TSTRING, lua_type(), luaL_callmeta(), luaL_checkany(), and luaL_typename.

◆ luaB_type()

static int luaB_type ( lua_State * L)
static

Definition at line 219 of file lua-5.1.5/src/lbaselib.c.

219 {
220 luaL_checkany(L, 1);
222 return 1;
223}

References lua_pushstring(), luaL_checkany(), and luaL_typename.

◆ luaB_unpack()

static int luaB_unpack ( lua_State * L)
static

Definition at line 342 of file lua-5.1.5/src/lbaselib.c.

342 {
343 int i, e, n;
345 i = luaL_optint(L, 2, 1);
346 e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
347 if (i > e) return 0; /* empty range */
348 n = e - i + 1; /* number of elements */
349 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
350 return luaL_error(L, "too many results to unpack");
351 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
352 while (i++ < e) /* push arg[i + 1...e] */
353 lua_rawgeti(L, 1, i);
354 return n;
355}
#define luaL_getn(L, i)
#define luaL_opt(L, f, n, d)

References lua_checkstack(), lua_rawgeti(), LUA_TTABLE, luaL_checkint, luaL_checktype(), luaL_error(), luaL_getn, luaL_opt, and luaL_optint.

◆ luaB_xpcall()

static int luaB_xpcall ( lua_State * L)
static

Definition at line 384 of file lua-5.1.5/src/lbaselib.c.

384 {
385 int status;
386 luaL_checkany(L, 2);
387 lua_settop(L, 2);
388 lua_insert(L, 1); /* put error function under function to be called */
389 status = lua_pcall(L, 0, LUA_MULTRET, 1);
390 lua_pushboolean(L, (status == 0));
391 lua_replace(L, 1);
392 return lua_gettop(L); /* return status + all results */
393}

References lua_gettop(), lua_insert, LUA_MULTRET, lua_pcall, lua_pushboolean(), lua_replace, lua_settop(), and luaL_checkany().

◆ luaB_yield()

static int luaB_yield ( lua_State * L)
static

Definition at line 593 of file lua-5.1.5/src/lbaselib.c.

593 {
594 return lua_yield(L, lua_gettop(L));
595}
#define lua_yield(L, n)

References lua_gettop(), and lua_yield.

◆ luaopen_base()

LUALIB_API int luaopen_base ( lua_State * L)

Definition at line 648 of file lua-5.1.5/src/lbaselib.c.

648 {
649 base_open(L);
651 return 2;
652}
static const luaL_Reg co_funcs[]
static void base_open(lua_State *L)
#define LUA_COLIBNAME

References base_open(), co_funcs, LUA_COLIBNAME, and luaL_register().

Variable Documentation

◆ base_funcs

const luaL_Reg base_funcs[]
static
Initial value:
= {
{"assert", luaB_assert},
{"collectgarbage", luaB_collectgarbage},
{"dofile", luaB_dofile},
{"error", luaB_error},
{"gcinfo", luaB_gcinfo},
{"getfenv", luaB_getfenv},
{"getmetatable", luaB_getmetatable},
{"loadfile", luaB_loadfile},
{"load", luaB_load},
{"loadstring", luaB_loadstring},
{"next", luaB_next},
{"pcall", luaB_pcall},
{"print", luaB_print},
{"rawequal", luaB_rawequal},
{"rawget", luaB_rawget},
{"rawset", luaB_rawset},
{"select", luaB_select},
{"setfenv", luaB_setfenv},
{"setmetatable", luaB_setmetatable},
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},
{"unpack", luaB_unpack},
{"xpcall", luaB_xpcall},
}
static int luaB_collectgarbage(lua_State *L)
static int luaB_tostring(lua_State *L)
static int luaB_pcall(lua_State *L)
static int luaB_rawget(lua_State *L)
static int luaB_error(lua_State *L)
static int luaB_load(lua_State *L)
static int luaB_rawequal(lua_State *L)
static int luaB_xpcall(lua_State *L)
static int luaB_tonumber(lua_State *L)
static int luaB_type(lua_State *L)
static int luaB_rawset(lua_State *L)
static int luaB_print(lua_State *L)
static int luaB_dofile(lua_State *L)
static int luaB_assert(lua_State *L)
static int luaB_gcinfo(lua_State *L)
static int luaB_getfenv(lua_State *L)
static int luaB_select(lua_State *L)
static int luaB_setfenv(lua_State *L)
static int luaB_unpack(lua_State *L)
static int luaB_getmetatable(lua_State *L)
static int luaB_loadfile(lua_State *L)
static int luaB_loadstring(lua_State *L)
static int luaB_setmetatable(lua_State *L)

Definition at line 447 of file lua-5.1.5/src/lbaselib.c.

447 {
448 {"assert", luaB_assert},
449 {"collectgarbage", luaB_collectgarbage},
450 {"dofile", luaB_dofile},
451 {"error", luaB_error},
452 {"gcinfo", luaB_gcinfo},
453 {"getfenv", luaB_getfenv},
454 {"getmetatable", luaB_getmetatable},
455 {"loadfile", luaB_loadfile},
456 {"load", luaB_load},
457 {"loadstring", luaB_loadstring},
458 {"next", luaB_next},
459 {"pcall", luaB_pcall},
460 {"print", luaB_print},
461 {"rawequal", luaB_rawequal},
462 {"rawget", luaB_rawget},
463 {"rawset", luaB_rawset},
464 {"select", luaB_select},
465 {"setfenv", luaB_setfenv},
466 {"setmetatable", luaB_setmetatable},
467 {"tonumber", luaB_tonumber},
468 {"tostring", luaB_tostring},
469 {"type", luaB_type},
470 {"unpack", luaB_unpack},
471 {"xpcall", luaB_xpcall},
472 {NULL, NULL}
473};

Referenced by base_open().

◆ co_funcs

const luaL_Reg co_funcs[]
static
Initial value:
= {
{"create", luaB_cocreate},
{"resume", luaB_coresume},
{"running", luaB_corunning},
{"status", luaB_costatus},
{"wrap", luaB_cowrap},
{"yield", luaB_yield},
}
static int luaB_costatus(lua_State *L)
static int luaB_cowrap(lua_State *L)
static int luaB_coresume(lua_State *L)
static int luaB_corunning(lua_State *L)
static int luaB_yield(lua_State *L)

Definition at line 605 of file lua-5.1.5/src/lbaselib.c.

605 {
606 {"create", luaB_cocreate},
607 {"resume", luaB_coresume},
608 {"running", luaB_corunning},
609 {"status", luaB_costatus},
610 {"wrap", luaB_cowrap},
611 {"yield", luaB_yield},
612 {NULL, NULL}
613};

Referenced by luaopen_base().

◆ statnames

const char* const statnames[]
static
Initial value:
=
{"running", "suspended", "normal", "dead"}

Definition at line 487 of file lua-5.1.5/src/lbaselib.c.

488 {"running", "suspended", "normal", "dead"};

Referenced by auxresume(), and luaB_costatus().