lapi.c 20 KB

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