lapi.c 29 KB

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