lapi.c 29 KB

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