aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-01-09 20:02:47 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-01-09 20:02:47 -0200
commitf083812c020186d0d919833100c1a0b6eda8c2c0 (patch)
tree29c2e1d25f05af62277aab03e8e070aaa1a0d664 /lbaselib.c
parent3533382a1ed7ba21f0233057c886be2dd8a71d92 (diff)
downloadlua-f083812c020186d0d919833100c1a0b6eda8c2c0.tar.gz
lua-f083812c020186d0d919833100c1a0b6eda8c2c0.tar.bz2
lua-f083812c020186d0d919833100c1a0b6eda8c2c0.zip
first implementation of coroutines
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c81
1 files changed, 46 insertions, 35 deletions
diff --git a/lbaselib.c b/lbaselib.c
index cb914f18..6cd97d95 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -148,30 +148,6 @@ static int luaB_eventtable (lua_State *L) {
148} 148}
149 149
150 150
151static int luaB_weakmode (lua_State *L) {
152 const char *mode = luaL_check_string(L, 2);
153 luaL_check_type(L, 1, LUA_TTABLE);
154 if (*mode == '?') {
155 char buff[3];
156 char *s = buff;
157 int imode = lua_getweakmode(L, 1);
158 if (imode & LUA_WEAK_KEY) *s++ = 'k';
159 if (imode & LUA_WEAK_VALUE) *s++ = 'v';
160 *s = '\0';
161 lua_pushstring(L, buff);
162 return 1;
163 }
164 else {
165 int imode = 0;
166 if (strchr(mode, 'k')) imode |= LUA_WEAK_KEY;
167 if (strchr(mode, 'v')) imode |= LUA_WEAK_VALUE;
168 lua_pushvalue(L, 1); /* push table */
169 lua_setweakmode(L, imode);
170 return 1; /* return the table */
171 }
172}
173
174
175static int luaB_globals (lua_State *L) { 151static int luaB_globals (lua_State *L) {
176 lua_getglobals(L); /* value to be returned */ 152 lua_getglobals(L); /* value to be returned */
177 if (!lua_isnone(L, 1)) { 153 if (!lua_isnone(L, 1)) {
@@ -287,6 +263,14 @@ static int luaB_loadfile (lua_State *L) {
287} 263}
288 264
289 265
266static int luaB_assert (lua_State *L) {
267 luaL_check_any(L, 1);
268 if (!lua_istrue(L, 1))
269 luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
270 lua_settop(L, 1);
271 return 1;
272}
273
290 274
291#define LUA_PATH "LUA_PATH" 275#define LUA_PATH "LUA_PATH"
292 276
@@ -428,6 +412,40 @@ static int luaB_tostring (lua_State *L) {
428} 412}
429 413
430 414
415static int luaB_resume (lua_State *L) {
416 lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1));
417 lua_resume(L, co);
418 return 0;
419}
420
421
422static int luaB_coroutine (lua_State *L) {
423 lua_State *NL;
424 int ref;
425 luaL_check_type(L, 1, LUA_TFUNCTION);
426 NL = lua_newthread(L, 0);
427 if (NL == NULL) lua_error(L, "unable to create new thread");
428 /* move function from L to NL */
429 ref = lua_ref(L, 1);
430 lua_getref(NL, ref);
431 lua_unref(L, ref);
432 lua_cobegin(NL, 0);
433 lua_newuserdatabox(L, NL);
434 lua_pushcclosure(L, luaB_resume, 1);
435 return 1;
436}
437
438
439static int luaB_yield (lua_State *L) {
440 return lua_yield(L, 0);
441}
442
443
444/*
445** {======================================================
446** Auxiliar table-related functions
447*/
448
431static int luaB_foreachi (lua_State *L) { 449static int luaB_foreachi (lua_State *L) {
432 int n, i; 450 int n, i;
433 luaL_check_type(L, 1, LUA_TTABLE); 451 luaL_check_type(L, 1, LUA_TTABLE);
@@ -464,15 +482,6 @@ static int luaB_foreach (lua_State *L) {
464} 482}
465 483
466 484
467static int luaB_assert (lua_State *L) {
468 luaL_check_any(L, 1);
469 if (!lua_istrue(L, 1))
470 luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
471 lua_settop(L, 1);
472 return 1;
473}
474
475
476static int luaB_getn (lua_State *L) { 485static int luaB_getn (lua_State *L) {
477 luaL_check_type(L, 1, LUA_TTABLE); 486 luaL_check_type(L, 1, LUA_TTABLE);
478 lua_pushnumber(L, lua_getn(L, 1)); 487 lua_pushnumber(L, lua_getn(L, 1));
@@ -521,7 +530,6 @@ static int luaB_tremove (lua_State *L) {
521 530
522 531
523 532
524
525/* 533/*
526** {====================================================== 534** {======================================================
527** Quicksort 535** Quicksort
@@ -627,6 +635,8 @@ static int luaB_sort (lua_State *L) {
627 635
628/* }====================================================== */ 636/* }====================================================== */
629 637
638/* }====================================================== */
639
630 640
631 641
632static const luaL_reg base_funcs[] = { 642static const luaL_reg base_funcs[] = {
@@ -634,6 +644,7 @@ static const luaL_reg base_funcs[] = {
634 {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, 644 {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
635 {"call", luaB_call}, 645 {"call", luaB_call},
636 {"collectgarbage", luaB_collectgarbage}, 646 {"collectgarbage", luaB_collectgarbage},
647 {"coroutine", luaB_coroutine},
637 {"dofile", luaB_dofile}, 648 {"dofile", luaB_dofile},
638 {"dostring", luaB_dostring}, 649 {"dostring", luaB_dostring},
639 {"error", luaB_error}, 650 {"error", luaB_error},
@@ -659,7 +670,7 @@ static const luaL_reg base_funcs[] = {
659 {"tinsert", luaB_tinsert}, 670 {"tinsert", luaB_tinsert},
660 {"tremove", luaB_tremove}, 671 {"tremove", luaB_tremove},
661 {"unpack", luaB_unpack}, 672 {"unpack", luaB_unpack},
662 {"weakmode", luaB_weakmode} 673 {"yield", luaB_yield}
663}; 674};
664 675
665 676