lapi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. ** $Id: lapi.c,v 2.6 2004/04/05 14:43:17 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_strlen (lua_State *L, int idx) {
  254. StkId o = luaA_index(L, idx);
  255. if (ttisstring(o))
  256. return tsvalue(o)->len;
  257. else {
  258. size_t l;
  259. lua_lock(L); /* `luaV_tostring' may create a new string */
  260. l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
  261. lua_unlock(L);
  262. return l;
  263. }
  264. }
  265. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  266. StkId o = luaA_index(L, idx);
  267. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  268. }
  269. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  270. StkId o = luaA_index(L, idx);
  271. switch (ttype(o)) {
  272. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  273. case LUA_TLIGHTUSERDATA: return pvalue(o);
  274. default: return NULL;
  275. }
  276. }
  277. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  278. StkId o = luaA_index(L, idx);
  279. return (!ttisthread(o)) ? NULL : thvalue(o);
  280. }
  281. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  282. StkId o = luaA_index(L, idx);
  283. switch (ttype(o)) {
  284. case LUA_TTABLE: return hvalue(o);
  285. case LUA_TFUNCTION: return clvalue(o);
  286. case LUA_TTHREAD: return thvalue(o);
  287. case LUA_TUSERDATA:
  288. case LUA_TLIGHTUSERDATA:
  289. return lua_touserdata(L, idx);
  290. default: return NULL;
  291. }
  292. }
  293. /*
  294. ** push functions (C -> stack)
  295. */
  296. LUA_API void lua_pushnil (lua_State *L) {
  297. lua_lock(L);
  298. setnilvalue(L->top);
  299. api_incr_top(L);
  300. lua_unlock(L);
  301. }
  302. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  303. lua_lock(L);
  304. setnvalue(L->top, n);
  305. api_incr_top(L);
  306. lua_unlock(L);
  307. }
  308. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  309. lua_lock(L);
  310. setnvalue(L->top, cast(lua_Number, n));
  311. api_incr_top(L);
  312. lua_unlock(L);
  313. }
  314. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  315. lua_lock(L);
  316. luaC_checkGC(L);
  317. setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
  318. api_incr_top(L);
  319. lua_unlock(L);
  320. }
  321. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  322. if (s == NULL)
  323. lua_pushnil(L);
  324. else
  325. lua_pushlstring(L, s, strlen(s));
  326. }
  327. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  328. va_list argp) {
  329. const char *ret;
  330. lua_lock(L);
  331. luaC_checkGC(L);
  332. ret = luaO_pushvfstring(L, fmt, argp);
  333. lua_unlock(L);
  334. return ret;
  335. }
  336. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  337. const char *ret;
  338. va_list argp;
  339. lua_lock(L);
  340. luaC_checkGC(L);
  341. va_start(argp, fmt);
  342. ret = luaO_pushvfstring(L, fmt, argp);
  343. va_end(argp);
  344. lua_unlock(L);
  345. return ret;
  346. }
  347. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  348. Closure *cl;
  349. lua_lock(L);
  350. luaC_checkGC(L);
  351. api_checknelems(L, n);
  352. cl = luaF_newCclosure(L, n);
  353. cl->c.f = fn;
  354. L->top -= n;
  355. while (n--)
  356. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  357. setclvalue(L, L->top, cl);
  358. lua_assert(iswhite(obj2gco(cl)));
  359. api_incr_top(L);
  360. lua_unlock(L);
  361. }
  362. LUA_API void lua_pushboolean (lua_State *L, int b) {
  363. lua_lock(L);
  364. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  365. api_incr_top(L);
  366. lua_unlock(L);
  367. }
  368. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  369. lua_lock(L);
  370. setpvalue(L->top, p);
  371. api_incr_top(L);
  372. lua_unlock(L);
  373. }
  374. /*
  375. ** get functions (Lua -> stack)
  376. */
  377. LUA_API void lua_gettable (lua_State *L, int idx) {
  378. StkId t;
  379. lua_lock(L);
  380. t = luaA_index(L, idx);
  381. api_checkvalidindex(L, t);
  382. luaV_gettable(L, t, L->top - 1, L->top - 1);
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  386. StkId t;
  387. TValue key;
  388. lua_lock(L);
  389. t = luaA_index(L, idx);
  390. api_checkvalidindex(L, t);
  391. setsvalue(L, &key, luaS_new(L, k));
  392. luaV_gettable(L, t, &key, L->top);
  393. api_incr_top(L);
  394. lua_unlock(L);
  395. }
  396. LUA_API void lua_rawget (lua_State *L, int idx) {
  397. StkId t;
  398. lua_lock(L);
  399. t = luaA_index(L, idx);
  400. api_check(L, ttistable(t));
  401. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  402. lua_unlock(L);
  403. }
  404. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  405. StkId o;
  406. lua_lock(L);
  407. o = luaA_index(L, idx);
  408. api_check(L, ttistable(o));
  409. setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  410. api_incr_top(L);
  411. lua_unlock(L);
  412. }
  413. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  414. lua_lock(L);
  415. luaC_checkGC(L);
  416. sethvalue(L, L->top, luaH_new(L, narray, luaO_log2(nrec) + 1));
  417. api_incr_top(L);
  418. lua_unlock(L);
  419. }
  420. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  421. const TValue *obj;
  422. Table *mt = NULL;
  423. int res;
  424. lua_lock(L);
  425. obj = luaA_index(L, objindex);
  426. switch (ttype(obj)) {
  427. case LUA_TTABLE:
  428. mt = hvalue(obj)->metatable;
  429. break;
  430. case LUA_TUSERDATA:
  431. mt = uvalue(obj)->metatable;
  432. break;
  433. }
  434. if (mt == NULL)
  435. res = 0;
  436. else {
  437. sethvalue(L, L->top, mt);
  438. api_incr_top(L);
  439. res = 1;
  440. }
  441. lua_unlock(L);
  442. return res;
  443. }
  444. LUA_API void lua_getfenv (lua_State *L, int idx) {
  445. StkId o;
  446. lua_lock(L);
  447. o = luaA_index(L, idx);
  448. api_checkvalidindex(L, o);
  449. setobj2s(L, L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
  450. api_incr_top(L);
  451. lua_unlock(L);
  452. }
  453. /*
  454. ** set functions (stack -> Lua)
  455. */
  456. LUA_API void lua_settable (lua_State *L, int idx) {
  457. StkId t;
  458. lua_lock(L);
  459. api_checknelems(L, 2);
  460. t = luaA_index(L, idx);
  461. api_checkvalidindex(L, t);
  462. luaV_settable(L, t, L->top - 2, L->top - 1);
  463. L->top -= 2; /* pop index and value */
  464. lua_unlock(L);
  465. }
  466. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  467. StkId t;
  468. TValue key;
  469. lua_lock(L);
  470. api_checknelems(L, 1);
  471. t = luaA_index(L, idx);
  472. api_checkvalidindex(L, t);
  473. setsvalue(L, &key, luaS_new(L, k));
  474. luaV_settable(L, t, &key, L->top - 1);
  475. L->top--; /* pop value */
  476. lua_unlock(L);
  477. }
  478. LUA_API void lua_rawset (lua_State *L, int idx) {
  479. StkId t;
  480. lua_lock(L);
  481. api_checknelems(L, 2);
  482. t = luaA_index(L, idx);
  483. api_check(L, ttistable(t));
  484. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  485. luaC_barrier(L, hvalue(t), L->top-1);
  486. L->top -= 2;
  487. lua_unlock(L);
  488. }
  489. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  490. StkId o;
  491. lua_lock(L);
  492. api_checknelems(L, 1);
  493. o = luaA_index(L, idx);
  494. api_check(L, ttistable(o));
  495. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  496. luaC_barrier(L, hvalue(o), L->top-1);
  497. L->top--;
  498. lua_unlock(L);
  499. }
  500. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  501. TValue *obj;
  502. Table *mt;
  503. int res = 1;
  504. lua_lock(L);
  505. api_checknelems(L, 1);
  506. obj = luaA_index(L, objindex);
  507. api_checkvalidindex(L, obj);
  508. if (ttisnil(L->top - 1))
  509. mt = NULL;
  510. else {
  511. api_check(L, ttistable(L->top - 1));
  512. mt = hvalue(L->top - 1);
  513. }
  514. switch (ttype(obj)) {
  515. case LUA_TTABLE: {
  516. hvalue(obj)->metatable = mt;
  517. if (mt)
  518. luaC_objbarrier(L, hvalue(obj), mt);
  519. break;
  520. }
  521. case LUA_TUSERDATA: {
  522. uvalue(obj)->metatable = mt;
  523. if (mt)
  524. luaC_objbarrier(L, rawuvalue(obj), mt);
  525. break;
  526. }
  527. default: {
  528. res = 0; /* cannot set */
  529. break;
  530. }
  531. }
  532. L->top--;
  533. lua_unlock(L);
  534. return res;
  535. }
  536. LUA_API int lua_setfenv (lua_State *L, int idx) {
  537. StkId o;
  538. int res = 0;
  539. lua_lock(L);
  540. api_checknelems(L, 1);
  541. o = luaA_index(L, idx);
  542. api_checkvalidindex(L, o);
  543. L->top--;
  544. api_check(L, ttistable(L->top));
  545. if (isLfunction(o)) {
  546. res = 1;
  547. clvalue(o)->l.g = *(L->top);
  548. }
  549. lua_unlock(L);
  550. return res;
  551. }
  552. /*
  553. ** `load' and `call' functions (run Lua code)
  554. */
  555. #define adjuststack(L,nres) \
  556. { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
  557. LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
  558. StkId func;
  559. lua_lock(L);
  560. api_checknelems(L, nargs+1);
  561. func = L->top - (nargs+1);
  562. luaD_call(L, func, nresults);
  563. adjuststack(L, nresults);
  564. lua_unlock(L);
  565. }
  566. /*
  567. ** Execute a protected call.
  568. */
  569. struct CallS { /* data to `f_call' */
  570. StkId func;
  571. int nresults;
  572. };
  573. static void f_call (lua_State *L, void *ud) {
  574. struct CallS *c = cast(struct CallS *, ud);
  575. luaD_call(L, c->func, c->nresults);
  576. }
  577. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
  578. struct CallS c;
  579. int status;
  580. ptrdiff_t func;
  581. lua_lock(L);
  582. if (errfunc == 0)
  583. func = 0;
  584. else {
  585. StkId o = luaA_index(L, errfunc);
  586. api_checkvalidindex(L, o);
  587. func = savestack(L, o);
  588. }
  589. c.func = L->top - (nargs+1); /* function to be called */
  590. c.nresults = nresults;
  591. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  592. adjuststack(L, nresults);
  593. lua_unlock(L);
  594. return status;
  595. }
  596. /*
  597. ** Execute a protected C call.
  598. */
  599. struct CCallS { /* data to `f_Ccall' */
  600. lua_CFunction func;
  601. void *ud;
  602. };
  603. static void f_Ccall (lua_State *L, void *ud) {
  604. struct CCallS *c = cast(struct CCallS *, ud);
  605. Closure *cl;
  606. cl = luaF_newCclosure(L, 0);
  607. cl->c.f = c->func;
  608. setclvalue(L, L->top, cl); /* push function */
  609. incr_top(L);
  610. setpvalue(L->top, c->ud); /* push only argument */
  611. incr_top(L);
  612. luaD_call(L, L->top - 2, 0);
  613. }
  614. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  615. struct CCallS c;
  616. int status;
  617. lua_lock(L);
  618. c.func = func;
  619. c.ud = ud;
  620. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  621. lua_unlock(L);
  622. return status;
  623. }
  624. LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
  625. const char *chunkname) {
  626. ZIO z;
  627. int status;
  628. lua_lock(L);
  629. if (!chunkname) chunkname = "?";
  630. luaZ_init(L, &z, reader, data);
  631. status = luaD_protectedparser(L, &z, chunkname);
  632. lua_unlock(L);
  633. return status;
  634. }
  635. LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
  636. int status;
  637. TValue *o;
  638. lua_lock(L);
  639. api_checknelems(L, 1);
  640. o = L->top - 1;
  641. if (isLfunction(o))
  642. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  643. else
  644. status = 0;
  645. lua_unlock(L);
  646. return status;
  647. }
  648. /*
  649. ** Garbage-collection function
  650. */
  651. LUA_API int lua_gc (lua_State *L, int what, int data) {
  652. global_State *g = G(L);
  653. switch (what) {
  654. case LUA_GCSTOP: {
  655. g->GCthreshold = MAXLMEM;
  656. return 0;
  657. }
  658. case LUA_GCRESTART: {
  659. g->GCthreshold = g->nblocks;
  660. return 0;
  661. }
  662. case LUA_GCCOLLECT: {
  663. lua_lock(L);
  664. luaC_fullgc(L);
  665. lua_unlock(L);
  666. return 0;
  667. }
  668. case LUA_GCCOUNT: {
  669. /* GC values are expressed in Kbytes: #bytes/2^10 */
  670. return cast(int, g->nblocks >> 10);
  671. }
  672. default: return -1; /* invalid option */
  673. }
  674. }
  675. /*
  676. ** miscellaneous functions
  677. */
  678. LUA_API const char *lua_version (void) {
  679. return LUA_VERSION;
  680. }
  681. LUA_API int lua_error (lua_State *L) {
  682. lua_lock(L);
  683. api_checknelems(L, 1);
  684. luaG_errormsg(L);
  685. lua_unlock(L);
  686. return 0; /* to avoid warnings */
  687. }
  688. LUA_API int lua_next (lua_State *L, int idx) {
  689. StkId t;
  690. int more;
  691. lua_lock(L);
  692. t = luaA_index(L, idx);
  693. api_check(L, ttistable(t));
  694. more = luaH_next(L, hvalue(t), L->top - 1);
  695. if (more) {
  696. api_incr_top(L);
  697. }
  698. else /* no more elements */
  699. L->top -= 1; /* remove key */
  700. lua_unlock(L);
  701. return more;
  702. }
  703. LUA_API void lua_concat (lua_State *L, int n) {
  704. lua_lock(L);
  705. luaC_checkGC(L);
  706. api_checknelems(L, n);
  707. if (n >= 2) {
  708. luaV_concat(L, n, L->top - L->base - 1);
  709. L->top -= (n-1);
  710. }
  711. else if (n == 0) { /* push empty string */
  712. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  713. api_incr_top(L);
  714. }
  715. /* else n == 1; nothing to do */
  716. lua_unlock(L);
  717. }
  718. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  719. *ud = G(L)->ud;
  720. return G(L)->realloc;
  721. }
  722. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  723. Udata *u;
  724. lua_lock(L);
  725. luaC_checkGC(L);
  726. u = luaS_newudata(L, size);
  727. setuvalue(L, L->top, u);
  728. api_incr_top(L);
  729. lua_unlock(L);
  730. return u + 1;
  731. }
  732. static const char *aux_upvalue (lua_State *L, StkId fi, int n, TValue **val) {
  733. Closure *f;
  734. if (!ttisfunction(fi)) return NULL;
  735. f = clvalue(fi);
  736. if (f->c.isC) {
  737. if (n > f->c.nupvalues) return NULL;
  738. *val = &f->c.upvalue[n-1];
  739. return "";
  740. }
  741. else {
  742. Proto *p = f->l.p;
  743. if (n > p->sizeupvalues) return NULL;
  744. *val = f->l.upvals[n-1]->v;
  745. return getstr(p->upvalues[n-1]);
  746. }
  747. }
  748. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  749. const char *name;
  750. TValue *val;
  751. lua_lock(L);
  752. name = aux_upvalue(L, luaA_index(L, funcindex), n, &val);
  753. if (name) {
  754. setobj2s(L, L->top, val);
  755. api_incr_top(L);
  756. }
  757. lua_unlock(L);
  758. return name;
  759. }
  760. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  761. const char *name;
  762. TValue *val;
  763. StkId fi;
  764. lua_lock(L);
  765. fi = luaA_index(L, funcindex);
  766. api_checknelems(L, 1);
  767. name = aux_upvalue(L, fi, n, &val);
  768. if (name) {
  769. L->top--;
  770. setobj(L, val, L->top);
  771. luaC_barrier(L, clvalue(fi), L->top);
  772. }
  773. lua_unlock(L);
  774. return name;
  775. }