lapi.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. ** $Id: lapi.c,v 2.228 2014/07/18 14:46:47 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 getudatamem(uvalue(o));
  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, 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 *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 o;
  612. Table *t;
  613. lua_lock(L);
  614. api_checknelems(L, 2);
  615. o = index2addr(L, idx);
  616. api_check(ttistable(o), "table expected");
  617. t = hvalue(o);
  618. setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
  619. invalidateTMcache(t);
  620. luaC_barrierback(L, t, L->top-1);
  621. L->top -= 2;
  622. lua_unlock(L);
  623. }
  624. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  625. StkId o;
  626. Table *t;
  627. lua_lock(L);
  628. api_checknelems(L, 1);
  629. o = index2addr(L, idx);
  630. api_check(ttistable(o), "table expected");
  631. t = hvalue(o);
  632. luaH_setint(L, t, n, L->top - 1);
  633. luaC_barrierback(L, t, L->top-1);
  634. L->top--;
  635. lua_unlock(L);
  636. }
  637. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  638. StkId o;
  639. Table *t;
  640. TValue k;
  641. lua_lock(L);
  642. api_checknelems(L, 1);
  643. o = index2addr(L, idx);
  644. api_check(ttistable(o), "table expected");
  645. t = hvalue(o);
  646. setpvalue(&k, cast(void *, p));
  647. setobj2t(L, luaH_set(L, t, &k), L->top - 1);
  648. luaC_barrierback(L, t, L->top - 1);
  649. L->top--;
  650. lua_unlock(L);
  651. }
  652. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  653. TValue *obj;
  654. Table *mt;
  655. lua_lock(L);
  656. api_checknelems(L, 1);
  657. obj = index2addr(L, objindex);
  658. if (ttisnil(L->top - 1))
  659. mt = NULL;
  660. else {
  661. api_check(ttistable(L->top - 1), "table expected");
  662. mt = hvalue(L->top - 1);
  663. }
  664. switch (ttnov(obj)) {
  665. case LUA_TTABLE: {
  666. hvalue(obj)->metatable = mt;
  667. if (mt) {
  668. luaC_objbarrier(L, gcvalue(obj), mt);
  669. luaC_checkfinalizer(L, gcvalue(obj), mt);
  670. }
  671. break;
  672. }
  673. case LUA_TUSERDATA: {
  674. uvalue(obj)->metatable = mt;
  675. if (mt) {
  676. luaC_objbarrier(L, uvalue(obj), mt);
  677. luaC_checkfinalizer(L, gcvalue(obj), mt);
  678. }
  679. break;
  680. }
  681. default: {
  682. G(L)->mt[ttnov(obj)] = mt;
  683. break;
  684. }
  685. }
  686. L->top--;
  687. lua_unlock(L);
  688. return 1;
  689. }
  690. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  691. StkId o;
  692. lua_lock(L);
  693. api_checknelems(L, 1);
  694. o = index2addr(L, idx);
  695. api_check(ttisfulluserdata(o), "full userdata expected");
  696. setuservalue(L, uvalue(o), L->top - 1);
  697. luaC_barrier(L, gcvalue(o), L->top - 1);
  698. L->top--;
  699. lua_unlock(L);
  700. }
  701. /*
  702. ** `load' and `call' functions (run Lua code)
  703. */
  704. #define checkresults(L,na,nr) \
  705. api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  706. "results from function overflow current stack size")
  707. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_Ctx ctx,
  708. lua_KFunction k) {
  709. StkId func;
  710. lua_lock(L);
  711. api_check(k == NULL || !isLua(L->ci),
  712. "cannot use continuations inside hooks");
  713. api_checknelems(L, nargs+1);
  714. api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
  715. checkresults(L, nargs, nresults);
  716. func = L->top - (nargs+1);
  717. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  718. L->ci->u.c.k = k; /* save continuation */
  719. L->ci->u.c.ctx = ctx; /* save context */
  720. luaD_call(L, func, nresults, 1); /* do the call */
  721. }
  722. else /* no continuation or no yieldable */
  723. luaD_call(L, func, nresults, 0); /* just do the call */
  724. adjustresults(L, nresults);
  725. lua_unlock(L);
  726. }
  727. /*
  728. ** Execute a protected call.
  729. */
  730. struct CallS { /* data to `f_call' */
  731. StkId func;
  732. int nresults;
  733. };
  734. static void f_call (lua_State *L, void *ud) {
  735. struct CallS *c = cast(struct CallS *, ud);
  736. luaD_call(L, c->func, c->nresults, 0);
  737. }
  738. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  739. lua_Ctx ctx, lua_KFunction k) {
  740. struct CallS c;
  741. int status;
  742. ptrdiff_t func;
  743. lua_lock(L);
  744. api_check(k == NULL || !isLua(L->ci),
  745. "cannot use continuations inside hooks");
  746. api_checknelems(L, nargs+1);
  747. api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
  748. checkresults(L, nargs, nresults);
  749. if (errfunc == 0)
  750. func = 0;
  751. else {
  752. StkId o = index2addr(L, errfunc);
  753. api_checkstackindex(errfunc, o);
  754. func = savestack(L, o);
  755. }
  756. c.func = L->top - (nargs+1); /* function to be called */
  757. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  758. c.nresults = nresults; /* do a 'conventional' protected call */
  759. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  760. }
  761. else { /* prepare continuation (call is already protected by 'resume') */
  762. CallInfo *ci = L->ci;
  763. ci->u.c.k = k; /* save continuation */
  764. ci->u.c.ctx = ctx; /* save context */
  765. /* save information for error recovery */
  766. ci->extra = savestack(L, c.func);
  767. ci->u.c.old_errfunc = L->errfunc;
  768. L->errfunc = func;
  769. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  770. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  771. luaD_call(L, c.func, nresults, 1); /* do the call */
  772. ci->callstatus &= ~CIST_YPCALL;
  773. L->errfunc = ci->u.c.old_errfunc;
  774. status = LUA_OK; /* if it is here, there were no errors */
  775. }
  776. adjustresults(L, nresults);
  777. lua_unlock(L);
  778. return status;
  779. }
  780. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  781. const char *chunkname, const char *mode) {
  782. ZIO z;
  783. int status;
  784. lua_lock(L);
  785. if (!chunkname) chunkname = "?";
  786. luaZ_init(L, &z, reader, data);
  787. status = luaD_protectedparser(L, &z, chunkname, mode);
  788. if (status == LUA_OK) { /* no errors? */
  789. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  790. if (f->nupvalues == 1) { /* does it have one upvalue? */
  791. /* get global table from registry */
  792. Table *reg = hvalue(&G(L)->l_registry);
  793. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  794. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  795. setobj(L, f->upvals[0]->v, gt);
  796. luaC_upvalbarrier(L, f->upvals[0]);
  797. }
  798. }
  799. lua_unlock(L);
  800. return status;
  801. }
  802. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  803. int status;
  804. TValue *o;
  805. lua_lock(L);
  806. api_checknelems(L, 1);
  807. o = L->top - 1;
  808. if (isLfunction(o))
  809. status = luaU_dump(L, getproto(o), writer, data, strip);
  810. else
  811. status = 1;
  812. lua_unlock(L);
  813. return status;
  814. }
  815. LUA_API int lua_status (lua_State *L) {
  816. return L->status;
  817. }
  818. /*
  819. ** Garbage-collection function
  820. */
  821. LUA_API int lua_gc (lua_State *L, int what, int data) {
  822. int res = 0;
  823. global_State *g;
  824. lua_lock(L);
  825. g = G(L);
  826. switch (what) {
  827. case LUA_GCSTOP: {
  828. g->gcrunning = 0;
  829. break;
  830. }
  831. case LUA_GCRESTART: {
  832. luaE_setdebt(g, 0);
  833. g->gcrunning = 1;
  834. break;
  835. }
  836. case LUA_GCCOLLECT: {
  837. luaC_fullgc(L, 0);
  838. break;
  839. }
  840. case LUA_GCCOUNT: {
  841. /* GC values are expressed in Kbytes: #bytes/2^10 */
  842. res = cast_int(gettotalbytes(g) >> 10);
  843. break;
  844. }
  845. case LUA_GCCOUNTB: {
  846. res = cast_int(gettotalbytes(g) & 0x3ff);
  847. break;
  848. }
  849. case LUA_GCSTEP: {
  850. l_mem debt = 1; /* =1 to signal that it did an actual step */
  851. int oldrunning = g->gcrunning;
  852. g->gcrunning = 1; /* allow GC to run */
  853. if (data == 0) {
  854. luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
  855. luaC_step(L);
  856. }
  857. else { /* add 'data' to total debt */
  858. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  859. luaE_setdebt(g, debt);
  860. luaC_checkGC(L);
  861. }
  862. g->gcrunning = oldrunning; /* restore previous state */
  863. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  864. res = 1; /* signal it */
  865. break;
  866. }
  867. case LUA_GCSETPAUSE: {
  868. res = g->gcpause;
  869. g->gcpause = data;
  870. break;
  871. }
  872. case LUA_GCSETSTEPMUL: {
  873. res = g->gcstepmul;
  874. if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
  875. g->gcstepmul = data;
  876. break;
  877. }
  878. case LUA_GCISRUNNING: {
  879. res = g->gcrunning;
  880. break;
  881. }
  882. default: res = -1; /* invalid option */
  883. }
  884. lua_unlock(L);
  885. return res;
  886. }
  887. /*
  888. ** miscellaneous functions
  889. */
  890. LUA_API int lua_error (lua_State *L) {
  891. lua_lock(L);
  892. api_checknelems(L, 1);
  893. luaG_errormsg(L);
  894. /* code unreachable; will unlock when control actually leaves the kernel */
  895. return 0; /* to avoid warnings */
  896. }
  897. LUA_API int lua_next (lua_State *L, int idx) {
  898. StkId t;
  899. int more;
  900. lua_lock(L);
  901. t = index2addr(L, idx);
  902. api_check(ttistable(t), "table expected");
  903. more = luaH_next(L, hvalue(t), L->top - 1);
  904. if (more) {
  905. api_incr_top(L);
  906. }
  907. else /* no more elements */
  908. L->top -= 1; /* remove key */
  909. lua_unlock(L);
  910. return more;
  911. }
  912. LUA_API void lua_concat (lua_State *L, int n) {
  913. lua_lock(L);
  914. api_checknelems(L, n);
  915. if (n >= 2) {
  916. luaC_checkGC(L);
  917. luaV_concat(L, n);
  918. }
  919. else if (n == 0) { /* push empty string */
  920. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  921. api_incr_top(L);
  922. }
  923. /* else n == 1; nothing to do */
  924. lua_unlock(L);
  925. }
  926. LUA_API void lua_len (lua_State *L, int idx) {
  927. StkId t;
  928. lua_lock(L);
  929. t = index2addr(L, idx);
  930. luaV_objlen(L, L->top, t);
  931. api_incr_top(L);
  932. lua_unlock(L);
  933. }
  934. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  935. lua_Alloc f;
  936. lua_lock(L);
  937. if (ud) *ud = G(L)->ud;
  938. f = G(L)->frealloc;
  939. lua_unlock(L);
  940. return f;
  941. }
  942. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  943. lua_lock(L);
  944. G(L)->ud = ud;
  945. G(L)->frealloc = f;
  946. lua_unlock(L);
  947. }
  948. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  949. Udata *u;
  950. lua_lock(L);
  951. luaC_checkGC(L);
  952. u = luaS_newudata(L, size);
  953. setuvalue(L, L->top, u);
  954. api_incr_top(L);
  955. lua_unlock(L);
  956. return getudatamem(u);
  957. }
  958. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  959. GCObject **owner, UpVal **uv) {
  960. switch (ttype(fi)) {
  961. case LUA_TCCL: { /* C closure */
  962. CClosure *f = clCvalue(fi);
  963. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  964. *val = &f->upvalue[n-1];
  965. if (owner) *owner = obj2gco(f);
  966. return "";
  967. }
  968. case LUA_TLCL: { /* Lua closure */
  969. LClosure *f = clLvalue(fi);
  970. TString *name;
  971. Proto *p = f->p;
  972. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  973. *val = f->upvals[n-1]->v;
  974. if (uv) *uv = f->upvals[n - 1];
  975. name = p->upvalues[n-1].name;
  976. return (name == NULL) ? "(*no name)" : getstr(name);
  977. }
  978. default: return NULL; /* not a closure */
  979. }
  980. }
  981. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  982. const char *name;
  983. TValue *val = NULL; /* to avoid warnings */
  984. lua_lock(L);
  985. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL);
  986. if (name) {
  987. setobj2s(L, L->top, val);
  988. api_incr_top(L);
  989. }
  990. lua_unlock(L);
  991. return name;
  992. }
  993. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  994. const char *name;
  995. TValue *val = NULL; /* to avoid warnings */
  996. GCObject *owner = NULL;
  997. UpVal *uv = NULL;
  998. StkId fi;
  999. lua_lock(L);
  1000. fi = index2addr(L, funcindex);
  1001. api_checknelems(L, 1);
  1002. name = aux_upvalue(fi, n, &val, &owner, &uv);
  1003. if (name) {
  1004. L->top--;
  1005. setobj(L, val, L->top);
  1006. if (owner) { luaC_barrier(L, owner, L->top); }
  1007. else if (uv) { luaC_upvalbarrier(L, uv); }
  1008. }
  1009. lua_unlock(L);
  1010. return name;
  1011. }
  1012. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1013. LClosure *f;
  1014. StkId fi = index2addr(L, fidx);
  1015. api_check(ttisLclosure(fi), "Lua function expected");
  1016. f = clLvalue(fi);
  1017. api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1018. if (pf) *pf = f;
  1019. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1020. }
  1021. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1022. StkId fi = index2addr(L, fidx);
  1023. switch (ttype(fi)) {
  1024. case LUA_TLCL: { /* lua closure */
  1025. return *getupvalref(L, fidx, n, NULL);
  1026. }
  1027. case LUA_TCCL: { /* C closure */
  1028. CClosure *f = clCvalue(fi);
  1029. api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1030. return &f->upvalue[n - 1];
  1031. }
  1032. default: {
  1033. api_check(0, "closure expected");
  1034. return NULL;
  1035. }
  1036. }
  1037. }
  1038. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1039. int fidx2, int n2) {
  1040. LClosure *f1;
  1041. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1042. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1043. luaC_upvdeccount(L, *up1);
  1044. *up1 = *up2;
  1045. (*up1)->refcount++;
  1046. if (upisopen(*up1)) (*up1)->u.open.touched = 1;
  1047. luaC_upvalbarrier(L, *up1);
  1048. }