aboutsummaryrefslogtreecommitdiff
path: root/lapi.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 /lapi.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 'lapi.c')
-rw-r--r--lapi.c69
1 files changed, 35 insertions, 34 deletions
diff --git a/lapi.c b/lapi.c
index a6ef5663..2b14c15e 100644
--- a/lapi.c
+++ b/lapi.c
@@ -353,7 +353,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
353 } 353 }
354 /* first operand at top - 2, second at top - 1; result go to top - 2 */ 354 /* first operand at top - 2, second at top - 1; result go to top - 2 */
355 luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); 355 luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
356 L->top.p--; /* remove second operand */ 356 L->top.p--; /* pop second operand */
357 lua_unlock(L); 357 lua_unlock(L);
358} 358}
359 359
@@ -666,47 +666,49 @@ LUA_API int lua_pushthread (lua_State *L) {
666 666
667 667
668static int auxgetstr (lua_State *L, const TValue *t, const char *k) { 668static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
669 TValue aux; 669 int tag;
670 TString *str = luaS_new(L, k); 670 TString *str = luaS_new(L, k);
671 luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, aux); 671 luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
672 if (!isemptyV(aux)) { 672 if (!tagisempty(tag)) {
673 api_incr_top(L); 673 api_incr_top(L);
674 } 674 }
675 else { 675 else {
676 setsvalue2s(L, L->top.p, str); 676 setsvalue2s(L, L->top.p, str);
677 api_incr_top(L); 677 api_incr_top(L);
678 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); 678 tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
679 } 679 }
680 lua_unlock(L); 680 lua_unlock(L);
681 return ttype(s2v(L->top.p - 1)); 681 return novariant(tag);
682} 682}
683 683
684 684
685static TValue getGlobalTable (lua_State *L) { 685static void getGlobalTable (lua_State *L, TValue *gt) {
686 Table *registry = hvalue(&G(L)->l_registry); 686 Table *registry = hvalue(&G(L)->l_registry);
687 return luaH_getint(registry, LUA_RIDX_GLOBALS); 687 int tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
688 (void)tag; /* avoid not-used warnings when checks are off */
689 api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
688} 690}
689 691
690 692
691LUA_API int lua_getglobal (lua_State *L, const char *name) { 693LUA_API int lua_getglobal (lua_State *L, const char *name) {
692 TValue gt; 694 TValue gt;
693 lua_lock(L); 695 lua_lock(L);
694 gt = getGlobalTable(L); 696 getGlobalTable(L, &gt);
695 return auxgetstr(L, &gt, name); 697 return auxgetstr(L, &gt, name);
696} 698}
697 699
698 700
699LUA_API int lua_gettable (lua_State *L, int idx) { 701LUA_API int lua_gettable (lua_State *L, int idx) {
700 TValue aux; 702 int tag;
701 TValue *t; 703 TValue *t;
702 lua_lock(L); 704 lua_lock(L);
703 api_checkpop(L, 1); 705 api_checkpop(L, 1);
704 t = index2value(L, idx); 706 t = index2value(L, idx);
705 luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, aux); 707 luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
706 if (isemptyV(aux)) 708 if (tagisempty(tag))
707 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); 709 tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
708 lua_unlock(L); 710 lua_unlock(L);
709 return ttype(s2v(L->top.p - 1)); 711 return novariant(tag);
710} 712}
711 713
712 714
@@ -718,29 +720,27 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
718 720
719LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { 721LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
720 TValue *t; 722 TValue *t;
721 TValue aux; 723 int tag;
722 lua_lock(L); 724 lua_lock(L);
723 t = index2value(L, idx); 725 t = index2value(L, idx);
724 luaV_fastgeti(t, n, s2v(L->top.p), aux); 726 luaV_fastgeti(t, n, s2v(L->top.p), tag);
725 if (isemptyV(aux)) { 727 if (tagisempty(tag)) {
726 TValue key; 728 TValue key;
727 setivalue(&key, n); 729 setivalue(&key, n);
728 luaV_finishget(L, t, &key, L->top.p, aux); 730 tag = luaV_finishget(L, t, &key, L->top.p, tag);
729 } 731 }
730 api_incr_top(L); 732 api_incr_top(L);
731 lua_unlock(L); 733 lua_unlock(L);
732 return ttype(s2v(L->top.p - 1)); 734 return novariant(tag);
733} 735}
734 736
735 737
736l_sinline int finishrawget (lua_State *L, TValue res) { 738static int finishrawget (lua_State *L, int tag) {
737 if (isemptyV(res)) /* avoid copying empty items to the stack */ 739 if (tagisempty(tag)) /* avoid copying empty items to the stack */
738 setnilvalue(s2v(L->top.p)); 740 setnilvalue(s2v(L->top.p));
739 else
740 setobjV(L, s2v(L->top.p), res);
741 api_incr_top(L); 741 api_incr_top(L);
742 lua_unlock(L); 742 lua_unlock(L);
743 return ttypeV(res); 743 return novariant(tag);
744} 744}
745 745
746 746
@@ -753,23 +753,23 @@ l_sinline Table *gettable (lua_State *L, int idx) {
753 753
754LUA_API int lua_rawget (lua_State *L, int idx) { 754LUA_API int lua_rawget (lua_State *L, int idx) {
755 Table *t; 755 Table *t;
756 TValue res; 756 int tag;
757 lua_lock(L); 757 lua_lock(L);
758 api_checkpop(L, 1); 758 api_checkpop(L, 1);
759 t = gettable(L, idx); 759 t = gettable(L, idx);
760 res = luaH_get(t, s2v(L->top.p - 1)); 760 tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
761 L->top.p--; /* pop key */ 761 L->top.p--; /* pop key */
762 return finishrawget(L, res); 762 return finishrawget(L, tag);
763} 763}
764 764
765 765
766LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { 766LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
767 Table *t; 767 Table *t;
768 TValue aux; 768 int tag;
769 lua_lock(L); 769 lua_lock(L);
770 t = gettable(L, idx); 770 t = gettable(L, idx);
771 luaH_fastgeti(t, n, s2v(L->top.p), aux); 771 luaH_fastgeti(t, n, s2v(L->top.p), tag);
772 return finishrawget(L, aux); 772 return finishrawget(L, tag);
773} 773}
774 774
775 775
@@ -779,7 +779,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
779 lua_lock(L); 779 lua_lock(L);
780 t = gettable(L, idx); 780 t = gettable(L, idx);
781 setpvalue(&k, cast_voidp(p)); 781 setpvalue(&k, cast_voidp(p));
782 return finishrawget(L, luaH_get(t, &k)); 782 return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
783} 783}
784 784
785 785
@@ -872,7 +872,7 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
872LUA_API void lua_setglobal (lua_State *L, const char *name) { 872LUA_API void lua_setglobal (lua_State *L, const char *name) {
873 TValue gt; 873 TValue gt;
874 lua_lock(L); /* unlock done in 'auxsetstr' */ 874 lua_lock(L); /* unlock done in 'auxsetstr' */
875 gt = getGlobalTable(L); 875 getGlobalTable(L, &gt);
876 auxsetstr(L, &gt, name); 876 auxsetstr(L, &gt, name);
877} 877}
878 878
@@ -1122,7 +1122,8 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1122 LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ 1122 LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
1123 if (f->nupvalues >= 1) { /* does it have an upvalue? */ 1123 if (f->nupvalues >= 1) { /* does it have an upvalue? */
1124 /* get global table from registry */ 1124 /* get global table from registry */
1125 TValue gt = getGlobalTable(L); 1125 TValue gt;
1126 getGlobalTable(L, &gt);
1126 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ 1127 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1127 setobj(L, f->upvals[0]->v.p, &gt); 1128 setobj(L, f->upvals[0]->v.p, &gt);
1128 luaC_barrier(L, f->upvals[0], &gt); 1129 luaC_barrier(L, f->upvals[0], &gt);
@@ -1266,7 +1267,7 @@ LUA_API int lua_next (lua_State *L, int idx) {
1266 if (more) 1267 if (more)
1267 api_incr_top(L); 1268 api_incr_top(L);
1268 else /* no more elements */ 1269 else /* no more elements */
1269 L->top.p -= 1; /* remove key */ 1270 L->top.p--; /* pop key */
1270 lua_unlock(L); 1271 lua_unlock(L);
1271 return more; 1272 return more;
1272} 1273}