From 86a4de256e88844e34535901d25923aa851d963e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Feb 2002 15:25:36 -0200 Subject: [PATCH] no more lua_[gs]etstr --- lauxlib.c | 3 ++- lbaselib.c | 12 ++++++++---- ldblib.c | 20 +++++++++++++------- liolib.c | 24 ++++++++++++++++-------- lua.h | 18 +++++++++--------- 5 files changed, 48 insertions(+), 29 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 831d3e34..09d72f18 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -240,8 +240,9 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { } else { /* no free elements */ ref = lua_getn(L, t) + 1; /* use next `n' */ + lua_pushliteral(L, "n"); lua_pushnumber(L, ref); - lua_setstr(L, t, "n"); /* n = n+1 */ + lua_rawset(L, t); /* n = n+1 */ } lua_rawseti(L, t, ref); return ref; diff --git a/lbaselib.c b/lbaselib.c index 74d176dd..f1a7b5ab 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -20,8 +20,9 @@ static void aux_setn (lua_State *L, int t, int n) { + lua_pushliteral(L, "n"); lua_pushnumber(L, n); - lua_setstr(L, t, "n"); + lua_rawset(L, t); } @@ -444,7 +445,8 @@ static int luaB_coroutine (lua_State *L) { } lua_cobegin(NL, n-1); lua_newuserdatabox(L, NL); - lua_getstr(L, LUA_REGISTRYINDEX, "Coroutine"); + lua_pushliteral(L, "Coroutine"); + lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); lua_pushcclosure(L, luaB_resume, 1); return 1; @@ -700,10 +702,12 @@ LUALIB_API int lua_baselibopen (lua_State *L) { lua_pushcclosure(L, luaB_require, 1); lua_setglobal(L, "require"); /* create metatable for coroutines */ + lua_pushliteral(L, "Coroutine"); lua_newtable(L); + lua_pushliteral(L, "gc"); lua_pushcfunction(L, gc_coroutine); - lua_setstr(L, -2, "gc"); - lua_setstr(L, LUA_REGISTRYINDEX, "Coroutine"); + lua_rawset(L, -3); + lua_rawset(L, LUA_REGISTRYINDEX); return 0; } diff --git a/ldblib.c b/ldblib.c index 27131a9b..58eb56c9 100644 --- a/ldblib.c +++ b/ldblib.c @@ -18,14 +18,16 @@ static void settabss (lua_State *L, const char *i, const char *v) { + lua_pushstring(L, i); lua_pushstring(L, v); - lua_setstr(L, -2, i); + lua_rawset(L, -3); } static void settabsi (lua_State *L, const char *i, int v) { + lua_pushstring(L, i); lua_pushnumber(L, v); - lua_setstr(L, -2, i); + lua_rawset(L, -3); } @@ -69,8 +71,9 @@ static int getinfo (lua_State *L) { settabss(L, "namewhat", ar.namewhat); break; case 'f': - lua_pushvalue(L, -2); - lua_setstr(L, -2, "func"); + lua_pushliteral(L, "func"); + lua_pushvalue(L, -3); + lua_rawset(L, -3); break; } } @@ -112,7 +115,8 @@ static int setlocal (lua_State *L) { static void hookf (lua_State *L, const char *key) { - lua_getstr(L, LUA_REGISTRYINDEX, key); + lua_pushstring(L, key); + lua_rawget(L, LUA_REGISTRYINDEX); if (lua_isfunction(L, -1)) { lua_pushvalue(L, -2); /* original argument (below function) */ lua_rawcall(L, 1, 0); @@ -143,9 +147,11 @@ static void sethook (lua_State *L, const char *key, lua_Hook hook, (*sethookf)(L, hook); else luaL_argerror(L, 1, "function expected"); - lua_getstr(L, LUA_REGISTRYINDEX, key); /* get old value */ + lua_pushstring(L, key); + lua_rawget(L, LUA_REGISTRYINDEX); /* get old value */ + lua_pushstring(L, key); lua_pushvalue(L, 1); - lua_setstr(L, LUA_REGISTRYINDEX, key); /* set new value */ + lua_rawset(L, LUA_REGISTRYINDEX); /* set new value */ } diff --git a/liolib.c b/liolib.c index ad94e6f4..fdc5edce 100644 --- a/liolib.c +++ b/liolib.c @@ -73,7 +73,8 @@ static int pushresult (lua_State *L, int i) { static int checkfile (lua_State *L, int findex, const char *tname) { int res; lua_getmetatable(L, findex); - lua_getstr(L, LUA_REGISTRYINDEX, tname); + lua_pushstring(L, tname); + lua_rawget(L, LUA_REGISTRYINDEX); res = lua_equal(L, -1, -2); lua_pop(L, 2); return res; @@ -111,7 +112,8 @@ static FILE *getopthandle (lua_State *L, int inout) { static void newfile (lua_State *L, FILE *f) { lua_newuserdatabox(L, f); - lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE); + lua_pushliteral(L, FILEHANDLE); + lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); } @@ -147,7 +149,8 @@ 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_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); + lua_pushliteral(L, CLOSEDFILEHANDLE); + lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, 1); status = (CLOSEFILE(L, f) == 0); } @@ -467,14 +470,16 @@ 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_setstr(L, -2, key); + lua_rawset(L, -3); } static int getfield (lua_State *L, const char *key, int d) { int res; - lua_getstr(L, -1, key); + lua_pushstring(L, key); + lua_gettable(L, -2); if (lua_isnumber(L, -1)) res = (int)(lua_tonumber(L, -1)); else { @@ -693,15 +698,18 @@ static const luaL_reg iolib[] = { LUALIB_API int lua_iolibopen (lua_State *L) { + lua_pushliteral(L, FILEHANDLE); lua_newtable(L); /* meta table for FILEHANDLE */ /* close files when collected */ + lua_pushliteral(L, "gc"); lua_pushcfunction(L, file_collect); - lua_setstr(L, -2, "gc"); + lua_rawset(L, -3); /* put new metatable into registry */ - lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE); + lua_rawset(L, LUA_REGISTRYINDEX); /* meta table for CLOSEDFILEHANDLE */ + lua_pushliteral(L, CLOSEDFILEHANDLE); lua_newtable(L); - lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); + lua_rawset(L, LUA_REGISTRYINDEX); luaL_openl(L, iolib); /* predefined file handles */ newfilewithname(L, stdin, basicfiles[INFILE]); diff --git a/lua.h b/lua.h index 0845a386..ac0a2f96 100644 --- a/lua.h +++ b/lua.h @@ -149,7 +149,6 @@ LUA_API void lua_pushboolean (lua_State *L, int b); /* ** get functions (Lua -> stack) */ -LUA_API void lua_getstr (lua_State *L, int index, const char *name); LUA_API void lua_gettable (lua_State *L, int index); LUA_API void lua_rawget (lua_State *L, int index); LUA_API void lua_rawgeti (lua_State *L, int index, int n); @@ -160,7 +159,6 @@ LUA_API void lua_getmetatable (lua_State *L, int objindex); /* ** set functions (stack -> Lua) */ -LUA_API void lua_setstr (lua_State *L, int index, const char *name); LUA_API void lua_settable (lua_State *L, int index); LUA_API void lua_rawset (lua_State *L, int index); LUA_API void lua_rawseti (lua_State *L, int index, int n); @@ -232,13 +230,6 @@ LUA_API void lua_newuserdatabox (lua_State *L, void *u); #define lua_pushliteral(L, s) lua_pushlstring(L, "" s, \ (sizeof(s)/sizeof(char))-1) -#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) -#define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX) -#define lua_setglobals(L) lua_replace(L, LUA_GLOBALSINDEX) -#define lua_getglobal(L,s) lua_getstr(L, LUA_GLOBALSINDEX, s) -#define lua_setglobal(L,s) lua_setstr(L, LUA_GLOBALSINDEX, s) - - /* ** compatibility macros and functions @@ -246,6 +237,15 @@ LUA_API void lua_newuserdatabox (lua_State *L, void *u); LUA_API int lua_pushupvalues (lua_State *L); +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) +#define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +#define lua_setglobals(L) lua_replace(L, LUA_GLOBALSINDEX) +#define lua_setglobal(L,s) \ + (lua_pushstring(L, s), lua_insert(L, -2), lua_settable(L, LUA_GLOBALSINDEX)) + +#define lua_getglobal(L,s) \ + (lua_pushstring(L, s), lua_gettable(L, LUA_GLOBALSINDEX)) + #define lua_isnull lua_isnone