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
 

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 luaB_costatus (lua_State *L)
 
static int luaB_yieldable (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 7 of file lua-5.3.6/src/lcorolib.c.

◆ LUA_LIB

#define LUA_LIB

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

Function Documentation

◆ auxresume()

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

Definition at line 28 of file lua-5.3.6/src/lcorolib.c.

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

◆ getco()

static lua_State * getco ( lua_State * L)
static

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

21 {
22 lua_State *co = lua_tothread(L, 1);
23 luaL_argcheck(L, co, 1, "thread expected");
24 return co;
25}
LUA_API lua_State * lua_tothread(lua_State *L, int idx)
#define luaL_argcheck(L, cond, numarg, extramsg)

References lua_tothread(), and luaL_argcheck.

Referenced by luaB_coresume(), and luaB_costatus().

◆ luaB_auxwrap()

static int luaB_auxwrap ( lua_State * L)
static

Definition at line 74 of file lua-5.3.6/src/lcorolib.c.

74 {
76 int r = auxresume(L, co, lua_gettop(L));
77 if (r < 0) {
78 if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */
79 luaL_where(L, 1); /* add extra info */
80 lua_insert(L, -2);
81 lua_concat(L, 2);
82 }
83 return lua_error(L); /* propagate error */
84 }
85 return r;
86}
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_TSTRING
#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_tothread(), LUA_TSTRING, lua_type(), lua_upvalueindex, and luaL_where().

Referenced by luaB_cowrap().

◆ luaB_cocreate()

static int luaB_cocreate ( lua_State * L)
static

Definition at line 89 of file lua-5.3.6/src/lcorolib.c.

89 {
90 lua_State *NL;
92 NL = lua_newthread(L);
93 lua_pushvalue(L, 1); /* move function to top */
94 lua_xmove(L, NL, 1); /* move function from L to NL */
95 return 1;
96}
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 57 of file lua-5.3.6/src/lcorolib.c.

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

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

◆ luaB_corunning()

static int luaB_corunning ( lua_State * L)
static

Definition at line 144 of file lua-5.3.6/src/lcorolib.c.

144 {
145 int ismain = lua_pushthread(L);
146 lua_pushboolean(L, ismain);
147 return 2;
148}
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 111 of file lua-5.3.6/src/lcorolib.c.

111 {
112 lua_State *co = getco(L);
113 if (L == co) lua_pushliteral(L, "running");
114 else {
115 switch (lua_status(co)) {
116 case LUA_YIELD:
117 lua_pushliteral(L, "suspended");
118 break;
119 case LUA_OK: {
120 lua_Debug ar;
121 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
122 lua_pushliteral(L, "normal"); /* it is running */
123 else if (lua_gettop(co) == 0)
124 lua_pushliteral(L, "dead");
125 else
126 lua_pushliteral(L, "suspended"); /* initial state */
127 break;
128 }
129 default: /* some error occurred */
130 lua_pushliteral(L, "dead");
131 break;
132 }
133 }
134 return 1;
135}
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)

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

◆ luaB_cowrap()

static int luaB_cowrap ( lua_State * L)
static

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

99 {
100 luaB_cocreate(L);
102 return 1;
103}
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 106 of file lua-5.3.6/src/lcorolib.c.

106 {
107 return lua_yield(L, lua_gettop(L));
108}
#define lua_yield(L, n)

References lua_gettop(), and lua_yield.

◆ luaB_yieldable()

static int luaB_yieldable ( lua_State * L)
static

Definition at line 138 of file lua-5.3.6/src/lcorolib.c.

138 {
140 return 1;
141}
LUA_API int lua_isyieldable(lua_State *L)

References lua_isyieldable(), and lua_pushboolean().

◆ luaopen_coroutine()

LUAMOD_API int luaopen_coroutine ( lua_State * L)

Definition at line 164 of file lua-5.3.6/src/lcorolib.c.

164 {
166 return 1;
167}
#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},
}
#define NULL
Definition gmacros.h:924
static int luaB_costatus(lua_State *L)
static int luaB_cowrap(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 151 of file lua-5.3.6/src/lcorolib.c.

151 {
152 {"create", luaB_cocreate},
153 {"resume", luaB_coresume},
154 {"running", luaB_corunning},
155 {"status", luaB_costatus},
156 {"wrap", luaB_cowrap},
157 {"yield", luaB_yield},
158 {"isyieldable", luaB_yieldable},
159 {NULL, NULL}
160};

Referenced by luaopen_coroutine().