Lua 5.1.4: lapi.c
L0001
L0006
L0007
L0008 #include <assert.h>
L0009 #include <math.h>
L0010 #include <stdarg.h>
L0011 #include <string.h>
L0012
L0013 #define lapi_c
L0014 #define LUA_CORE
L0015
L0016 #include "lua.h"
L0017
L0018 #include "lapi.h"
L0019 #include "ldebug.h"
L0020 #include "ldo.h"
L0021 #include "lfunc.h"
L0022 #include "lgc.h"
L0023 #include "lmem.h"
L0024 #include "lobject.h"
L0025 #include "lstate.h"
L0026 #include "lstring.h"
L0027 #include "ltable.h"
L0028 #include "ltm.h"
L0029 #include "lundump.h"
L0030 #include "lvm.h"
L0031
L0032
L0033
L0034 const char lua_ident[] =
L0035 "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
L0036 "$Authors: " LUA_AUTHORS " $\n"
L0037 "$URL: www.lua.org $\n";
L0038
L0039
L0040
L0041 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
L0042
L0043 #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
L0044
L0045 #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
L0046
L0047
L0048
L0049 static TValue *index2adr (lua_State *L, int idx) {
L0050 if (idx > 0) {
L0051 TValue *o = L->base + (idx - 1);
L0052 api_check(L, idx <= L->ci->top - L->base);
L0053 if (o >= L->top) return cast(TValue *, luaO_nilobject);
L0054 else return o;
L0055 }
L0056 else if (idx > LUA_REGISTRYINDEX) {
L0057 api_check(L, idx != 0 && -idx <= L->top - L->base);
L0058 return L->top + idx;
L0059 }
L0060 else switch (idx) {
L0061 case LUA_REGISTRYINDEX: return registry(L);
L0062 case LUA_ENVIRONINDEX: {
L0063 Closure *func = curr_func(L);
L0064 sethvalue(L, &L->env, func->c.env);
L0065 return &L->env;
L0066 }
L0067 case LUA_GLOBALSINDEX: return gt(L);
L0068 default: {
L0069 Closure *func = curr_func(L);
L0070 idx = LUA_GLOBALSINDEX - idx;
L0071 return (idx <= func->c.nupvalues)
L0072 ? &func->c.upvalue[idx-1]
L0073 : cast(TValue *, luaO_nilobject);
L0074 }
L0075 }
L0076 }
L0077
L0078
L0079 static Table *getcurrenv (lua_State *L) {
L0080 if (L->ci == L->base_ci)
L0081 return hvalue(gt(L));
L0082 else {
L0083 Closure *func = curr_func(L);
L0084 return func->c.env;
L0085 }
L0086 }
L0087
L0088
L0089 void luaA_pushobject (lua_State *L, const TValue *o) {
L0090 setobj2s(L, L->top, o);
L0091 api_incr_top(L);
L0092 }
L0093
L0094
L0095 LUA_API int lua_checkstack (lua_State *L, int size) {
L0096 int res = 1;
L0097 lua_lock(L);
L0098 if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
L0099 res = 0;
L0100 else if (size > 0) {
L0101 luaD_checkstack(L, size);
L0102 if (L->ci->top < L->top + size)
L0103 L->ci->top = L->top + size;
L0104 }
L0105 lua_unlock(L);
L0106 return res;
L0107 }
L0108
L0109
L0110 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
L0111 int i;
L0112 if (from == to) return;
L0113 lua_lock(to);
L0114 api_checknelems(from, n);
L0115 api_check(from, G(from) == G(to));
L0116 api_check(from, to->ci->top - to->top >= n);
L0117 from->top -= n;
L0118 for (i = 0; i < n; i++) {
L0119 setobj2s(to, to->top++, from->top + i);
L0120 }
L0121 lua_unlock(to);
L0122 }
L0123
L0124
L0125 LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
L0126 to->nCcalls = from->nCcalls;
L0127 }
L0128
L0129
L0130 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
L0131 lua_CFunction old;
L0132 lua_lock(L);
L0133 old = G(L)->panic;
L0134 G(L)->panic = panicf;
L0135 lua_unlock(L);
L0136 return old;
L0137 }
L0138
L0139
L0140 LUA_API lua_State *lua_newthread (lua_State *L) {
L0141 lua_State *L1;
L0142 lua_lock(L);
L0143 luaC_checkGC(L);
L0144 L1 = luaE_newthread(L);
L0145 setthvalue(L, L->top, L1);
L0146 api_incr_top(L);
L0147 lua_unlock(L);
L0148 luai_userstatethread(L, L1);
L0149 return L1;
L0150 }
L0151
L0152
L0153
L0154
L0157
L0158
L0159 LUA_API int lua_gettop (lua_State *L) {
L0160 return cast_int(L->top - L->base);
L0161 }
L0162
L0163
L0164 LUA_API void lua_settop (lua_State *L, int idx) {
L0165 lua_lock(L);
L0166 if (idx >= 0) {
L0167 api_check(L, idx <= L->stack_last - L->base);
L0168 while (L->top < L->base + idx)
L0169 setnilvalue(L->top++);
L0170 L->top = L->base + idx;
L0171 }
L0172 else {
L0173 api_check(L, -(idx+1) <= (L->top - L->base));
L0174 L->top += idx+1;
L0175 }
L0176 lua_unlock(L);
L0177 }
L0178
L0179
L0180 LUA_API void lua_remove (lua_State *L, int idx) {
L0181 StkId p;
L0182 lua_lock(L);
L0183 p = index2adr(L, idx);
L0184 api_checkvalidindex(L, p);
L0185 while (++p < L->top) setobjs2s(L, p-1, p);
L0186 L->top--;
L0187 lua_unlock(L);
L0188 }
L0189
L0190
L0191 LUA_API void lua_insert (lua_State *L, int idx) {
L0192 StkId p;
L0193 StkId q;
L0194 lua_lock(L);
L0195 p = index2adr(L, idx);
L0196 api_checkvalidindex(L, p);
L0197 for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
L0198 setobjs2s(L, p, L->top);
L0199 lua_unlock(L);
L0200 }
L0201
L0202
L0203 LUA_API void lua_replace (lua_State *L, int idx) {
L0204 StkId o;
L0205 lua_lock(L);
L0206
L0207 if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
L0208 luaG_runerror(L, "no calling environment");
L0209 api_checknelems(L, 1);
L0210 o = index2adr(L, idx);
L0211 api_checkvalidindex(L, o);
L0212 if (idx == LUA_ENVIRONINDEX) {
L0213 Closure *func = curr_func(L);
L0214 api_check(L, ttistable(L->top - 1));
L0215 func->c.env = hvalue(L->top - 1);
L0216 luaC_barrier(L, func, L->top - 1);
L0217 }
L0218 else {
L0219 setobj(L, o, L->top - 1);
L0220 if (idx < LUA_GLOBALSINDEX)
L0221 luaC_barrier(L, curr_func(L), L->top - 1);
L0222 }
L0223 L->top--;
L0224 lua_unlock(L);
L0225 }
L0226
L0227
L0228 LUA_API void lua_pushvalue (lua_State *L, int idx) {
L0229 lua_lock(L);
L0230 setobj2s(L, L->top, index2adr(L, idx));
L0231 api_incr_top(L);
L0232 lua_unlock(L);
L0233 }
L0234
L0235
L0236
L0237
L0240
L0241
L0242 LUA_API int lua_type (lua_State *L, int idx) {
L0243 StkId o = index2adr(L, idx);
L0244 return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
L0245 }
L0246
L0247
L0248 LUA_API const char *lua_typename (lua_State *L, int t) {
L0249 UNUSED(L);
L0250 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
L0251 }
L0252
L0253
L0254 LUA_API int lua_iscfunction (lua_State *L, int idx) {
L0255 StkId o = index2adr(L, idx);
L0256 return iscfunction(o);
L0257 }
L0258
L0259
L0260 LUA_API int lua_isnumber (lua_State *L, int idx) {
L0261 TValue n;
L0262 const TValue *o = index2adr(L, idx);
L0263 return tonumber(o, &n);
L0264 }
L0265
L0266
L0267 LUA_API int lua_isstring (lua_State *L, int idx) {
L0268 int t = lua_type(L, idx);
L0269 return (t == LUA_TSTRING || t == LUA_TNUMBER);
L0270 }
L0271
L0272
L0273 LUA_API int lua_isuserdata (lua_State *L, int idx) {
L0274 const TValue *o = index2adr(L, idx);
L0275 return (ttisuserdata(o) || ttislightuserdata(o));
L0276 }
L0277
L0278
L0279 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
L0280 StkId o1 = index2adr(L, index1);
L0281 StkId o2 = index2adr(L, index2);
L0282 return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
L0283 : luaO_rawequalObj(o1, o2);
L0284 }
L0285
L0286
L0287 LUA_API int lua_equal (lua_State *L, int index1, int index2) {
L0288 StkId o1, o2;
L0289 int i;
L0290 lua_lock(L);
L0291 o1 = index2adr(L, index1);
L0292 o2 = index2adr(L, index2);
L0293 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
L0294 lua_unlock(L);
L0295 return i;
L0296 }
L0297
L0298
L0299 LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
L0300 StkId o1, o2;
L0301 int i;
L0302 lua_lock(L);
L0303 o1 = index2adr(L, index1);
L0304 o2 = index2adr(L, index2);
L0305 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
L0306 : luaV_lessthan(L, o1, o2);
L0307 lua_unlock(L);
L0308 return i;
L0309 }
L0310
L0311
L0312
L0313 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
L0314 TValue n;
L0315 const TValue *o = index2adr(L, idx);
L0316 if (tonumber(o, &n))
L0317 return nvalue(o);
L0318 else
L0319 return 0;
L0320 }
L0321
L0322
L0323 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
L0324 TValue n;
L0325 const TValue *o = index2adr(L, idx);
L0326 if (tonumber(o, &n)) {
L0327 lua_Integer res;
L0328 lua_Number num = nvalue(o);
L0329 lua_number2integer(res, num);
L0330 return res;
L0331 }
L0332 else
L0333 return 0;
L0334 }
L0335
L0336
L0337 LUA_API int lua_toboolean (lua_State *L, int idx) {
L0338 const TValue *o = index2adr(L, idx);
L0339 return !l_isfalse(o);
L0340 }
L0341
L0342
L0343 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
L0344 StkId o = index2adr(L, idx);
L0345 if (!ttisstring(o)) {
L0346 lua_lock(L);
L0347 if (!luaV_tostring(L, o)) {
L0348 if (len != NULL) *len = 0;
L0349 lua_unlock(L);
L0350 return NULL;
L0351 }
L0352 luaC_checkGC(L);
L0353 o = index2adr(L, idx);
L0354 lua_unlock(L);
L0355 }
L0356 if (len != NULL) *len = tsvalue(o)->len;
L0357 return svalue(o);
L0358 }
L0359
L0360
L0361 LUA_API size_t lua_objlen (lua_State *L, int idx) {
L0362 StkId o = index2adr(L, idx);
L0363 switch (ttype(o)) {
L0364 case LUA_TSTRING: return tsvalue(o)->len;
L0365 case LUA_TUSERDATA: return uvalue(o)->len;
L0366 case LUA_TTABLE: return luaH_getn(hvalue(o));
L0367 case LUA_TNUMBER: {
L0368 size_t l;
L0369 lua_lock(L);
L0370 l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
L0371 lua_unlock(L);
L0372 return l;
L0373 }
L0374 default: return 0;
L0375 }
L0376 }
L0377
L0378
L0379 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
L0380 StkId o = index2adr(L, idx);
L0381 return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
L0382 }
L0383
L0384
L0385 LUA_API void *lua_touserdata (lua_State *L, int idx) {
L0386 StkId o = index2adr(L, idx);
L0387 switch (ttype(o)) {
L0388 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
L0389 case LUA_TLIGHTUSERDATA: return pvalue(o);
L0390 default: return NULL;
L0391 }
L0392 }
L0393
L0394
L0395 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
L0396 StkId o = index2adr(L, idx);
L0397 return (!ttisthread(o)) ? NULL : thvalue(o);
L0398 }
L0399
L0400
L0401 LUA_API const void *lua_topointer (lua_State *L, int idx) {
L0402 StkId o = index2adr(L, idx);
L0403 switch (ttype(o)) {
L0404 case LUA_TTABLE: return hvalue(o);
L0405 case LUA_TFUNCTION: return clvalue(o);
L0406 case LUA_TTHREAD: return thvalue(o);
L0407 case LUA_TUSERDATA:
L0408 case LUA_TLIGHTUSERDATA:
L0409 return lua_touserdata(L, idx);
L0410 default: return NULL;
L0411 }
L0412 }
L0413
L0414
L0415
L0416
L0419
L0420
L0421 LUA_API void lua_pushnil (lua_State *L) {
L0422 lua_lock(L);
L0423 setnilvalue(L->top);
L0424 api_incr_top(L);
L0425 lua_unlock(L);
L0426 }
L0427
L0428
L0429 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
L0430 lua_lock(L);
L0431 setnvalue(L->top, n);
L0432 api_incr_top(L);
L0433 lua_unlock(L);
L0434 }
L0435
L0436
L0437 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
L0438 lua_lock(L);
L0439 setnvalue(L->top, cast_num(n));
L0440 api_incr_top(L);
L0441 lua_unlock(L);
L0442 }
L0443
L0444
L0445 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
L0446 lua_lock(L);
L0447 luaC_checkGC(L);
L0448 setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
L0449 api_incr_top(L);
L0450 lua_unlock(L);
L0451 }
L0452
L0453
L0454 LUA_API void lua_pushstring (lua_State *L, const char *s) {
L0455 if (s == NULL)
L0456 lua_pushnil(L);
L0457 else
L0458 lua_pushlstring(L, s, strlen(s));
L0459 }
L0460
L0461
L0462 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
L0463 va_list argp) {
L0464 const char *ret;
L0465 lua_lock(L);
L0466 luaC_checkGC(L);
L0467 ret = luaO_pushvfstring(L, fmt, argp);
L0468 lua_unlock(L);
L0469 return ret;
L0470 }
L0471
L0472
L0473 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
L0474 const char *ret;
L0475 va_list argp;
L0476 lua_lock(L);
L0477 luaC_checkGC(L);
L0478 va_start(argp, fmt);
L0479 ret = luaO_pushvfstring(L, fmt, argp);
L0480 va_end(argp);
L0481 lua_unlock(L);
L0482 return ret;
L0483 }
L0484
L0485
L0486 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
L0487 Closure *cl;
L0488 lua_lock(L);
L0489 luaC_checkGC(L);
L0490 api_checknelems(L, n);
L0491 cl = luaF_newCclosure(L, n, getcurrenv(L));
L0492 cl->c.f = fn;
L0493 L->top -= n;
L0494 while (n--)
L0495 setobj2n(L, &cl->c.upvalue[n], L->top+n);
L0496 setclvalue(L, L->top, cl);
L0497 lua_assert(iswhite(obj2gco(cl)));
L0498 api_incr_top(L);
L0499 lua_unlock(L);
L0500 }
L0501
L0502
L0503 LUA_API void lua_pushboolean (lua_State *L, int b) {
L0504 lua_lock(L);
L0505 setbvalue(L->top, (b != 0));
L0506 api_incr_top(L);
L0507 lua_unlock(L);
L0508 }
L0509
L0510
L0511 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
L0512 lua_lock(L);
L0513 setpvalue(L->top, p);
L0514 api_incr_top(L);
L0515 lua_unlock(L);
L0516 }
L0517
L0518
L0519 LUA_API int lua_pushthread (lua_State *L) {
L0520 lua_lock(L);
L0521 setthvalue(L, L->top, L);
L0522 api_incr_top(L);
L0523 lua_unlock(L);
L0524 return (G(L)->mainthread == L);
L0525 }
L0526
L0527
L0528
L0529
L0532
L0533
L0534 LUA_API void lua_gettable (lua_State *L, int idx) {
L0535 StkId t;
L0536 lua_lock(L);
L0537 t = index2adr(L, idx);
L0538 api_checkvalidindex(L, t);
L0539 luaV_gettable(L, t, L->top - 1, L->top - 1);
L0540 lua_unlock(L);
L0541 }
L0542
L0543
L0544 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
L0545 StkId t;
L0546 TValue key;
L0547 lua_lock(L);
L0548 t = index2adr(L, idx);
L0549 api_checkvalidindex(L, t);
L0550 setsvalue(L, &key, luaS_new(L, k));
L0551 luaV_gettable(L, t, &key, L->top);
L0552 api_incr_top(L);
L0553 lua_unlock(L);
L0554 }
L0555
L0556
L0557 LUA_API void lua_rawget (lua_State *L, int idx) {
L0558 StkId t;
L0559 lua_lock(L);
L0560 t = index2adr(L, idx);
L0561 api_check(L, ttistable(t));
L0562 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
L0563 lua_unlock(L);
L0564 }
L0565
L0566
L0567 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
L0568 StkId o;
L0569 lua_lock(L);
L0570 o = index2adr(L, idx);
L0571 api_check(L, ttistable(o));
L0572 setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
L0573 api_incr_top(L);
L0574 lua_unlock(L);
L0575 }
L0576
L0577
L0578 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
L0579 lua_lock(L);
L0580 luaC_checkGC(L);
L0581 sethvalue(L, L->top, luaH_new(L, narray, nrec));
L0582 api_incr_top(L);
L0583 lua_unlock(L);
L0584 }
L0585
L0586
L0587 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
L0588 const TValue *obj;
L0589 Table *mt = NULL;
L0590 int res;
L0591 lua_lock(L);
L0592 obj = index2adr(L, objindex);
L0593 switch (ttype(obj)) {
L0594 case LUA_TTABLE:
L0595 mt = hvalue(obj)->metatable;
L0596 break;
L0597 case LUA_TUSERDATA:
L0598 mt = uvalue(obj)->metatable;
L0599 break;
L0600 default:
L0601 mt = G(L)->mt[ttype(obj)];
L0602 break;
L0603 }
L0604 if (mt == NULL)
L0605 res = 0;
L0606 else {
L0607 sethvalue(L, L->top, mt);
L0608 api_incr_top(L);
L0609 res = 1;
L0610 }
L0611 lua_unlock(L);
L0612 return res;
L0613 }
L0614
L0615
L0616 LUA_API void lua_getfenv (lua_State *L, int idx) {
L0617 StkId o;
L0618 lua_lock(L);
L0619 o = index2adr(L, idx);
L0620 api_checkvalidindex(L, o);
L0621 switch (ttype(o)) {
L0622 case LUA_TFUNCTION:
L0623 sethvalue(L, L->top, clvalue(o)->c.env);
L0624 break;
L0625 case LUA_TUSERDATA:
L0626 sethvalue(L, L->top, uvalue(o)->env);
L0627 break;
L0628 case LUA_TTHREAD:
L0629 setobj2s(L, L->top, gt(thvalue(o)));
L0630 break;
L0631 default:
L0632 setnilvalue(L->top);
L0633 break;
L0634 }
L0635 api_incr_top(L);
L0636 lua_unlock(L);
L0637 }
L0638
L0639
L0640
L0643
L0644
L0645 LUA_API void lua_settable (lua_State *L, int idx) {
L0646 StkId t;
L0647 lua_lock(L);
L0648 api_checknelems(L, 2);
L0649 t = index2adr(L, idx);
L0650 api_checkvalidindex(L, t);
L0651 luaV_settable(L, t, L->top - 2, L->top - 1);
L0652 L->top -= 2;
L0653 lua_unlock(L);
L0654 }
L0655
L0656
L0657 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
L0658 StkId t;
L0659 TValue key;
L0660 lua_lock(L);
L0661 api_checknelems(L, 1);
L0662 t = index2adr(L, idx);
L0663 api_checkvalidindex(L, t);
L0664 setsvalue(L, &key, luaS_new(L, k));
L0665 luaV_settable(L, t, &key, L->top - 1);
L0666 L->top--;
L0667 lua_unlock(L);
L0668 }
L0669
L0670
L0671 LUA_API void lua_rawset (lua_State *L, int idx) {
L0672 StkId t;
L0673 lua_lock(L);
L0674 api_checknelems(L, 2);
L0675 t = index2adr(L, idx);
L0676 api_check(L, ttistable(t));
L0677 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
L0678 luaC_barriert(L, hvalue(t), L->top-1);
L0679 L->top -= 2;
L0680 lua_unlock(L);
L0681 }
L0682
L0683
L0684 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
L0685 StkId o;
L0686 lua_lock(L);
L0687 api_checknelems(L, 1);
L0688 o = index2adr(L, idx);
L0689 api_check(L, ttistable(o));
L0690 setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
L0691 luaC_barriert(L, hvalue(o), L->top-1);
L0692 L->top--;
L0693 lua_unlock(L);
L0694 }
L0695
L0696
L0697 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
L0698 TValue *obj;
L0699 Table *mt;
L0700 lua_lock(L);
L0701 api_checknelems(L, 1);
L0702 obj = index2adr(L, objindex);
L0703 api_checkvalidindex(L, obj);
L0704 if (ttisnil(L->top - 1))
L0705 mt = NULL;
L0706 else {
L0707 api_check(L, ttistable(L->top - 1));
L0708 mt = hvalue(L->top - 1);
L0709 }
L0710 switch (ttype(obj)) {
L0711 case LUA_TTABLE: {
L0712 hvalue(obj)->metatable = mt;
L0713 if (mt)
L0714 luaC_objbarriert(L, hvalue(obj), mt);
L0715 break;
L0716 }
L0717 case LUA_TUSERDATA: {
L0718 uvalue(obj)->metatable = mt;
L0719 if (mt)
L0720 luaC_objbarrier(L, rawuvalue(obj), mt);
L0721 break;
L0722 }
L0723 default: {
L0724 G(L)->mt[ttype(obj)] = mt;
L0725 break;
L0726 }
L0727 }
L0728 L->top--;
L0729 lua_unlock(L);
L0730 return 1;
L0731 }
L0732
L0733
L0734 LUA_API int lua_setfenv (lua_State *L, int idx) {
L0735 StkId o;
L0736 int res = 1;
L0737 lua_lock(L);
L0738 api_checknelems(L, 1);
L0739 o = index2adr(L, idx);
L0740 api_checkvalidindex(L, o);
L0741 api_check(L, ttistable(L->top - 1));
L0742 switch (ttype(o)) {
L0743 case LUA_TFUNCTION:
L0744 clvalue(o)->c.env = hvalue(L->top - 1);
L0745 break;
L0746 case LUA_TUSERDATA:
L0747 uvalue(o)->env = hvalue(L->top - 1);
L0748 break;
L0749 case LUA_TTHREAD:
L0750 sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
L0751 break;
L0752 default:
L0753 res = 0;
L0754 break;
L0755 }
L0756 if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
L0757 L->top--;
L0758 lua_unlock(L);
L0759 return res;
L0760 }
L0761
L0762
L0763
L0766
L0767
L0768 #define adjustresults(L,nres) \
L0769 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
L0770
L0771
L0772 #define checkresults(L,na,nr) \
L0773 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
L0774
L0775
L0776 LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
L0777 StkId func;
L0778 lua_lock(L);
L0779 api_checknelems(L, nargs+1);
L0780 checkresults(L, nargs, nresults);
L0781 func = L->top - (nargs+1);
L0782 luaD_call(L, func, nresults);
L0783 adjustresults(L, nresults);
L0784 lua_unlock(L);
L0785 }
L0786
L0787
L0788
L0789
L0792 struct CallS {
L0793 StkId func;
L0794 int nresults;
L0795 };
L0796
L0797
L0798 static void f_call (lua_State *L, void *ud) {
L0799 struct CallS *c = cast(struct CallS *, ud);
L0800 luaD_call(L, c->func, c->nresults);
L0801 }
L0802
L0803
L0804
L0805 LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
L0806 struct CallS c;
L0807 int status;
L0808 ptrdiff_t func;
L0809 lua_lock(L);
L0810 api_checknelems(L, nargs+1);
L0811 checkresults(L, nargs, nresults);
L0812 if (errfunc == 0)
L0813 func = 0;
L0814 else {
L0815 StkId o = index2adr(L, errfunc);
L0816 api_checkvalidindex(L, o);
L0817 func = savestack(L, o);
L0818 }
L0819 c.func = L->top - (nargs+1);
L0820 c.nresults = nresults;
L0821 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
L0822 adjustresults(L, nresults);
L0823 lua_unlock(L);
L0824 return status;
L0825 }
L0826
L0827
L0828
L0831 struct CCallS {
L0832 lua_CFunction func;
L0833 void *ud;
L0834 };
L0835
L0836
L0837 static void f_Ccall (lua_State *L, void *ud) {
L0838 struct CCallS *c = cast(struct CCallS *, ud);
L0839 Closure *cl;
L0840 cl = luaF_newCclosure(L, 0, getcurrenv(L));
L0841 cl->c.f = c->func;
L0842 setclvalue(L, L->top, cl);
L0843 api_incr_top(L);
L0844 setpvalue(L->top, c->ud);
L0845 api_incr_top(L);
L0846 luaD_call(L, L->top - 2, 0);
L0847 }
L0848
L0849
L0850 LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
L0851 struct CCallS c;
L0852 int status;
L0853 lua_lock(L);
L0854 c.func = func;
L0855 c.ud = ud;
L0856 status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
L0857 lua_unlock(L);
L0858 return status;
L0859 }
L0860
L0861
L0862 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
L0863 const char *chunkname) {
L0864 ZIO z;
L0865 int status;
L0866 lua_lock(L);
L0867 if (!chunkname) chunkname = "?";
L0868 luaZ_init(L, &z, reader, data);
L0869 status = luaD_protectedparser(L, &z, chunkname);
L0870 lua_unlock(L);
L0871 return status;
L0872 }
L0873
L0874
L0875 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
L0876 int status;
L0877 TValue *o;
L0878 lua_lock(L);
L0879 api_checknelems(L, 1);
L0880 o = L->top - 1;
L0881 if (isLfunction(o))
L0882 status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
L0883 else
L0884 status = 1;
L0885 lua_unlock(L);
L0886 return status;
L0887 }
L0888
L0889
L0890 LUA_API int lua_status (lua_State *L) {
L0891 return L->status;
L0892 }
L0893
L0894
L0895
L0898
L0899 LUA_API int lua_gc (lua_State *L, int what, int data) {
L0900 int res = 0;
L0901 global_State *g;
L0902 lua_lock(L);
L0903 g = G(L);
L0904 switch (what) {
L0905 case LUA_GCSTOP: {
L0906 g->GCthreshold = MAX_LUMEM;
L0907 break;
L0908 }
L0909 case LUA_GCRESTART: {
L0910 g->GCthreshold = g->totalbytes;
L0911 break;
L0912 }
L0913 case LUA_GCCOLLECT: {
L0914 luaC_fullgc(L);
L0915 break;
L0916 }
L0917 case LUA_GCCOUNT: {
L0918
L0919 res = cast_int(g->totalbytes >> 10);
L0920 break;
L0921 }
L0922 case LUA_GCCOUNTB: {
L0923 res = cast_int(g->totalbytes & 0x3ff);
L0924 break;
L0925 }
L0926 case LUA_GCSTEP: {
L0927 lu_mem a = (cast(lu_mem, data) << 10);
L0928 if (a <= g->totalbytes)
L0929 g->GCthreshold = g->totalbytes - a;
L0930 else
L0931 g->GCthreshold = 0;
L0932 while (g->GCthreshold <= g->totalbytes) {
L0933 luaC_step(L);
L0934 if (g->gcstate == GCSpause) {
L0935 res = 1;
L0936 break;
L0937 }
L0938 }
L0939 break;
L0940 }
L0941 case LUA_GCSETPAUSE: {
L0942 res = g->gcpause;
L0943 g->gcpause = data;
L0944 break;
L0945 }
L0946 case LUA_GCSETSTEPMUL: {
L0947 res = g->gcstepmul;
L0948 g->gcstepmul = data;
L0949 break;
L0950 }
L0951 default: res = -1;
L0952 }
L0953 lua_unlock(L);
L0954 return res;
L0955 }
L0956
L0957
L0958
L0959
L0962
L0963
L0964 LUA_API int lua_error (lua_State *L) {
L0965 lua_lock(L);
L0966 api_checknelems(L, 1);
L0967 luaG_errormsg(L);
L0968 lua_unlock(L);
L0969 return 0;
L0970 }
L0971
L0972
L0973 LUA_API int lua_next (lua_State *L, int idx) {
L0974 StkId t;
L0975 int more;
L0976 lua_lock(L);
L0977 t = index2adr(L, idx);
L0978 api_check(L, ttistable(t));
L0979 more = luaH_next(L, hvalue(t), L->top - 1);
L0980 if (more) {
L0981 api_incr_top(L);
L0982 }
L0983 else
L0984 L->top -= 1;
L0985 lua_unlock(L);
L0986 return more;
L0987 }
L0988
L0989
L0990 LUA_API void lua_concat (lua_State *L, int n) {
L0991 lua_lock(L);
L0992 api_checknelems(L, n);
L0993 if (n >= 2) {
L0994 luaC_checkGC(L);
L0995 luaV_concat(L, n, cast_int(L->top - L->base) - 1);
L0996 L->top -= (n-1);
L0997 }
L0998 else if (n == 0) {
L0999 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
L1000 api_incr_top(L);
L1001 }
L1002
L1003 lua_unlock(L);
L1004 }
L1005
L1006
L1007 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
L1008 lua_Alloc f;
L1009 lua_lock(L);
L1010 if (ud) *ud = G(L)->ud;
L1011 f = G(L)->frealloc;
L1012 lua_unlock(L);
L1013 return f;
L1014 }
L1015
L1016
L1017 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
L1018 lua_lock(L);
L1019 G(L)->ud = ud;
L1020 G(L)->frealloc = f;
L1021 lua_unlock(L);
L1022 }
L1023
L1024
L1025 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
L1026 Udata *u;
L1027 lua_lock(L);
L1028 luaC_checkGC(L);
L1029 u = luaS_newudata(L, size, getcurrenv(L));
L1030 setuvalue(L, L->top, u);
L1031 api_incr_top(L);
L1032 lua_unlock(L);
L1033 return u + 1;
L1034 }
L1035
L1036
L1037
L1038
L1039 static const char *aux_upvalue (StkId fi, int n, TValue **val) {
L1040 Closure *f;
L1041 if (!ttisfunction(fi)) return NULL;
L1042 f = clvalue(fi);
L1043 if (f->c.isC) {
L1044 if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
L1045 *val = &f->c.upvalue[n-1];
L1046 return "";
L1047 }
L1048 else {
L1049 Proto *p = f->l.p;
L1050 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
L1051 *val = f->l.upvals[n-1]->v;
L1052 return getstr(p->upvalues[n-1]);
L1053 }
L1054 }
L1055
L1056
L1057 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
L1058 const char *name;
L1059 TValue *val;
L1060 lua_lock(L);
L1061 name = aux_upvalue(index2adr(L, funcindex), n, &val);
L1062 if (name) {
L1063 setobj2s(L, L->top, val);
L1064 api_incr_top(L);
L1065 }
L1066 lua_unlock(L);
L1067 return name;
L1068 }
L1069
L1070
L1071 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
L1072 const char *name;
L1073 TValue *val;
L1074 StkId fi;
L1075 lua_lock(L);
L1076 fi = index2adr(L, funcindex);
L1077 api_checknelems(L, 1);
L1078 name = aux_upvalue(fi, n, &val);
L1079 if (name) {
L1080 L->top--;
L1081 setobj(L, val, L->top);
L1082 luaC_barrier(L, clvalue(fi), L->top);
L1083 }
L1084 lua_unlock(L);
L1085 return name;
L1086 }
L1087
Generated by pretty.lua