aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c110
1 files changed, 52 insertions, 58 deletions
diff --git a/lapi.c b/lapi.c
index 767fa55e..34b335fd 100644
--- a/lapi.c
+++ b/lapi.c
@@ -637,50 +637,44 @@ LUA_API int lua_pushthread (lua_State *L) {
637 637
638 638
639l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { 639l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
640 const TValue *slot; 640 int hres;
641 TString *str = luaS_new(L, k); 641 TString *str = luaS_new(L, k);
642 if (luaV_fastget(L, t, str, slot, luaH_getstr)) { 642 luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres);
643 setobj2s(L, L->top.p, slot); 643 if (hres == HOK) {
644 api_incr_top(L); 644 api_incr_top(L);
645 } 645 }
646 else { 646 else {
647 setsvalue2s(L, L->top.p, str); 647 setsvalue2s(L, L->top.p, str);
648 api_incr_top(L); 648 api_incr_top(L);
649 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); 649 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres);
650 } 650 }
651 lua_unlock(L); 651 lua_unlock(L);
652 return ttype(s2v(L->top.p - 1)); 652 return ttype(s2v(L->top.p - 1));
653} 653}
654 654
655 655
656/* 656static void getGlobalTable (lua_State *L, TValue *gt) {
657** Get the global table in the registry. Since all predefined 657 Table *registry = hvalue(&G(L)->l_registry);
658** indices in the registry were inserted right when the registry 658 luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
659** was created and never removed, they must always be in the array 659}
660** part of the registry.
661*/
662#define getGtable(L) \
663 (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
664 660
665 661
666LUA_API int lua_getglobal (lua_State *L, const char *name) { 662LUA_API int lua_getglobal (lua_State *L, const char *name) {
667 const TValue *G; 663 TValue gt;
668 lua_lock(L); 664 lua_lock(L);
669 G = getGtable(L); 665 getGlobalTable(L, &gt);
670 return auxgetstr(L, G, name); 666 return auxgetstr(L, &gt, name);
671} 667}
672 668
673 669
674LUA_API int lua_gettable (lua_State *L, int idx) { 670LUA_API int lua_gettable (lua_State *L, int idx) {
675 const TValue *slot; 671 int hres;
676 TValue *t; 672 TValue *t;
677 lua_lock(L); 673 lua_lock(L);
678 t = index2value(L, idx); 674 t = index2value(L, idx);
679 if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) { 675 luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, hres);
680 setobj2s(L, L->top.p - 1, slot); 676 if (hres != HOK)
681 } 677 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres);
682 else
683 luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
684 lua_unlock(L); 678 lua_unlock(L);
685 return ttype(s2v(L->top.p - 1)); 679 return ttype(s2v(L->top.p - 1));
686} 680}
@@ -694,16 +688,14 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
694 688
695LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { 689LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
696 TValue *t; 690 TValue *t;
697 const TValue *slot; 691 int hres;
698 lua_lock(L); 692 lua_lock(L);
699 t = index2value(L, idx); 693 t = index2value(L, idx);
700 if (luaV_fastgeti(L, t, n, slot)) { 694 luaV_fastgeti(t, n, s2v(L->top.p), hres);
701 setobj2s(L, L->top.p, slot); 695 if (hres != HOK) {
702 } 696 TValue key;
703 else { 697 setivalue(&key, n);
704 TValue aux; 698 luaV_finishget(L, t, &key, L->top.p, hres);
705 setivalue(&aux, n);
706 luaV_finishget(L, t, &aux, L->top.p, slot);
707 } 699 }
708 api_incr_top(L); 700 api_incr_top(L);
709 lua_unlock(L); 701 lua_unlock(L);
@@ -711,11 +703,9 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
711} 703}
712 704
713 705
714l_sinline int finishrawget (lua_State *L, const TValue *val) { 706l_sinline int finishrawget (lua_State *L, int hres) {
715 if (isempty(val)) /* avoid copying empty items to the stack */ 707 if (hres != HOK) /* avoid copying empty items to the stack */
716 setnilvalue(s2v(L->top.p)); 708 setnilvalue(s2v(L->top.p));
717 else
718 setobj2s(L, L->top.p, val);
719 api_incr_top(L); 709 api_incr_top(L);
720 lua_unlock(L); 710 lua_unlock(L);
721 return ttype(s2v(L->top.p - 1)); 711 return ttype(s2v(L->top.p - 1));
@@ -731,13 +721,13 @@ static Table *gettable (lua_State *L, int idx) {
731 721
732LUA_API int lua_rawget (lua_State *L, int idx) { 722LUA_API int lua_rawget (lua_State *L, int idx) {
733 Table *t; 723 Table *t;
734 const TValue *val; 724 int hres;
735 lua_lock(L); 725 lua_lock(L);
736 api_checknelems(L, 1); 726 api_checknelems(L, 1);
737 t = gettable(L, idx); 727 t = gettable(L, idx);
738 val = luaH_get(t, s2v(L->top.p - 1)); 728 hres = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
739 L->top.p--; /* remove key */ 729 L->top.p--; /* remove key */
740 return finishrawget(L, val); 730 return finishrawget(L, hres);
741} 731}
742 732
743 733
@@ -745,7 +735,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
745 Table *t; 735 Table *t;
746 lua_lock(L); 736 lua_lock(L);
747 t = gettable(L, idx); 737 t = gettable(L, idx);
748 return finishrawget(L, luaH_getint(t, n)); 738 return finishrawget(L, luaH_getint(t, n, s2v(L->top.p)));
749} 739}
750 740
751 741
@@ -755,7 +745,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
755 lua_lock(L); 745 lua_lock(L);
756 t = gettable(L, idx); 746 t = gettable(L, idx);
757 setpvalue(&k, cast_voidp(p)); 747 setpvalue(&k, cast_voidp(p));
758 return finishrawget(L, luaH_get(t, &k)); 748 return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
759} 749}
760 750
761 751
@@ -827,17 +817,18 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
827** t[k] = value at the top of the stack (where 'k' is a string) 817** t[k] = value at the top of the stack (where 'k' is a string)
828*/ 818*/
829static void auxsetstr (lua_State *L, const TValue *t, const char *k) { 819static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
830 const TValue *slot; 820 int hres;
831 TString *str = luaS_new(L, k); 821 TString *str = luaS_new(L, k);
832 api_checknelems(L, 1); 822 api_checknelems(L, 1);
833 if (luaV_fastget(L, t, str, slot, luaH_getstr)) { 823 luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
834 luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); 824 if (hres == HOK) {
825 luaV_finishfastset(L, t, s2v(L->top.p - 1));
835 L->top.p--; /* pop value */ 826 L->top.p--; /* pop value */
836 } 827 }
837 else { 828 else {
838 setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ 829 setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
839 api_incr_top(L); 830 api_incr_top(L);
840 luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot); 831 luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
841 L->top.p -= 2; /* pop value and key */ 832 L->top.p -= 2; /* pop value and key */
842 } 833 }
843 lua_unlock(L); /* lock done by caller */ 834 lua_unlock(L); /* lock done by caller */
@@ -845,24 +836,25 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
845 836
846 837
847LUA_API void lua_setglobal (lua_State *L, const char *name) { 838LUA_API void lua_setglobal (lua_State *L, const char *name) {
848 const TValue *G; 839 TValue gt;
849 lua_lock(L); /* unlock done in 'auxsetstr' */ 840 lua_lock(L); /* unlock done in 'auxsetstr' */
850 G = getGtable(L); 841 getGlobalTable(L, &gt);
851 auxsetstr(L, G, name); 842 auxsetstr(L, &gt, name);
852} 843}
853 844
854 845
855LUA_API void lua_settable (lua_State *L, int idx) { 846LUA_API void lua_settable (lua_State *L, int idx) {
856 TValue *t; 847 TValue *t;
857 const TValue *slot; 848 int hres;
858 lua_lock(L); 849 lua_lock(L);
859 api_checknelems(L, 2); 850 api_checknelems(L, 2);
860 t = index2value(L, idx); 851 t = index2value(L, idx);
861 if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) { 852 luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
862 luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); 853 if (hres == HOK) {
854 luaV_finishfastset(L, t, s2v(L->top.p - 1));
863 } 855 }
864 else 856 else
865 luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot); 857 luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
866 L->top.p -= 2; /* pop index and value */ 858 L->top.p -= 2; /* pop index and value */
867 lua_unlock(L); 859 lua_unlock(L);
868} 860}
@@ -876,17 +868,18 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
876 868
877LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { 869LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
878 TValue *t; 870 TValue *t;
879 const TValue *slot; 871 int hres;
880 lua_lock(L); 872 lua_lock(L);
881 api_checknelems(L, 1); 873 api_checknelems(L, 1);
882 t = index2value(L, idx); 874 t = index2value(L, idx);
883 if (luaV_fastgeti(L, t, n, slot)) { 875 luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
884 luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); 876 if (hres == HOK) {
877 luaV_finishfastset(L, t, s2v(L->top.p - 1));
885 } 878 }
886 else { 879 else {
887 TValue aux; 880 TValue temp;
888 setivalue(&aux, n); 881 setivalue(&temp, n);
889 luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot); 882 luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
890 } 883 }
891 L->top.p--; /* pop value */ 884 L->top.p--; /* pop value */
892 lua_unlock(L); 885 lua_unlock(L);
@@ -1096,10 +1089,11 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1096 LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ 1089 LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
1097 if (f->nupvalues >= 1) { /* does it have an upvalue? */ 1090 if (f->nupvalues >= 1) { /* does it have an upvalue? */
1098 /* get global table from registry */ 1091 /* get global table from registry */
1099 const TValue *gt = getGtable(L); 1092 TValue gt;
1093 getGlobalTable(L, &gt);
1100 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ 1094 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1101 setobj(L, f->upvals[0]->v.p, gt); 1095 setobj(L, f->upvals[0]->v.p, &gt);
1102 luaC_barrier(L, f->upvals[0], gt); 1096 luaC_barrier(L, f->upvals[0], &gt);
1103 } 1097 }
1104 } 1098 }
1105 lua_unlock(L); 1099 lua_unlock(L);