Esempio n. 1
0
static int auxresume(lua_State* L, lua_State* co, int narg)
{
	int status = costatus(L, co);
	if (!lua_checkstack(co, narg))
		luaL_error(L, "too many arguments to resume");
	if (status != CO_SUS)
	{
		lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
		return -1;	/* error flag */
	}
	lua_xmove(L, co, narg);
	lua_setlevel(L, co);
	status = lua_resume(co, narg);
	if (status == 0 || status == LUA_YIELD)
	{
		int nres = lua_gettop(co);
		if (!lua_checkstack(L, nres + 1))
			luaL_error(L, "too many results to resume");
		lua_xmove(co, L, nres);  /* move yielded values */
		return nres;
	}
	else
	{
		lua_xmove(co, L, 1);  /* move error message */
		return -1;	/* error flag */
	}
}
Esempio n. 2
0
static int
lua_general_coroutine_spawn(lua_State *Lp) {
  int nargs;
  lua_State *L;
  noit_lua_resume_info_t *ri_parent = NULL, *ri = NULL;

  nargs = lua_gettop(Lp);
  if(nargs < 1 || !lua_isfunction(Lp,1))
    luaL_error(Lp, "noit.coroutine_spawn(func, ...): bad arguments");
  ri_parent = noit_lua_get_resume_info(Lp);
  assert(ri_parent);

  ri = lua_general_new_resume_info(ri_parent->lmc);
  L = ri->coro_state;
  lua_xmove(Lp, L, nargs);
#if !defined(LUA_JITLIBNAME) && LUA_VERSION_NUM < 502
  lua_setlevel(Lp, L);
#endif
  ri->lmc->resume(ri, nargs-1);
  return 0;
}