aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c61
1 files changed, 30 insertions, 31 deletions
diff --git a/lapi.c b/lapi.c
index 7df63798..a6ef5663 100644
--- a/lapi.c
+++ b/lapi.c
@@ -666,47 +666,45 @@ 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 int hres; 669 TValue aux;
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, hres); 671 luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, aux);
672 if (hres == HOK) { 672 if (!isemptyV(aux)) {
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, hres); 678 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux);
679 } 679 }
680 lua_unlock(L); 680 lua_unlock(L);
681 return ttype(s2v(L->top.p - 1)); 681 return ttype(s2v(L->top.p - 1));
682} 682}
683 683
684 684
685static void getGlobalTable (lua_State *L, TValue *gt) { 685static TValue getGlobalTable (lua_State *L) {
686 Table *registry = hvalue(&G(L)->l_registry); 686 Table *registry = hvalue(&G(L)->l_registry);
687 int hres = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); 687 return luaH_getint(registry, LUA_RIDX_GLOBALS);
688 (void)hres; /* avoid warnings (not used) when checks are off */
689 api_check(L, hres == HOK, "global table must exist");
690} 688}
691 689
692 690
693LUA_API int lua_getglobal (lua_State *L, const char *name) { 691LUA_API int lua_getglobal (lua_State *L, const char *name) {
694 TValue gt; 692 TValue gt;
695 lua_lock(L); 693 lua_lock(L);
696 getGlobalTable(L, &gt); 694 gt = getGlobalTable(L);
697 return auxgetstr(L, &gt, name); 695 return auxgetstr(L, &gt, name);
698} 696}
699 697
700 698
701LUA_API int lua_gettable (lua_State *L, int idx) { 699LUA_API int lua_gettable (lua_State *L, int idx) {
702 int hres; 700 TValue aux;
703 TValue *t; 701 TValue *t;
704 lua_lock(L); 702 lua_lock(L);
705 api_checkpop(L, 1); 703 api_checkpop(L, 1);
706 t = index2value(L, idx); 704 t = index2value(L, idx);
707 luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, hres); 705 luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, aux);
708 if (hres != HOK) 706 if (isemptyV(aux))
709 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres); 707 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux);
710 lua_unlock(L); 708 lua_unlock(L);
711 return ttype(s2v(L->top.p - 1)); 709 return ttype(s2v(L->top.p - 1));
712} 710}
@@ -720,14 +718,14 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
720 718
721LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { 719LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
722 TValue *t; 720 TValue *t;
723 int hres; 721 TValue aux;
724 lua_lock(L); 722 lua_lock(L);
725 t = index2value(L, idx); 723 t = index2value(L, idx);
726 luaV_fastgeti(t, n, s2v(L->top.p), hres); 724 luaV_fastgeti(t, n, s2v(L->top.p), aux);
727 if (hres != HOK) { 725 if (isemptyV(aux)) {
728 TValue key; 726 TValue key;
729 setivalue(&key, n); 727 setivalue(&key, n);
730 luaV_finishget(L, t, &key, L->top.p, hres); 728 luaV_finishget(L, t, &key, L->top.p, aux);
731 } 729 }
732 api_incr_top(L); 730 api_incr_top(L);
733 lua_unlock(L); 731 lua_unlock(L);
@@ -735,12 +733,14 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
735} 733}
736 734
737 735
738l_sinline int finishrawget (lua_State *L, int hres) { 736l_sinline int finishrawget (lua_State *L, TValue res) {
739 if (hres != HOK) /* avoid copying empty items to the stack */ 737 if (isemptyV(res)) /* avoid copying empty items to the stack */
740 setnilvalue(s2v(L->top.p)); 738 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 ttype(s2v(L->top.p - 1)); 743 return ttypeV(res);
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 lua_lock(L); 757 lua_lock(L);
757 api_checkpop(L, 1); 758 api_checkpop(L, 1);
758 t = gettable(L, idx); 759 t = gettable(L, idx);
759 if (luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)) != HOK) 760 res = luaH_get(t, s2v(L->top.p - 1));
760 setnilvalue(s2v(L->top.p - 1)); 761 L->top.p--; /* pop key */
761 lua_unlock(L); 762 return finishrawget(L, res);
762 return ttype(s2v(L->top.p - 1));
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 int hres; 768 TValue aux;
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), hres); 771 luaH_fastgeti(t, n, s2v(L->top.p), aux);
772 return finishrawget(L, hres); 772 return finishrawget(L, aux);
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, s2v(L->top.p))); 782 return finishrawget(L, luaH_get(t, &k));
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 getGlobalTable(L, &gt); 875 gt = getGlobalTable(L);
876 auxsetstr(L, &gt, name); 876 auxsetstr(L, &gt, name);
877} 877}
878 878
@@ -1122,8 +1122,7 @@ 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; 1125 TValue gt = getGlobalTable(L);
1126 getGlobalTable(L, &gt);
1127 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ 1126 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1128 setobj(L, f->upvals[0]->v.p, &gt); 1127 setobj(L, f->upvals[0]->v.p, &gt);
1129 luaC_barrier(L, f->upvals[0], &gt); 1128 luaC_barrier(L, f->upvals[0], &gt);