/src/middleware/lua/lapi.c

https://bitbucket.org/vivkin/gam3b00bs/ · C · 1087 lines · 853 code · 196 blank · 38 comment · 109 complexity · 2751d5c9103e098a6eb4850009a6a205 MD5 · raw file

  1. /*
  2. ** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <assert.h>
  7. #include <math.h>
  8. #include <stdarg.h>
  9. #include <string.h>
  10. #define lapi_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lfunc.h"
  17. #include "lgc.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "ltm.h"
  24. #include "lundump.h"
  25. #include "lvm.h"
  26. const char lua_ident[] =
  27. "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
  28. "$Authors: " LUA_AUTHORS " $\n"
  29. "$URL: www.lua.org $\n";
  30. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
  31. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
  32. #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
  33. static TValue *index2adr (lua_State *L, int idx) {
  34. if (idx > 0) {
  35. TValue *o = L->base + (idx - 1);
  36. api_check(L, idx <= L->ci->top - L->base);
  37. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  38. else return o;
  39. }
  40. else if (idx > LUA_REGISTRYINDEX) {
  41. api_check(L, idx != 0 && -idx <= L->top - L->base);
  42. return L->top + idx;
  43. }
  44. else switch (idx) { /* pseudo-indices */
  45. case LUA_REGISTRYINDEX: return registry(L);
  46. case LUA_ENVIRONINDEX: {
  47. Closure *func = curr_func(L);
  48. sethvalue(L, &L->env, func->c.env);
  49. return &L->env;
  50. }
  51. case LUA_GLOBALSINDEX: return gt(L);
  52. default: {
  53. Closure *func = curr_func(L);
  54. idx = LUA_GLOBALSINDEX - idx;
  55. return (idx <= func->c.nupvalues)
  56. ? &func->c.upvalue[idx-1]
  57. : cast(TValue *, luaO_nilobject);
  58. }
  59. }
  60. }
  61. static Table *getcurrenv (lua_State *L) {
  62. if (L->ci == L->base_ci) /* no enclosing function? */
  63. return hvalue(gt(L)); /* use global table as environment */
  64. else {
  65. Closure *func = curr_func(L);
  66. return func->c.env;
  67. }
  68. }
  69. void luaA_pushobject (lua_State *L, const TValue *o) {
  70. setobj2s(L, L->top, o);
  71. api_incr_top(L);
  72. }
  73. LUA_API int lua_checkstack (lua_State *L, int size) {
  74. int res = 1;
  75. lua_lock(L);
  76. if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
  77. res = 0; /* stack overflow */
  78. else if (size > 0) {
  79. luaD_checkstack(L, size);
  80. if (L->ci->top < L->top + size)
  81. L->ci->top = L->top + size;
  82. }
  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));
  92. api_check(from, to->ci->top - to->top >= n);
  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 void lua_setlevel (lua_State *from, lua_State *to) {
  100. to->nCcalls = from->nCcalls;
  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 lua_State *lua_newthread (lua_State *L) {
  111. lua_State *L1;
  112. lua_lock(L);
  113. luaC_checkGC(L);
  114. L1 = luaE_newthread(L);
  115. setthvalue(L, L->top, L1);
  116. api_incr_top(L);
  117. lua_unlock(L);
  118. luai_userstatethread(L, L1);
  119. return L1;
  120. }
  121. /*
  122. ** basic stack manipulation
  123. */
  124. LUA_API int lua_gettop (lua_State *L) {
  125. return cast_int(L->top - L->base);
  126. }
  127. LUA_API void lua_settop (lua_State *L, int idx) {
  128. lua_lock(L);
  129. if (idx >= 0) {
  130. api_check(L, idx <= L->stack_last - L->base);
  131. while (L->top < L->base + idx)
  132. setnilvalue(L->top++);
  133. L->top = L->base + idx;
  134. }
  135. else {
  136. api_check(L, -(idx+1) <= (L->top - L->base));
  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 = index2adr(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 = index2adr(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. LUA_API void lua_replace (lua_State *L, int idx) {
  161. StkId o;
  162. lua_lock(L);
  163. /* explicit test for incompatible code */
  164. if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
  165. luaG_runerror(L, "no calling environment");
  166. api_checknelems(L, 1);
  167. o = index2adr(L, idx);
  168. api_checkvalidindex(L, o);
  169. if (idx == LUA_ENVIRONINDEX) {
  170. Closure *func = curr_func(L);
  171. api_check(L, ttistable(L->top - 1));
  172. func->c.env = hvalue(L->top - 1);
  173. luaC_barrier(L, func, L->top - 1);
  174. }
  175. else {
  176. setobj(L, o, L->top - 1);
  177. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  178. luaC_barrier(L, curr_func(L), L->top - 1);
  179. }
  180. L->top--;
  181. lua_unlock(L);
  182. }
  183. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  184. lua_lock(L);
  185. setobj2s(L, L->top, index2adr(L, idx));
  186. api_incr_top(L);
  187. lua_unlock(L);
  188. }
  189. /*
  190. ** access functions (stack -> C)
  191. */
  192. LUA_API int lua_type (lua_State *L, int idx) {
  193. StkId o = index2adr(L, idx);
  194. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  195. }
  196. LUA_API const char *lua_typename (lua_State *L, int t) {
  197. UNUSED(L);
  198. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  199. }
  200. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  201. StkId o = index2adr(L, idx);
  202. return iscfunction(o);
  203. }
  204. LUA_API int lua_isnumber (lua_State *L, int idx) {
  205. TValue n;
  206. const TValue *o = index2adr(L, idx);
  207. return tonumber(o, &n);
  208. }
  209. LUA_API int lua_isstring (lua_State *L, int idx) {
  210. int t = lua_type(L, idx);
  211. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  212. }
  213. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  214. const TValue *o = index2adr(L, idx);
  215. return (ttisuserdata(o) || ttislightuserdata(o));
  216. }
  217. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  218. StkId o1 = index2adr(L, index1);
  219. StkId o2 = index2adr(L, index2);
  220. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  221. : luaO_rawequalObj(o1, o2);
  222. }
  223. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  224. StkId o1, o2;
  225. int i;
  226. lua_lock(L); /* may call tag method */
  227. o1 = index2adr(L, index1);
  228. o2 = index2adr(L, index2);
  229. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
  230. lua_unlock(L);
  231. return i;
  232. }
  233. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  234. StkId o1, o2;
  235. int i;
  236. lua_lock(L); /* may call tag method */
  237. o1 = index2adr(L, index1);
  238. o2 = index2adr(L, index2);
  239. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  240. : luaV_lessthan(L, o1, o2);
  241. lua_unlock(L);
  242. return i;
  243. }
  244. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  245. TValue n;
  246. const TValue *o = index2adr(L, idx);
  247. if (tonumber(o, &n))
  248. return nvalue(o);
  249. else
  250. return 0;
  251. }
  252. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  253. TValue n;
  254. const TValue *o = index2adr(L, idx);
  255. if (tonumber(o, &n)) {
  256. lua_Integer res;
  257. lua_Number num = nvalue(o);
  258. lua_number2integer(res, num);
  259. return res;
  260. }
  261. else
  262. return 0;
  263. }
  264. LUA_API int lua_toboolean (lua_State *L, int idx) {
  265. const TValue *o = index2adr(L, idx);
  266. return !l_isfalse(o);
  267. }
  268. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  269. StkId o = index2adr(L, idx);
  270. if (!ttisstring(o)) {
  271. lua_lock(L); /* `luaV_tostring' may create a new string */
  272. if (!luaV_tostring(L, o)) { /* conversion failed? */
  273. if (len != NULL) *len = 0;
  274. lua_unlock(L);
  275. return NULL;
  276. }
  277. luaC_checkGC(L);
  278. o = index2adr(L, idx); /* previous call may reallocate the stack */
  279. lua_unlock(L);
  280. }
  281. if (len != NULL) *len = tsvalue(o)->len;
  282. return svalue(o);
  283. }
  284. LUA_API size_t lua_objlen (lua_State *L, int idx) {
  285. StkId o = index2adr(L, idx);
  286. switch (ttype(o)) {
  287. case LUA_TSTRING: return tsvalue(o)->len;
  288. case LUA_TUSERDATA: return uvalue(o)->len;
  289. case LUA_TTABLE: return luaH_getn(hvalue(o));
  290. case LUA_TNUMBER: {
  291. size_t l;
  292. lua_lock(L); /* `luaV_tostring' may create a new string */
  293. l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
  294. lua_unlock(L);
  295. return l;
  296. }
  297. default: return 0;
  298. }
  299. }
  300. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  301. StkId o = index2adr(L, idx);
  302. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  303. }
  304. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  305. StkId o = index2adr(L, idx);
  306. switch (ttype(o)) {
  307. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  308. case LUA_TLIGHTUSERDATA: return pvalue(o);
  309. default: return NULL;
  310. }
  311. }
  312. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  313. StkId o = index2adr(L, idx);
  314. return (!ttisthread(o)) ? NULL : thvalue(o);
  315. }
  316. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  317. StkId o = index2adr(L, idx);
  318. switch (ttype(o)) {
  319. case LUA_TTABLE: return hvalue(o);
  320. case LUA_TFUNCTION: return clvalue(o);
  321. case LUA_TTHREAD: return thvalue(o);
  322. case LUA_TUSERDATA:
  323. case LUA_TLIGHTUSERDATA:
  324. return lua_touserdata(L, idx);
  325. default: return NULL;
  326. }
  327. }
  328. /*
  329. ** push functions (C -> stack)
  330. */
  331. LUA_API void lua_pushnil (lua_State *L) {
  332. lua_lock(L);
  333. setnilvalue(L->top);
  334. api_incr_top(L);
  335. lua_unlock(L);
  336. }
  337. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  338. lua_lock(L);
  339. setnvalue(L->top, n);
  340. api_incr_top(L);
  341. lua_unlock(L);
  342. }
  343. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  344. lua_lock(L);
  345. setnvalue(L->top, cast_num(n));
  346. api_incr_top(L);
  347. lua_unlock(L);
  348. }
  349. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  350. lua_lock(L);
  351. luaC_checkGC(L);
  352. setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
  353. api_incr_top(L);
  354. lua_unlock(L);
  355. }
  356. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  357. if (s == NULL)
  358. lua_pushnil(L);
  359. else
  360. lua_pushlstring(L, s, strlen(s));
  361. }
  362. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  363. va_list argp) {
  364. const char *ret;
  365. lua_lock(L);
  366. luaC_checkGC(L);
  367. ret = luaO_pushvfstring(L, fmt, argp);
  368. lua_unlock(L);
  369. return ret;
  370. }
  371. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  372. const char *ret;
  373. va_list argp;
  374. lua_lock(L);
  375. luaC_checkGC(L);
  376. va_start(argp, fmt);
  377. ret = luaO_pushvfstring(L, fmt, argp);
  378. va_end(argp);
  379. lua_unlock(L);
  380. return ret;
  381. }
  382. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  383. Closure *cl;
  384. lua_lock(L);
  385. luaC_checkGC(L);
  386. api_checknelems(L, n);
  387. cl = luaF_newCclosure(L, n, getcurrenv(L));
  388. cl->c.f = fn;
  389. L->top -= n;
  390. while (n--)
  391. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  392. setclvalue(L, L->top, cl);
  393. lua_assert(iswhite(obj2gco(cl)));
  394. api_incr_top(L);
  395. lua_unlock(L);
  396. }
  397. LUA_API void lua_pushboolean (lua_State *L, int b) {
  398. lua_lock(L);
  399. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  400. api_incr_top(L);
  401. lua_unlock(L);
  402. }
  403. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  404. lua_lock(L);
  405. setpvalue(L->top, p);
  406. api_incr_top(L);
  407. lua_unlock(L);
  408. }
  409. LUA_API int lua_pushthread (lua_State *L) {
  410. lua_lock(L);
  411. setthvalue(L, L->top, L);
  412. api_incr_top(L);
  413. lua_unlock(L);
  414. return (G(L)->mainthread == L);
  415. }
  416. /*
  417. ** get functions (Lua -> stack)
  418. */
  419. LUA_API void lua_gettable (lua_State *L, int idx) {
  420. StkId t;
  421. lua_lock(L);
  422. t = index2adr(L, idx);
  423. api_checkvalidindex(L, t);
  424. luaV_gettable(L, t, L->top - 1, L->top - 1);
  425. lua_unlock(L);
  426. }
  427. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  428. StkId t;
  429. TValue key;
  430. lua_lock(L);
  431. t = index2adr(L, idx);
  432. api_checkvalidindex(L, t);
  433. setsvalue(L, &key, luaS_new(L, k));
  434. luaV_gettable(L, t, &key, L->top);
  435. api_incr_top(L);
  436. lua_unlock(L);
  437. }
  438. LUA_API void lua_rawget (lua_State *L, int idx) {
  439. StkId t;
  440. lua_lock(L);
  441. t = index2adr(L, idx);
  442. api_check(L, ttistable(t));
  443. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  444. lua_unlock(L);
  445. }
  446. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  447. StkId o;
  448. lua_lock(L);
  449. o = index2adr(L, idx);
  450. api_check(L, ttistable(o));
  451. setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  452. api_incr_top(L);
  453. lua_unlock(L);
  454. }
  455. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  456. lua_lock(L);
  457. luaC_checkGC(L);
  458. sethvalue(L, L->top, luaH_new(L, narray, nrec));
  459. api_incr_top(L);
  460. lua_unlock(L);
  461. }
  462. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  463. const TValue *obj;
  464. Table *mt = NULL;
  465. int res;
  466. lua_lock(L);
  467. obj = index2adr(L, objindex);
  468. switch (ttype(obj)) {
  469. case LUA_TTABLE:
  470. mt = hvalue(obj)->metatable;
  471. break;
  472. case LUA_TUSERDATA:
  473. mt = uvalue(obj)->metatable;
  474. break;
  475. default:
  476. mt = G(L)->mt[ttype(obj)];
  477. break;
  478. }
  479. if (mt == NULL)
  480. res = 0;
  481. else {
  482. sethvalue(L, L->top, mt);
  483. api_incr_top(L);
  484. res = 1;
  485. }
  486. lua_unlock(L);
  487. return res;
  488. }
  489. LUA_API void lua_getfenv (lua_State *L, int idx) {
  490. StkId o;
  491. lua_lock(L);
  492. o = index2adr(L, idx);
  493. api_checkvalidindex(L, o);
  494. switch (ttype(o)) {
  495. case LUA_TFUNCTION:
  496. sethvalue(L, L->top, clvalue(o)->c.env);
  497. break;
  498. case LUA_TUSERDATA:
  499. sethvalue(L, L->top, uvalue(o)->env);
  500. break;
  501. case LUA_TTHREAD:
  502. setobj2s(L, L->top, gt(thvalue(o)));
  503. break;
  504. default:
  505. setnilvalue(L->top);
  506. break;
  507. }
  508. api_incr_top(L);
  509. lua_unlock(L);
  510. }
  511. /*
  512. ** set functions (stack -> Lua)
  513. */
  514. LUA_API void lua_settable (lua_State *L, int idx) {
  515. StkId t;
  516. lua_lock(L);
  517. api_checknelems(L, 2);
  518. t = index2adr(L, idx);
  519. api_checkvalidindex(L, t);
  520. luaV_settable(L, t, L->top - 2, L->top - 1);
  521. L->top -= 2; /* pop index and value */
  522. lua_unlock(L);
  523. }
  524. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  525. StkId t;
  526. TValue key;
  527. lua_lock(L);
  528. api_checknelems(L, 1);
  529. t = index2adr(L, idx);
  530. api_checkvalidindex(L, t);
  531. setsvalue(L, &key, luaS_new(L, k));
  532. luaV_settable(L, t, &key, L->top - 1);
  533. L->top--; /* pop value */
  534. lua_unlock(L);
  535. }
  536. LUA_API void lua_rawset (lua_State *L, int idx) {
  537. StkId t;
  538. lua_lock(L);
  539. api_checknelems(L, 2);
  540. t = index2adr(L, idx);
  541. api_check(L, ttistable(t));
  542. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  543. luaC_barriert(L, hvalue(t), L->top-1);
  544. L->top -= 2;
  545. lua_unlock(L);
  546. }
  547. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  548. StkId o;
  549. lua_lock(L);
  550. api_checknelems(L, 1);
  551. o = index2adr(L, idx);
  552. api_check(L, ttistable(o));
  553. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  554. luaC_barriert(L, hvalue(o), L->top-1);
  555. L->top--;
  556. lua_unlock(L);
  557. }
  558. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  559. TValue *obj;
  560. Table *mt;
  561. lua_lock(L);
  562. api_checknelems(L, 1);
  563. obj = index2adr(L, objindex);
  564. api_checkvalidindex(L, obj);
  565. if (ttisnil(L->top - 1))
  566. mt = NULL;
  567. else {
  568. api_check(L, ttistable(L->top - 1));
  569. mt = hvalue(L->top - 1);
  570. }
  571. switch (ttype(obj)) {
  572. case LUA_TTABLE: {
  573. hvalue(obj)->metatable = mt;
  574. if (mt)
  575. luaC_objbarriert(L, hvalue(obj), mt);
  576. break;
  577. }
  578. case LUA_TUSERDATA: {
  579. uvalue(obj)->metatable = mt;
  580. if (mt)
  581. luaC_objbarrier(L, rawuvalue(obj), mt);
  582. break;
  583. }
  584. default: {
  585. G(L)->mt[ttype(obj)] = mt;
  586. break;
  587. }
  588. }
  589. L->top--;
  590. lua_unlock(L);
  591. return 1;
  592. }
  593. LUA_API int lua_setfenv (lua_State *L, int idx) {
  594. StkId o;
  595. int res = 1;
  596. lua_lock(L);
  597. api_checknelems(L, 1);
  598. o = index2adr(L, idx);
  599. api_checkvalidindex(L, o);
  600. api_check(L, ttistable(L->top - 1));
  601. switch (ttype(o)) {
  602. case LUA_TFUNCTION:
  603. clvalue(o)->c.env = hvalue(L->top - 1);
  604. break;
  605. case LUA_TUSERDATA:
  606. uvalue(o)->env = hvalue(L->top - 1);
  607. break;
  608. case LUA_TTHREAD:
  609. sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
  610. break;
  611. default:
  612. res = 0;
  613. break;
  614. }
  615. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  616. L->top--;
  617. lua_unlock(L);
  618. return res;
  619. }
  620. /*
  621. ** `load' and `call' functions (run Lua code)
  622. */
  623. #define adjustresults(L,nres) \
  624. { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
  625. #define checkresults(L,na,nr) \
  626. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
  627. LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
  628. StkId func;
  629. lua_lock(L);
  630. api_checknelems(L, nargs+1);
  631. checkresults(L, nargs, nresults);
  632. func = L->top - (nargs+1);
  633. luaD_call(L, func, nresults);
  634. adjustresults(L, nresults);
  635. lua_unlock(L);
  636. }
  637. /*
  638. ** Execute a protected call.
  639. */
  640. struct CallS { /* data to `f_call' */
  641. StkId func;
  642. int nresults;
  643. };
  644. static void f_call (lua_State *L, void *ud) {
  645. struct CallS *c = cast(struct CallS *, ud);
  646. luaD_call(L, c->func, c->nresults);
  647. }
  648. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
  649. struct CallS c;
  650. int status;
  651. ptrdiff_t func;
  652. lua_lock(L);
  653. api_checknelems(L, nargs+1);
  654. checkresults(L, nargs, nresults);
  655. if (errfunc == 0)
  656. func = 0;
  657. else {
  658. StkId o = index2adr(L, errfunc);
  659. api_checkvalidindex(L, o);
  660. func = savestack(L, o);
  661. }
  662. c.func = L->top - (nargs+1); /* function to be called */
  663. c.nresults = nresults;
  664. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  665. adjustresults(L, nresults);
  666. lua_unlock(L);
  667. return status;
  668. }
  669. /*
  670. ** Execute a protected C call.
  671. */
  672. struct CCallS { /* data to `f_Ccall' */
  673. lua_CFunction func;
  674. void *ud;
  675. };
  676. static void f_Ccall (lua_State *L, void *ud) {
  677. struct CCallS *c = cast(struct CCallS *, ud);
  678. Closure *cl;
  679. cl = luaF_newCclosure(L, 0, getcurrenv(L));
  680. cl->c.f = c->func;
  681. setclvalue(L, L->top, cl); /* push function */
  682. api_incr_top(L);
  683. setpvalue(L->top, c->ud); /* push only argument */
  684. api_incr_top(L);
  685. luaD_call(L, L->top - 2, 0);
  686. }
  687. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  688. struct CCallS c;
  689. int status;
  690. lua_lock(L);
  691. c.func = func;
  692. c.ud = ud;
  693. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  694. lua_unlock(L);
  695. return status;
  696. }
  697. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  698. const char *chunkname) {
  699. ZIO z;
  700. int status;
  701. lua_lock(L);
  702. if (!chunkname) chunkname = "?";
  703. luaZ_init(L, &z, reader, data);
  704. status = luaD_protectedparser(L, &z, chunkname);
  705. lua_unlock(L);
  706. return status;
  707. }
  708. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  709. int status;
  710. TValue *o;
  711. lua_lock(L);
  712. api_checknelems(L, 1);
  713. o = L->top - 1;
  714. if (isLfunction(o))
  715. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  716. else
  717. status = 1;
  718. lua_unlock(L);
  719. return status;
  720. }
  721. LUA_API int lua_status (lua_State *L) {
  722. return L->status;
  723. }
  724. /*
  725. ** Garbage-collection function
  726. */
  727. LUA_API int lua_gc (lua_State *L, int what, int data) {
  728. int res = 0;
  729. global_State *g;
  730. lua_lock(L);
  731. g = G(L);
  732. switch (what) {
  733. case LUA_GCSTOP: {
  734. g->GCthreshold = MAX_LUMEM;
  735. break;
  736. }
  737. case LUA_GCRESTART: {
  738. g->GCthreshold = g->totalbytes;
  739. break;
  740. }
  741. case LUA_GCCOLLECT: {
  742. luaC_fullgc(L);
  743. break;
  744. }
  745. case LUA_GCCOUNT: {
  746. /* GC values are expressed in Kbytes: #bytes/2^10 */
  747. res = cast_int(g->totalbytes >> 10);
  748. break;
  749. }
  750. case LUA_GCCOUNTB: {
  751. res = cast_int(g->totalbytes & 0x3ff);
  752. break;
  753. }
  754. case LUA_GCSTEP: {
  755. lu_mem a = (cast(lu_mem, data) << 10);
  756. if (a <= g->totalbytes)
  757. g->GCthreshold = g->totalbytes - a;
  758. else
  759. g->GCthreshold = 0;
  760. while (g->GCthreshold <= g->totalbytes) {
  761. luaC_step(L);
  762. if (g->gcstate == GCSpause) { /* end of cycle? */
  763. res = 1; /* signal it */
  764. break;
  765. }
  766. }
  767. break;
  768. }
  769. case LUA_GCSETPAUSE: {
  770. res = g->gcpause;
  771. g->gcpause = data;
  772. break;
  773. }
  774. case LUA_GCSETSTEPMUL: {
  775. res = g->gcstepmul;
  776. g->gcstepmul = data;
  777. break;
  778. }
  779. default: res = -1; /* invalid option */
  780. }
  781. lua_unlock(L);
  782. return res;
  783. }
  784. /*
  785. ** miscellaneous functions
  786. */
  787. LUA_API int lua_error (lua_State *L) {
  788. lua_lock(L);
  789. api_checknelems(L, 1);
  790. luaG_errormsg(L);
  791. lua_unlock(L);
  792. return 0; /* to avoid warnings */
  793. }
  794. LUA_API int lua_next (lua_State *L, int idx) {
  795. StkId t;
  796. int more;
  797. lua_lock(L);
  798. t = index2adr(L, idx);
  799. api_check(L, ttistable(t));
  800. more = luaH_next(L, hvalue(t), L->top - 1);
  801. if (more) {
  802. api_incr_top(L);
  803. }
  804. else /* no more elements */
  805. L->top -= 1; /* remove key */
  806. lua_unlock(L);
  807. return more;
  808. }
  809. LUA_API void lua_concat (lua_State *L, int n) {
  810. lua_lock(L);
  811. api_checknelems(L, n);
  812. if (n >= 2) {
  813. luaC_checkGC(L);
  814. luaV_concat(L, n, cast_int(L->top - L->base) - 1);
  815. L->top -= (n-1);
  816. }
  817. else if (n == 0) { /* push empty string */
  818. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  819. api_incr_top(L);
  820. }
  821. /* else n == 1; nothing to do */
  822. lua_unlock(L);
  823. }
  824. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  825. lua_Alloc f;
  826. lua_lock(L);
  827. if (ud) *ud = G(L)->ud;
  828. f = G(L)->frealloc;
  829. lua_unlock(L);
  830. return f;
  831. }
  832. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  833. lua_lock(L);
  834. G(L)->ud = ud;
  835. G(L)->frealloc = f;
  836. lua_unlock(L);
  837. }
  838. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  839. Udata *u;
  840. lua_lock(L);
  841. luaC_checkGC(L);
  842. u = luaS_newudata(L, size, getcurrenv(L));
  843. setuvalue(L, L->top, u);
  844. api_incr_top(L);
  845. lua_unlock(L);
  846. return u + 1;
  847. }
  848. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  849. Closure *f;
  850. if (!ttisfunction(fi)) return NULL;
  851. f = clvalue(fi);
  852. if (f->c.isC) {
  853. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  854. *val = &f->c.upvalue[n-1];
  855. return "";
  856. }
  857. else {
  858. Proto *p = f->l.p;
  859. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  860. *val = f->l.upvals[n-1]->v;
  861. return getstr(p->upvalues[n-1]);
  862. }
  863. }
  864. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  865. const char *name;
  866. TValue *val;
  867. lua_lock(L);
  868. name = aux_upvalue(index2adr(L, funcindex), n, &val);
  869. if (name) {
  870. setobj2s(L, L->top, val);
  871. api_incr_top(L);
  872. }
  873. lua_unlock(L);
  874. return name;
  875. }
  876. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  877. const char *name;
  878. TValue *val;
  879. StkId fi;
  880. lua_lock(L);
  881. fi = index2adr(L, funcindex);
  882. api_checknelems(L, 1);
  883. name = aux_upvalue(fi, n, &val);
  884. if (name) {
  885. L->top--;
  886. setobj(L, val, L->top);
  887. luaC_barrier(L, clvalue(fi), L->top);
  888. }
  889. lua_unlock(L);
  890. return name;
  891. }