1
0
mirror of https://github.com/lua/lua.git synced 2025-02-04 06:13:04 +08:00

no more lua_[gs]etstr

This commit is contained in:
Roberto Ierusalimschy 2002-02-07 15:25:36 -02:00
parent 53aaee6ee6
commit 86a4de256e
5 changed files with 48 additions and 29 deletions

View File

@ -240,8 +240,9 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
} }
else { /* no free elements */ else { /* no free elements */
ref = lua_getn(L, t) + 1; /* use next `n' */ ref = lua_getn(L, t) + 1; /* use next `n' */
lua_pushliteral(L, "n");
lua_pushnumber(L, ref); lua_pushnumber(L, ref);
lua_setstr(L, t, "n"); /* n = n+1 */ lua_rawset(L, t); /* n = n+1 */
} }
lua_rawseti(L, t, ref); lua_rawseti(L, t, ref);
return ref; return ref;

View File

@ -20,8 +20,9 @@
static void aux_setn (lua_State *L, int t, int n) { static void aux_setn (lua_State *L, int t, int n) {
lua_pushliteral(L, "n");
lua_pushnumber(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_cobegin(NL, n-1);
lua_newuserdatabox(L, NL); lua_newuserdatabox(L, NL);
lua_getstr(L, LUA_REGISTRYINDEX, "Coroutine"); lua_pushliteral(L, "Coroutine");
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_pushcclosure(L, luaB_resume, 1); lua_pushcclosure(L, luaB_resume, 1);
return 1; return 1;
@ -700,10 +702,12 @@ LUALIB_API int lua_baselibopen (lua_State *L) {
lua_pushcclosure(L, luaB_require, 1); lua_pushcclosure(L, luaB_require, 1);
lua_setglobal(L, "require"); lua_setglobal(L, "require");
/* create metatable for coroutines */ /* create metatable for coroutines */
lua_pushliteral(L, "Coroutine");
lua_newtable(L); lua_newtable(L);
lua_pushliteral(L, "gc");
lua_pushcfunction(L, gc_coroutine); lua_pushcfunction(L, gc_coroutine);
lua_setstr(L, -2, "gc"); lua_rawset(L, -3);
lua_setstr(L, LUA_REGISTRYINDEX, "Coroutine"); lua_rawset(L, LUA_REGISTRYINDEX);
return 0; return 0;
} }

View File

@ -18,14 +18,16 @@
static void settabss (lua_State *L, const char *i, const char *v) { static void settabss (lua_State *L, const char *i, const char *v) {
lua_pushstring(L, i);
lua_pushstring(L, v); lua_pushstring(L, v);
lua_setstr(L, -2, i); lua_rawset(L, -3);
} }
static void settabsi (lua_State *L, const char *i, int v) { static void settabsi (lua_State *L, const char *i, int v) {
lua_pushstring(L, i);
lua_pushnumber(L, v); 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); settabss(L, "namewhat", ar.namewhat);
break; break;
case 'f': case 'f':
lua_pushvalue(L, -2); lua_pushliteral(L, "func");
lua_setstr(L, -2, "func"); lua_pushvalue(L, -3);
lua_rawset(L, -3);
break; break;
} }
} }
@ -112,7 +115,8 @@ static int setlocal (lua_State *L) {
static void hookf (lua_State *L, const char *key) { 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)) { if (lua_isfunction(L, -1)) {
lua_pushvalue(L, -2); /* original argument (below function) */ lua_pushvalue(L, -2); /* original argument (below function) */
lua_rawcall(L, 1, 0); lua_rawcall(L, 1, 0);
@ -143,9 +147,11 @@ static void sethook (lua_State *L, const char *key, lua_Hook hook,
(*sethookf)(L, hook); (*sethookf)(L, hook);
else else
luaL_argerror(L, 1, "function expected"); 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_pushvalue(L, 1);
lua_setstr(L, LUA_REGISTRYINDEX, key); /* set new value */ lua_rawset(L, LUA_REGISTRYINDEX); /* set new value */
} }

View File

@ -73,7 +73,8 @@ static int pushresult (lua_State *L, int i) {
static int checkfile (lua_State *L, int findex, const char *tname) { static int checkfile (lua_State *L, int findex, const char *tname) {
int res; int res;
lua_getmetatable(L, findex); 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); res = lua_equal(L, -1, -2);
lua_pop(L, 2); lua_pop(L, 2);
return res; return res;
@ -111,7 +112,8 @@ static FILE *getopthandle (lua_State *L, int inout) {
static void newfile (lua_State *L, FILE *f) { static void newfile (lua_State *L, FILE *f) {
lua_newuserdatabox(L, f); lua_newuserdatabox(L, f);
lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE); lua_pushliteral(L, FILEHANDLE);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
} }
@ -147,7 +149,8 @@ static int io_close (lua_State *L) {
int status = 1; int status = 1;
if (f != stdin && f != stdout && f != stderr) { if (f != stdin && f != stdout && f != stderr) {
lua_settop(L, 1); /* make sure file is on top */ 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); lua_setmetatable(L, 1);
status = (CLOSEFILE(L, f) == 0); 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) { static void setfield (lua_State *L, const char *key, int value) {
lua_pushstring(L, key);
lua_pushnumber(L, value); lua_pushnumber(L, value);
lua_setstr(L, -2, key); lua_rawset(L, -3);
} }
static int getfield (lua_State *L, const char *key, int d) { static int getfield (lua_State *L, const char *key, int d) {
int res; int res;
lua_getstr(L, -1, key); lua_pushstring(L, key);
lua_gettable(L, -2);
if (lua_isnumber(L, -1)) if (lua_isnumber(L, -1))
res = (int)(lua_tonumber(L, -1)); res = (int)(lua_tonumber(L, -1));
else { else {
@ -693,15 +698,18 @@ static const luaL_reg iolib[] = {
LUALIB_API int lua_iolibopen (lua_State *L) { LUALIB_API int lua_iolibopen (lua_State *L) {
lua_pushliteral(L, FILEHANDLE);
lua_newtable(L); /* meta table for FILEHANDLE */ lua_newtable(L); /* meta table for FILEHANDLE */
/* close files when collected */ /* close files when collected */
lua_pushliteral(L, "gc");
lua_pushcfunction(L, file_collect); lua_pushcfunction(L, file_collect);
lua_setstr(L, -2, "gc"); lua_rawset(L, -3);
/* put new metatable into registry */ /* put new metatable into registry */
lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE); lua_rawset(L, LUA_REGISTRYINDEX);
/* meta table for CLOSEDFILEHANDLE */ /* meta table for CLOSEDFILEHANDLE */
lua_pushliteral(L, CLOSEDFILEHANDLE);
lua_newtable(L); lua_newtable(L);
lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); lua_rawset(L, LUA_REGISTRYINDEX);
luaL_openl(L, iolib); luaL_openl(L, iolib);
/* predefined file handles */ /* predefined file handles */
newfilewithname(L, stdin, basicfiles[INFILE]); newfilewithname(L, stdin, basicfiles[INFILE]);

18
lua.h
View File

@ -149,7 +149,6 @@ LUA_API void lua_pushboolean (lua_State *L, int b);
/* /*
** get functions (Lua -> stack) ** 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_gettable (lua_State *L, int index);
LUA_API void lua_rawget (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); 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) ** 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_settable (lua_State *L, int index);
LUA_API void lua_rawset (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); 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, \ #define lua_pushliteral(L, s) lua_pushlstring(L, "" s, \
(sizeof(s)/sizeof(char))-1) (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 ** 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); 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 #define lua_isnull lua_isnone