lapi.c 21 KB

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