aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-12-10 20:09:51 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-12-10 20:09:51 -0200
commit9cd36059ad6f3f6750b8cff54c305ae347c6caca (patch)
tree6b71bb5dab3c7f1e8fec12bc03830b58cdc59fc0 /liolib.c
parent592a309177edc52847b1196969ad6d49ba21f4fb (diff)
downloadlua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.tar.gz
lua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.tar.bz2
lua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.zip
new API functions lua_getstr/lua_setstr
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c24
1 files changed, 8 insertions, 16 deletions
diff --git a/liolib.c b/liolib.c
index a4f6eed5..6d584572 100644
--- a/liolib.c
+++ b/liolib.c
@@ -73,8 +73,7 @@ static int pushresult (lua_State *L, int i) {
73static int checkfile (lua_State *L, int findex, const char *tname) { 73static int checkfile (lua_State *L, int findex, const char *tname) {
74 int res; 74 int res;
75 lua_geteventtable(L, findex); 75 lua_geteventtable(L, findex);
76 lua_pushstring(L, tname); 76 lua_getstr(L, LUA_REGISTRYINDEX, tname);
77 lua_gettable(L, LUA_REGISTRYINDEX);
78 res = lua_equal(L, -1, -2); 77 res = lua_equal(L, -1, -2);
79 lua_pop(L, 2); 78 lua_pop(L, 2);
80 return res; 79 return res;
@@ -112,8 +111,7 @@ static FILE *getopthandle (lua_State *L, int inout) {
112 111
113static void newfile (lua_State *L, FILE *f) { 112static void newfile (lua_State *L, FILE *f) {
114 lua_newuserdatabox(L, f); 113 lua_newuserdatabox(L, f);
115 lua_pushliteral(L, FILEHANDLE); 114 lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
116 lua_gettable(L, LUA_REGISTRYINDEX);
117 lua_seteventtable(L, -2); 115 lua_seteventtable(L, -2);
118} 116}
119 117
@@ -149,8 +147,7 @@ static int io_close (lua_State *L) {
149 int status = 1; 147 int status = 1;
150 if (f != stdin && f != stdout && f != stderr) { 148 if (f != stdin && f != stdout && f != stderr) {
151 lua_settop(L, 1); /* make sure file is on top */ 149 lua_settop(L, 1); /* make sure file is on top */
152 lua_pushliteral(L, CLOSEDFILEHANDLE); 150 lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
153 lua_gettable(L, LUA_REGISTRYINDEX);
154 lua_seteventtable(L, 1); 151 lua_seteventtable(L, 1);
155 status = (CLOSEFILE(L, f) == 0); 152 status = (CLOSEFILE(L, f) == 0);
156 } 153 }
@@ -470,16 +467,14 @@ static int io_clock (lua_State *L) {
470*/ 467*/
471 468
472static void setfield (lua_State *L, const char *key, int value) { 469static void setfield (lua_State *L, const char *key, int value) {
473 lua_pushstring(L, key);
474 lua_pushnumber(L, value); 470 lua_pushnumber(L, value);
475 lua_rawset(L, -3); 471 lua_setstr(L, -2, key);
476} 472}
477 473
478 474
479static int getfield (lua_State *L, const char *key, int d) { 475static int getfield (lua_State *L, const char *key, int d) {
480 int res; 476 int res;
481 lua_pushstring(L, key); 477 lua_getstr(L, -1, key);
482 lua_rawget(L, -2);
483 if (lua_isnumber(L, -1)) 478 if (lua_isnumber(L, -1))
484 res = (int)(lua_tonumber(L, -1)); 479 res = (int)(lua_tonumber(L, -1));
485 else { 480 else {
@@ -698,18 +693,15 @@ static const luaL_reg iolib[] = {
698 693
699 694
700LUALIB_API int lua_iolibopen (lua_State *L) { 695LUALIB_API int lua_iolibopen (lua_State *L) {
701 lua_pushliteral(L, FILEHANDLE);
702 lua_newtable(L); /* event table for FILEHANDLE */ 696 lua_newtable(L); /* event table for FILEHANDLE */
703 /* close files when collected */ 697 /* close files when collected */
704 lua_pushliteral(L, "gc");
705 lua_pushcfunction(L, file_collect); 698 lua_pushcfunction(L, file_collect);
706 lua_settable(L, -3); 699 lua_setstr(L, -2, "gc");
707 /* put new eventtable into registry */ 700 /* put new eventtable into registry */
708 lua_settable(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = eventtable */ 701 lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
709 lua_pushliteral(L, CLOSEDFILEHANDLE);
710 /* event table for CLOSEDFILEHANDLE */ 702 /* event table for CLOSEDFILEHANDLE */
711 lua_newtable(L); 703 lua_newtable(L);
712 lua_settable(L, LUA_REGISTRYINDEX); 704 lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
713 luaL_openl(L, iolib); 705 luaL_openl(L, iolib);
714 /* predefined file handles */ 706 /* predefined file handles */
715 newfilewithname(L, stdin, basicfiles[INFILE]); 707 newfilewithname(L, stdin, basicfiles[INFILE]);