diff options
| -rw-r--r-- | lauxlib.c | 3 | ||||
| -rw-r--r-- | lbaselib.c | 12 | ||||
| -rw-r--r-- | ldblib.c | 20 | ||||
| -rw-r--r-- | liolib.c | 24 | ||||
| -rw-r--r-- | lua.h | 18 |
5 files changed, 48 insertions, 29 deletions
| @@ -240,8 +240,9 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { | |||
| 240 | } | 240 | } |
| 241 | else { /* no free elements */ | 241 | else { /* no free elements */ |
| 242 | ref = lua_getn(L, t) + 1; /* use next `n' */ | 242 | ref = lua_getn(L, t) + 1; /* use next `n' */ |
| 243 | lua_pushliteral(L, "n"); | ||
| 243 | lua_pushnumber(L, ref); | 244 | lua_pushnumber(L, ref); |
| 244 | lua_setstr(L, t, "n"); /* n = n+1 */ | 245 | lua_rawset(L, t); /* n = n+1 */ |
| 245 | } | 246 | } |
| 246 | lua_rawseti(L, t, ref); | 247 | lua_rawseti(L, t, ref); |
| 247 | return ref; | 248 | return ref; |
| @@ -20,8 +20,9 @@ | |||
| 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"); | ||
| 23 | lua_pushnumber(L, n); | 24 | lua_pushnumber(L, n); |
| 24 | lua_setstr(L, t, "n"); | 25 | lua_rawset(L, t); |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | 28 | ||
| @@ -444,7 +445,8 @@ static int luaB_coroutine (lua_State *L) { | |||
| 444 | } | 445 | } |
| 445 | lua_cobegin(NL, n-1); | 446 | lua_cobegin(NL, n-1); |
| 446 | lua_newuserdatabox(L, NL); | 447 | lua_newuserdatabox(L, NL); |
| 447 | lua_getstr(L, LUA_REGISTRYINDEX, "Coroutine"); | 448 | lua_pushliteral(L, "Coroutine"); |
| 449 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 448 | lua_setmetatable(L, -2); | 450 | lua_setmetatable(L, -2); |
| 449 | lua_pushcclosure(L, luaB_resume, 1); | 451 | lua_pushcclosure(L, luaB_resume, 1); |
| 450 | return 1; | 452 | return 1; |
| @@ -700,10 +702,12 @@ LUALIB_API int lua_baselibopen (lua_State *L) { | |||
| 700 | lua_pushcclosure(L, luaB_require, 1); | 702 | lua_pushcclosure(L, luaB_require, 1); |
| 701 | lua_setglobal(L, "require"); | 703 | lua_setglobal(L, "require"); |
| 702 | /* create metatable for coroutines */ | 704 | /* create metatable for coroutines */ |
| 705 | lua_pushliteral(L, "Coroutine"); | ||
| 703 | lua_newtable(L); | 706 | lua_newtable(L); |
| 707 | lua_pushliteral(L, "gc"); | ||
| 704 | lua_pushcfunction(L, gc_coroutine); | 708 | lua_pushcfunction(L, gc_coroutine); |
| 705 | lua_setstr(L, -2, "gc"); | 709 | lua_rawset(L, -3); |
| 706 | lua_setstr(L, LUA_REGISTRYINDEX, "Coroutine"); | 710 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 707 | return 0; | 711 | return 0; |
| 708 | } | 712 | } |
| 709 | 713 | ||
| @@ -18,14 +18,16 @@ | |||
| 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); | ||
| 21 | lua_pushstring(L, v); | 22 | lua_pushstring(L, v); |
| 22 | lua_setstr(L, -2, i); | 23 | lua_rawset(L, -3); |
| 23 | } | 24 | } |
| 24 | 25 | ||
| 25 | 26 | ||
| 26 | static void settabsi (lua_State *L, const char *i, int v) { | 27 | static void settabsi (lua_State *L, const char *i, int v) { |
| 28 | lua_pushstring(L, i); | ||
| 27 | lua_pushnumber(L, v); | 29 | lua_pushnumber(L, v); |
| 28 | lua_setstr(L, -2, i); | 30 | lua_rawset(L, -3); |
| 29 | } | 31 | } |
| 30 | 32 | ||
| 31 | 33 | ||
| @@ -69,8 +71,9 @@ static int getinfo (lua_State *L) { | |||
| 69 | settabss(L, "namewhat", ar.namewhat); | 71 | settabss(L, "namewhat", ar.namewhat); |
| 70 | break; | 72 | break; |
| 71 | case 'f': | 73 | case 'f': |
| 72 | lua_pushvalue(L, -2); | 74 | lua_pushliteral(L, "func"); |
| 73 | lua_setstr(L, -2, "func"); | 75 | lua_pushvalue(L, -3); |
| 76 | lua_rawset(L, -3); | ||
| 74 | break; | 77 | break; |
| 75 | } | 78 | } |
| 76 | } | 79 | } |
| @@ -112,7 +115,8 @@ static int setlocal (lua_State *L) { | |||
| 112 | 115 | ||
| 113 | 116 | ||
| 114 | static void hookf (lua_State *L, const char *key) { | 117 | static void hookf (lua_State *L, const char *key) { |
| 115 | lua_getstr(L, LUA_REGISTRYINDEX, key); | 118 | lua_pushstring(L, key); |
| 119 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 116 | if (lua_isfunction(L, -1)) { | 120 | if (lua_isfunction(L, -1)) { |
| 117 | lua_pushvalue(L, -2); /* original argument (below function) */ | 121 | lua_pushvalue(L, -2); /* original argument (below function) */ |
| 118 | lua_rawcall(L, 1, 0); | 122 | lua_rawcall(L, 1, 0); |
| @@ -143,9 +147,11 @@ static void sethook (lua_State *L, const char *key, lua_Hook hook, | |||
| 143 | (*sethookf)(L, hook); | 147 | (*sethookf)(L, hook); |
| 144 | else | 148 | else |
| 145 | luaL_argerror(L, 1, "function expected"); | 149 | luaL_argerror(L, 1, "function expected"); |
| 146 | lua_getstr(L, LUA_REGISTRYINDEX, key); /* get old value */ | 150 | lua_pushstring(L, key); |
| 151 | lua_rawget(L, LUA_REGISTRYINDEX); /* get old value */ | ||
| 152 | lua_pushstring(L, key); | ||
| 147 | lua_pushvalue(L, 1); | 153 | lua_pushvalue(L, 1); |
| 148 | lua_setstr(L, LUA_REGISTRYINDEX, key); /* set new value */ | 154 | lua_rawset(L, LUA_REGISTRYINDEX); /* set new value */ |
| 149 | } | 155 | } |
| 150 | 156 | ||
| 151 | 157 | ||
| @@ -73,7 +73,8 @@ 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_getmetatable(L, findex); | 75 | lua_getmetatable(L, findex); |
| 76 | lua_getstr(L, LUA_REGISTRYINDEX, tname); | 76 | lua_pushstring(L, tname); |
| 77 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 77 | res = lua_equal(L, -1, -2); | 78 | res = lua_equal(L, -1, -2); |
| 78 | lua_pop(L, 2); | 79 | lua_pop(L, 2); |
| 79 | return res; | 80 | return res; |
| @@ -111,7 +112,8 @@ static FILE *getopthandle (lua_State *L, int inout) { | |||
| 111 | 112 | ||
| 112 | static void newfile (lua_State *L, FILE *f) { | 113 | static void newfile (lua_State *L, FILE *f) { |
| 113 | lua_newuserdatabox(L, f); | 114 | lua_newuserdatabox(L, f); |
| 114 | lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE); | 115 | lua_pushliteral(L, FILEHANDLE); |
| 116 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 115 | lua_setmetatable(L, -2); | 117 | lua_setmetatable(L, -2); |
| 116 | } | 118 | } |
| 117 | 119 | ||
| @@ -147,7 +149,8 @@ static int io_close (lua_State *L) { | |||
| 147 | int status = 1; | 149 | int status = 1; |
| 148 | if (f != stdin && f != stdout && f != stderr) { | 150 | if (f != stdin && f != stdout && f != stderr) { |
| 149 | lua_settop(L, 1); /* make sure file is on top */ | 151 | lua_settop(L, 1); /* make sure file is on top */ |
| 150 | lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); | 152 | lua_pushliteral(L, CLOSEDFILEHANDLE); |
| 153 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 151 | lua_setmetatable(L, 1); | 154 | lua_setmetatable(L, 1); |
| 152 | status = (CLOSEFILE(L, f) == 0); | 155 | status = (CLOSEFILE(L, f) == 0); |
| 153 | } | 156 | } |
| @@ -467,14 +470,16 @@ static int io_clock (lua_State *L) { | |||
| 467 | */ | 470 | */ |
| 468 | 471 | ||
| 469 | static void setfield (lua_State *L, const char *key, int value) { | 472 | static void setfield (lua_State *L, const char *key, int value) { |
| 473 | lua_pushstring(L, key); | ||
| 470 | lua_pushnumber(L, value); | 474 | lua_pushnumber(L, value); |
| 471 | lua_setstr(L, -2, key); | 475 | lua_rawset(L, -3); |
| 472 | } | 476 | } |
| 473 | 477 | ||
| 474 | 478 | ||
| 475 | static int getfield (lua_State *L, const char *key, int d) { | 479 | static int getfield (lua_State *L, const char *key, int d) { |
| 476 | int res; | 480 | int res; |
| 477 | lua_getstr(L, -1, key); | 481 | lua_pushstring(L, key); |
| 482 | lua_gettable(L, -2); | ||
| 478 | if (lua_isnumber(L, -1)) | 483 | if (lua_isnumber(L, -1)) |
| 479 | res = (int)(lua_tonumber(L, -1)); | 484 | res = (int)(lua_tonumber(L, -1)); |
| 480 | else { | 485 | else { |
| @@ -693,15 +698,18 @@ static const luaL_reg iolib[] = { | |||
| 693 | 698 | ||
| 694 | 699 | ||
| 695 | LUALIB_API int lua_iolibopen (lua_State *L) { | 700 | LUALIB_API int lua_iolibopen (lua_State *L) { |
| 701 | lua_pushliteral(L, FILEHANDLE); | ||
| 696 | lua_newtable(L); /* meta table for FILEHANDLE */ | 702 | lua_newtable(L); /* meta table for FILEHANDLE */ |
| 697 | /* close files when collected */ | 703 | /* close files when collected */ |
| 704 | lua_pushliteral(L, "gc"); | ||
| 698 | lua_pushcfunction(L, file_collect); | 705 | lua_pushcfunction(L, file_collect); |
| 699 | lua_setstr(L, -2, "gc"); | 706 | lua_rawset(L, -3); |
| 700 | /* put new metatable into registry */ | 707 | /* put new metatable into registry */ |
| 701 | lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE); | 708 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 702 | /* meta table for CLOSEDFILEHANDLE */ | 709 | /* meta table for CLOSEDFILEHANDLE */ |
| 710 | lua_pushliteral(L, CLOSEDFILEHANDLE); | ||
| 703 | lua_newtable(L); | 711 | lua_newtable(L); |
| 704 | lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE); | 712 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 705 | luaL_openl(L, iolib); | 713 | luaL_openl(L, iolib); |
| 706 | /* predefined file handles */ | 714 | /* predefined file handles */ |
| 707 | newfilewithname(L, stdin, basicfiles[INFILE]); | 715 | newfilewithname(L, stdin, basicfiles[INFILE]); |
| @@ -149,7 +149,6 @@ LUA_API void lua_pushboolean (lua_State *L, int b); | |||
| 149 | /* | 149 | /* |
| 150 | ** get functions (Lua -> stack) | 150 | ** get functions (Lua -> stack) |
| 151 | */ | 151 | */ |
| 152 | LUA_API void lua_getstr (lua_State *L, int index, const char *name); | ||
| 153 | LUA_API void lua_gettable (lua_State *L, int index); | 152 | LUA_API void lua_gettable (lua_State *L, int index); |
| 154 | LUA_API void lua_rawget (lua_State *L, int index); | 153 | LUA_API void lua_rawget (lua_State *L, int index); |
| 155 | LUA_API void lua_rawgeti (lua_State *L, int index, int n); | 154 | 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); | |||
| 160 | /* | 159 | /* |
| 161 | ** set functions (stack -> Lua) | 160 | ** set functions (stack -> Lua) |
| 162 | */ | 161 | */ |
| 163 | LUA_API void lua_setstr (lua_State *L, int index, const char *name); | ||
| 164 | LUA_API void lua_settable (lua_State *L, int index); | 162 | LUA_API void lua_settable (lua_State *L, int index); |
| 165 | LUA_API void lua_rawset (lua_State *L, int index); | 163 | LUA_API void lua_rawset (lua_State *L, int index); |
| 166 | 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); |
| @@ -232,13 +230,6 @@ LUA_API void lua_newuserdatabox (lua_State *L, void *u); | |||
| 232 | #define lua_pushliteral(L, s) lua_pushlstring(L, "" s, \ | 230 | #define lua_pushliteral(L, s) lua_pushlstring(L, "" s, \ |
| 233 | (sizeof(s)/sizeof(char))-1) | 231 | (sizeof(s)/sizeof(char))-1) |
| 234 | 232 | ||
| 235 | #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) | ||
| 236 | #define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX) | ||
| 237 | #define lua_setglobals(L) lua_replace(L, LUA_GLOBALSINDEX) | ||
| 238 | #define lua_getglobal(L,s) lua_getstr(L, LUA_GLOBALSINDEX, s) | ||
| 239 | #define lua_setglobal(L,s) lua_setstr(L, LUA_GLOBALSINDEX, s) | ||
| 240 | |||
| 241 | |||
| 242 | 233 | ||
| 243 | /* | 234 | /* |
| 244 | ** compatibility macros and functions | 235 | ** compatibility macros and functions |
| @@ -246,6 +237,15 @@ LUA_API void lua_newuserdatabox (lua_State *L, void *u); | |||
| 246 | 237 | ||
| 247 | LUA_API int lua_pushupvalues (lua_State *L); | 238 | LUA_API int lua_pushupvalues (lua_State *L); |
| 248 | 239 | ||
| 240 | #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) | ||
| 241 | #define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX) | ||
| 242 | #define lua_setglobals(L) lua_replace(L, LUA_GLOBALSINDEX) | ||
| 243 | #define lua_setglobal(L,s) \ | ||
| 244 | (lua_pushstring(L, s), lua_insert(L, -2), lua_settable(L, LUA_GLOBALSINDEX)) | ||
| 245 | |||
| 246 | #define lua_getglobal(L,s) \ | ||
| 247 | (lua_pushstring(L, s), lua_gettable(L, LUA_GLOBALSINDEX)) | ||
| 248 | |||
| 249 | #define lua_isnull lua_isnone | 249 | #define lua_isnull lua_isnone |
| 250 | 250 | ||
| 251 | 251 | ||
