lapi.c 29 KB

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