commit 9cd36059ad6f3f6750b8cff54c305ae347c6caca
parent 592a309177edc52847b1196969ad6d49ba21f4fb
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 10 Dec 2001 20:09:29 -0200
new API functions lua_getstr/lua_setstr
Diffstat:
7 files changed, 30 insertions(+), 45 deletions(-)
diff --git a/lapi.c b/lapi.c
@@ -329,11 +329,11 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
*/
-LUA_API void lua_getglobal (lua_State *L, const char *name) {
+LUA_API void lua_getstr (lua_State *L, int index, const char *name) {
TObject o;
lua_lock(L);
setsvalue(&o, luaS_new(L, name));
- luaV_gettable(L, gt(L), &o, L->top);
+ luaV_gettable(L, luaA_index(L, index), &o, L->top);
api_incr_top(L);
lua_unlock(L);
}
@@ -406,12 +406,12 @@ LUA_API void lua_geteventtable (lua_State *L, int objindex) {
*/
-LUA_API void lua_setglobal (lua_State *L, const char *name) {
+LUA_API void lua_setstr (lua_State *L, int index, const char *name) {
TObject o;
lua_lock(L);
api_checknelems(L, 1);
setsvalue(&o, luaS_new(L, name));
- luaV_settable(L, gt(L), &o, L->top - 1);
+ luaV_settable(L, luaA_index(L, index), &o, L->top - 1);
L->top--; /* remove element from the top */
lua_unlock(L);
}
diff --git a/lauxlib.c b/lauxlib.c
@@ -228,9 +228,8 @@ 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_settable(L, t); /* n = n+1 */
+ lua_setstr(L, t, "n"); /* n = n+1 */
}
lua_rawseti(L, t, ref);
return ref;
diff --git a/lbaselib.c b/lbaselib.c
@@ -20,9 +20,8 @@
static void aux_setn (lua_State *L, int t, int n) {
- lua_pushliteral(L, "n");
lua_pushnumber(L, n);
- lua_settable(L, t);
+ lua_setstr(L, t, "n");
}
diff --git a/ldblib.c b/ldblib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldblib.c,v 1.40 2001/10/26 17:33:30 roberto Exp $
+** $Id: ldblib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
@@ -18,16 +18,14 @@
static void settabss (lua_State *L, const char *i, const char *v) {
- lua_pushstring(L, i);
lua_pushstring(L, v);
- lua_settable(L, -3);
+ lua_setstr(L, -2, i);
}
static void settabsi (lua_State *L, const char *i, int v) {
- lua_pushstring(L, i);
lua_pushnumber(L, v);
- lua_settable(L, -3);
+ lua_setstr(L, -2, i);
}
@@ -71,9 +69,8 @@ static int getinfo (lua_State *L) {
settabss(L, "namewhat", ar.namewhat);
break;
case 'f':
- lua_pushliteral(L, "func");
- lua_pushvalue(L, -3);
- lua_settable(L, -3);
+ lua_pushvalue(L, -2);
+ lua_setstr(L, -2, "func");
break;
}
}
@@ -115,8 +112,7 @@ static int setlocal (lua_State *L) {
static void hookf (lua_State *L, const char *key) {
- lua_pushstring(L, key);
- lua_gettable(L, LUA_REGISTRYINDEX);
+ lua_getstr(L, LUA_REGISTRYINDEX, key);
if (lua_isfunction(L, -1)) {
lua_pushvalue(L, -2); /* original argument (below function) */
lua_rawcall(L, 1, 0);
@@ -147,11 +143,9 @@ static void sethook (lua_State *L, const char *key, lua_Hook hook,
(*sethookf)(L, hook);
else
luaL_argerror(L, 1, "function expected");
- lua_pushstring(L, key);
- lua_gettable(L, LUA_REGISTRYINDEX); /* get old value */
- lua_pushstring(L, key);
+ lua_getstr(L, LUA_REGISTRYINDEX, key); /* get old value */
lua_pushvalue(L, 1);
- lua_settable(L, LUA_REGISTRYINDEX); /* set new value */
+ lua_setstr(L, LUA_REGISTRYINDEX, key); /* set new value */
}
diff --git 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]);
diff --git a/lua.c b/lua.c
@@ -1,5 +1,5 @@
/*
-** $Id: lua.c,v 1.72 2001/11/27 20:56:47 roberto Exp $
+** $Id: lua.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/
@@ -133,9 +133,8 @@ static void getargs (char *argv[]) {
lua_settable(L, -3);
}
/* arg.n = maximum index in table `arg' */
- lua_pushliteral(L, "n");
lua_pushnumber(L, i-1);
- lua_settable(L, -3);
+ lua_setstr(L, -2, "n");
}
diff --git a/lua.h b/lua.h
@@ -146,7 +146,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
/*
** get functions (Lua -> stack)
*/
-LUA_API void lua_getglobal (lua_State *L, const char *name);
+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);
@@ -158,7 +158,7 @@ LUA_API void lua_geteventtable (lua_State *L, int objindex);
/*
** set functions (stack -> Lua)
*/
-LUA_API void lua_setglobal (lua_State *L, const char *name);
+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);
@@ -227,8 +227,10 @@ LUA_API int lua_getweakmode (lua_State *L, int index);
#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_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
+#define lua_getglobals(L) lua_pushvalue(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)