aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c148
1 files changed, 83 insertions, 65 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 363acdd9..c760172c 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ 2** $Id: lbaselib.c,v 1.59 2002/02/14 21:42:22 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*/
@@ -19,12 +19,6 @@
19 19
20 20
21 21
22static void aux_setn (lua_State *L, int t, int n) {
23 lua_pushliteral(L, "n");
24 lua_pushnumber(L, n);
25 lua_rawset(L, t);
26}
27
28 22
29/* 23/*
30** If your system does not support `stderr', redefine this function, or 24** If your system does not support `stderr', redefine this function, or
@@ -124,17 +118,6 @@ static int luaB_error (lua_State *L) {
124 return 0; /* to avoid warnings */ 118 return 0; /* to avoid warnings */
125} 119}
126 120
127static int luaB_setglobal (lua_State *L) {
128 luaL_check_any(L, 2);
129 lua_setglobal(L, luaL_check_string(L, 1));
130 return 0;
131}
132
133static int luaB_getglobal (lua_State *L) {
134 lua_getglobal(L, luaL_check_string(L, 1));
135 return 1;
136}
137
138 121
139static int luaB_metatable (lua_State *L) { 122static int luaB_metatable (lua_State *L) {
140 luaL_check_type(L, 1, LUA_TTABLE); 123 luaL_check_type(L, 1, LUA_TTABLE);
@@ -191,12 +174,7 @@ static int luaB_collectgarbage (lua_State *L) {
191 174
192static int luaB_type (lua_State *L) { 175static int luaB_type (lua_State *L) {
193 luaL_check_any(L, 1); 176 luaL_check_any(L, 1);
194 if (lua_isnone(L, 2)) 177 lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
195 lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
196 else {
197 lua_pushboolean(L,
198 (strcmp(lua_typename(L, lua_type(L, 1)), luaL_check_string(L, 2)) == 0));
199 }
200 return 1; 178 return 1;
201} 179}
202 180
@@ -248,6 +226,7 @@ static int luaB_loadstring (lua_State *L) {
248 return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop); 226 return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
249} 227}
250 228
229
251static int luaB_dofile (lua_State *L) { 230static int luaB_dofile (lua_State *L) {
252 int oldtop = lua_gettop(L); 231 int oldtop = lua_gettop(L);
253 const char *fname = luaL_opt_string(L, 1, NULL); 232 const char *fname = luaL_opt_string(L, 1, NULL);
@@ -411,6 +390,50 @@ static int luaB_tostring (lua_State *L) {
411} 390}
412 391
413 392
393static const luaL_reg base_funcs[] = {
394 {LUA_ALERT, luaB__ALERT},
395 {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
396 {"error", luaB_error},
397 {"metatable", luaB_metatable},
398 {"globals", luaB_globals},
399 {"next", luaB_next},
400 {"print", luaB_print},
401 {"tonumber", luaB_tonumber},
402 {"tostring", luaB_tostring},
403 {"type", luaB_type},
404 {"assert", luaB_assert},
405 {"unpack", luaB_unpack},
406 {"rawget", luaB_rawget},
407 {"rawset", luaB_rawset},
408 {"call", luaB_call},
409 {"collectgarbage", luaB_collectgarbage},
410 {"gcinfo", luaB_gcinfo},
411 {"loadfile", luaB_loadfile},
412 {"loadstring", luaB_loadstring},
413 {"dofile", luaB_dofile},
414 {"dostring", luaB_dostring},
415 {NULL, NULL}
416};
417
418
419static void base_open (lua_State *L) {
420 lua_pushliteral(L, "_G");
421 lua_pushvalue(L, LUA_GLOBALSINDEX);
422 luaL_openlib(L, base_funcs); /* open lib into global table */
423 lua_pushliteral(L, "_VERSION");
424 lua_pushliteral(L, LUA_VERSION);
425 lua_settable(L, -3); /* set global _VERSION */
426 lua_settable(L, -1); /* set global _G */
427}
428
429
430/*
431** {======================================================
432** Coroutine library
433** =======================================================
434*/
435
436
414static int luaB_resume (lua_State *L) { 437static int luaB_resume (lua_State *L) {
415 lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1)); 438 lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1));
416 if (lua_resume(L, co) != 0) 439 if (lua_resume(L, co) != 0)
@@ -457,6 +480,26 @@ static int luaB_yield (lua_State *L) {
457 return lua_yield(L, lua_gettop(L)); 480 return lua_yield(L, lua_gettop(L));
458} 481}
459 482
483static const luaL_reg co_funcs[] = {
484 {"create", luaB_coroutine},
485 {"yield", luaB_yield},
486 {NULL, NULL}
487};
488
489
490static void co_open (lua_State *L) {
491 luaL_opennamedlib(L, "co", co_funcs);
492 /* create metatable for coroutines */
493 lua_pushliteral(L, "Coroutine");
494 lua_newtable(L);
495 lua_pushliteral(L, "__gc");
496 lua_pushcfunction(L, gc_coroutine);
497 lua_rawset(L, -3);
498 lua_rawset(L, LUA_REGISTRYINDEX);
499}
500
501/* }====================================================== */
502
460 503
461/* 504/*
462** {====================================================== 505** {======================================================
@@ -499,6 +542,13 @@ static int luaB_foreach (lua_State *L) {
499} 542}
500 543
501 544
545static void aux_setn (lua_State *L, int t, int n) {
546 lua_pushliteral(L, "n");
547 lua_pushnumber(L, n);
548 lua_rawset(L, t);
549}
550
551
502static int luaB_getn (lua_State *L) { 552static int luaB_getn (lua_State *L) {
503 luaL_check_type(L, 1, LUA_TTABLE); 553 luaL_check_type(L, 1, LUA_TTABLE);
504 lua_pushnumber(L, lua_getn(L, 1)); 554 lua_pushnumber(L, lua_getn(L, 1));
@@ -653,61 +703,29 @@ static int luaB_sort (lua_State *L) {
653 703
654/* }====================================================== */ 704/* }====================================================== */
655 705
656/* }====================================================== */
657
658 706
659 707static const luaL_reg array_funcs[] = {
660static const luaL_reg base_funcs[] = {
661 {LUA_ALERT, luaB__ALERT},
662 {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
663 {"call", luaB_call},
664 {"collectgarbage", luaB_collectgarbage},
665 {"coroutine", luaB_coroutine},
666 {"dofile", luaB_dofile},
667 {"dostring", luaB_dostring},
668 {"error", luaB_error},
669 {"metatable", luaB_metatable},
670 {"foreach", luaB_foreach}, 708 {"foreach", luaB_foreach},
671 {"foreachi", luaB_foreachi}, 709 {"foreachi", luaB_foreachi},
672 {"gcinfo", luaB_gcinfo},
673 {"getglobal", luaB_getglobal}, /* compatibility with 4.0 */
674 {"globals", luaB_globals},
675 {"loadfile", luaB_loadfile},
676 {"loadstring", luaB_loadstring},
677 {"next", luaB_next},
678 {"print", luaB_print},
679 {"rawget", luaB_rawget},
680 {"rawset", luaB_rawset},
681 {"setglobal", luaB_setglobal}, /* compatibility with 4.0 */
682 {"tonumber", luaB_tonumber},
683 {"tostring", luaB_tostring},
684 {"type", luaB_type},
685 {"assert", luaB_assert},
686 {"getn", luaB_getn}, 710 {"getn", luaB_getn},
687 {"sort", luaB_sort}, 711 {"sort", luaB_sort},
688 {"tinsert", luaB_tinsert}, 712 {"insert", luaB_tinsert},
689 {"tremove", luaB_tremove}, 713 {"remove", luaB_tremove},
690 {"unpack", luaB_unpack}, 714 {NULL, NULL}
691 {"yield", luaB_yield}
692}; 715};
693 716
717/* }====================================================== */
718
694 719
695 720
696LUALIB_API int lua_baselibopen (lua_State *L) { 721LUALIB_API int lua_baselibopen (lua_State *L) {
697 luaL_openl(L, base_funcs); 722 base_open(L);
698 lua_pushliteral(L, LUA_VERSION); 723 co_open(L);
699 lua_setglobal(L, "_VERSION"); 724 luaL_opennamedlib(L, "A", array_funcs);
700 /* `require' needs an empty table as upvalue */ 725 /* `require' needs an empty table as upvalue */
701 lua_newtable(L); 726 lua_newtable(L);
702 lua_pushcclosure(L, luaB_require, 1); 727 lua_pushcclosure(L, luaB_require, 1);
703 lua_setglobal(L, "require"); 728 lua_setglobal(L, "require");
704 /* create metatable for coroutines */
705 lua_pushliteral(L, "Coroutine");
706 lua_newtable(L);
707 lua_pushliteral(L, "gc");
708 lua_pushcfunction(L, gc_coroutine);
709 lua_rawset(L, -3);
710 lua_rawset(L, LUA_REGISTRYINDEX);
711 return 0; 729 return 0;
712} 730}
713 731