lapi.c 30 KB

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