aboutsummaryrefslogtreecommitdiff
path: root/src/lua/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/lapi.c')
-rw-r--r--src/lua/lapi.c43
1 files changed, 28 insertions, 15 deletions
diff --git a/src/lua/lapi.c b/src/lua/lapi.c
index 3e24781..9048245 100644
--- a/src/lua/lapi.c
+++ b/src/lua/lapi.c
@@ -97,8 +97,9 @@ static StkId index2stack (lua_State *L, int idx) {
97 97
98LUA_API int lua_checkstack (lua_State *L, int n) { 98LUA_API int lua_checkstack (lua_State *L, int n) {
99 int res; 99 int res;
100 CallInfo *ci = L->ci; 100 CallInfo *ci;
101 lua_lock(L); 101 lua_lock(L);
102 ci = L->ci;
102 api_check(L, n >= 0, "negative 'n'"); 103 api_check(L, n >= 0, "negative 'n'");
103 if (L->stack_last - L->top > n) /* stack large enough? */ 104 if (L->stack_last - L->top > n) /* stack large enough? */
104 res = 1; /* yes; check is OK */ 105 res = 1; /* yes; check is OK */
@@ -170,10 +171,12 @@ LUA_API int lua_gettop (lua_State *L) {
170 171
171 172
172LUA_API void lua_settop (lua_State *L, int idx) { 173LUA_API void lua_settop (lua_State *L, int idx) {
173 CallInfo *ci = L->ci; 174 CallInfo *ci;
174 StkId func = ci->func; 175 StkId func;
175 ptrdiff_t diff; /* difference for new top */ 176 ptrdiff_t diff; /* difference for new top */
176 lua_lock(L); 177 lua_lock(L);
178 ci = L->ci;
179 func = ci->func;
177 if (idx >= 0) { 180 if (idx >= 0) {
178 api_check(L, idx <= ci->top - (func + 1), "new top too large"); 181 api_check(L, idx <= ci->top - (func + 1), "new top too large");
179 diff = ((func + 1) + idx) - L->top; 182 diff = ((func + 1) + idx) - L->top;
@@ -376,20 +379,22 @@ LUA_API int lua_toboolean (lua_State *L, int idx) {
376 379
377 380
378LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { 381LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
379 TValue *o = index2value(L, idx); 382 TValue *o;
383 lua_lock(L);
384 o = index2value(L, idx);
380 if (!ttisstring(o)) { 385 if (!ttisstring(o)) {
381 if (!cvt2str(o)) { /* not convertible? */ 386 if (!cvt2str(o)) { /* not convertible? */
382 if (len != NULL) *len = 0; 387 if (len != NULL) *len = 0;
388 lua_unlock(L);
383 return NULL; 389 return NULL;
384 } 390 }
385 lua_lock(L); /* 'luaO_tostring' may create a new string */
386 luaO_tostring(L, o); 391 luaO_tostring(L, o);
387 luaC_checkGC(L); 392 luaC_checkGC(L);
388 o = index2value(L, idx); /* previous call may reallocate the stack */ 393 o = index2value(L, idx); /* previous call may reallocate the stack */
389 lua_unlock(L);
390 } 394 }
391 if (len != NULL) 395 if (len != NULL)
392 *len = vslen(o); 396 *len = vslen(o);
397 lua_unlock(L);
393 return svalue(o); 398 return svalue(o);
394} 399}
395 400
@@ -563,6 +568,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
563 while (n--) { 568 while (n--) {
564 setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); 569 setobj2n(L, &cl->upvalue[n], s2v(L->top + n));
565 /* does not need barrier because closure is white */ 570 /* does not need barrier because closure is white */
571 lua_assert(iswhite(cl));
566 } 572 }
567 setclCvalue(L, s2v(L->top), cl); 573 setclCvalue(L, s2v(L->top), cl);
568 api_incr_top(L); 574 api_incr_top(L);
@@ -624,8 +630,9 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
624 630
625 631
626LUA_API int lua_getglobal (lua_State *L, const char *name) { 632LUA_API int lua_getglobal (lua_State *L, const char *name) {
627 Table *reg = hvalue(&G(L)->l_registry); 633 Table *reg;
628 lua_lock(L); 634 lua_lock(L);
635 reg = hvalue(&G(L)->l_registry);
629 return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); 636 return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
630} 637}
631 638
@@ -804,8 +811,9 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
804 811
805 812
806LUA_API void lua_setglobal (lua_State *L, const char *name) { 813LUA_API void lua_setglobal (lua_State *L, const char *name) {
807 Table *reg = hvalue(&G(L)->l_registry); 814 Table *reg;
808 lua_lock(L); /* unlock done in 'auxsetstr' */ 815 lua_lock(L); /* unlock done in 'auxsetstr' */
816 reg = hvalue(&G(L)->l_registry);
809 auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); 817 auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
810} 818}
811 819
@@ -1093,8 +1101,9 @@ LUA_API int lua_status (lua_State *L) {
1093LUA_API int lua_gc (lua_State *L, int what, ...) { 1101LUA_API int lua_gc (lua_State *L, int what, ...) {
1094 va_list argp; 1102 va_list argp;
1095 int res = 0; 1103 int res = 0;
1096 global_State *g = G(L); 1104 global_State *g;
1097 lua_lock(L); 1105 lua_lock(L);
1106 g = G(L);
1098 va_start(argp, what); 1107 va_start(argp, what);
1099 switch (what) { 1108 switch (what) {
1100 case LUA_GCSTOP: { 1109 case LUA_GCSTOP: {
@@ -1194,9 +1203,15 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
1194 1203
1195 1204
1196LUA_API int lua_error (lua_State *L) { 1205LUA_API int lua_error (lua_State *L) {
1206 TValue *errobj;
1197 lua_lock(L); 1207 lua_lock(L);
1208 errobj = s2v(L->top - 1);
1198 api_checknelems(L, 1); 1209 api_checknelems(L, 1);
1199 luaG_errormsg(L); 1210 /* error object is the memory error message? */
1211 if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
1212 luaM_error(L); /* raise a memory error */
1213 else
1214 luaG_errormsg(L); /* raise a regular error */
1200 /* code unreachable; will unlock when control actually leaves the kernel */ 1215 /* code unreachable; will unlock when control actually leaves the kernel */
1201 return 0; /* to avoid warnings */ 1216 return 0; /* to avoid warnings */
1202} 1217}
@@ -1238,14 +1253,12 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
1238LUA_API void lua_concat (lua_State *L, int n) { 1253LUA_API void lua_concat (lua_State *L, int n) {
1239 lua_lock(L); 1254 lua_lock(L);
1240 api_checknelems(L, n); 1255 api_checknelems(L, n);
1241 if (n >= 2) { 1256 if (n > 0)
1242 luaV_concat(L, n); 1257 luaV_concat(L, n);
1243 } 1258 else { /* nothing to concatenate */
1244 else if (n == 0) { /* push empty string */ 1259 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */
1245 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1246 api_incr_top(L); 1260 api_incr_top(L);
1247 } 1261 }
1248 /* else n == 1; nothing to do */
1249 luaC_checkGC(L); 1262 luaC_checkGC(L);
1250 lua_unlock(L); 1263 lua_unlock(L);
1251} 1264}