From 9cd36059ad6f3f6750b8cff54c305ae347c6caca Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Dec 2001 20:09:51 -0200 Subject: new API functions lua_getstr/lua_setstr --- liolib.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'liolib.c') 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) { static int checkfile (lua_State *L, int findex, const char *tname) { int res; lua_geteventtable(L, findex); - lua_pushstring(L, tname); - lua_gettable(L, LUA_REGISTRYINDEX); + lua_getstr(L, LUA_REGISTRYINDEX, tname); res = lua_equal(L, -1, -2); lua_pop(L, 2); return res; @@ -112,8 +111,7 @@ static FILE *getopthandle (lua_State *L, int inout) { static void newfile (lua_State *L, FILE *f) { lua_newuserdatabox(L, f); - lua_pushliteral(L, FILEHANDLE); - lua_gettable(L, LUA_REGISTRYINDEX); + lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE); lua_seteventtable(L, -2); } @@ -149,8 +147,7 @@ static int io_close (lua_State *L) { int status = 1; if (f != stdin && f != stdout && f != stderr) { lua_settop(L, 1); /* make sure file is on top */ - lua_pushliteral(L, CLOSEDFILEHANDLE); - lua_gettable(L, LUA_REGISTRYINDEX); + lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); lua_seteventtable(L, 1); status = (CLOSEFILE(L, f) == 0); } @@ -470,16 +467,14 @@ static int io_clock (lua_State *L) { */ static void setfield (lua_State *L, const char *key, int value) { - lua_pushstring(L, key); lua_pushnumber(L, value); - lua_rawset(L, -3); + lua_setstr(L, -2, key); } static int getfield (lua_State *L, const char *key, int d) { int res; - lua_pushstring(L, key); - lua_rawget(L, -2); + lua_getstr(L, -1, key); if (lua_isnumber(L, -1)) res = (int)(lua_tonumber(L, -1)); else { @@ -698,18 +693,15 @@ static const luaL_reg iolib[] = { LUALIB_API int lua_iolibopen (lua_State *L) { - lua_pushliteral(L, FILEHANDLE); lua_newtable(L); /* event table for FILEHANDLE */ /* close files when collected */ - lua_pushliteral(L, "gc"); lua_pushcfunction(L, file_collect); - lua_settable(L, -3); + lua_setstr(L, -2, "gc"); /* put new eventtable into registry */ - lua_settable(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = eventtable */ - lua_pushliteral(L, CLOSEDFILEHANDLE); + lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE); /* event table for CLOSEDFILEHANDLE */ lua_newtable(L); - lua_settable(L, LUA_REGISTRYINDEX); + lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); luaL_openl(L, iolib); /* predefined file handles */ newfilewithname(L, stdin, basicfiles[INFILE]); -- cgit v1.2.3-55-g6feb