aboutsummaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-03-21 11:23:21 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-03-21 11:23:21 -0300
commit0593256707ceddb1bc9cd4b25b822a7fbcfedd66 (patch)
tree6c6859b94086b71b27409b565ed34c114f03e7f8 /ltable.c
parentce6f5502c99ce9a367e25b678e375db6f8164d73 (diff)
downloadlua-0593256707ceddb1bc9cd4b25b822a7fbcfedd66.tar.gz
lua-0593256707ceddb1bc9cd4b25b822a7fbcfedd66.tar.bz2
lua-0593256707ceddb1bc9cd4b25b822a7fbcfedd66.zip
'luaH_get' functions return tag of the result
Undoing previous commit. Returning TValue increases code size without any visible gains. Returning the tag is a little simpler than returning a special code (HOK/HNOTFOUND) and the tag is useful by itself in some cases.
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c48
1 files changed, 30 insertions, 18 deletions
diff --git a/ltable.c b/ltable.c
index f675f39b..f62f36bc 100644
--- a/ltable.c
+++ b/ltable.c
@@ -904,14 +904,23 @@ static int hashkeyisempty (Table *t, lua_Integer key) {
904} 904}
905 905
906 906
907TValue luaH_getint (Table *t, lua_Integer key) { 907static int finishnodeget (const TValue *val, TValue *res) {
908 if (!ttisnil(val)) {
909 setobj(((lua_State*)NULL), res, val);
910 }
911 return ttypetag(val);
912}
913
914
915int luaH_getint (Table *t, lua_Integer key, TValue *res) {
908 if (keyinarray(t, key)) { 916 if (keyinarray(t, key)) {
909 TValue res; 917 int tag = *getArrTag(t, key - 1);
910 arr2objV(t, key, res); 918 if (!tagisempty(tag))
911 return res; 919 farr2val(t, key, tag, res);
920 return tag;
912 } 921 }
913 else 922 else
914 return *getintfromhash(t, key); 923 return finishnodeget(getintfromhash(t, key), res);
915} 924}
916 925
917 926
@@ -934,8 +943,8 @@ const TValue *luaH_Hgetshortstr (Table *t, TString *key) {
934} 943}
935 944
936 945
937TValue luaH_getshortstr (Table *t, TString *key) { 946int luaH_getshortstr (Table *t, TString *key, TValue *res) {
938 return *luaH_Hgetshortstr(t, key); 947 return finishnodeget(luaH_Hgetshortstr(t, key), res);
939} 948}
940 949
941 950
@@ -950,8 +959,8 @@ static const TValue *Hgetstr (Table *t, TString *key) {
950} 959}
951 960
952 961
953TValue luaH_getstr (Table *t, TString *key) { 962int luaH_getstr (Table *t, TString *key, TValue *res) {
954 return *Hgetstr(t, key); 963 return finishnodeget(Hgetstr(t, key), res);
955} 964}
956 965
957 966
@@ -967,31 +976,34 @@ TString *luaH_getstrkey (Table *t, TString *key) {
967/* 976/*
968** main search function 977** main search function
969*/ 978*/
970TValue luaH_get (Table *t, const TValue *key) { 979int luaH_get (Table *t, const TValue *key, TValue *res) {
980 const TValue *slot;
971 switch (ttypetag(key)) { 981 switch (ttypetag(key)) {
972 case LUA_VSHRSTR: 982 case LUA_VSHRSTR:
973 return *luaH_Hgetshortstr(t, tsvalue(key)); 983 slot = luaH_Hgetshortstr(t, tsvalue(key));
974 break; 984 break;
975 case LUA_VNUMINT: 985 case LUA_VNUMINT:
976 return luaH_getint(t, ivalue(key)); 986 return luaH_getint(t, ivalue(key), res);
977 case LUA_VNIL: 987 case LUA_VNIL:
978 return absentkey; 988 slot = &absentkey;
979 break; 989 break;
980 case LUA_VNUMFLT: { 990 case LUA_VNUMFLT: {
981 lua_Integer k; 991 lua_Integer k;
982 if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ 992 if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
983 return luaH_getint(t, k); /* use specialized version */ 993 return luaH_getint(t, k, res); /* use specialized version */
984 /* else... */ 994 /* else... */
985 } /* FALLTHROUGH */ 995 } /* FALLTHROUGH */
986 default: 996 default:
987 return *getgeneric(t, key, 0); 997 slot = getgeneric(t, key, 0);
998 break;
988 } 999 }
1000 return finishnodeget(slot, res);
989} 1001}
990 1002
991 1003
992static int finishnodeset (Table *t, const TValue *slot, TValue *val) { 1004static int finishnodeset (Table *t, const TValue *slot, TValue *val) {
993 if (!ttisnil(slot)) { 1005 if (!ttisnil(slot)) {
994 setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); 1006 setobj(((lua_State*)NULL), cast(TValue*, slot), val);
995 return HOK; /* success */ 1007 return HOK; /* success */
996 } 1008 }
997 else if (isabstkey(slot)) 1009 else if (isabstkey(slot))
@@ -1005,7 +1017,7 @@ static int rawfinishnodeset (const TValue *slot, TValue *val) {
1005 if (isabstkey(slot)) 1017 if (isabstkey(slot))
1006 return 0; /* no slot with that key */ 1018 return 0; /* no slot with that key */
1007 else { 1019 else {
1008 setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); 1020 setobj(((lua_State*)NULL), cast(TValue*, slot), val);
1009 return 1; /* success */ 1021 return 1; /* success */
1010 } 1022 }
1011} 1023}