lapi.c 19 KB

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