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

Go to the source code of this file.

Macros

#define lcorolib_c
 
#define LUA_LIB
 

Functions

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_costatus (lua_State *L)
 
static int luaB_corunning (lua_State *L)
 
LUAMOD_API int luaopen_coroutine (lua_State *L)
 

Variables

static const luaL_Reg co_funcs []
 

Macro Definition Documentation

◆ lcorolib_c

#define lcorolib_c

Definition at line 11 of file lua-5.2.4/src/lcorolib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 12 of file lua-5.2.4/src/lcorolib.c.

Function Documentation

◆ auxresume()

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

Definition at line 20 of file lua-5.2.4/src/lcorolib.c.

20 {
21 int status;
22 if (!lua_checkstack(co, narg)) {
23 lua_pushliteral(L, "too many arguments to resume");
24 return -1; /* error flag */
25 }
26 if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
27 lua_pushliteral(L, "cannot resume dead coroutine");
28 return -1; /* error flag */
29 }
30 lua_xmove(L, co, narg);
31 status = lua_resume(co, L, narg);
32 if (status == LUA_OK || status == LUA_YIELD) {
33 int nres = lua_gettop(co);
34 if (!lua_checkstack(L, nres + 1)) {
35 lua_pop(co, nres); /* remove results anyway */
36 lua_pushliteral(L, "too many results to resume");
37 return -1; /* error flag */
38 }
39 lua_xmove(co, L, nres); /* move yielded values */
40 return nres;
41 }
42 else {
43 lua_xmove(co, L, 1); /* move error message */
44 return -1; /* error flag */
45 }
46}
LUA_API int lua_status(lua_State *L)
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)
LUA_API int lua_resume(lua_State *L, int nargs)
#define lua_pushliteral(L, s)
#define LUA_YIELD
#define lua_pop(L, n)
#define LUA_OK

References lua_checkstack(), lua_gettop(), LUA_OK, lua_pop, lua_pushliteral, lua_resume(), lua_status(), lua_xmove(), and LUA_YIELD.

Referenced by luaB_auxwrap(), and luaB_coresume().

◆ luaB_auxwrap()

static int luaB_auxwrap ( lua_State * L)
static

Definition at line 67 of file lua-5.2.4/src/lcorolib.c.

67 {
69 int r = auxresume(L, co, lua_gettop(L));
70 if (r < 0) {
71 if (lua_isstring(L, -1)) { /* error object is a string? */
72 luaL_where(L, 1); /* add extra info */
73 lua_insert(L, -2);
74 lua_concat(L, 2);
75 }
76 return lua_error(L); /* propagate error */
77 }
78 return r;
79}
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)
LUA_API int lua_isstring(lua_State *L, int idx)
LUALIB_API void luaL_where(lua_State *L, int level)
#define lua_upvalueindex(i)
static int auxresume(lua_State *L, lua_State *co, int narg)
#define lua_insert(L, idx)

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 82 of file lua-5.2.4/src/lcorolib.c.

82 {
83 lua_State *NL;
85 NL = lua_newthread(L);
86 lua_pushvalue(L, 1); /* move function to top */
87 lua_xmove(L, NL, 1); /* move function from L to NL */
88 return 1;
89}
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API lua_State * lua_newthread(lua_State *L)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
#define LUA_TFUNCTION

References lua_newthread(), lua_pushvalue(), LUA_TFUNCTION, lua_xmove(), and luaL_checktype().

Referenced by luaB_cowrap().

◆ luaB_coresume()

static int luaB_coresume ( lua_State * L)
static

Definition at line 49 of file lua-5.2.4/src/lcorolib.c.

49 {
50 lua_State *co = lua_tothread(L, 1);
51 int r;
52 luaL_argcheck(L, co, 1, "coroutine expected");
53 r = auxresume(L, co, lua_gettop(L) - 1);
54 if (r < 0) {
55 lua_pushboolean(L, 0);
56 lua_insert(L, -2);
57 return 2; /* return false + error message */
58 }
59 else {
60 lua_pushboolean(L, 1);
61 lua_insert(L, -(r + 1));
62 return r + 1; /* return true + `resume' returns */
63 }
64}
LUA_API void lua_pushboolean(lua_State *L, int b)
#define luaL_argcheck(L, cond, numarg, extramsg)

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 132 of file lua-5.2.4/src/lcorolib.c.

132 {
133 int ismain = lua_pushthread(L);
134 lua_pushboolean(L, ismain);
135 return 2;
136}
LUA_API int lua_pushthread(lua_State *L)

References lua_pushboolean(), and lua_pushthread().

◆ luaB_costatus()

static int luaB_costatus ( lua_State * L)
static

Definition at line 104 of file lua-5.2.4/src/lcorolib.c.

104 {
105 lua_State *co = lua_tothread(L, 1);
106 luaL_argcheck(L, co, 1, "coroutine expected");
107 if (L == co) lua_pushliteral(L, "running");
108 else {
109 switch (lua_status(co)) {
110 case LUA_YIELD:
111 lua_pushliteral(L, "suspended");
112 break;
113 case LUA_OK: {
114 lua_Debug ar;
115 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
116 lua_pushliteral(L, "normal"); /* it is running */
117 else if (lua_gettop(co) == 0)
118 lua_pushliteral(L, "dead");
119 else
120 lua_pushliteral(L, "suspended"); /* initial state */
121 break;
122 }
123 default: /* some error occurred */
124 lua_pushliteral(L, "dead");
125 break;
126 }
127 }
128 return 1;
129}
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)

References lua_getstack(), lua_gettop(), LUA_OK, lua_pushliteral, lua_status(), lua_tothread(), LUA_YIELD, and luaL_argcheck.

◆ luaB_cowrap()

static int luaB_cowrap ( lua_State * L)
static

Definition at line 92 of file lua-5.2.4/src/lcorolib.c.

92 {
95 return 1;
96}
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
static int luaB_auxwrap(lua_State *L)
static int luaB_cocreate(lua_State *L)

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

◆ luaB_yield()

static int luaB_yield ( lua_State * L)
static

Definition at line 99 of file lua-5.2.4/src/lcorolib.c.

99 {
100 return lua_yield(L, lua_gettop(L));
101}
#define lua_yield(L, n)

References lua_gettop(), and lua_yield.

◆ luaopen_coroutine()

LUAMOD_API int luaopen_coroutine ( lua_State * L)

Definition at line 151 of file lua-5.2.4/src/lcorolib.c.

151 {
153 return 1;
154}
#define luaL_newlib(L, l)
static const luaL_Reg co_funcs[]

References co_funcs, and luaL_newlib.

Variable Documentation

◆ 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},
}
#define NULL
Definition gmacros.h:924
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 139 of file lua-5.2.4/src/lcorolib.c.

139 {
140 {"create", luaB_cocreate},
141 {"resume", luaB_coresume},
142 {"running", luaB_corunning},
143 {"status", luaB_costatus},
144 {"wrap", luaB_cowrap},
145 {"yield", luaB_yield},
146 {NULL, NULL}
147};

Referenced by luaopen_coroutine().