lapi.c 21 KB

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