diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-12-04 11:08:42 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-12-04 11:08:42 -0300 |
commit | 23051e830a8b212f831443eb888e93e30fa8bb19 (patch) | |
tree | 782f56415ad3a8799c4dea8d6d329f1550d3d7c3 /ltable.c | |
parent | f15589f3b0da477e5dda8863cbf4c0b36469e36d (diff) | |
download | lua-23051e830a8b212f831443eb888e93e30fa8bb19.tar.gz lua-23051e830a8b212f831443eb888e93e30fa8bb19.tar.bz2 lua-23051e830a8b212f831443eb888e93e30fa8bb19.zip |
Changes in the API of 'luaH_set' and related functions
Functions to set values in a table (luaH_set, luaH_newkey, etc.) receive
the new value, instead of returning a slot where to put the value.
Diffstat (limited to 'ltable.c')
-rw-r--r-- | ltable.c | 42 |
1 files changed, 27 insertions, 15 deletions
@@ -485,7 +485,7 @@ static void reinsert (lua_State *L, Table *ot, Table *t) { | |||
485 | already present in the table */ | 485 | already present in the table */ |
486 | TValue k; | 486 | TValue k; |
487 | getnodekey(L, &k, old); | 487 | getnodekey(L, &k, old); |
488 | setobjt2t(L, luaH_set(L, t, &k), gval(old)); | 488 | luaH_set(L, t, &k, gval(old)); |
489 | } | 489 | } |
490 | } | 490 | } |
491 | } | 491 | } |
@@ -632,7 +632,7 @@ static Node *getfreepos (Table *t) { | |||
632 | ** put new key in its main position; otherwise (colliding node is in its main | 632 | ** put new key in its main position; otherwise (colliding node is in its main |
633 | ** position), new key goes to an empty position. | 633 | ** position), new key goes to an empty position. |
634 | */ | 634 | */ |
635 | TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | 635 | void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { |
636 | Node *mp; | 636 | Node *mp; |
637 | TValue aux; | 637 | TValue aux; |
638 | if (unlikely(ttisnil(key))) | 638 | if (unlikely(ttisnil(key))) |
@@ -654,7 +654,8 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
654 | if (f == NULL) { /* cannot find a free place? */ | 654 | if (f == NULL) { /* cannot find a free place? */ |
655 | rehash(L, t, key); /* grow table */ | 655 | rehash(L, t, key); /* grow table */ |
656 | /* whatever called 'newkey' takes care of TM cache */ | 656 | /* whatever called 'newkey' takes care of TM cache */ |
657 | return luaH_set(L, t, key); /* insert key into grown table */ | 657 | luaH_set(L, t, key, value); /* insert key into grown table */ |
658 | return; | ||
658 | } | 659 | } |
659 | lua_assert(!isdummy(t)); | 660 | lua_assert(!isdummy(t)); |
660 | othern = mainposition(t, keytt(mp), &keyval(mp)); | 661 | othern = mainposition(t, keytt(mp), &keyval(mp)); |
@@ -682,7 +683,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
682 | setnodekey(L, mp, key); | 683 | setnodekey(L, mp, key); |
683 | luaC_barrierback(L, obj2gco(t), key); | 684 | luaC_barrierback(L, obj2gco(t), key); |
684 | lua_assert(isempty(gval(mp))); | 685 | lua_assert(isempty(gval(mp))); |
685 | return gval(mp); | 686 | setobj2t(L, gval(mp), value); |
686 | } | 687 | } |
687 | 688 | ||
688 | 689 | ||
@@ -770,28 +771,39 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
770 | 771 | ||
771 | 772 | ||
772 | /* | 773 | /* |
774 | ** Finish a raw "set table" operation, where 'slot' is where the value | ||
775 | ** should have been (the result of a previous "get table"). | ||
776 | ** Beware: when using this function you probably need to check a GC | ||
777 | ** barrier and invalidate the TM cache. | ||
778 | */ | ||
779 | void luaH_finishset (lua_State *L, Table *t, const TValue *key, | ||
780 | const TValue *slot, TValue *value) { | ||
781 | if (isabstkey(slot)) | ||
782 | luaH_newkey(L, t, key, value); | ||
783 | else | ||
784 | setobj2t(L, cast(TValue *, slot), value); | ||
785 | } | ||
786 | |||
787 | |||
788 | /* | ||
773 | ** beware: when using this function you probably need to check a GC | 789 | ** beware: when using this function you probably need to check a GC |
774 | ** barrier and invalidate the TM cache. | 790 | ** barrier and invalidate the TM cache. |
775 | */ | 791 | */ |
776 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { | 792 | void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { |
777 | const TValue *p = luaH_get(t, key); | 793 | const TValue *slot = luaH_get(t, key); |
778 | if (!isabstkey(p)) | 794 | luaH_finishset(L, t, key, slot, value); |
779 | return cast(TValue *, p); | ||
780 | else return luaH_newkey(L, t, key); | ||
781 | } | 795 | } |
782 | 796 | ||
783 | 797 | ||
784 | void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { | 798 | void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { |
785 | const TValue *p = luaH_getint(t, key); | 799 | const TValue *p = luaH_getint(t, key); |
786 | TValue *cell; | 800 | if (isabstkey(p)) { |
787 | if (!isabstkey(p)) | ||
788 | cell = cast(TValue *, p); | ||
789 | else { | ||
790 | TValue k; | 801 | TValue k; |
791 | setivalue(&k, key); | 802 | setivalue(&k, key); |
792 | cell = luaH_newkey(L, t, &k); | 803 | luaH_newkey(L, t, &k, value); |
793 | } | 804 | } |
794 | setobj2t(L, cell, value); | 805 | else |
806 | setobj2t(L, cast(TValue *, p), value); | ||
795 | } | 807 | } |
796 | 808 | ||
797 | 809 | ||