lapi.c 29 KB

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