lapi.c 29 KB

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