diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-12-10 20:09:51 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-12-10 20:09:51 -0200 |
commit | 9cd36059ad6f3f6750b8cff54c305ae347c6caca (patch) | |
tree | 6b71bb5dab3c7f1e8fec12bc03830b58cdc59fc0 | |
parent | 592a309177edc52847b1196969ad6d49ba21f4fb (diff) | |
download | lua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.tar.gz lua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.tar.bz2 lua-9cd36059ad6f3f6750b8cff54c305ae347c6caca.zip |
new API functions lua_getstr/lua_setstr
-rw-r--r-- | lapi.c | 8 | ||||
-rw-r--r-- | lauxlib.c | 3 | ||||
-rw-r--r-- | lbaselib.c | 3 | ||||
-rw-r--r-- | ldblib.c | 22 | ||||
-rw-r--r-- | liolib.c | 24 | ||||
-rw-r--r-- | lua.c | 5 | ||||
-rw-r--r-- | lua.h | 10 |
7 files changed, 30 insertions, 45 deletions
@@ -329,11 +329,11 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { | |||
329 | */ | 329 | */ |
330 | 330 | ||
331 | 331 | ||
332 | LUA_API void lua_getglobal (lua_State *L, const char *name) { | 332 | LUA_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 | ||
409 | LUA_API void lua_setglobal (lua_State *L, const char *name) { | 409 | LUA_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 | } |
@@ -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; |
@@ -20,9 +20,8 @@ | |||
20 | 20 | ||
21 | 21 | ||
22 | static void aux_setn (lua_State *L, int t, int n) { | 22 | static 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 | ||
@@ -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 | ||
20 | static void settabss (lua_State *L, const char *i, const char *v) { | 20 | static 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 | ||
27 | static void settabsi (lua_State *L, const char *i, int v) { | 26 | static 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 | ||
117 | static void hookf (lua_State *L, const char *key) { | 114 | static 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 | ||
@@ -73,8 +73,7 @@ static int pushresult (lua_State *L, int i) { | |||
73 | static int checkfile (lua_State *L, int findex, const char *tname) { | 73 | static 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 | ||
113 | static void newfile (lua_State *L, FILE *f) { | 112 | static 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 | ||
472 | static void setfield (lua_State *L, const char *key, int value) { | 469 | static 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 | ||
479 | static int getfield (lua_State *L, const char *key, int d) { | 475 | static 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 | ||
700 | LUALIB_API int lua_iolibopen (lua_State *L) { | 695 | LUALIB_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]); |
@@ -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 | ||
@@ -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 | */ |
149 | LUA_API void lua_getglobal (lua_State *L, const char *name); | 149 | LUA_API void lua_getstr (lua_State *L, int index, const char *name); |
150 | LUA_API void lua_gettable (lua_State *L, int index); | 150 | LUA_API void lua_gettable (lua_State *L, int index); |
151 | LUA_API void lua_rawget (lua_State *L, int index); | 151 | LUA_API void lua_rawget (lua_State *L, int index); |
152 | LUA_API void lua_rawgeti (lua_State *L, int index, int n); | 152 | 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); | |||
158 | /* | 158 | /* |
159 | ** set functions (stack -> Lua) | 159 | ** set functions (stack -> Lua) |
160 | */ | 160 | */ |
161 | LUA_API void lua_setglobal (lua_State *L, const char *name); | 161 | LUA_API void lua_setstr (lua_State *L, int index, const char *name); |
162 | LUA_API void lua_settable (lua_State *L, int index); | 162 | LUA_API void lua_settable (lua_State *L, int index); |
163 | LUA_API void lua_rawset (lua_State *L, int index); | 163 | LUA_API void lua_rawset (lua_State *L, int index); |
164 | LUA_API void lua_rawseti (lua_State *L, int index, int n); | 164 | 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); | |||
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 | ||