aboutsummaryrefslogtreecommitdiff
path: root/src/lj_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_api.c')
-rw-r--r--src/lj_api.c138
1 files changed, 74 insertions, 64 deletions
diff --git a/src/lj_api.c b/src/lj_api.c
index 24ae6611..f1cfebbc 100644
--- a/src/lj_api.c
+++ b/src/lj_api.c
@@ -28,8 +28,8 @@
28 28
29/* -- Common helper functions --------------------------------------------- */ 29/* -- Common helper functions --------------------------------------------- */
30 30
31#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) 31#define lj_checkapi_slot(idx) \
32#define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L)) 32 lj_checkapi((idx) <= (L->top - L->base), "stack slot %d out of range", (idx))
33 33
34static TValue *index2adr(lua_State *L, int idx) 34static TValue *index2adr(lua_State *L, int idx)
35{ 35{
@@ -37,7 +37,8 @@ static TValue *index2adr(lua_State *L, int idx)
37 TValue *o = L->base + (idx - 1); 37 TValue *o = L->base + (idx - 1);
38 return o < L->top ? o : niltv(L); 38 return o < L->top ? o : niltv(L);
39 } else if (idx > LUA_REGISTRYINDEX) { 39 } else if (idx > LUA_REGISTRYINDEX) {
40 api_check(L, idx != 0 && -idx <= L->top - L->base); 40 lj_checkapi(idx != 0 && -idx <= L->top - L->base,
41 "bad stack slot %d", idx);
41 return L->top + idx; 42 return L->top + idx;
42 } else if (idx == LUA_GLOBALSINDEX) { 43 } else if (idx == LUA_GLOBALSINDEX) {
43 TValue *o = &G(L)->tmptv; 44 TValue *o = &G(L)->tmptv;
@@ -47,7 +48,8 @@ static TValue *index2adr(lua_State *L, int idx)
47 return registry(L); 48 return registry(L);
48 } else { 49 } else {
49 GCfunc *fn = curr_func(L); 50 GCfunc *fn = curr_func(L);
50 api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn)); 51 lj_checkapi(fn->c.gct == ~LJ_TFUNC && !isluafunc(fn),
52 "calling frame is not a C function");
51 if (idx == LUA_ENVIRONINDEX) { 53 if (idx == LUA_ENVIRONINDEX) {
52 TValue *o = &G(L)->tmptv; 54 TValue *o = &G(L)->tmptv;
53 settabV(L, o, tabref(fn->c.env)); 55 settabV(L, o, tabref(fn->c.env));
@@ -59,13 +61,27 @@ static TValue *index2adr(lua_State *L, int idx)
59 } 61 }
60} 62}
61 63
62static TValue *stkindex2adr(lua_State *L, int idx) 64static LJ_AINLINE TValue *index2adr_check(lua_State *L, int idx)
65{
66 TValue *o = index2adr(L, idx);
67 lj_checkapi(o != niltv(L), "invalid stack slot %d", idx);
68 return o;
69}
70
71static TValue *index2adr_stack(lua_State *L, int idx)
63{ 72{
64 if (idx > 0) { 73 if (idx > 0) {
65 TValue *o = L->base + (idx - 1); 74 TValue *o = L->base + (idx - 1);
75 if (o < L->top) {
76 return o;
77 } else {
78 lj_checkapi(0, "invalid stack slot %d", idx);
79 return niltv(L);
80 }
66 return o < L->top ? o : niltv(L); 81 return o < L->top ? o : niltv(L);
67 } else { 82 } else {
68 api_check(L, idx != 0 && -idx <= L->top - L->base); 83 lj_checkapi(idx != 0 && -idx <= L->top - L->base,
84 "invalid stack slot %d", idx);
69 return L->top + idx; 85 return L->top + idx;
70 } 86 }
71} 87}
@@ -99,17 +115,17 @@ LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
99 lj_err_callerv(L, LJ_ERR_STKOVM, msg); 115 lj_err_callerv(L, LJ_ERR_STKOVM, msg);
100} 116}
101 117
102LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) 118LUA_API void lua_xmove(lua_State *L, lua_State *to, int n)
103{ 119{
104 TValue *f, *t; 120 TValue *f, *t;
105 if (from == to) return; 121 if (L == to) return;
106 api_checknelems(from, n); 122 lj_checkapi_slot(n);
107 api_check(from, G(from) == G(to)); 123 lj_checkapi(G(L) == G(to), "move across global states");
108 lj_state_checkstack(to, (MSize)n); 124 lj_state_checkstack(to, (MSize)n);
109 f = from->top; 125 f = L->top;
110 t = to->top = to->top + n; 126 t = to->top = to->top + n;
111 while (--n >= 0) copyTV(to, --t, --f); 127 while (--n >= 0) copyTV(to, --t, --f);
112 from->top = f; 128 L->top = f;
113} 129}
114 130
115LUA_API const lua_Number *lua_version(lua_State *L) 131LUA_API const lua_Number *lua_version(lua_State *L)
@@ -129,7 +145,7 @@ LUA_API int lua_gettop(lua_State *L)
129LUA_API void lua_settop(lua_State *L, int idx) 145LUA_API void lua_settop(lua_State *L, int idx)
130{ 146{
131 if (idx >= 0) { 147 if (idx >= 0) {
132 api_check(L, idx <= tvref(L->maxstack) - L->base); 148 lj_checkapi(idx <= tvref(L->maxstack) - L->base, "bad stack slot %d", idx);
133 if (L->base + idx > L->top) { 149 if (L->base + idx > L->top) {
134 if (L->base + idx >= tvref(L->maxstack)) 150 if (L->base + idx >= tvref(L->maxstack))
135 lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base)); 151 lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
@@ -138,23 +154,21 @@ LUA_API void lua_settop(lua_State *L, int idx)
138 L->top = L->base + idx; 154 L->top = L->base + idx;
139 } 155 }
140 } else { 156 } else {
141 api_check(L, -(idx+1) <= (L->top - L->base)); 157 lj_checkapi(-(idx+1) <= (L->top - L->base), "bad stack slot %d", idx);
142 L->top += idx+1; /* Shrinks top (idx < 0). */ 158 L->top += idx+1; /* Shrinks top (idx < 0). */
143 } 159 }
144} 160}
145 161
146LUA_API void lua_remove(lua_State *L, int idx) 162LUA_API void lua_remove(lua_State *L, int idx)
147{ 163{
148 TValue *p = stkindex2adr(L, idx); 164 TValue *p = index2adr_stack(L, idx);
149 api_checkvalidindex(L, p);
150 while (++p < L->top) copyTV(L, p-1, p); 165 while (++p < L->top) copyTV(L, p-1, p);
151 L->top--; 166 L->top--;
152} 167}
153 168
154LUA_API void lua_insert(lua_State *L, int idx) 169LUA_API void lua_insert(lua_State *L, int idx)
155{ 170{
156 TValue *q, *p = stkindex2adr(L, idx); 171 TValue *q, *p = index2adr_stack(L, idx);
157 api_checkvalidindex(L, p);
158 for (q = L->top; q > p; q--) copyTV(L, q, q-1); 172 for (q = L->top; q > p; q--) copyTV(L, q, q-1);
159 copyTV(L, p, L->top); 173 copyTV(L, p, L->top);
160} 174}
@@ -162,19 +176,18 @@ LUA_API void lua_insert(lua_State *L, int idx)
162static void copy_slot(lua_State *L, TValue *f, int idx) 176static void copy_slot(lua_State *L, TValue *f, int idx)
163{ 177{
164 if (idx == LUA_GLOBALSINDEX) { 178 if (idx == LUA_GLOBALSINDEX) {
165 api_check(L, tvistab(f)); 179 lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
166 /* NOBARRIER: A thread (i.e. L) is never black. */ 180 /* NOBARRIER: A thread (i.e. L) is never black. */
167 setgcref(L->env, obj2gco(tabV(f))); 181 setgcref(L->env, obj2gco(tabV(f)));
168 } else if (idx == LUA_ENVIRONINDEX) { 182 } else if (idx == LUA_ENVIRONINDEX) {
169 GCfunc *fn = curr_func(L); 183 GCfunc *fn = curr_func(L);
170 if (fn->c.gct != ~LJ_TFUNC) 184 if (fn->c.gct != ~LJ_TFUNC)
171 lj_err_msg(L, LJ_ERR_NOENV); 185 lj_err_msg(L, LJ_ERR_NOENV);
172 api_check(L, tvistab(f)); 186 lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
173 setgcref(fn->c.env, obj2gco(tabV(f))); 187 setgcref(fn->c.env, obj2gco(tabV(f)));
174 lj_gc_barrier(L, fn, f); 188 lj_gc_barrier(L, fn, f);
175 } else { 189 } else {
176 TValue *o = index2adr(L, idx); 190 TValue *o = index2adr_check(L, idx);
177 api_checkvalidindex(L, o);
178 copyTV(L, o, f); 191 copyTV(L, o, f);
179 if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */ 192 if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */
180 lj_gc_barrier(L, curr_func(L), f); 193 lj_gc_barrier(L, curr_func(L), f);
@@ -183,7 +196,7 @@ static void copy_slot(lua_State *L, TValue *f, int idx)
183 196
184LUA_API void lua_replace(lua_State *L, int idx) 197LUA_API void lua_replace(lua_State *L, int idx)
185{ 198{
186 api_checknelems(L, 1); 199 lj_checkapi_slot(1);
187 copy_slot(L, L->top - 1, idx); 200 copy_slot(L, L->top - 1, idx);
188 L->top--; 201 L->top--;
189} 202}
@@ -219,7 +232,7 @@ LUA_API int lua_type(lua_State *L, int idx)
219#else 232#else
220 int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u); 233 int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
221#endif 234#endif
222 lua_assert(tt != LUA_TNIL || tvisnil(o)); 235 lj_assertL(tt != LUA_TNIL || tvisnil(o), "bad tag conversion");
223 return tt; 236 return tt;
224 } 237 }
225} 238}
@@ -677,14 +690,14 @@ LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
677{ 690{
678 GCfunc *fn; 691 GCfunc *fn;
679 lj_gc_check(L); 692 lj_gc_check(L);
680 api_checknelems(L, n); 693 lj_checkapi_slot(n);
681 fn = lj_func_newC(L, (MSize)n, getcurrenv(L)); 694 fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
682 fn->c.f = f; 695 fn->c.f = f;
683 L->top -= n; 696 L->top -= n;
684 while (n--) 697 while (n--)
685 copyTV(L, &fn->c.upvalue[n], L->top+n); 698 copyTV(L, &fn->c.upvalue[n], L->top+n);
686 setfuncV(L, L->top, fn); 699 setfuncV(L, L->top, fn);
687 lua_assert(iswhite(obj2gco(fn))); 700 lj_assertL(iswhite(obj2gco(fn)), "new GC object is not white");
688 incr_top(L); 701 incr_top(L);
689} 702}
690 703
@@ -754,7 +767,7 @@ LUA_API void *lua_newuserdata(lua_State *L, size_t size)
754 767
755LUA_API void lua_concat(lua_State *L, int n) 768LUA_API void lua_concat(lua_State *L, int n)
756{ 769{
757 api_checknelems(L, n); 770 lj_checkapi_slot(n);
758 if (n >= 2) { 771 if (n >= 2) {
759 n--; 772 n--;
760 do { 773 do {
@@ -780,9 +793,8 @@ LUA_API void lua_concat(lua_State *L, int n)
780 793
781LUA_API void lua_gettable(lua_State *L, int idx) 794LUA_API void lua_gettable(lua_State *L, int idx)
782{ 795{
783 cTValue *v, *t = index2adr(L, idx); 796 cTValue *t = index2adr_check(L, idx);
784 api_checkvalidindex(L, t); 797 cTValue *v = lj_meta_tget(L, t, L->top-1);
785 v = lj_meta_tget(L, t, L->top-1);
786 if (v == NULL) { 798 if (v == NULL) {
787 L->top += 2; 799 L->top += 2;
788 lj_vm_call(L, L->top-2, 1+1); 800 lj_vm_call(L, L->top-2, 1+1);
@@ -794,9 +806,8 @@ LUA_API void lua_gettable(lua_State *L, int idx)
794 806
795LUA_API void lua_getfield(lua_State *L, int idx, const char *k) 807LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
796{ 808{
797 cTValue *v, *t = index2adr(L, idx); 809 cTValue *v, *t = index2adr_check(L, idx);
798 TValue key; 810 TValue key;
799 api_checkvalidindex(L, t);
800 setstrV(L, &key, lj_str_newz(L, k)); 811 setstrV(L, &key, lj_str_newz(L, k));
801 v = lj_meta_tget(L, t, &key); 812 v = lj_meta_tget(L, t, &key);
802 if (v == NULL) { 813 if (v == NULL) {
@@ -812,14 +823,14 @@ LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
812LUA_API void lua_rawget(lua_State *L, int idx) 823LUA_API void lua_rawget(lua_State *L, int idx)
813{ 824{
814 cTValue *t = index2adr(L, idx); 825 cTValue *t = index2adr(L, idx);
815 api_check(L, tvistab(t)); 826 lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
816 copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1)); 827 copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
817} 828}
818 829
819LUA_API void lua_rawgeti(lua_State *L, int idx, int n) 830LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
820{ 831{
821 cTValue *v, *t = index2adr(L, idx); 832 cTValue *v, *t = index2adr(L, idx);
822 api_check(L, tvistab(t)); 833 lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
823 v = lj_tab_getint(tabV(t), n); 834 v = lj_tab_getint(tabV(t), n);
824 if (v) { 835 if (v) {
825 copyTV(L, L->top, v); 836 copyTV(L, L->top, v);
@@ -861,8 +872,7 @@ LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
861 872
862LUA_API void lua_getfenv(lua_State *L, int idx) 873LUA_API void lua_getfenv(lua_State *L, int idx)
863{ 874{
864 cTValue *o = index2adr(L, idx); 875 cTValue *o = index2adr_check(L, idx);
865 api_checkvalidindex(L, o);
866 if (tvisfunc(o)) { 876 if (tvisfunc(o)) {
867 settabV(L, L->top, tabref(funcV(o)->c.env)); 877 settabV(L, L->top, tabref(funcV(o)->c.env));
868 } else if (tvisudata(o)) { 878 } else if (tvisudata(o)) {
@@ -879,7 +889,7 @@ LUA_API int lua_next(lua_State *L, int idx)
879{ 889{
880 cTValue *t = index2adr(L, idx); 890 cTValue *t = index2adr(L, idx);
881 int more; 891 int more;
882 api_check(L, tvistab(t)); 892 lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
883 more = lj_tab_next(L, tabV(t), L->top-1); 893 more = lj_tab_next(L, tabV(t), L->top-1);
884 if (more) { 894 if (more) {
885 incr_top(L); /* Return new key and value slot. */ 895 incr_top(L); /* Return new key and value slot. */
@@ -905,7 +915,7 @@ LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
905{ 915{
906 GCfunc *fn = funcV(index2adr(L, idx)); 916 GCfunc *fn = funcV(index2adr(L, idx));
907 n--; 917 n--;
908 api_check(L, (uint32_t)n < fn->l.nupvalues); 918 lj_checkapi((uint32_t)n < fn->l.nupvalues, "bad upvalue %d", n);
909 return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : 919 return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
910 (void *)&fn->c.upvalue[n]; 920 (void *)&fn->c.upvalue[n];
911} 921}
@@ -915,8 +925,10 @@ LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
915 GCfunc *fn1 = funcV(index2adr(L, idx1)); 925 GCfunc *fn1 = funcV(index2adr(L, idx1));
916 GCfunc *fn2 = funcV(index2adr(L, idx2)); 926 GCfunc *fn2 = funcV(index2adr(L, idx2));
917 n1--; n2--; 927 n1--; n2--;
918 api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues); 928 lj_checkapi(isluafunc(fn1), "stack slot %d is not a Lua function", idx1);
919 api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues); 929 lj_checkapi(isluafunc(fn2), "stack slot %d is not a Lua function", idx2);
930 lj_checkapi((uint32_t)n1 < fn1->l.nupvalues, "bad upvalue %d", n1+1);
931 lj_checkapi((uint32_t)n2 < fn2->l.nupvalues, "bad upvalue %d", n2+1);
920 setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]); 932 setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
921 lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1])); 933 lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
922} 934}
@@ -945,9 +957,8 @@ LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
945LUA_API void lua_settable(lua_State *L, int idx) 957LUA_API void lua_settable(lua_State *L, int idx)
946{ 958{
947 TValue *o; 959 TValue *o;
948 cTValue *t = index2adr(L, idx); 960 cTValue *t = index2adr_check(L, idx);
949 api_checknelems(L, 2); 961 lj_checkapi_slot(2);
950 api_checkvalidindex(L, t);
951 o = lj_meta_tset(L, t, L->top-2); 962 o = lj_meta_tset(L, t, L->top-2);
952 if (o) { 963 if (o) {
953 /* NOBARRIER: lj_meta_tset ensures the table is not black. */ 964 /* NOBARRIER: lj_meta_tset ensures the table is not black. */
@@ -966,9 +977,8 @@ LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
966{ 977{
967 TValue *o; 978 TValue *o;
968 TValue key; 979 TValue key;
969 cTValue *t = index2adr(L, idx); 980 cTValue *t = index2adr_check(L, idx);
970 api_checknelems(L, 1); 981 lj_checkapi_slot(1);
971 api_checkvalidindex(L, t);
972 setstrV(L, &key, lj_str_newz(L, k)); 982 setstrV(L, &key, lj_str_newz(L, k));
973 o = lj_meta_tset(L, t, &key); 983 o = lj_meta_tset(L, t, &key);
974 if (o) { 984 if (o) {
@@ -987,7 +997,7 @@ LUA_API void lua_rawset(lua_State *L, int idx)
987{ 997{
988 GCtab *t = tabV(index2adr(L, idx)); 998 GCtab *t = tabV(index2adr(L, idx));
989 TValue *dst, *key; 999 TValue *dst, *key;
990 api_checknelems(L, 2); 1000 lj_checkapi_slot(2);
991 key = L->top-2; 1001 key = L->top-2;
992 dst = lj_tab_set(L, t, key); 1002 dst = lj_tab_set(L, t, key);
993 copyTV(L, dst, key+1); 1003 copyTV(L, dst, key+1);
@@ -999,7 +1009,7 @@ LUA_API void lua_rawseti(lua_State *L, int idx, int n)
999{ 1009{
1000 GCtab *t = tabV(index2adr(L, idx)); 1010 GCtab *t = tabV(index2adr(L, idx));
1001 TValue *dst, *src; 1011 TValue *dst, *src;
1002 api_checknelems(L, 1); 1012 lj_checkapi_slot(1);
1003 dst = lj_tab_setint(L, t, n); 1013 dst = lj_tab_setint(L, t, n);
1004 src = L->top-1; 1014 src = L->top-1;
1005 copyTV(L, dst, src); 1015 copyTV(L, dst, src);
@@ -1011,13 +1021,12 @@ LUA_API int lua_setmetatable(lua_State *L, int idx)
1011{ 1021{
1012 global_State *g; 1022 global_State *g;
1013 GCtab *mt; 1023 GCtab *mt;
1014 cTValue *o = index2adr(L, idx); 1024 cTValue *o = index2adr_check(L, idx);
1015 api_checknelems(L, 1); 1025 lj_checkapi_slot(1);
1016 api_checkvalidindex(L, o);
1017 if (tvisnil(L->top-1)) { 1026 if (tvisnil(L->top-1)) {
1018 mt = NULL; 1027 mt = NULL;
1019 } else { 1028 } else {
1020 api_check(L, tvistab(L->top-1)); 1029 lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
1021 mt = tabV(L->top-1); 1030 mt = tabV(L->top-1);
1022 } 1031 }
1023 g = G(L); 1032 g = G(L);
@@ -1054,11 +1063,10 @@ LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
1054 1063
1055LUA_API int lua_setfenv(lua_State *L, int idx) 1064LUA_API int lua_setfenv(lua_State *L, int idx)
1056{ 1065{
1057 cTValue *o = index2adr(L, idx); 1066 cTValue *o = index2adr_check(L, idx);
1058 GCtab *t; 1067 GCtab *t;
1059 api_checknelems(L, 1); 1068 lj_checkapi_slot(1);
1060 api_checkvalidindex(L, o); 1069 lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
1061 api_check(L, tvistab(L->top-1));
1062 t = tabV(L->top-1); 1070 t = tabV(L->top-1);
1063 if (tvisfunc(o)) { 1071 if (tvisfunc(o)) {
1064 setgcref(funcV(o)->c.env, obj2gco(t)); 1072 setgcref(funcV(o)->c.env, obj2gco(t));
@@ -1081,7 +1089,7 @@ LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
1081 TValue *val; 1089 TValue *val;
1082 GCobj *o; 1090 GCobj *o;
1083 const char *name; 1091 const char *name;
1084 api_checknelems(L, 1); 1092 lj_checkapi_slot(1);
1085 name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val, &o); 1093 name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val, &o);
1086 if (name) { 1094 if (name) {
1087 L->top--; 1095 L->top--;
@@ -1108,8 +1116,9 @@ static TValue *api_call_base(lua_State *L, int nargs)
1108 1116
1109LUA_API void lua_call(lua_State *L, int nargs, int nresults) 1117LUA_API void lua_call(lua_State *L, int nargs, int nresults)
1110{ 1118{
1111 api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR); 1119 lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
1112 api_checknelems(L, nargs+1); 1120 "thread called in wrong state %d", L->status);
1121 lj_checkapi_slot(nargs+1);
1113 lj_vm_call(L, api_call_base(L, nargs), nresults+1); 1122 lj_vm_call(L, api_call_base(L, nargs), nresults+1);
1114} 1123}
1115 1124
@@ -1119,13 +1128,13 @@ LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
1119 uint8_t oldh = hook_save(g); 1128 uint8_t oldh = hook_save(g);
1120 ptrdiff_t ef; 1129 ptrdiff_t ef;
1121 int status; 1130 int status;
1122 api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR); 1131 lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
1123 api_checknelems(L, nargs+1); 1132 "thread called in wrong state %d", L->status);
1133 lj_checkapi_slot(nargs+1);
1124 if (errfunc == 0) { 1134 if (errfunc == 0) {
1125 ef = 0; 1135 ef = 0;
1126 } else { 1136 } else {
1127 cTValue *o = stkindex2adr(L, errfunc); 1137 cTValue *o = index2adr_stack(L, errfunc);
1128 api_checkvalidindex(L, o);
1129 ef = savestack(L, o); 1138 ef = savestack(L, o);
1130 } 1139 }
1131 status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef); 1140 status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);
@@ -1151,7 +1160,8 @@ LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
1151 global_State *g = G(L); 1160 global_State *g = G(L);
1152 uint8_t oldh = hook_save(g); 1161 uint8_t oldh = hook_save(g);
1153 int status; 1162 int status;
1154 api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR); 1163 lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
1164 "thread called in wrong state %d", L->status);
1155 status = lj_vm_cpcall(L, func, ud, cpcall); 1165 status = lj_vm_cpcall(L, func, ud, cpcall);
1156 if (status) hook_restore(g, oldh); 1166 if (status) hook_restore(g, oldh);
1157 return status; 1167 return status;