diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-04-12 16:57:29 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-04-12 16:57:29 -0300 |
| commit | ae5283dc368b7b2d5f0811f7b74860b46774bd8d (patch) | |
| tree | a63d3c280e6695e00ad57044403c281e5d8b2ba2 | |
| parent | 1ce6cb6032d1a25a91beb101d2b790d273670204 (diff) | |
| download | lua-ae5283dc368b7b2d5f0811f7b74860b46774bd8d.tar.gz lua-ae5283dc368b7b2d5f0811f7b74860b46774bd8d.tar.bz2 lua-ae5283dc368b7b2d5f0811f7b74860b46774bd8d.zip | |
`co' library goes with basic library (and not with `tab')
| -rw-r--r-- | lbaselib.c | 79 | ||||
| -rw-r--r-- | ltablib.c | 78 |
2 files changed, 79 insertions, 78 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.65 2002/04/09 19:46:56 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.66 2002/04/09 20:19:06 roberto Exp roberto $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -421,6 +421,82 @@ static const luaL_reg base_funcs[] = { | |||
| 421 | }; | 421 | }; |
| 422 | 422 | ||
| 423 | 423 | ||
| 424 | /* | ||
| 425 | ** {====================================================== | ||
| 426 | ** Coroutine library | ||
| 427 | ** ======================================================= | ||
| 428 | */ | ||
| 429 | |||
| 430 | |||
| 431 | static int luaB_resume (lua_State *L) { | ||
| 432 | lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); | ||
| 433 | lua_settop(L, 0); | ||
| 434 | if (lua_resume(L, co) != 0) | ||
| 435 | lua_error(L, "error running co-routine"); | ||
| 436 | return lua_gettop(L); | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | |||
| 441 | static int gc_coroutine (lua_State *L) { | ||
| 442 | lua_State *co = (lua_State *)lua_getfrombox(L, 1); | ||
| 443 | lua_closethread(L, co); | ||
| 444 | return 0; | ||
| 445 | } | ||
| 446 | |||
| 447 | |||
| 448 | static int luaB_coroutine (lua_State *L) { | ||
| 449 | lua_State *NL; | ||
| 450 | int ref; | ||
| 451 | int i; | ||
| 452 | int n = lua_gettop(L); | ||
| 453 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, | ||
| 454 | "Lua function expected"); | ||
| 455 | NL = lua_newthread(L); | ||
| 456 | if (NL == NULL) lua_error(L, "unable to create new thread"); | ||
| 457 | /* move function and arguments from L to NL */ | ||
| 458 | for (i=0; i<n; i++) { | ||
| 459 | ref = lua_ref(L, 1); | ||
| 460 | lua_getref(NL, ref); | ||
| 461 | lua_insert(NL, 1); | ||
| 462 | lua_unref(L, ref); | ||
| 463 | } | ||
| 464 | lua_cobegin(NL, n-1); | ||
| 465 | lua_newpointerbox(L, NL); | ||
| 466 | lua_pushliteral(L, "Coroutine"); | ||
| 467 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 468 | lua_setmetatable(L, -2); | ||
| 469 | lua_pushcclosure(L, luaB_resume, 1); | ||
| 470 | return 1; | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | static int luaB_yield (lua_State *L) { | ||
| 475 | return lua_yield(L, lua_gettop(L)); | ||
| 476 | } | ||
| 477 | |||
| 478 | static const luaL_reg co_funcs[] = { | ||
| 479 | {"create", luaB_coroutine}, | ||
| 480 | {"yield", luaB_yield}, | ||
| 481 | {NULL, NULL} | ||
| 482 | }; | ||
| 483 | |||
| 484 | |||
| 485 | static void co_open (lua_State *L) { | ||
| 486 | luaL_opennamedlib(L, "co", co_funcs, 0); | ||
| 487 | /* create metatable for coroutines */ | ||
| 488 | lua_pushliteral(L, "Coroutine"); | ||
| 489 | lua_newtable(L); | ||
| 490 | lua_pushliteral(L, "__gc"); | ||
| 491 | lua_pushcfunction(L, gc_coroutine); | ||
| 492 | lua_rawset(L, -3); | ||
| 493 | lua_rawset(L, LUA_REGISTRYINDEX); | ||
| 494 | } | ||
| 495 | |||
| 496 | /* }====================================================== */ | ||
| 497 | |||
| 498 | |||
| 499 | |||
| 424 | static void base_open (lua_State *L) { | 500 | static void base_open (lua_State *L) { |
| 425 | lua_pushliteral(L, "_G"); | 501 | lua_pushliteral(L, "_G"); |
| 426 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 502 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| @@ -434,6 +510,7 @@ static void base_open (lua_State *L) { | |||
| 434 | 510 | ||
| 435 | LUALIB_API int lua_baselibopen (lua_State *L) { | 511 | LUALIB_API int lua_baselibopen (lua_State *L) { |
| 436 | base_open(L); | 512 | base_open(L); |
| 513 | co_open(L); | ||
| 437 | /* `require' needs an empty table as upvalue */ | 514 | /* `require' needs an empty table as upvalue */ |
| 438 | lua_newtable(L); | 515 | lua_newtable(L); |
| 439 | lua_pushcclosure(L, luaB_require, 1); | 516 | lua_pushcclosure(L, luaB_require, 1); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: $ | 2 | ** $Id: ltablib.c,v 1.1 2002/04/09 20:19:06 roberto Exp roberto $ |
| 3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -214,81 +214,6 @@ static int luaB_sort (lua_State *L) { | |||
| 214 | /* }====================================================== */ | 214 | /* }====================================================== */ |
| 215 | 215 | ||
| 216 | 216 | ||
| 217 | /* | ||
| 218 | ** {====================================================== | ||
| 219 | ** Coroutine library | ||
| 220 | ** ======================================================= | ||
| 221 | */ | ||
| 222 | |||
| 223 | |||
| 224 | static int luaB_resume (lua_State *L) { | ||
| 225 | lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); | ||
| 226 | lua_settop(L, 0); | ||
| 227 | if (lua_resume(L, co) != 0) | ||
| 228 | lua_error(L, "error running co-routine"); | ||
| 229 | return lua_gettop(L); | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | static int gc_coroutine (lua_State *L) { | ||
| 235 | lua_State *co = (lua_State *)lua_getfrombox(L, 1); | ||
| 236 | lua_closethread(L, co); | ||
| 237 | return 0; | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | static int luaB_coroutine (lua_State *L) { | ||
| 242 | lua_State *NL; | ||
| 243 | int ref; | ||
| 244 | int i; | ||
| 245 | int n = lua_gettop(L); | ||
| 246 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, | ||
| 247 | "Lua function expected"); | ||
| 248 | NL = lua_newthread(L); | ||
| 249 | if (NL == NULL) lua_error(L, "unable to create new thread"); | ||
| 250 | /* move function and arguments from L to NL */ | ||
| 251 | for (i=0; i<n; i++) { | ||
| 252 | ref = lua_ref(L, 1); | ||
| 253 | lua_getref(NL, ref); | ||
| 254 | lua_insert(NL, 1); | ||
| 255 | lua_unref(L, ref); | ||
| 256 | } | ||
| 257 | lua_cobegin(NL, n-1); | ||
| 258 | lua_newpointerbox(L, NL); | ||
| 259 | lua_pushliteral(L, "Coroutine"); | ||
| 260 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 261 | lua_setmetatable(L, -2); | ||
| 262 | lua_pushcclosure(L, luaB_resume, 1); | ||
| 263 | return 1; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | static int luaB_yield (lua_State *L) { | ||
| 268 | return lua_yield(L, lua_gettop(L)); | ||
| 269 | } | ||
| 270 | |||
| 271 | static const luaL_reg co_funcs[] = { | ||
| 272 | {"create", luaB_coroutine}, | ||
| 273 | {"yield", luaB_yield}, | ||
| 274 | {NULL, NULL} | ||
| 275 | }; | ||
| 276 | |||
| 277 | |||
| 278 | static void co_open (lua_State *L) { | ||
| 279 | luaL_opennamedlib(L, "co", co_funcs, 0); | ||
| 280 | /* create metatable for coroutines */ | ||
| 281 | lua_pushliteral(L, "Coroutine"); | ||
| 282 | lua_newtable(L); | ||
| 283 | lua_pushliteral(L, "__gc"); | ||
| 284 | lua_pushcfunction(L, gc_coroutine); | ||
| 285 | lua_rawset(L, -3); | ||
| 286 | lua_rawset(L, LUA_REGISTRYINDEX); | ||
| 287 | } | ||
| 288 | |||
| 289 | /* }====================================================== */ | ||
| 290 | |||
| 291 | |||
| 292 | static const luaL_reg tab_funcs[] = { | 217 | static const luaL_reg tab_funcs[] = { |
| 293 | {"foreach", luaB_foreach}, | 218 | {"foreach", luaB_foreach}, |
| 294 | {"foreachi", luaB_foreachi}, | 219 | {"foreachi", luaB_foreachi}, |
| @@ -302,7 +227,6 @@ static const luaL_reg tab_funcs[] = { | |||
| 302 | 227 | ||
| 303 | LUALIB_API int lua_tablibopen (lua_State *L) { | 228 | LUALIB_API int lua_tablibopen (lua_State *L) { |
| 304 | luaL_opennamedlib(L, "tab", tab_funcs, 0); | 229 | luaL_opennamedlib(L, "tab", tab_funcs, 0); |
| 305 | co_open(L); | ||
| 306 | return 0; | 230 | return 0; |
| 307 | } | 231 | } |
| 308 | 232 | ||
