lapi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. ** $Id: lapi.c,v 1.250 2003/12/01 18:22:56 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) /*{ 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 TObject *luaA_index (lua_State *L, int idx) {
  38. if (idx > 0) {
  39. TObject *o = L->base + (idx - 1);
  40. api_check(L, idx <= L->stack_last - L->base);
  41. if (o >= L->top) return cast(TObject *, &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. TObject *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(TObject *, &luaO_nilobject);
  58. }
  59. }
  60. }
  61. void luaA_pushobject (lua_State *L, const TObject *o) {
  62. setobj2s(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->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->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(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(q, q-1);
  146. setobjs2s(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(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->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. TObject n;
  182. const TObject *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 TObject *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. TObject n;
  223. const TObject *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. TObject n;
  231. const TObject *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 TObject *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)->tsv.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)->tsv.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 (uvalue(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->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(&cl->c.upvalue[n], L->top+n);
  361. setclvalue(L->top, cl);
  362. api_incr_top(L);
  363. lua_unlock(L);
  364. }
  365. LUA_API void lua_pushboolean (lua_State *L, int b) {
  366. lua_lock(L);
  367. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  368. api_incr_top(L);
  369. lua_unlock(L);
  370. }
  371. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  372. lua_lock(L);
  373. setpvalue(L->top, p);
  374. api_incr_top(L);
  375. lua_unlock(L);
  376. }
  377. /*
  378. ** get functions (Lua -> stack)
  379. */
  380. LUA_API void lua_gettable (lua_State *L, int idx) {
  381. StkId t;
  382. lua_lock(L);
  383. t = luaA_index(L, idx);
  384. api_checkvalidindex(L, t);
  385. luaV_gettable(L, t, L->top - 1, L->top - 1);
  386. lua_unlock(L);
  387. }
  388. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  389. StkId t;
  390. TObject key;
  391. lua_lock(L);
  392. t = luaA_index(L, idx);
  393. api_checkvalidindex(L, t);
  394. setsvalue(&key, luaS_new(L, k));
  395. luaV_gettable(L, t, &key, L->top);
  396. api_incr_top(L);
  397. lua_unlock(L);
  398. }
  399. LUA_API void lua_rawget (lua_State *L, int idx) {
  400. StkId t;
  401. lua_lock(L);
  402. t = luaA_index(L, idx);
  403. api_check(L, ttistable(t));
  404. setobj2s(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  405. lua_unlock(L);
  406. }
  407. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  408. StkId o;
  409. lua_lock(L);
  410. o = luaA_index(L, idx);
  411. api_check(L, ttistable(o));
  412. setobj2s(L->top, luaH_getnum(hvalue(o), n));
  413. api_incr_top(L);
  414. lua_unlock(L);
  415. }
  416. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  417. lua_lock(L);
  418. luaC_checkGC(L);
  419. sethvalue(L->top, luaH_new(L, narray, luaO_log2(nrec) + 1));
  420. api_incr_top(L);
  421. lua_unlock(L);
  422. }
  423. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  424. const TObject *obj;
  425. Table *mt = NULL;
  426. int res;
  427. lua_lock(L);
  428. obj = luaA_index(L, objindex);
  429. switch (ttype(obj)) {
  430. case LUA_TTABLE:
  431. mt = hvalue(obj)->metatable;
  432. break;
  433. case LUA_TUSERDATA:
  434. mt = uvalue(obj)->uv.metatable;
  435. break;
  436. }
  437. if (mt == NULL)
  438. res = 0;
  439. else {
  440. sethvalue(L->top, mt);
  441. api_incr_top(L);
  442. res = 1;
  443. }
  444. lua_unlock(L);
  445. return res;
  446. }
  447. LUA_API void lua_getfenv (lua_State *L, int idx) {
  448. StkId o;
  449. lua_lock(L);
  450. o = luaA_index(L, idx);
  451. api_checkvalidindex(L, o);
  452. setobj2s(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
  453. api_incr_top(L);
  454. lua_unlock(L);
  455. }
  456. /*
  457. ** set functions (stack -> Lua)
  458. */
  459. LUA_API void lua_settable (lua_State *L, int idx) {
  460. StkId t;
  461. lua_lock(L);
  462. api_checknelems(L, 2);
  463. t = luaA_index(L, idx);
  464. api_checkvalidindex(L, t);
  465. luaV_settable(L, t, L->top - 2, L->top - 1);
  466. L->top -= 2; /* pop index and value */
  467. lua_unlock(L);
  468. }
  469. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  470. StkId t;
  471. TObject key;
  472. lua_lock(L);
  473. api_checknelems(L, 1);
  474. t = luaA_index(L, idx);
  475. api_checkvalidindex(L, t);
  476. setsvalue(&key, luaS_new(L, k));
  477. luaV_settable(L, t, &key, L->top - 1);
  478. L->top--; /* pop value */
  479. lua_unlock(L);
  480. }
  481. LUA_API void lua_rawset (lua_State *L, int idx) {
  482. StkId t;
  483. lua_lock(L);
  484. api_checknelems(L, 2);
  485. t = luaA_index(L, idx);
  486. api_check(L, ttistable(t));
  487. setobj2t(luaH_set(L, hvalue(t), L->top-2), L->top-1);
  488. luaC_barrier(L, hvalue(t), L->top-1);
  489. L->top -= 2;
  490. lua_unlock(L);
  491. }
  492. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  493. StkId o;
  494. lua_lock(L);
  495. api_checknelems(L, 1);
  496. o = luaA_index(L, idx);
  497. api_check(L, ttistable(o));
  498. setobj2t(luaH_setnum(L, hvalue(o), n), L->top-1);
  499. luaC_barrier(L, hvalue(o), L->top-1);
  500. L->top--;
  501. lua_unlock(L);
  502. }
  503. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  504. TObject *obj;
  505. Table *mt;
  506. int res = 1;
  507. lua_lock(L);
  508. api_checknelems(L, 1);
  509. obj = luaA_index(L, objindex);
  510. api_checkvalidindex(L, obj);
  511. if (ttisnil(L->top - 1))
  512. mt = NULL;
  513. else {
  514. api_check(L, ttistable(L->top - 1));
  515. mt = hvalue(L->top - 1);
  516. }
  517. switch (ttype(obj)) {
  518. case LUA_TTABLE: {
  519. hvalue(obj)->metatable = mt;
  520. if (mt)
  521. luaC_objbarrier(L, hvalue(obj), mt);
  522. break;
  523. }
  524. case LUA_TUSERDATA: {
  525. uvalue(obj)->uv.metatable = mt;
  526. if (mt)
  527. luaC_objbarrier(L, uvalue(obj), mt);
  528. break;
  529. }
  530. default: {
  531. res = 0; /* cannot set */
  532. break;
  533. }
  534. }
  535. L->top--;
  536. lua_unlock(L);
  537. return res;
  538. }
  539. LUA_API int lua_setfenv (lua_State *L, int idx) {
  540. StkId o;
  541. int res = 0;
  542. lua_lock(L);
  543. api_checknelems(L, 1);
  544. o = luaA_index(L, idx);
  545. api_checkvalidindex(L, o);
  546. L->top--;
  547. api_check(L, ttistable(L->top));
  548. if (isLfunction(o)) {
  549. res = 1;
  550. clvalue(o)->l.g = *(L->top);
  551. }
  552. lua_unlock(L);
  553. return res;
  554. }
  555. /*
  556. ** `load' and `call' functions (run Lua code)
  557. */
  558. LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
  559. StkId func;
  560. lua_lock(L);
  561. api_checknelems(L, nargs+1);
  562. func = L->top - (nargs+1);
  563. luaD_call(L, func, 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. lua_unlock(L);
  593. return status;
  594. }
  595. /*
  596. ** Execute a protected C call.
  597. */
  598. struct CCallS { /* data to `f_Ccall' */
  599. lua_CFunction func;
  600. void *ud;
  601. };
  602. static void f_Ccall (lua_State *L, void *ud) {
  603. struct CCallS *c = cast(struct CCallS *, ud);
  604. Closure *cl;
  605. cl = luaF_newCclosure(L, 0);
  606. cl->c.f = c->func;
  607. setclvalue(L->top, cl); /* push function */
  608. incr_top(L);
  609. setpvalue(L->top, c->ud); /* push only argument */
  610. incr_top(L);
  611. luaD_call(L, L->top - 2, 0);
  612. }
  613. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  614. struct CCallS c;
  615. int status;
  616. lua_lock(L);
  617. c.func = func;
  618. c.ud = ud;
  619. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  620. lua_unlock(L);
  621. return status;
  622. }
  623. LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
  624. const char *chunkname) {
  625. ZIO z;
  626. int status;
  627. lua_lock(L);
  628. if (!chunkname) chunkname = "?";
  629. luaZ_init(L, &z, reader, data);
  630. status = luaD_protectedparser(L, &z, chunkname);
  631. lua_unlock(L);
  632. return status;
  633. }
  634. LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
  635. int status;
  636. TObject *o;
  637. lua_lock(L);
  638. api_checknelems(L, 1);
  639. o = L->top - 1;
  640. if (isLfunction(o))
  641. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  642. else
  643. status = 0;
  644. lua_unlock(L);
  645. return status;
  646. }
  647. /*
  648. ** Garbage-collection functions
  649. */
  650. /* GC values are expressed in Kbytes: #bytes/2^10 */
  651. #define GCscale(x) (cast(int, (x)>>10))
  652. #define GCunscale(x) (cast(lu_mem, x)<<10)
  653. #define MAX_THRESHOLD (cast(lu_mem, ~0) >> 10)
  654. LUA_API int lua_getgcthreshold (lua_State *L) {
  655. int threshold;
  656. lua_lock(L);
  657. threshold = GCscale(G(L)->GCthreshold);
  658. lua_unlock(L);
  659. return threshold;
  660. }
  661. LUA_API int lua_getgccount (lua_State *L) {
  662. int count;
  663. lua_lock(L);
  664. count = GCscale(G(L)->nblocks);
  665. lua_unlock(L);
  666. return count;
  667. }
  668. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  669. lua_lock(L);
  670. if (cast(lu_mem, newthreshold) > MAX_THRESHOLD)
  671. newthreshold = cast(int, MAX_THRESHOLD);
  672. G(L)->GCthreshold = GCunscale(newthreshold);
  673. luaC_checkGC(L);
  674. lua_unlock(L);
  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->top, luaS_newlstr(L, NULL, 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->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, TObject **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. TObject *val;
  752. lua_lock(L);
  753. name = aux_upvalue(L, luaA_index(L, funcindex), n, &val);
  754. if (name) {
  755. setobj2s(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. TObject *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(val, L->top);
  772. luaC_barrier(L, clvalue(fi), L->top);
  773. }
  774. lua_unlock(L);
  775. return name;
  776. }