aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-12-10 20:09:51 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-12-10 20:09:51 -0200
commit9cd36059ad6f3f6750b8cff54c305ae347c6caca (patch)
tree6b71bb5dab3c7f1e8fec12bc03830b58cdc59fc0
parent592a309177edc52847b1196969ad6d49ba21f4fb (diff)
downloadlua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.tar.gz
lua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.tar.bz2
lua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.zip
new API functions lua_getstr/lua_setstr
-rw-r--r--lapi.c8
-rw-r--r--lauxlib.c3
-rw-r--r--lbaselib.c3
-rw-r--r--ldblib.c22
-rw-r--r--liolib.c24
-rw-r--r--lua.c5
-rw-r--r--lua.h10
7 files changed, 30 insertions, 45 deletions
diff --git a/lapi.c b/lapi.c
index 6e941752..af151636 100644
--- a/lapi.c
+++ b/lapi.c
@@ -329,11 +329,11 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
329*/ 329*/
330 330
331 331
332LUA_API void lua_getglobal (lua_State *L, const char *name) { 332LUA_API void lua_getstr (lua_State *L, int index, const char *name) {
333 TObject o; 333 TObject o;
334 lua_lock(L); 334 lua_lock(L);
335 setsvalue(&o, luaS_new(L, name)); 335 setsvalue(&o, luaS_new(L, name));
336 luaV_gettable(L, gt(L), &o, L->top); 336 luaV_gettable(L, luaA_index(L, index), &o, L->top);
337 api_incr_top(L); 337 api_incr_top(L);
338 lua_unlock(L); 338 lua_unlock(L);
339} 339}
@@ -406,12 +406,12 @@ LUA_API void lua_geteventtable (lua_State *L, int objindex) {
406*/ 406*/
407 407
408 408
409LUA_API void lua_setglobal (lua_State *L, const char *name) { 409LUA_API void lua_setstr (lua_State *L, int index, const char *name) {
410 TObject o; 410 TObject o;
411 lua_lock(L); 411 lua_lock(L);
412 api_checknelems(L, 1); 412 api_checknelems(L, 1);
413 setsvalue(&o, luaS_new(L, name)); 413 setsvalue(&o, luaS_new(L, name));
414 luaV_settable(L, gt(L), &o, L->top - 1); 414 luaV_settable(L, luaA_index(L, index), &o, L->top - 1);
415 L->top--; /* remove element from the top */ 415 L->top--; /* remove element from the top */
416 lua_unlock(L); 416 lua_unlock(L);
417} 417}
diff --git a/lauxlib.c b/lauxlib.c
index 849daa33..9a4b6fde 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -228,9 +228,8 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
228 } 228 }
229 else { /* no free elements */ 229 else { /* no free elements */
230 ref = lua_getn(L, t) + 1; /* use next `n' */ 230 ref = lua_getn(L, t) + 1; /* use next `n' */
231 lua_pushliteral(L, "n");
232 lua_pushnumber(L, ref); 231 lua_pushnumber(L, ref);
233 lua_settable(L, t); /* n = n+1 */ 232 lua_setstr(L, t, "n"); /* n = n+1 */
234 } 233 }
235 lua_rawseti(L, t, ref); 234 lua_rawseti(L, t, ref);
236 return ref; 235 return ref;
diff --git a/lbaselib.c b/lbaselib.c
index adb9af0c..bb66ab32 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -20,9 +20,8 @@
20 20
21 21
22static void aux_setn (lua_State *L, int t, int n) { 22static void aux_setn (lua_State *L, int t, int n) {
23 lua_pushliteral(L, "n");
24 lua_pushnumber(L, n); 23 lua_pushnumber(L, n);
25 lua_settable(L, t); 24 lua_setstr(L, t, "n");
26} 25}
27 26
28 27
diff --git a/ldblib.c b/ldblib.c
index 25dcb2f7..27131a9b 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.40 2001/10/26 17:33:30 roberto Exp $ 2** $Id: ldblib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,16 +18,14 @@
18 18
19 19
20static void settabss (lua_State *L, const char *i, const char *v) { 20static void settabss (lua_State *L, const char *i, const char *v) {
21 lua_pushstring(L, i);
22 lua_pushstring(L, v); 21 lua_pushstring(L, v);
23 lua_settable(L, -3); 22 lua_setstr(L, -2, i);
24} 23}
25 24
26 25
27static void settabsi (lua_State *L, const char *i, int v) { 26static void settabsi (lua_State *L, const char *i, int v) {
28 lua_pushstring(L, i);
29 lua_pushnumber(L, v); 27 lua_pushnumber(L, v);
30 lua_settable(L, -3); 28 lua_setstr(L, -2, i);
31} 29}
32 30
33 31
@@ -71,9 +69,8 @@ static int getinfo (lua_State *L) {
71 settabss(L, "namewhat", ar.namewhat); 69 settabss(L, "namewhat", ar.namewhat);
72 break; 70 break;
73 case 'f': 71 case 'f':
74 lua_pushliteral(L, "func"); 72 lua_pushvalue(L, -2);
75 lua_pushvalue(L, -3); 73 lua_setstr(L, -2, "func");
76 lua_settable(L, -3);
77 break; 74 break;
78 } 75 }
79 } 76 }
@@ -115,8 +112,7 @@ static int setlocal (lua_State *L) {
115 112
116 113
117static void hookf (lua_State *L, const char *key) { 114static void hookf (lua_State *L, const char *key) {
118 lua_pushstring(L, key); 115 lua_getstr(L, LUA_REGISTRYINDEX, key);
119 lua_gettable(L, LUA_REGISTRYINDEX);
120 if (lua_isfunction(L, -1)) { 116 if (lua_isfunction(L, -1)) {
121 lua_pushvalue(L, -2); /* original argument (below function) */ 117 lua_pushvalue(L, -2); /* original argument (below function) */
122 lua_rawcall(L, 1, 0); 118 lua_rawcall(L, 1, 0);
@@ -147,11 +143,9 @@ static void sethook (lua_State *L, const char *key, lua_Hook hook,
147 (*sethookf)(L, hook); 143 (*sethookf)(L, hook);
148 else 144 else
149 luaL_argerror(L, 1, "function expected"); 145 luaL_argerror(L, 1, "function expected");
150 lua_pushstring(L, key); 146 lua_getstr(L, LUA_REGISTRYINDEX, key); /* get old value */
151 lua_gettable(L, LUA_REGISTRYINDEX); /* get old value */
152 lua_pushstring(L, key);
153 lua_pushvalue(L, 1); 147 lua_pushvalue(L, 1);
154 lua_settable(L, LUA_REGISTRYINDEX); /* set new value */ 148 lua_setstr(L, LUA_REGISTRYINDEX, key); /* set new value */
155} 149}
156 150
157 151
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) {
73static int checkfile (lua_State *L, int findex, const char *tname) { 73static int checkfile (lua_State *L, int findex, const char *tname) {
74 int res; 74 int res;
75 lua_geteventtable(L, findex); 75 lua_geteventtable(L, findex);
76 lua_pushstring(L, tname); 76 lua_getstr(L, LUA_REGISTRYINDEX, tname);
77 lua_gettable(L, LUA_REGISTRYINDEX);
78 res = lua_equal(L, -1, -2); 77 res = lua_equal(L, -1, -2);
79 lua_pop(L, 2); 78 lua_pop(L, 2);
80 return res; 79 return res;
@@ -112,8 +111,7 @@ static FILE *getopthandle (lua_State *L, int inout) {
112 111
113static void newfile (lua_State *L, FILE *f) { 112static void newfile (lua_State *L, FILE *f) {
114 lua_newuserdatabox(L, f); 113 lua_newuserdatabox(L, f);
115 lua_pushliteral(L, FILEHANDLE); 114 lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
116 lua_gettable(L, LUA_REGISTRYINDEX);
117 lua_seteventtable(L, -2); 115 lua_seteventtable(L, -2);
118} 116}
119 117
@@ -149,8 +147,7 @@ static int io_close (lua_State *L) {
149 int status = 1; 147 int status = 1;
150 if (f != stdin && f != stdout && f != stderr) { 148 if (f != stdin && f != stdout && f != stderr) {
151 lua_settop(L, 1); /* make sure file is on top */ 149 lua_settop(L, 1); /* make sure file is on top */
152 lua_pushliteral(L, CLOSEDFILEHANDLE); 150 lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
153 lua_gettable(L, LUA_REGISTRYINDEX);
154 lua_seteventtable(L, 1); 151 lua_seteventtable(L, 1);
155 status = (CLOSEFILE(L, f) == 0); 152 status = (CLOSEFILE(L, f) == 0);
156 } 153 }
@@ -470,16 +467,14 @@ static int io_clock (lua_State *L) {
470*/ 467*/
471 468
472static void setfield (lua_State *L, const char *key, int value) { 469static void setfield (lua_State *L, const char *key, int value) {
473 lua_pushstring(L, key);
474 lua_pushnumber(L, value); 470 lua_pushnumber(L, value);
475 lua_rawset(L, -3); 471 lua_setstr(L, -2, key);
476} 472}
477 473
478 474
479static int getfield (lua_State *L, const char *key, int d) { 475static int getfield (lua_State *L, const char *key, int d) {
480 int res; 476 int res;
481 lua_pushstring(L, key); 477 lua_getstr(L, -1, key);
482 lua_rawget(L, -2);
483 if (lua_isnumber(L, -1)) 478 if (lua_isnumber(L, -1))
484 res = (int)(lua_tonumber(L, -1)); 479 res = (int)(lua_tonumber(L, -1));
485 else { 480 else {
@@ -698,18 +693,15 @@ static const luaL_reg iolib[] = {
698 693
699 694
700LUALIB_API int lua_iolibopen (lua_State *L) { 695LUALIB_API int lua_iolibopen (lua_State *L) {
701 lua_pushliteral(L, FILEHANDLE);
702 lua_newtable(L); /* event table for FILEHANDLE */ 696 lua_newtable(L); /* event table for FILEHANDLE */
703 /* close files when collected */ 697 /* close files when collected */
704 lua_pushliteral(L, "gc");
705 lua_pushcfunction(L, file_collect); 698 lua_pushcfunction(L, file_collect);
706 lua_settable(L, -3); 699 lua_setstr(L, -2, "gc");
707 /* put new eventtable into registry */ 700 /* put new eventtable into registry */
708 lua_settable(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = eventtable */ 701 lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
709 lua_pushliteral(L, CLOSEDFILEHANDLE);
710 /* event table for CLOSEDFILEHANDLE */ 702 /* event table for CLOSEDFILEHANDLE */
711 lua_newtable(L); 703 lua_newtable(L);
712 lua_settable(L, LUA_REGISTRYINDEX); 704 lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
713 luaL_openl(L, iolib); 705 luaL_openl(L, iolib);
714 /* predefined file handles */ 706 /* predefined file handles */
715 newfilewithname(L, stdin, basicfiles[INFILE]); 707 newfilewithname(L, stdin, basicfiles[INFILE]);
diff --git a/lua.c b/lua.c
index 933fb8ca..0453cf74 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.72 2001/11/27 20:56:47 roberto Exp $ 2** $Id: lua.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -133,9 +133,8 @@ static void getargs (char *argv[]) {
133 lua_settable(L, -3); 133 lua_settable(L, -3);
134 } 134 }
135 /* arg.n = maximum index in table `arg' */ 135 /* arg.n = maximum index in table `arg' */
136 lua_pushliteral(L, "n");
137 lua_pushnumber(L, i-1); 136 lua_pushnumber(L, i-1);
138 lua_settable(L, -3); 137 lua_setstr(L, -2, "n");
139} 138}
140 139
141 140
diff --git a/lua.h b/lua.h
index 5c63a770..fd7efb04 100644
--- a/lua.h
+++ b/lua.h
@@ -146,7 +146,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
146/* 146/*
147** get functions (Lua -> stack) 147** get functions (Lua -> stack)
148*/ 148*/
149LUA_API void lua_getglobal (lua_State *L, const char *name); 149LUA_API void lua_getstr (lua_State *L, int index, const char *name);
150LUA_API void lua_gettable (lua_State *L, int index); 150LUA_API void lua_gettable (lua_State *L, int index);
151LUA_API void lua_rawget (lua_State *L, int index); 151LUA_API void lua_rawget (lua_State *L, int index);
152LUA_API void lua_rawgeti (lua_State *L, int index, int n); 152LUA_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);
158/* 158/*
159** set functions (stack -> Lua) 159** set functions (stack -> Lua)
160*/ 160*/
161LUA_API void lua_setglobal (lua_State *L, const char *name); 161LUA_API void lua_setstr (lua_State *L, int index, const char *name);
162LUA_API void lua_settable (lua_State *L, int index); 162LUA_API void lua_settable (lua_State *L, int index);
163LUA_API void lua_rawset (lua_State *L, int index); 163LUA_API void lua_rawset (lua_State *L, int index);
164LUA_API void lua_rawseti (lua_State *L, int index, int n); 164LUA_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);
227#define lua_pushliteral(L, s) lua_pushlstring(L, "" s, \ 227#define lua_pushliteral(L, s) lua_pushlstring(L, "" s, \
228 (sizeof(s)/sizeof(char))-1) 228 (sizeof(s)/sizeof(char))-1)
229 229
230#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX); 230#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
231#define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX); 231#define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX)
232#define lua_getglobal(L,s) lua_getstr(L, LUA_GLOBALSINDEX, s)
233#define lua_setglobal(L,s) lua_setstr(L, LUA_GLOBALSINDEX, s)
232 234
233 235
234 236