diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-10 09:57:55 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-10 09:57:55 -0300 |
| commit | 533737f26e3f8036d7978e09427ea5ff75aec9df (patch) | |
| tree | 003004a423cf20bc4e1493918760494b0d694f93 /liolib.c | |
| parent | a41d60e1d1f3a954648884d4ab7fb7e9ccdd52d6 (diff) | |
| download | lua-533737f26e3f8036d7978e09427ea5ff75aec9df.tar.gz lua-533737f26e3f8036d7978e09427ea5ff75aec9df.tar.bz2 lua-533737f26e3f8036d7978e09427ea5ff75aec9df.zip | |
new functions `lua_getfield' and `lua_setfield'
Diffstat (limited to 'liolib.c')
| -rw-r--r-- | liolib.c | 16 |
1 files changed, 6 insertions, 10 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.46 2003/08/25 19:49:47 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.47 2003/10/07 20:13:41 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -236,8 +236,7 @@ static int io_readline (lua_State *L); | |||
| 236 | 236 | ||
| 237 | 237 | ||
| 238 | static void aux_lines (lua_State *L, int idx, int close) { | 238 | static void aux_lines (lua_State *L, int idx, int close) { |
| 239 | lua_pushliteral(L, FILEHANDLE); | 239 | lua_getfield(L, LUA_REGISTRYINDEX, FILEHANDLE); |
| 240 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 241 | lua_pushvalue(L, idx); | 240 | lua_pushvalue(L, idx); |
| 242 | lua_pushboolean(L, close); /* close/not close file when finished */ | 241 | lua_pushboolean(L, close); /* close/not close file when finished */ |
| 243 | lua_pushcclosure(L, io_readline, 3); | 242 | lua_pushcclosure(L, io_readline, 3); |
| @@ -512,9 +511,8 @@ static void createmeta (lua_State *L) { | |||
| 512 | *newfile(L) = stdout; | 511 | *newfile(L) = stdout; |
| 513 | lua_rawseti(L, -2, IO_OUTPUT); | 512 | lua_rawseti(L, -2, IO_OUTPUT); |
| 514 | /* file methods */ | 513 | /* file methods */ |
| 515 | lua_pushliteral(L, "__index"); | 514 | lua_pushvalue(L, -1); /* push metatable */ |
| 516 | lua_pushvalue(L, -2); /* push metatable */ | 515 | lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ |
| 517 | lua_rawset(L, -3); /* metatable.__index = metatable */ | ||
| 518 | luaL_openlib(L, NULL, flib, 0); | 516 | luaL_openlib(L, NULL, flib, 0); |
| 519 | } | 517 | } |
| 520 | 518 | ||
| @@ -594,8 +592,7 @@ static void setboolfield (lua_State *L, const char *key, int value) { | |||
| 594 | 592 | ||
| 595 | static int getboolfield (lua_State *L, const char *key) { | 593 | static int getboolfield (lua_State *L, const char *key) { |
| 596 | int res; | 594 | int res; |
| 597 | lua_pushstring(L, key); | 595 | lua_getfield(L, -1, key); |
| 598 | lua_gettable(L, -2); | ||
| 599 | res = lua_toboolean(L, -1); | 596 | res = lua_toboolean(L, -1); |
| 600 | lua_pop(L, 1); | 597 | lua_pop(L, 1); |
| 601 | return res; | 598 | return res; |
| @@ -604,8 +601,7 @@ static int getboolfield (lua_State *L, const char *key) { | |||
| 604 | 601 | ||
| 605 | static int getfield (lua_State *L, const char *key, int d) { | 602 | static int getfield (lua_State *L, const char *key, int d) { |
| 606 | int res; | 603 | int res; |
| 607 | lua_pushstring(L, key); | 604 | lua_getfield(L, -1, key); |
| 608 | lua_gettable(L, -2); | ||
| 609 | if (lua_isnumber(L, -1)) | 605 | if (lua_isnumber(L, -1)) |
| 610 | res = (int)lua_tointeger(L, -1); | 606 | res = (int)lua_tointeger(L, -1); |
| 611 | else { | 607 | else { |
