lapi.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. ** $Id: lapi.c,v 2.243 2014/11/12 13:28:54 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lapi_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stdarg.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "lmem.h"
  18. #include "lobject.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. #include "lundump.h"
  24. #include "lvm.h"
  25. const char lua_ident[] =
  26. "$LuaVersion: " LUA_COPYRIGHT " $"
  27. "$LuaAuthors: " LUA_AUTHORS " $";
  28. /* value at a non-valid index */
  29. #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
  30. /* corresponding test */
  31. #define isvalid(o) ((o) != luaO_nilobject)
  32. /* test for pseudo index */
  33. #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
  34. /* test for upvalue */
  35. #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
  36. /* test for valid but not pseudo index */
  37. #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
  38. #define api_checkvalidindex(o) api_check(isvalid(o), "invalid index")
  39. #define api_checkstackindex(i, o) \
  40. api_check(isstackindex(i, o), "index not in the stack")
  41. static TValue *index2addr (lua_State *L, int idx) {
  42. CallInfo *ci = L->ci;
  43. if (idx > 0) {
  44. TValue *o = ci->func + idx;
  45. api_check(idx <= ci->top - (ci->func + 1), "unacceptable index");
  46. if (o >= L->top) return NONVALIDVALUE;
  47. else return o;
  48. }
  49. else if (!ispseudo(idx)) { /* negative index */
  50. api_check(idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  51. return L->top + idx;
  52. }
  53. else if (idx == LUA_REGISTRYINDEX)
  54. return &G(L)->l_registry;
  55. else { /* upvalues */
  56. idx = LUA_REGISTRYINDEX - idx;
  57. api_check(idx <= MAXUPVAL + 1, "upvalue index too large");
  58. if (ttislcf(ci->func)) /* light C function? */
  59. return NONVALIDVALUE; /* it has no upvalues */
  60. else {
  61. CClosure *func = clCvalue(ci->func);
  62. return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
  63. }
  64. }
  65. }
  66. /*
  67. ** to be called by 'lua_checkstack' in protected mode, to grow stack
  68. ** capturing memory errors
  69. */
  70. static void growstack (lua_State *L, void *ud) {
  71. int size = *(int *)ud;
  72. luaD_growstack(L, size);
  73. }
  74. LUA_API int lua_checkstack (lua_State *L, int n) {
  75. int res;
  76. CallInfo *ci = L->ci;
  77. lua_lock(L);
  78. api_check(n >= 0, "negative 'n'");
  79. if (L->stack_last - L->top > n) /* stack large enough? */
  80. res = 1; /* yes; check is OK */
  81. else { /* no; need to grow stack */
  82. int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
  83. if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
  84. res = 0; /* no */
  85. else /* try to grow stack */
  86. res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK);
  87. }
  88. if (res && ci->top < L->top + n)
  89. ci->top = L->top + n; /* adjust frame top */
  90. lua_unlock(L);
  91. return res;
  92. }
  93. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  94. int i;
  95. if (from == to) return;
  96. lua_lock(to);
  97. api_checknelems(from, n);
  98. api_check(G(from) == G(to), "moving among independent states");
  99. api_check(to->ci->top - to->top >= n, "not enough elements to move");
  100. from->top -= n;
  101. for (i = 0; i < n; i++) {
  102. setobj2s(to, to->top++, from->top + i);
  103. }
  104. lua_unlock(to);
  105. }
  106. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  107. lua_CFunction old;
  108. lua_lock(L);
  109. old = G(L)->panic;
  110. G(L)->panic = panicf;
  111. lua_unlock(L);
  112. return old;
  113. }
  114. LUA_API const lua_Number *lua_version (lua_State *L) {
  115. static const lua_Number version = LUA_VERSION_NUM;
  116. if (L == NULL) return &version;
  117. else return G(L)->version;
  118. }
  119. /*
  120. ** basic stack manipulation
  121. */
  122. /*
  123. ** convert an acceptable stack index into an absolute index
  124. */
  125. LUA_API int lua_absindex (lua_State *L, int idx) {
  126. return (idx > 0 || ispseudo(idx))
  127. ? idx
  128. : cast_int(L->top - L->ci->func + idx);
  129. }
  130. LUA_API int lua_gettop (lua_State *L) {
  131. return cast_int(L->top - (L->ci->func + 1));
  132. }
  133. LUA_API void lua_settop (lua_State *L, int idx) {
  134. StkId func = L->ci->func;
  135. lua_lock(L);
  136. if (idx >= 0) {
  137. api_check(idx <= L->stack_last - (func + 1), "new top too large");
  138. while (L->top < (func + 1) + idx)
  139. setnilvalue(L->top++);
  140. L->top = (func + 1) + idx;
  141. }
  142. else {
  143. api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top");
  144. L->top += idx+1; /* 'subtract' index (index is negative) */
  145. }
  146. lua_unlock(L);
  147. }
  148. /*
  149. ** Reverse the stack segment from 'from' to 'to'
  150. ** (auxiliary to 'lua_rotate')
  151. */
  152. static void reverse (lua_State *L, StkId from, StkId to) {
  153. for (; from < to; from++, to--) {
  154. TValue temp;
  155. setobj(L, &temp, from);
  156. setobjs2s(L, from, to);
  157. setobj2s(L, to, &temp);
  158. }
  159. }
  160. /*
  161. ** Let x = AB, where A is a prefix of length 'n'. Then,
  162. ** rotate x n == BA. But BA == (A^r . B^r)^r.
  163. */
  164. LUA_API void lua_rotate (lua_State *L, int idx, int n) {
  165. StkId p, t, m;
  166. lua_lock(L);
  167. t = L->top - 1; /* end of stack segment being rotated */
  168. p = index2addr(L, idx); /* start of segment */
  169. api_checkstackindex(idx, p);
  170. api_check((n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
  171. m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
  172. reverse(L, p, m); /* reverse the prefix with length 'n' */
  173. reverse(L, m + 1, t); /* reverse the suffix */
  174. reverse(L, p, t); /* reverse the entire segment */
  175. lua_unlock(L);
  176. }
  177. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  178. TValue *fr, *to;
  179. lua_lock(L);
  180. fr = index2addr(L, fromidx);
  181. to = index2addr(L, toidx);
  182. api_checkvalidindex(to);
  183. setobj(L, to, fr);
  184. if (isupvalue(toidx)) /* function upvalue? */
  185. luaC_barrier(L, clCvalue(L->ci->func), fr);
  186. /* LUA_REGISTRYINDEX does not need gc barrier
  187. (collector revisits it before finishing collection) */
  188. lua_unlock(L);
  189. }
  190. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  191. lua_lock(L);
  192. setobj2s(L, L->top, index2addr(L, idx));
  193. api_incr_top(L);
  194. lua_unlock(L);
  195. }
  196. /*
  197. ** access functions (stack -> C)
  198. */
  199. LUA_API int lua_type (lua_State *L, int idx) {
  200. StkId o = index2addr(L, idx);
  201. return (isvalid(o) ? ttnov(o) : LUA_TNONE);
  202. }
  203. LUA_API const char *lua_typename (lua_State *L, int t) {
  204. UNUSED(L);
  205. api_check(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
  206. return ttypename(t);
  207. }
  208. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  209. StkId o = index2addr(L, idx);
  210. return (ttislcf(o) || (ttisCclosure(o)));
  211. }
  212. LUA_API int lua_isinteger (lua_State *L, int idx) {
  213. StkId o = index2addr(L, idx);
  214. return ttisinteger(o);
  215. }
  216. LUA_API int lua_isnumber (lua_State *L, int idx) {
  217. lua_Number n;
  218. const TValue *o = index2addr(L, idx);
  219. return tonumber(o, &n);
  220. }
  221. LUA_API int lua_isstring (lua_State *L, int idx) {
  222. const TValue *o = index2addr(L, idx);
  223. return (ttisstring(o) || cvt2str(o));
  224. }
  225. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  226. const TValue *o = index2addr(L, idx);
  227. return (ttisfulluserdata(o) || ttislightuserdata(o));
  228. }
  229. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  230. StkId o1 = index2addr(L, index1);
  231. StkId o2 = index2addr(L, index2);
  232. return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
  233. }
  234. LUA_API void lua_arith (lua_State *L, int op) {
  235. lua_lock(L);
  236. if (op != LUA_OPUNM && op != LUA_OPBNOT)
  237. api_checknelems(L, 2); /* all other operations expect two operands */
  238. else { /* for unary operations, add fake 2nd operand */
  239. api_checknelems(L, 1);
  240. setobjs2s(L, L->top, L->top - 1);
  241. L->top++;
  242. }
  243. /* first operand at top - 2, second at top - 1; result go to top - 2 */
  244. luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
  245. L->top--; /* remove second operand */
  246. lua_unlock(L);
  247. }
  248. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  249. StkId o1, o2;
  250. int i = 0;
  251. lua_lock(L); /* may call tag method */
  252. o1 = index2addr(L, index1);
  253. o2 = index2addr(L, index2);
  254. if (isvalid(o1) && isvalid(o2)) {
  255. switch (op) {
  256. case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
  257. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  258. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  259. default: api_check(0, "invalid option");
  260. }
  261. }
  262. lua_unlock(L);
  263. return i;
  264. }
  265. LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
  266. size_t sz = luaO_str2num(s, L->top);
  267. if (sz != 0)
  268. api_incr_top(L);
  269. return sz;
  270. }
  271. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  272. lua_Number n;
  273. const TValue *o = index2addr(L, idx);
  274. int isnum = tonumber(o, &n);
  275. if (!isnum)
  276. n = 0; /* call to 'tonumber' may change 'n' even if it fails */
  277. if (pisnum) *pisnum = isnum;
  278. return n;
  279. }
  280. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  281. lua_Integer res;
  282. const TValue *o = index2addr(L, idx);
  283. int isnum = tointeger(o, &res);
  284. if (!isnum)
  285. res = 0; /* call to 'tointeger' may change 'n' even if it fails */
  286. if (pisnum) *pisnum = isnum;
  287. return res;
  288. }
  289. LUA_API int lua_toboolean (lua_State *L, int idx) {
  290. const TValue *o = index2addr(L, idx);
  291. return !l_isfalse(o);
  292. }
  293. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  294. StkId o = index2addr(L, idx);
  295. if (!ttisstring(o)) {
  296. if (!cvt2str(o)) { /* not convertible? */
  297. if (len != NULL) *len = 0;
  298. return NULL;
  299. }
  300. lua_lock(L); /* 'luaO_tostring' may create a new string */
  301. luaC_checkGC(L);
  302. o = index2addr(L, idx); /* previous call may reallocate the stack */
  303. luaO_tostring(L, o);
  304. lua_unlock(L);
  305. }
  306. if (len != NULL) *len = tsvalue(o)->len;
  307. return svalue(o);
  308. }
  309. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  310. StkId o = index2addr(L, idx);
  311. switch (ttnov(o)) {
  312. case LUA_TSTRING: return tsvalue(o)->len;
  313. case LUA_TUSERDATA: return uvalue(o)->len;
  314. case LUA_TTABLE: return luaH_getn(hvalue(o));
  315. default: return 0;
  316. }
  317. }
  318. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  319. StkId o = index2addr(L, idx);
  320. if (ttislcf(o)) return fvalue(o);
  321. else if (ttisCclosure(o))
  322. return clCvalue(o)->f;
  323. else return NULL; /* not a C function */
  324. }
  325. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  326. StkId o = index2addr(L, idx);
  327. switch (ttnov(o)) {
  328. case LUA_TUSERDATA: return getudatamem(uvalue(o));
  329. case LUA_TLIGHTUSERDATA: return pvalue(o);
  330. default: return NULL;
  331. }
  332. }
  333. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  334. StkId o = index2addr(L, idx);
  335. return (!ttisthread(o)) ? NULL : thvalue(o);
  336. }
  337. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  338. StkId o = index2addr(L, idx);
  339. switch (ttype(o)) {
  340. case LUA_TTABLE: return hvalue(o);
  341. case LUA_TLCL: return clLvalue(o);
  342. case LUA_TCCL: return clCvalue(o);
  343. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  344. case LUA_TTHREAD: return thvalue(o);
  345. case LUA_TUSERDATA:
  346. case LUA_TLIGHTUSERDATA:
  347. return lua_touserdata(L, idx);
  348. default: return NULL;
  349. }
  350. }
  351. /*
  352. ** push functions (C -> stack)
  353. */
  354. LUA_API void lua_pushnil (lua_State *L) {
  355. lua_lock(L);
  356. setnilvalue(L->top);
  357. api_incr_top(L);
  358. lua_unlock(L);
  359. }
  360. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  361. lua_lock(L);
  362. setfltvalue(L->top, n);
  363. api_incr_top(L);
  364. lua_unlock(L);
  365. }
  366. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  367. lua_lock(L);
  368. setivalue(L->top, n);
  369. api_incr_top(L);
  370. lua_unlock(L);
  371. }
  372. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  373. TString *ts;
  374. lua_lock(L);
  375. luaC_checkGC(L);
  376. ts = luaS_newlstr(L, s, len);
  377. setsvalue2s(L, L->top, ts);
  378. api_incr_top(L);
  379. lua_unlock(L);
  380. return getstr(ts);
  381. }
  382. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  383. if (s == NULL) {
  384. lua_pushnil(L);
  385. return NULL;
  386. }
  387. else {
  388. TString *ts;
  389. lua_lock(L);
  390. luaC_checkGC(L);
  391. ts = luaS_new(L, s);
  392. setsvalue2s(L, L->top, ts);
  393. api_incr_top(L);
  394. lua_unlock(L);
  395. return getstr(ts);
  396. }
  397. }
  398. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  399. va_list argp) {
  400. const char *ret;
  401. lua_lock(L);
  402. luaC_checkGC(L);
  403. ret = luaO_pushvfstring(L, fmt, argp);
  404. lua_unlock(L);
  405. return ret;
  406. }
  407. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  408. const char *ret;
  409. va_list argp;
  410. lua_lock(L);
  411. luaC_checkGC(L);
  412. va_start(argp, fmt);
  413. ret = luaO_pushvfstring(L, fmt, argp);
  414. va_end(argp);
  415. lua_unlock(L);
  416. return ret;
  417. }
  418. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  419. lua_lock(L);
  420. if (n == 0) {
  421. setfvalue(L->top, fn);
  422. }
  423. else {
  424. CClosure *cl;
  425. api_checknelems(L, n);
  426. api_check(n <= MAXUPVAL, "upvalue index too large");
  427. luaC_checkGC(L);
  428. cl = luaF_newCclosure(L, n);
  429. cl->f = fn;
  430. L->top -= n;
  431. while (n--) {
  432. setobj2n(L, &cl->upvalue[n], L->top + n);
  433. /* does not need barrier because closure is white */
  434. }
  435. setclCvalue(L, L->top, cl);
  436. }
  437. api_incr_top(L);
  438. lua_unlock(L);
  439. }
  440. LUA_API void lua_pushboolean (lua_State *L, int b) {
  441. lua_lock(L);
  442. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  443. api_incr_top(L);
  444. lua_unlock(L);
  445. }
  446. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  447. lua_lock(L);
  448. setpvalue(L->top, p);
  449. api_incr_top(L);
  450. lua_unlock(L);
  451. }
  452. LUA_API int lua_pushthread (lua_State *L) {
  453. lua_lock(L);
  454. setthvalue(L, L->top, L);
  455. api_incr_top(L);
  456. lua_unlock(L);
  457. return (G(L)->mainthread == L);
  458. }
  459. /*
  460. ** get functions (Lua -> stack)
  461. */
  462. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  463. Table *reg = hvalue(&G(L)->l_registry);
  464. const TValue *gt; /* global table */
  465. lua_lock(L);
  466. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  467. setsvalue2s(L, L->top++, luaS_new(L, name));
  468. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  469. lua_unlock(L);
  470. return ttnov(L->top - 1);
  471. }
  472. LUA_API int lua_gettable (lua_State *L, int idx) {
  473. StkId t;
  474. lua_lock(L);
  475. t = index2addr(L, idx);
  476. luaV_gettable(L, t, L->top - 1, L->top - 1);
  477. lua_unlock(L);
  478. return ttnov(L->top - 1);
  479. }
  480. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  481. StkId t;
  482. lua_lock(L);
  483. t = index2addr(L, idx);
  484. setsvalue2s(L, L->top, luaS_new(L, k));
  485. api_incr_top(L);
  486. luaV_gettable(L, t, L->top - 1, L->top - 1);
  487. lua_unlock(L);
  488. return ttnov(L->top - 1);
  489. }
  490. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  491. StkId t;
  492. lua_lock(L);
  493. t = index2addr(L, idx);
  494. setivalue(L->top, n);
  495. api_incr_top(L);
  496. luaV_gettable(L, t, L->top - 1, L->top - 1);
  497. lua_unlock(L);
  498. return ttnov(L->top - 1);
  499. }
  500. LUA_API int lua_rawget (lua_State *L, int idx) {
  501. StkId t;
  502. lua_lock(L);
  503. t = index2addr(L, idx);
  504. api_check(ttistable(t), "table expected");
  505. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  506. lua_unlock(L);
  507. return ttnov(L->top - 1);
  508. }
  509. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  510. StkId t;
  511. lua_lock(L);
  512. t = index2addr(L, idx);
  513. api_check(ttistable(t), "table expected");
  514. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  515. api_incr_top(L);
  516. lua_unlock(L);
  517. return ttnov(L->top - 1);
  518. }
  519. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  520. StkId t;
  521. TValue k;
  522. lua_lock(L);
  523. t = index2addr(L, idx);
  524. api_check(ttistable(t), "table expected");
  525. setpvalue(&k, cast(void *, p));
  526. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  527. api_incr_top(L);
  528. lua_unlock(L);
  529. return ttnov(L->top - 1);
  530. }
  531. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  532. Table *t;
  533. lua_lock(L);
  534. luaC_checkGC(L);
  535. t = luaH_new(L);
  536. sethvalue(L, L->top, t);
  537. api_incr_top(L);
  538. if (narray > 0 || nrec > 0)
  539. luaH_resize(L, t, narray, nrec);
  540. lua_unlock(L);
  541. }
  542. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  543. const TValue *obj;
  544. Table *mt;
  545. int res = 0;
  546. lua_lock(L);
  547. obj = index2addr(L, objindex);
  548. switch (ttnov(obj)) {
  549. case LUA_TTABLE:
  550. mt = hvalue(obj)->metatable;
  551. break;
  552. case LUA_TUSERDATA:
  553. mt = uvalue(obj)->metatable;
  554. break;
  555. default:
  556. mt = G(L)->mt[ttnov(obj)];
  557. break;
  558. }
  559. if (mt != NULL) {
  560. sethvalue(L, L->top, mt);
  561. api_incr_top(L);
  562. res = 1;
  563. }
  564. lua_unlock(L);
  565. return res;
  566. }
  567. LUA_API int lua_getuservalue (lua_State *L, int idx) {
  568. StkId o;
  569. lua_lock(L);
  570. o = index2addr(L, idx);
  571. api_check(ttisfulluserdata(o), "full userdata expected");
  572. getuservalue(L, uvalue(o), L->top);
  573. api_incr_top(L);
  574. lua_unlock(L);
  575. return ttnov(L->top - 1);
  576. }
  577. /*
  578. ** set functions (stack -> Lua)
  579. */
  580. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  581. Table *reg = hvalue(&G(L)->l_registry);
  582. const TValue *gt; /* global table */
  583. lua_lock(L);
  584. api_checknelems(L, 1);
  585. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  586. setsvalue2s(L, L->top++, luaS_new(L, name));
  587. luaV_settable(L, gt, L->top - 1, L->top - 2);
  588. L->top -= 2; /* pop value and key */
  589. lua_unlock(L);
  590. }
  591. LUA_API void lua_settable (lua_State *L, int idx) {
  592. StkId t;
  593. lua_lock(L);
  594. api_checknelems(L, 2);
  595. t = index2addr(L, idx);
  596. luaV_settable(L, t, L->top - 2, L->top - 1);
  597. L->top -= 2; /* pop index and value */
  598. lua_unlock(L);
  599. }
  600. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  601. StkId t;
  602. lua_lock(L);
  603. api_checknelems(L, 1);
  604. t = index2addr(L, idx);
  605. setsvalue2s(L, L->top++, luaS_new(L, k));
  606. luaV_settable(L, t, L->top - 1, L->top - 2);
  607. L->top -= 2; /* pop value and key */
  608. lua_unlock(L);
  609. }
  610. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  611. StkId t;
  612. lua_lock(L);
  613. api_checknelems(L, 1);
  614. t = index2addr(L, idx);
  615. setivalue(L->top++, n);
  616. luaV_settable(L, t, L->top - 1, L->top - 2);
  617. L->top -= 2; /* pop value and key */
  618. lua_unlock(L);
  619. }
  620. LUA_API void lua_rawset (lua_State *L, int idx) {
  621. StkId o;
  622. Table *t;
  623. lua_lock(L);
  624. api_checknelems(L, 2);
  625. o = index2addr(L, idx);
  626. api_check(ttistable(o), "table expected");
  627. t = hvalue(o);
  628. setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
  629. invalidateTMcache(t);
  630. luaC_barrierback(L, t, L->top-1);
  631. L->top -= 2;
  632. lua_unlock(L);
  633. }
  634. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  635. StkId o;
  636. Table *t;
  637. lua_lock(L);
  638. api_checknelems(L, 1);
  639. o = index2addr(L, idx);
  640. api_check(ttistable(o), "table expected");
  641. t = hvalue(o);
  642. luaH_setint(L, t, n, L->top - 1);
  643. luaC_barrierback(L, t, L->top-1);
  644. L->top--;
  645. lua_unlock(L);
  646. }
  647. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  648. StkId o;
  649. Table *t;
  650. TValue k;
  651. lua_lock(L);
  652. api_checknelems(L, 1);
  653. o = index2addr(L, idx);
  654. api_check(ttistable(o), "table expected");
  655. t = hvalue(o);
  656. setpvalue(&k, cast(void *, p));
  657. setobj2t(L, luaH_set(L, t, &k), L->top - 1);
  658. luaC_barrierback(L, t, L->top - 1);
  659. L->top--;
  660. lua_unlock(L);
  661. }
  662. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  663. TValue *obj;
  664. Table *mt;
  665. lua_lock(L);
  666. api_checknelems(L, 1);
  667. obj = index2addr(L, objindex);
  668. if (ttisnil(L->top - 1))
  669. mt = NULL;
  670. else {
  671. api_check(ttistable(L->top - 1), "table expected");
  672. mt = hvalue(L->top - 1);
  673. }
  674. switch (ttnov(obj)) {
  675. case LUA_TTABLE: {
  676. hvalue(obj)->metatable = mt;
  677. if (mt) {
  678. luaC_objbarrier(L, gcvalue(obj), mt);
  679. luaC_checkfinalizer(L, gcvalue(obj), mt);
  680. }
  681. break;
  682. }
  683. case LUA_TUSERDATA: {
  684. uvalue(obj)->metatable = mt;
  685. if (mt) {
  686. luaC_objbarrier(L, uvalue(obj), mt);
  687. luaC_checkfinalizer(L, gcvalue(obj), mt);
  688. }
  689. break;
  690. }
  691. default: {
  692. G(L)->mt[ttnov(obj)] = mt;
  693. break;
  694. }
  695. }
  696. L->top--;
  697. lua_unlock(L);
  698. return 1;
  699. }
  700. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  701. StkId o;
  702. lua_lock(L);
  703. api_checknelems(L, 1);
  704. o = index2addr(L, idx);
  705. api_check(ttisfulluserdata(o), "full userdata expected");
  706. setuservalue(L, uvalue(o), L->top - 1);
  707. luaC_barrier(L, gcvalue(o), L->top - 1);
  708. L->top--;
  709. lua_unlock(L);
  710. }
  711. /*
  712. ** 'load' and 'call' functions (run Lua code)
  713. */
  714. #define checkresults(L,na,nr) \
  715. api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  716. "results from function overflow current stack size")
  717. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  718. lua_KContext ctx, lua_KFunction k) {
  719. StkId func;
  720. lua_lock(L);
  721. api_check(k == NULL || !isLua(L->ci),
  722. "cannot use continuations inside hooks");
  723. api_checknelems(L, nargs+1);
  724. api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
  725. checkresults(L, nargs, nresults);
  726. func = L->top - (nargs+1);
  727. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  728. L->ci->u.c.k = k; /* save continuation */
  729. L->ci->u.c.ctx = ctx; /* save context */
  730. luaD_call(L, func, nresults, 1); /* do the call */
  731. }
  732. else /* no continuation or no yieldable */
  733. luaD_call(L, func, nresults, 0); /* just do the call */
  734. adjustresults(L, nresults);
  735. lua_unlock(L);
  736. }
  737. /*
  738. ** Execute a protected call.
  739. */
  740. struct CallS { /* data to 'f_call' */
  741. StkId func;
  742. int nresults;
  743. };
  744. static void f_call (lua_State *L, void *ud) {
  745. struct CallS *c = cast(struct CallS *, ud);
  746. luaD_call(L, c->func, c->nresults, 0);
  747. }
  748. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  749. lua_KContext ctx, lua_KFunction k) {
  750. struct CallS c;
  751. int status;
  752. ptrdiff_t func;
  753. lua_lock(L);
  754. api_check(k == NULL || !isLua(L->ci),
  755. "cannot use continuations inside hooks");
  756. api_checknelems(L, nargs+1);
  757. api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
  758. checkresults(L, nargs, nresults);
  759. if (errfunc == 0)
  760. func = 0;
  761. else {
  762. StkId o = index2addr(L, errfunc);
  763. api_checkstackindex(errfunc, o);
  764. func = savestack(L, o);
  765. }
  766. c.func = L->top - (nargs+1); /* function to be called */
  767. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  768. c.nresults = nresults; /* do a 'conventional' protected call */
  769. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  770. }
  771. else { /* prepare continuation (call is already protected by 'resume') */
  772. CallInfo *ci = L->ci;
  773. ci->u.c.k = k; /* save continuation */
  774. ci->u.c.ctx = ctx; /* save context */
  775. /* save information for error recovery */
  776. ci->extra = savestack(L, c.func);
  777. ci->u.c.old_errfunc = L->errfunc;
  778. L->errfunc = func;
  779. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  780. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  781. luaD_call(L, c.func, nresults, 1); /* do the call */
  782. ci->callstatus &= ~CIST_YPCALL;
  783. L->errfunc = ci->u.c.old_errfunc;
  784. status = LUA_OK; /* if it is here, there were no errors */
  785. }
  786. adjustresults(L, nresults);
  787. lua_unlock(L);
  788. return status;
  789. }
  790. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  791. const char *chunkname, const char *mode) {
  792. ZIO z;
  793. int status;
  794. lua_lock(L);
  795. if (!chunkname) chunkname = "?";
  796. luaZ_init(L, &z, reader, data);
  797. status = luaD_protectedparser(L, &z, chunkname, mode);
  798. if (status == LUA_OK) { /* no errors? */
  799. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  800. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  801. /* get global table from registry */
  802. Table *reg = hvalue(&G(L)->l_registry);
  803. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  804. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  805. setobj(L, f->upvals[0]->v, gt);
  806. luaC_upvalbarrier(L, f->upvals[0]);
  807. }
  808. }
  809. lua_unlock(L);
  810. return status;
  811. }
  812. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  813. int status;
  814. TValue *o;
  815. lua_lock(L);
  816. api_checknelems(L, 1);
  817. o = L->top - 1;
  818. if (isLfunction(o))
  819. status = luaU_dump(L, getproto(o), writer, data, strip);
  820. else
  821. status = 1;
  822. lua_unlock(L);
  823. return status;
  824. }
  825. LUA_API int lua_status (lua_State *L) {
  826. return L->status;
  827. }
  828. /*
  829. ** Garbage-collection function
  830. */
  831. LUA_API int lua_gc (lua_State *L, int what, int data) {
  832. int res = 0;
  833. global_State *g;
  834. lua_lock(L);
  835. g = G(L);
  836. switch (what) {
  837. case LUA_GCSTOP: {
  838. g->gcrunning = 0;
  839. break;
  840. }
  841. case LUA_GCRESTART: {
  842. luaE_setdebt(g, 0);
  843. g->gcrunning = 1;
  844. break;
  845. }
  846. case LUA_GCCOLLECT: {
  847. luaC_fullgc(L, 0);
  848. break;
  849. }
  850. case LUA_GCCOUNT: {
  851. /* GC values are expressed in Kbytes: #bytes/2^10 */
  852. res = cast_int(gettotalbytes(g) >> 10);
  853. break;
  854. }
  855. case LUA_GCCOUNTB: {
  856. res = cast_int(gettotalbytes(g) & 0x3ff);
  857. break;
  858. }
  859. case LUA_GCSTEP: {
  860. l_mem debt = 1; /* =1 to signal that it did an actual step */
  861. int oldrunning = g->gcrunning;
  862. g->gcrunning = 1; /* allow GC to run */
  863. if (data == 0) {
  864. luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
  865. luaC_step(L);
  866. }
  867. else { /* add 'data' to total debt */
  868. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  869. luaE_setdebt(g, debt);
  870. luaC_checkGC(L);
  871. }
  872. g->gcrunning = oldrunning; /* restore previous state */
  873. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  874. res = 1; /* signal it */
  875. break;
  876. }
  877. case LUA_GCSETPAUSE: {
  878. res = g->gcpause;
  879. g->gcpause = data;
  880. break;
  881. }
  882. case LUA_GCSETSTEPMUL: {
  883. res = g->gcstepmul;
  884. if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
  885. g->gcstepmul = data;
  886. break;
  887. }
  888. case LUA_GCISRUNNING: {
  889. res = g->gcrunning;
  890. break;
  891. }
  892. default: res = -1; /* invalid option */
  893. }
  894. lua_unlock(L);
  895. return res;
  896. }
  897. /*
  898. ** miscellaneous functions
  899. */
  900. LUA_API int lua_error (lua_State *L) {
  901. lua_lock(L);
  902. api_checknelems(L, 1);
  903. luaG_errormsg(L);
  904. /* code unreachable; will unlock when control actually leaves the kernel */
  905. return 0; /* to avoid warnings */
  906. }
  907. LUA_API int lua_next (lua_State *L, int idx) {
  908. StkId t;
  909. int more;
  910. lua_lock(L);
  911. t = index2addr(L, idx);
  912. api_check(ttistable(t), "table expected");
  913. more = luaH_next(L, hvalue(t), L->top - 1);
  914. if (more) {
  915. api_incr_top(L);
  916. }
  917. else /* no more elements */
  918. L->top -= 1; /* remove key */
  919. lua_unlock(L);
  920. return more;
  921. }
  922. LUA_API void lua_concat (lua_State *L, int n) {
  923. lua_lock(L);
  924. api_checknelems(L, n);
  925. if (n >= 2) {
  926. luaC_checkGC(L);
  927. luaV_concat(L, n);
  928. }
  929. else if (n == 0) { /* push empty string */
  930. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  931. api_incr_top(L);
  932. }
  933. /* else n == 1; nothing to do */
  934. lua_unlock(L);
  935. }
  936. LUA_API void lua_len (lua_State *L, int idx) {
  937. StkId t;
  938. lua_lock(L);
  939. t = index2addr(L, idx);
  940. luaV_objlen(L, L->top, t);
  941. api_incr_top(L);
  942. lua_unlock(L);
  943. }
  944. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  945. lua_Alloc f;
  946. lua_lock(L);
  947. if (ud) *ud = G(L)->ud;
  948. f = G(L)->frealloc;
  949. lua_unlock(L);
  950. return f;
  951. }
  952. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  953. lua_lock(L);
  954. G(L)->ud = ud;
  955. G(L)->frealloc = f;
  956. lua_unlock(L);
  957. }
  958. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  959. Udata *u;
  960. lua_lock(L);
  961. luaC_checkGC(L);
  962. u = luaS_newudata(L, size);
  963. setuvalue(L, L->top, u);
  964. api_incr_top(L);
  965. lua_unlock(L);
  966. return getudatamem(u);
  967. }
  968. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  969. CClosure **owner, UpVal **uv) {
  970. switch (ttype(fi)) {
  971. case LUA_TCCL: { /* C closure */
  972. CClosure *f = clCvalue(fi);
  973. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  974. *val = &f->upvalue[n-1];
  975. if (owner) *owner = f;
  976. return "";
  977. }
  978. case LUA_TLCL: { /* Lua closure */
  979. LClosure *f = clLvalue(fi);
  980. TString *name;
  981. Proto *p = f->p;
  982. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  983. *val = f->upvals[n-1]->v;
  984. if (uv) *uv = f->upvals[n - 1];
  985. name = p->upvalues[n-1].name;
  986. return (name == NULL) ? "(*no name)" : getstr(name);
  987. }
  988. default: return NULL; /* not a closure */
  989. }
  990. }
  991. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  992. const char *name;
  993. TValue *val = NULL; /* to avoid warnings */
  994. lua_lock(L);
  995. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL);
  996. if (name) {
  997. setobj2s(L, L->top, val);
  998. api_incr_top(L);
  999. }
  1000. lua_unlock(L);
  1001. return name;
  1002. }
  1003. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1004. const char *name;
  1005. TValue *val = NULL; /* to avoid warnings */
  1006. CClosure *owner = NULL;
  1007. UpVal *uv = NULL;
  1008. StkId fi;
  1009. lua_lock(L);
  1010. fi = index2addr(L, funcindex);
  1011. api_checknelems(L, 1);
  1012. name = aux_upvalue(fi, n, &val, &owner, &uv);
  1013. if (name) {
  1014. L->top--;
  1015. setobj(L, val, L->top);
  1016. if (owner) { luaC_barrier(L, owner, L->top); }
  1017. else if (uv) { luaC_upvalbarrier(L, uv); }
  1018. }
  1019. lua_unlock(L);
  1020. return name;
  1021. }
  1022. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1023. LClosure *f;
  1024. StkId fi = index2addr(L, fidx);
  1025. api_check(ttisLclosure(fi), "Lua function expected");
  1026. f = clLvalue(fi);
  1027. api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1028. if (pf) *pf = f;
  1029. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1030. }
  1031. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1032. StkId fi = index2addr(L, fidx);
  1033. switch (ttype(fi)) {
  1034. case LUA_TLCL: { /* lua closure */
  1035. return *getupvalref(L, fidx, n, NULL);
  1036. }
  1037. case LUA_TCCL: { /* C closure */
  1038. CClosure *f = clCvalue(fi);
  1039. api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1040. return &f->upvalue[n - 1];
  1041. }
  1042. default: {
  1043. api_check(0, "closure expected");
  1044. return NULL;
  1045. }
  1046. }
  1047. }
  1048. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1049. int fidx2, int n2) {
  1050. LClosure *f1;
  1051. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1052. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1053. luaC_upvdeccount(L, *up1);
  1054. *up1 = *up2;
  1055. (*up1)->refcount++;
  1056. if (upisopen(*up1)) (*up1)->u.open.touched = 1;
  1057. luaC_upvalbarrier(L, *up1);
  1058. }