aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c79
1 files changed, 78 insertions, 1 deletions
diff --git a/lbaselib.c b/lbaselib.c
index b6d4ce8e..2aaafe43 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -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
431static 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
441static 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
448static 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
474static int luaB_yield (lua_State *L) {
475 return lua_yield(L, lua_gettop(L));
476}
477
478static const luaL_reg co_funcs[] = {
479 {"create", luaB_coroutine},
480 {"yield", luaB_yield},
481 {NULL, NULL}
482};
483
484
485static 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
424static void base_open (lua_State *L) { 500static 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
435LUALIB_API int lua_baselibopen (lua_State *L) { 511LUALIB_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);