annotate lib/lua/lua-5.2.2/lcorolib.c @ 2575:a76d408c5132 tip

DrawTranslucent -> DrawTextureGrayShade Removed old texture drawing stuff
author a.parshin
date Wed, 09 Mar 2016 01:39:52 +0200
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Coroutine Library
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define lcorolib_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #define LUA_LIB
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lauxlib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lualib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 static int auxresume (lua_State *L, lua_State *co, int narg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 if (!lua_checkstack(co, narg)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 lua_pushliteral(L, "too many arguments to resume");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 return -1; /* error flag */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 lua_pushliteral(L, "cannot resume dead coroutine");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 return -1; /* error flag */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 lua_xmove(L, co, narg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 status = lua_resume(co, L, narg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 if (status == LUA_OK || status == LUA_YIELD) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 int nres = lua_gettop(co);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 if (!lua_checkstack(L, nres + 1)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 lua_pop(co, nres); /* remove results anyway */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 lua_pushliteral(L, "too many results to resume");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 return -1; /* error flag */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 lua_xmove(co, L, nres); /* move yielded values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 return nres;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 lua_xmove(co, L, 1); /* move error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 return -1; /* error flag */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 static int luaB_coresume (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 lua_State *co = lua_tothread(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 int r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 luaL_argcheck(L, co, 1, "coroutine expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 r = auxresume(L, co, lua_gettop(L) - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 if (r < 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 lua_pushboolean(L, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 lua_insert(L, -2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 return 2; /* return false + error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 lua_pushboolean(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 lua_insert(L, -(r + 1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 return r + 1; /* return true + `resume' returns */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 static int luaB_auxwrap (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 int r = auxresume(L, co, lua_gettop(L));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 if (r < 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 if (lua_isstring(L, -1)) { /* error object is a string? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 luaL_where(L, 1); /* add extra info */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 lua_insert(L, -2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 lua_concat(L, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 return lua_error(L); /* propagate error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 return r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 static int luaB_cocreate (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 lua_State *NL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 luaL_checktype(L, 1, LUA_TFUNCTION);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 NL = lua_newthread(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 lua_pushvalue(L, 1); /* move function to top */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 lua_xmove(L, NL, 1); /* move function from L to NL */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 static int luaB_cowrap (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 luaB_cocreate(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 lua_pushcclosure(L, luaB_auxwrap, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 static int luaB_yield (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 return lua_yield(L, lua_gettop(L));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 static int luaB_costatus (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 lua_State *co = lua_tothread(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 luaL_argcheck(L, co, 1, "coroutine expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 if (L == co) lua_pushliteral(L, "running");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 switch (lua_status(co)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 case LUA_YIELD:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 lua_pushliteral(L, "suspended");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 case LUA_OK: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 lua_Debug ar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 lua_pushliteral(L, "normal"); /* it is running */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 else if (lua_gettop(co) == 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 lua_pushliteral(L, "dead");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 lua_pushliteral(L, "suspended"); /* initial state */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 default: /* some error occurred */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 lua_pushliteral(L, "dead");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 static int luaB_corunning (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 int ismain = lua_pushthread(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 lua_pushboolean(L, ismain);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 return 2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 static const luaL_Reg co_funcs[] = {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 {"create", luaB_cocreate},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 {"resume", luaB_coresume},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 {"running", luaB_corunning},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 {"status", luaB_costatus},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 {"wrap", luaB_cowrap},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 {"yield", luaB_yield},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 {NULL, NULL}
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 LUAMOD_API int luaopen_coroutine (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 luaL_newlib(L, co_funcs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155