選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1285 行
29 KiB

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