diff options
-rw-r--r-- | lapi.c | 92 | ||||
-rw-r--r-- | lbaselib.c | 22 | ||||
-rw-r--r-- | lobject.h | 17 | ||||
-rw-r--r-- | ltablib.c | 6 | ||||
-rw-r--r-- | lua.h | 4 |
5 files changed, 98 insertions, 43 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.285 2018/02/20 16:52:50 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.286 2018/02/23 13:13:31 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -668,35 +668,65 @@ static int finishrawget (lua_State *L, const TValue *val) { | |||
668 | } | 668 | } |
669 | 669 | ||
670 | 670 | ||
671 | static Table *gettable (lua_State *L, int idx) { | ||
672 | TValue *t = index2value(L, idx); | ||
673 | api_check(L, ttistable(t), "table expected"); | ||
674 | return hvalue(t); | ||
675 | } | ||
676 | |||
677 | |||
671 | LUA_API int lua_rawget (lua_State *L, int idx) { | 678 | LUA_API int lua_rawget (lua_State *L, int idx) { |
672 | TValue *t; | 679 | Table *t; |
673 | const TValue *val; | 680 | const TValue *val; |
674 | lua_lock(L); | 681 | lua_lock(L); |
675 | t = index2value(L, idx); | 682 | t = gettable(L, idx); |
676 | api_check(L, ttistable(t), "table expected"); | 683 | val = luaH_get(t, s2v(L->top - 1)); |
677 | val = luaH_get(hvalue(t), s2v(L->top - 1)); | ||
678 | L->top--; /* remove key */ | 684 | L->top--; /* remove key */ |
679 | return finishrawget(L, val); | 685 | return finishrawget(L, val); |
680 | } | 686 | } |
681 | 687 | ||
682 | 688 | ||
683 | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { | 689 | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { |
684 | TValue *t; | 690 | Table *t; |
685 | lua_lock(L); | 691 | lua_lock(L); |
686 | t = index2value(L, idx); | 692 | t = gettable(L, idx); |
687 | api_check(L, ttistable(t), "table expected"); | 693 | return finishrawget(L, luaH_getint(t, n)); |
688 | return finishrawget(L, luaH_getint(hvalue(t), n)); | ||
689 | } | 694 | } |
690 | 695 | ||
691 | 696 | ||
692 | LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { | 697 | LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { |
693 | TValue *t; | 698 | Table *t; |
694 | TValue k; | 699 | TValue k; |
695 | lua_lock(L); | 700 | lua_lock(L); |
696 | t = index2value(L, idx); | 701 | t = gettable(L, idx); |
697 | api_check(L, ttistable(t), "table expected"); | ||
698 | setpvalue(&k, cast_voidp(p)); | 702 | setpvalue(&k, cast_voidp(p)); |
699 | return finishrawget(L, luaH_get(hvalue(t), &k)); | 703 | return finishrawget(L, luaH_get(t, &k)); |
704 | } | ||
705 | |||
706 | |||
707 | static int auxkeyman (lua_State *L, int idx, int remove) { | ||
708 | Table *t; | ||
709 | const TValue *val; | ||
710 | int res; | ||
711 | lua_lock(L); | ||
712 | t = gettable(L, idx); | ||
713 | val = luaH_get(t, s2v(L->top - 1)); | ||
714 | L->top--; /* remove key */ | ||
715 | res = !isempty(val); | ||
716 | if (remove && res) /* key is present and should be removed? */ | ||
717 | setempty(cast(TValue*, val)); | ||
718 | lua_unlock(L); | ||
719 | return res; | ||
720 | } | ||
721 | |||
722 | |||
723 | LUA_API void lua_removekey (lua_State *L, int idx) { | ||
724 | auxkeyman(L, idx, 1); | ||
725 | } | ||
726 | |||
727 | |||
728 | LUA_API int lua_keyin (lua_State *L, int idx) { | ||
729 | return auxkeyman(L, idx, 0); | ||
700 | } | 730 | } |
701 | 731 | ||
702 | 732 | ||
@@ -834,45 +864,42 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { | |||
834 | 864 | ||
835 | 865 | ||
836 | LUA_API void lua_rawset (lua_State *L, int idx) { | 866 | LUA_API void lua_rawset (lua_State *L, int idx) { |
837 | TValue *o; | 867 | Table *t; |
838 | TValue *slot; | 868 | TValue *slot; |
839 | lua_lock(L); | 869 | lua_lock(L); |
840 | api_checknelems(L, 2); | 870 | api_checknelems(L, 2); |
841 | o = index2value(L, idx); | 871 | t = gettable(L, idx); |
842 | api_check(L, ttistable(o), "table expected"); | 872 | slot = luaH_set(L, t, s2v(L->top - 2)); |
843 | slot = luaH_set(L, hvalue(o), s2v(L->top - 2)); | ||
844 | setobj2t(L, slot, s2v(L->top - 1)); | 873 | setobj2t(L, slot, s2v(L->top - 1)); |
845 | invalidateTMcache(hvalue(o)); | 874 | invalidateTMcache(t); |
846 | luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); | 875 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); |
847 | L->top -= 2; | 876 | L->top -= 2; |
848 | lua_unlock(L); | 877 | lua_unlock(L); |
849 | } | 878 | } |
850 | 879 | ||
851 | 880 | ||
852 | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { | 881 | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { |
853 | TValue *o; | 882 | Table *t; |
854 | lua_lock(L); | 883 | lua_lock(L); |
855 | api_checknelems(L, 1); | 884 | api_checknelems(L, 1); |
856 | o = index2value(L, idx); | 885 | t = gettable(L, idx); |
857 | api_check(L, ttistable(o), "table expected"); | 886 | luaH_setint(L, t, n, s2v(L->top - 1)); |
858 | luaH_setint(L, hvalue(o), n, s2v(L->top - 1)); | 887 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); |
859 | luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); | ||
860 | L->top--; | 888 | L->top--; |
861 | lua_unlock(L); | 889 | lua_unlock(L); |
862 | } | 890 | } |
863 | 891 | ||
864 | 892 | ||
865 | LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { | 893 | LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { |
866 | TValue *o; | 894 | Table *t; |
867 | TValue k, *slot; | 895 | TValue k, *slot; |
868 | lua_lock(L); | 896 | lua_lock(L); |
869 | api_checknelems(L, 1); | 897 | api_checknelems(L, 1); |
870 | o = index2value(L, idx); | 898 | t = gettable(L, idx); |
871 | api_check(L, ttistable(o), "table expected"); | ||
872 | setpvalue(&k, cast_voidp(p)); | 899 | setpvalue(&k, cast_voidp(p)); |
873 | slot = luaH_set(L, hvalue(o), &k); | 900 | slot = luaH_set(L, t, &k); |
874 | setobj2t(L, slot, s2v(L->top - 1)); | 901 | setobj2t(L, slot, s2v(L->top - 1)); |
875 | luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); | 902 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); |
876 | L->top--; | 903 | L->top--; |
877 | lua_unlock(L); | 904 | lua_unlock(L); |
878 | } | 905 | } |
@@ -1193,12 +1220,11 @@ LUA_API int lua_error (lua_State *L) { | |||
1193 | 1220 | ||
1194 | 1221 | ||
1195 | LUA_API int lua_next (lua_State *L, int idx) { | 1222 | LUA_API int lua_next (lua_State *L, int idx) { |
1196 | TValue *t; | 1223 | Table *t; |
1197 | int more; | 1224 | int more; |
1198 | lua_lock(L); | 1225 | lua_lock(L); |
1199 | t = index2value(L, idx); | 1226 | t = gettable(L, idx); |
1200 | api_check(L, ttistable(t), "table expected"); | 1227 | more = luaH_next(L, t, L->top - 1); |
1201 | more = luaH_next(L, hvalue(t), L->top - 1); | ||
1202 | if (more) { | 1228 | if (more) { |
1203 | api_incr_top(L); | 1229 | api_incr_top(L); |
1204 | } | 1230 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.318 2017/11/16 13:19:06 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.319 2018/02/05 17:10:52 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -170,6 +170,24 @@ static int luaB_rawset (lua_State *L) { | |||
170 | } | 170 | } |
171 | 171 | ||
172 | 172 | ||
173 | static int luaB_keyin (lua_State *L) { | ||
174 | luaL_checktype(L, 1, LUA_TTABLE); | ||
175 | luaL_checkany(L, 2); | ||
176 | lua_settop(L, 2); | ||
177 | lua_pushboolean(L, lua_keyin(L, 1)); | ||
178 | return 1; | ||
179 | } | ||
180 | |||
181 | |||
182 | static int luaB_removekey (lua_State *L) { | ||
183 | luaL_checktype(L, 1, LUA_TTABLE); | ||
184 | luaL_checkany(L, 2); | ||
185 | lua_settop(L, 2); | ||
186 | lua_removekey(L, 1); | ||
187 | return 0; | ||
188 | } | ||
189 | |||
190 | |||
173 | static int pushmode (lua_State *L, int oldmode) { | 191 | static int pushmode (lua_State *L, int oldmode) { |
174 | lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); | 192 | lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); |
175 | return 1; | 193 | return 1; |
@@ -501,6 +519,8 @@ static const luaL_Reg base_funcs[] = { | |||
501 | {"rawlen", luaB_rawlen}, | 519 | {"rawlen", luaB_rawlen}, |
502 | {"rawget", luaB_rawget}, | 520 | {"rawget", luaB_rawget}, |
503 | {"rawset", luaB_rawset}, | 521 | {"rawset", luaB_rawset}, |
522 | {"keyin", luaB_keyin}, | ||
523 | {"removekey", luaB_removekey}, | ||
504 | {"select", luaB_select}, | 524 | {"select", luaB_select}, |
505 | {"setmetatable", luaB_setmetatable}, | 525 | {"setmetatable", luaB_setmetatable}, |
506 | {"tonumber", luaB_tonumber}, | 526 | {"tonumber", luaB_tonumber}, |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.h,v 2.136 2018/02/22 17:28:10 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 2.137 2018/02/23 13:13:31 roberto Exp roberto $ |
3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -156,13 +156,20 @@ typedef StackValue *StkId; /* index to stack elements */ | |||
156 | #define LUA_TEMPTY (LUA_TNIL | (1 << 4)) | 156 | #define LUA_TEMPTY (LUA_TNIL | (1 << 4)) |
157 | 157 | ||
158 | #define ttisnilorempty(v) checktype((v), LUA_TNIL) | 158 | #define ttisnilorempty(v) checktype((v), LUA_TNIL) |
159 | /* | ||
160 | ** By default, entries with any kind of nil are considered empty | ||
161 | */ | ||
162 | #define isempty(v) ttisnilorempty(v) | ||
163 | 159 | ||
164 | #define isreallyempty(v) checktag((v), LUA_TEMPTY) | 160 | #define isreallyempty(v) checktag((v), LUA_TEMPTY) |
165 | 161 | ||
162 | |||
163 | #if defined(LUA_NILINTABLE) | ||
164 | |||
165 | #define isempty(v) isreallyempty(v) | ||
166 | |||
167 | #else /* By default, entries with any kind of nil are considered empty */ | ||
168 | |||
169 | #define isempty(v) ttisnilorempty(v) | ||
170 | |||
171 | #endif | ||
172 | |||
166 | /* macro defining an empty value */ | 173 | /* macro defining an empty value */ |
167 | #define EMPTYCONSTANT {NULL}, LUA_TEMPTY | 174 | #define EMPTYCONSTANT {NULL}, LUA_TEMPTY |
168 | 175 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.92 2016/02/08 12:55:19 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.93 2016/02/25 19:41:54 roberto Exp roberto $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -113,8 +113,8 @@ static int tremove (lua_State *L) { | |||
113 | lua_geti(L, 1, pos + 1); | 113 | lua_geti(L, 1, pos + 1); |
114 | lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ | 114 | lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ |
115 | } | 115 | } |
116 | lua_pushnil(L); | 116 | lua_pushinteger(L, pos); |
117 | lua_seti(L, 1, pos); /* t[pos] = nil */ | 117 | lua_removekey(L, 1); /* remove entry t[pos] */ |
118 | return 1; | 118 | return 1; |
119 | } | 119 | } |
120 | 120 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.340 2018/02/17 19:29:29 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.341 2018/02/20 16:52:50 roberto Exp roberto $ |
3 | ** Lua - A Scripting Language | 3 | ** Lua - A Scripting Language |
4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
5 | ** See Copyright Notice at the end of this file | 5 | ** See Copyright Notice at the end of this file |
@@ -331,6 +331,8 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); | |||
331 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); | 331 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); |
332 | LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); | 332 | LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); |
333 | 333 | ||
334 | LUA_API void (lua_removekey) (lua_State *L, int idx); | ||
335 | LUA_API int (lua_keyin) (lua_State *L, int idx); | ||
334 | 336 | ||
335 | 337 | ||
336 | /* | 338 | /* |