Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lcorolib.c File Reference
#include "lprefix.h"
#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
 
#define COS_RUN   0
 
#define COS_DEAD   1
 
#define COS_YIELD   2
 
#define COS_NORM   3
 

Functions

static lua_Stategetco (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 auxstatus (lua_State *L, lua_State *co)
 
static int luaB_costatus (lua_State *L)
 
static int luaB_yieldable (lua_State *L)
 
static int luaB_corunning (lua_State *L)
 
static int luaB_close (lua_State *L)
 
LUAMOD_API int luaopen_coroutine (lua_State *L)
 

Variables

static const char *const statname []
 
static const luaL_Reg co_funcs []
 

Macro Definition Documentation

◆ COS_DEAD

#define COS_DEAD   1

Definition at line 118 of file lua-5.4.3/src/lcorolib.c.

Referenced by auxstatus(), and luaB_close().

◆ COS_NORM

#define COS_NORM   3

Definition at line 120 of file lua-5.4.3/src/lcorolib.c.

Referenced by auxstatus().

◆ COS_RUN

#define COS_RUN   0

Definition at line 117 of file lua-5.4.3/src/lcorolib.c.

Referenced by auxstatus().

◆ COS_YIELD

#define COS_YIELD   2

Definition at line 119 of file lua-5.4.3/src/lcorolib.c.

Referenced by auxstatus(), and luaB_close().

◆ lcorolib_c

#define lcorolib_c

Definition at line 7 of file lua-5.4.3/src/lcorolib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.4.3/src/lcorolib.c.

Function Documentation

◆ auxresume()

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

Definition at line 32 of file lua-5.4.3/src/lcorolib.c.

32 {
33 int status, nres;
34 if (l_unlikely(!lua_checkstack(co, narg))) {
35 lua_pushliteral(L, "too many arguments to resume");
36 return -1; /* error flag */
37 }
38 lua_xmove(L, co, narg);
39 status = lua_resume(co, L, narg, &nres);
40 if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
41 if (l_unlikely(!lua_checkstack(L, nres + 1))) {
42 lua_pop(co, nres); /* remove results anyway */
43 lua_pushliteral(L, "too many results to resume");
44 return -1; /* error flag */
45 }
46 lua_xmove(co, L, nres); /* move yielded values */
47 return nres;
48 }
49 else {
50 lua_xmove(co, L, 1); /* move error message */
51 return -1; /* error flag */
52 }
53}
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_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_OK, lua_pop, lua_pushliteral, lua_resume(), lua_xmove(), and LUA_YIELD.

Referenced by luaB_auxwrap(), and luaB_coresume().

◆ auxstatus()

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

Definition at line 127 of file lua-5.4.3/src/lcorolib.c.

127 {
128 if (L == co) return COS_RUN;
129 else {
130 switch (lua_status(co)) {
131 case LUA_YIELD:
132 return COS_YIELD;
133 case LUA_OK: {
134 lua_Debug ar;
135 if (lua_getstack(co, 0, &ar)) /* does it have frames? */
136 return COS_NORM; /* it is running */
137 else if (lua_gettop(co) == 0)
138 return COS_DEAD;
139 else
140 return COS_YIELD; /* initial state */
141 }
142 default: /* some error occurred */
143 return COS_DEAD;
144 }
145 }
146}
LUA_API int lua_status(lua_State *L)
LUA_API int lua_gettop(lua_State *L)
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)
#define COS_DEAD
#define COS_NORM
#define COS_YIELD
#define COS_RUN

References COS_DEAD, COS_NORM, COS_RUN, COS_YIELD, lua_getstack(), lua_gettop(), LUA_OK, lua_status(), and LUA_YIELD.

Referenced by luaB_close(), and luaB_costatus().

◆ getco()

static lua_State * getco ( lua_State * L)
static

Definition at line 21 of file lua-5.4.3/src/lcorolib.c.

21 {
22 lua_State *co = lua_tothread(L, 1);
23 luaL_argexpected(L, co, 1, "thread");
24 return co;
25}
LUA_API lua_State * lua_tothread(lua_State *L, int idx)
#define luaL_argexpected(L, cond, arg, tname)

References lua_tothread(), and luaL_argexpected.

Referenced by luaB_close(), luaB_coresume(), luaB_costatus(), and luaB_yieldable().

◆ luaB_auxwrap()

static int luaB_auxwrap ( lua_State * L)
static

Definition at line 73 of file lua-5.4.3/src/lcorolib.c.

73 {
75 int r = auxresume(L, co, lua_gettop(L));
76 if (l_unlikely(r < 0)) { /* error? */
77 int stat = lua_status(co);
78 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
79 stat = lua_resetthread(co); /* close its tbc variables */
80 lua_assert(stat != LUA_OK);
81 lua_xmove(co, L, 1); /* copy error message */
82 }
83 if (stat != LUA_ERRMEM && /* not a memory error and ... */
84 lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
85 luaL_where(L, 1); /* add extra info, if available */
86 lua_insert(L, -2);
87 lua_concat(L, 2);
88 }
89 return lua_error(L); /* propagate error */
90 }
91 return r;
92}
LUA_API void lua_concat(lua_State *L, int n)
LUA_API int lua_type(lua_State *L, int idx)
LUA_API int lua_error(lua_State *L)
LUALIB_API void luaL_where(lua_State *L, int level)
#define lua_assert(c)
#define LUA_TSTRING
#define LUA_ERRMEM
#define lua_upvalueindex(i)
#define lua_insert(L, idx)
static int auxresume(lua_State *L, lua_State *co, int narg)
LUA_API int lua_resetthread(lua_State *L)

References auxresume(), lua_assert, lua_concat(), LUA_ERRMEM, lua_error(), lua_gettop(), lua_insert, LUA_OK, lua_resetthread(), lua_status(), lua_tothread(), LUA_TSTRING, lua_type(), lua_upvalueindex, lua_xmove(), LUA_YIELD, and luaL_where().

Referenced by luaB_cowrap().

◆ luaB_close()

static int luaB_close ( lua_State * L)
static

Definition at line 170 of file lua-5.4.3/src/lcorolib.c.

170 {
171 lua_State *co = getco(L);
172 int status = auxstatus(L, co);
173 switch (status) {
174 case COS_DEAD: case COS_YIELD: {
175 status = lua_resetthread(co);
176 if (status == LUA_OK) {
177 lua_pushboolean(L, 1);
178 return 1;
179 }
180 else {
181 lua_pushboolean(L, 0);
182 lua_xmove(co, L, 1); /* copy error message */
183 return 2;
184 }
185 }
186 default: /* normal or running coroutine */
187 return luaL_error(L, "cannot close a %s coroutine", statname[status]);
188 }
189}
LUA_API void lua_pushboolean(lua_State *L, int b)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
static const char *const statname[]
static lua_State * getco(lua_State *L)
static int auxstatus(lua_State *L, lua_State *co)

References auxstatus(), COS_DEAD, COS_YIELD, getco(), LUA_OK, lua_pushboolean(), lua_resetthread(), lua_xmove(), luaL_error(), and statname.

◆ luaB_cocreate()

static int luaB_cocreate ( lua_State * L)
static

Definition at line 95 of file lua-5.4.3/src/lcorolib.c.

95 {
96 lua_State *NL;
98 NL = lua_newthread(L);
99 lua_pushvalue(L, 1); /* move function to top */
100 lua_xmove(L, NL, 1); /* move function from L to NL */
101 return 1;
102}
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 56 of file lua-5.4.3/src/lcorolib.c.

56 {
57 lua_State *co = getco(L);
58 int r;
59 r = auxresume(L, co, lua_gettop(L) - 1);
60 if (l_unlikely(r < 0)) {
61 lua_pushboolean(L, 0);
62 lua_insert(L, -2);
63 return 2; /* return false + error message */
64 }
65 else {
66 lua_pushboolean(L, 1);
67 lua_insert(L, -(r + 1));
68 return r + 1; /* return true + 'resume' returns */
69 }
70}

References auxresume(), getco(), lua_gettop(), lua_insert, and lua_pushboolean().

◆ luaB_corunning()

static int luaB_corunning ( lua_State * L)
static

Definition at line 163 of file lua-5.4.3/src/lcorolib.c.

163 {
164 int ismain = lua_pushthread(L);
165 lua_pushboolean(L, ismain);
166 return 2;
167}
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 149 of file lua-5.4.3/src/lcorolib.c.

149 {
150 lua_State *co = getco(L);
152 return 1;
153}
LUA_API void lua_pushstring(lua_State *L, const char *s)

References auxstatus(), getco(), lua_pushstring(), and statname.

◆ luaB_cowrap()

static int luaB_cowrap ( lua_State * L)
static

Definition at line 105 of file lua-5.4.3/src/lcorolib.c.

105 {
106 luaB_cocreate(L);
108 return 1;
109}
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 112 of file lua-5.4.3/src/lcorolib.c.

112 {
113 return lua_yield(L, lua_gettop(L));
114}
#define lua_yield(L, n)

References lua_gettop(), and lua_yield.

◆ luaB_yieldable()

static int luaB_yieldable ( lua_State * L)
static

Definition at line 156 of file lua-5.4.3/src/lcorolib.c.

156 {
157 lua_State *co = lua_isnone(L, 1) ? L : getco(L);
159 return 1;
160}
#define lua_isnone(L, n)
LUA_API int lua_isyieldable(lua_State *L)

References getco(), lua_isnone, lua_isyieldable(), and lua_pushboolean().

◆ luaopen_coroutine()

LUAMOD_API int luaopen_coroutine ( lua_State * L)

Definition at line 206 of file lua-5.4.3/src/lcorolib.c.

206 {
208 return 1;
209}
#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},
{"isyieldable", luaB_yieldable},
{"close", luaB_close},
}
#define NULL
Definition gmacros.h:924
static int luaB_costatus(lua_State *L)
static int luaB_cowrap(lua_State *L)
static int luaB_close(lua_State *L)
static int luaB_yieldable(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 192 of file lua-5.4.3/src/lcorolib.c.

192 {
193 {"create", luaB_cocreate},
194 {"resume", luaB_coresume},
195 {"running", luaB_corunning},
196 {"status", luaB_costatus},
197 {"wrap", luaB_cowrap},
198 {"yield", luaB_yield},
199 {"isyieldable", luaB_yieldable},
200 {"close", luaB_close},
201 {NULL, NULL}
202};

Referenced by luaopen_coroutine().

◆ statname

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

Definition at line 123 of file lua-5.4.3/src/lcorolib.c.

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

Referenced by luaB_close(), and luaB_costatus().