Skip to content
Permalink
Browse files
* Add coroutine.error() call to abort a coroutine.
  • Loading branch information
jjensen committed Mar 8, 2011
1 parent 79cfee3 commit 663f15d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
@@ -19,6 +19,9 @@
#include "lauxlib.h"
#include "lualib.h"

#if LUA_EXT_RESUMABLEVM
#include "lstate.h"
#endif /* LUA_EXT_RESUMABLEVM */

NAMESPACE_LUA_BEGIN

@@ -742,6 +745,9 @@ static const char *const statnames[] =
static int costatus (lua_State *L, lua_State *co) {
if (L == co) return CO_RUN;
switch (lua_status(co)) {
#if LUA_EXT_RESUMABLEVM
case LUA_ERRFORCECO:
#endif /* LUA_EXT_RESUMABLEVM */
case LUA_YIELD:
return CO_SUS;
case 0: {
@@ -827,6 +833,19 @@ static int luaB_coresume (lua_State *L) {
}


#if LUA_EXT_RESUMABLEVM

static int luaB_coerror (lua_State *L) {
lua_State *co = lua_tothread(L, 1);
luaL_argcheck(L, co, 1, "coroutine expected");
co->status = LUA_ERRFORCECO;
luaB_coresume(L);
return 0;
}

#endif /* LUA_EXT_RESUMABLEVM */


static int luaB_auxwrap (lua_State *L) {
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
int r = auxresume(L, co, lua_gettop(L));
@@ -877,6 +896,9 @@ static int luaB_corunning (lua_State *L) {

static const luaL_Reg co_funcs[] = {
{"create", luaB_cocreate},
#if LUA_EXT_RESUMABLEVM
{"error", luaB_coerror},
#endif /* LUA_EXT_RESUMABLEVM */
{"resume", luaB_coresume},
{"running", luaB_corunning},
{"status", luaB_costatus},
@@ -618,6 +618,12 @@ static int f_costart (lua_State *L, void *ud) {
}


static int f_coerror (lua_State *L, void *ud) {
lua_pushstring(L, "*error");
lua_error(L);
return 0;
}

#else

void luaD_call (lua_State *L, StkId func, int nResults) {
@@ -676,6 +682,10 @@ LUA_API int lua_resume (lua_State *L, int nargs) {
int status;
lua_lock(L);
switch (L->status) {
case LUA_ERRFORCECO:
pf = f_coerror;
ud = 0;
break;
case LUA_YIELD:
pf = f_coresume;
ud = L->top - nargs;
@@ -51,6 +51,7 @@ LUA_EXTERN_C_BEGIN
#define LUA_ERRERR 5
#if LUA_EXT_RESUMABLEVM
#define LUA_ERREXC 6
#define LUA_ERRFORCECO 7
#endif /* LUA_EXT_RESUMABLEVM */

typedef struct lua_State lua_State;

0 comments on commit 663f15d

Please sign in to comment.