diff options
Diffstat (limited to 'liolib.c')
| -rw-r--r-- | liolib.c | 24 |
1 files changed, 16 insertions, 8 deletions
| @@ -73,7 +73,8 @@ static int pushresult (lua_State *L, int i) { | |||
| 73 | static int checkfile (lua_State *L, int findex, const char *tname) { | 73 | static int checkfile (lua_State *L, int findex, const char *tname) { |
| 74 | int res; | 74 | int res; |
| 75 | lua_getmetatable(L, findex); | 75 | lua_getmetatable(L, findex); |
| 76 | lua_getstr(L, LUA_REGISTRYINDEX, tname); | 76 | lua_pushstring(L, tname); |
| 77 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 77 | res = lua_equal(L, -1, -2); | 78 | res = lua_equal(L, -1, -2); |
| 78 | lua_pop(L, 2); | 79 | lua_pop(L, 2); |
| 79 | return res; | 80 | return res; |
| @@ -111,7 +112,8 @@ static FILE *getopthandle (lua_State *L, int inout) { | |||
| 111 | 112 | ||
| 112 | static void newfile (lua_State *L, FILE *f) { | 113 | static void newfile (lua_State *L, FILE *f) { |
| 113 | lua_newuserdatabox(L, f); | 114 | lua_newuserdatabox(L, f); |
| 114 | lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE); | 115 | lua_pushliteral(L, FILEHANDLE); |
| 116 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 115 | lua_setmetatable(L, -2); | 117 | lua_setmetatable(L, -2); |
| 116 | } | 118 | } |
| 117 | 119 | ||
| @@ -147,7 +149,8 @@ static int io_close (lua_State *L) { | |||
| 147 | int status = 1; | 149 | int status = 1; |
| 148 | if (f != stdin && f != stdout && f != stderr) { | 150 | if (f != stdin && f != stdout && f != stderr) { |
| 149 | lua_settop(L, 1); /* make sure file is on top */ | 151 | lua_settop(L, 1); /* make sure file is on top */ |
| 150 | lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); | 152 | lua_pushliteral(L, CLOSEDFILEHANDLE); |
| 153 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 151 | lua_setmetatable(L, 1); | 154 | lua_setmetatable(L, 1); |
| 152 | status = (CLOSEFILE(L, f) == 0); | 155 | status = (CLOSEFILE(L, f) == 0); |
| 153 | } | 156 | } |
| @@ -467,14 +470,16 @@ static int io_clock (lua_State *L) { | |||
| 467 | */ | 470 | */ |
| 468 | 471 | ||
| 469 | static void setfield (lua_State *L, const char *key, int value) { | 472 | static void setfield (lua_State *L, const char *key, int value) { |
| 473 | lua_pushstring(L, key); | ||
| 470 | lua_pushnumber(L, value); | 474 | lua_pushnumber(L, value); |
| 471 | lua_setstr(L, -2, key); | 475 | lua_rawset(L, -3); |
| 472 | } | 476 | } |
| 473 | 477 | ||
| 474 | 478 | ||
| 475 | static int getfield (lua_State *L, const char *key, int d) { | 479 | static int getfield (lua_State *L, const char *key, int d) { |
| 476 | int res; | 480 | int res; |
| 477 | lua_getstr(L, -1, key); | 481 | lua_pushstring(L, key); |
| 482 | lua_gettable(L, -2); | ||
| 478 | if (lua_isnumber(L, -1)) | 483 | if (lua_isnumber(L, -1)) |
| 479 | res = (int)(lua_tonumber(L, -1)); | 484 | res = (int)(lua_tonumber(L, -1)); |
| 480 | else { | 485 | else { |
| @@ -693,15 +698,18 @@ static const luaL_reg iolib[] = { | |||
| 693 | 698 | ||
| 694 | 699 | ||
| 695 | LUALIB_API int lua_iolibopen (lua_State *L) { | 700 | LUALIB_API int lua_iolibopen (lua_State *L) { |
| 701 | lua_pushliteral(L, FILEHANDLE); | ||
| 696 | lua_newtable(L); /* meta table for FILEHANDLE */ | 702 | lua_newtable(L); /* meta table for FILEHANDLE */ |
| 697 | /* close files when collected */ | 703 | /* close files when collected */ |
| 704 | lua_pushliteral(L, "gc"); | ||
| 698 | lua_pushcfunction(L, file_collect); | 705 | lua_pushcfunction(L, file_collect); |
| 699 | lua_setstr(L, -2, "gc"); | 706 | lua_rawset(L, -3); |
| 700 | /* put new metatable into registry */ | 707 | /* put new metatable into registry */ |
| 701 | lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE); | 708 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 702 | /* meta table for CLOSEDFILEHANDLE */ | 709 | /* meta table for CLOSEDFILEHANDLE */ |
| 710 | lua_pushliteral(L, CLOSEDFILEHANDLE); | ||
| 703 | lua_newtable(L); | 711 | lua_newtable(L); |
| 704 | lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); | 712 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 705 | luaL_openl(L, iolib); | 713 | luaL_openl(L, iolib); |
| 706 | /* predefined file handles */ | 714 | /* predefined file handles */ |
| 707 | newfilewithname(L, stdin, basicfiles[INFILE]); | 715 | newfilewithname(L, stdin, basicfiles[INFILE]); |
