diff options
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 92 |
1 files changed, 59 insertions, 33 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 | } |