lapi.c 19 KB

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