aboutsummaryrefslogtreecommitdiff
path: root/lcorolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-08 10:52:20 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-08 10:52:20 -0300
commit1bd70a8e40a8600658c706d5f171138e9b902aba (patch)
tree9b4408457bbd03a3fda83b21bbf54dba56234d08 /lcorolib.c
parentef83457427cdb2d9f29d94177131db8e6e2eb606 (diff)
downloadlua-1bd70a8e40a8600658c706d5f171138e9b902aba.tar.gz
lua-1bd70a8e40a8600658c706d5f171138e9b902aba.tar.bz2
lua-1bd70a8e40a8600658c706d5f171138e9b902aba.zip
new function 'lua_isyieldable' (and 'coroutine.isyieldable')
Diffstat (limited to 'lcorolib.c')
-rw-r--r--lcorolib.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/lcorolib.c b/lcorolib.c
index cd7e4e1e..4bcb7e9a 100644
--- a/lcorolib.c
+++ b/lcorolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcorolib.c,v 1.4 2012/04/27 18:59:04 roberto Exp roberto $ 2** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp roberto $
3** Coroutine Library 3** Coroutine Library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -17,6 +17,13 @@
17#include "lualib.h" 17#include "lualib.h"
18 18
19 19
20static lua_State *getco (lua_State *L) {
21 lua_State *co = lua_tothread(L, 1);
22 luaL_argcheck(L, co, 1, "coroutine expected");
23 return co;
24}
25
26
20static int auxresume (lua_State *L, lua_State *co, int narg) { 27static int auxresume (lua_State *L, lua_State *co, int narg) {
21 int status; 28 int status;
22 if (!lua_checkstack(co, narg)) { 29 if (!lua_checkstack(co, narg)) {
@@ -47,9 +54,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) {
47 54
48 55
49static int luaB_coresume (lua_State *L) { 56static int luaB_coresume (lua_State *L) {
50 lua_State *co = lua_tothread(L, 1); 57 lua_State *co = getco(L);
51 int r; 58 int r;
52 luaL_argcheck(L, co, 1, "coroutine expected");
53 r = auxresume(L, co, lua_gettop(L) - 1); 59 r = auxresume(L, co, lua_gettop(L) - 1);
54 if (r < 0) { 60 if (r < 0) {
55 lua_pushboolean(L, 0); 61 lua_pushboolean(L, 0);
@@ -102,8 +108,7 @@ static int luaB_yield (lua_State *L) {
102 108
103 109
104static int luaB_costatus (lua_State *L) { 110static int luaB_costatus (lua_State *L) {
105 lua_State *co = lua_tothread(L, 1); 111 lua_State *co = getco(L);
106 luaL_argcheck(L, co, 1, "coroutine expected");
107 if (L == co) lua_pushliteral(L, "running"); 112 if (L == co) lua_pushliteral(L, "running");
108 else { 113 else {
109 switch (lua_status(co)) { 114 switch (lua_status(co)) {
@@ -129,6 +134,12 @@ static int luaB_costatus (lua_State *L) {
129} 134}
130 135
131 136
137static int luaB_yieldable (lua_State *L) {
138 lua_pushboolean(L, lua_isyieldable(L));
139 return 1;
140}
141
142
132static int luaB_corunning (lua_State *L) { 143static int luaB_corunning (lua_State *L) {
133 int ismain = lua_pushthread(L); 144 int ismain = lua_pushthread(L);
134 lua_pushboolean(L, ismain); 145 lua_pushboolean(L, ismain);
@@ -143,6 +154,7 @@ static const luaL_Reg co_funcs[] = {
143 {"status", luaB_costatus}, 154 {"status", luaB_costatus},
144 {"wrap", luaB_cowrap}, 155 {"wrap", luaB_cowrap},
145 {"yield", luaB_yield}, 156 {"yield", luaB_yield},
157 {"isyieldable", luaB_yieldable},
146 {NULL, NULL} 158 {NULL, NULL}
147}; 159};
148 160