lapi.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. ** $Id: lapi.c,v 2.88 2009/08/07 16:17:41 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <string.h>
  8. #define lapi_c
  9. #define LUA_CORE
  10. #include "lua.h"
  11. #include "lapi.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "lmem.h"
  17. #include "lobject.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lundump.h"
  23. #include "lvm.h"
  24. const char lua_ident[] =
  25. "$LuaVersion: " LUA_COPYRIGHT " $"
  26. "$LuaAuthors: " LUA_AUTHORS " $";
  27. #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
  28. "not enough elements in the stack")
  29. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject, \
  30. "invalid index")
  31. static TValue *index2addr (lua_State *L, int idx) {
  32. CallInfo *ci = L->ci;
  33. if (idx > 0) {
  34. TValue *o = ci->func + idx;
  35. api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
  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 - (ci->func + 1), "invalid index");
  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. api_check(L, idx <= UCHAR_MAX + 1, "upvalue index too large");
  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->previous == NULL) /* 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. LUA_API int lua_checkstack (lua_State *L, int size) {
  70. int res = 1;
  71. CallInfo *ci = L->ci;
  72. lua_lock(L);
  73. if (L->stack_last - L->top <= size) { /* need to grow stack? */
  74. int inuse = L->top - L->stack + EXTRA_STACK;
  75. if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
  76. res = 0; /* no */
  77. else
  78. luaD_growstack(L, size);
  79. }
  80. if (res && ci->top < L->top + size)
  81. ci->top = L->top + size; /* adjust frame top */
  82. lua_unlock(L);
  83. return res;
  84. }
  85. LUA_API lua_State *lua_mainthread (lua_State *L) {
  86. return G(L)->mainthread;
  87. }
  88. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  89. int i;
  90. if (from == to) return;
  91. lua_lock(to);
  92. api_checknelems(from, n);
  93. api_check(from, G(from) == G(to), "moving among independent states");
  94. api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
  95. from->top -= n;
  96. for (i = 0; i < n; i++) {
  97. setobj2s(to, to->top++, from->top + i);
  98. }
  99. lua_unlock(to);
  100. }
  101. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  102. lua_CFunction old;
  103. lua_lock(L);
  104. old = G(L)->panic;
  105. G(L)->panic = panicf;
  106. lua_unlock(L);
  107. return old;
  108. }
  109. LUA_API const lua_Number *lua_version (lua_State *L) {
  110. static const lua_Number version = LUA_VERSION_NUM;
  111. if (L == NULL) return &version;
  112. else return G(L)->version;
  113. }
  114. /*
  115. ** basic stack manipulation
  116. */
  117. LUA_API int lua_gettop (lua_State *L) {
  118. return cast_int(L->top - (L->ci->func + 1));
  119. }
  120. LUA_API void lua_settop (lua_State *L, int idx) {
  121. StkId func = L->ci->func;
  122. lua_lock(L);
  123. if (idx >= 0) {
  124. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  125. while (L->top < (func + 1) + idx)
  126. setnilvalue(L->top++);
  127. L->top = (func + 1) + idx;
  128. }
  129. else {
  130. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  131. L->top += idx+1; /* `subtract' index (index is negative) */
  132. }
  133. lua_unlock(L);
  134. }
  135. LUA_API void lua_remove (lua_State *L, int idx) {
  136. StkId p;
  137. lua_lock(L);
  138. p = index2addr(L, idx);
  139. api_checkvalidindex(L, p);
  140. while (++p < L->top) setobjs2s(L, p-1, p);
  141. L->top--;
  142. lua_unlock(L);
  143. }
  144. LUA_API void lua_insert (lua_State *L, int idx) {
  145. StkId p;
  146. StkId q;
  147. lua_lock(L);
  148. p = index2addr(L, idx);
  149. api_checkvalidindex(L, p);
  150. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  151. setobjs2s(L, p, L->top);
  152. lua_unlock(L);
  153. }
  154. LUA_API void lua_replace (lua_State *L, int idx) {
  155. StkId o;
  156. lua_lock(L);
  157. /* explicit test for incompatible code */
  158. if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
  159. luaG_runerror(L, "no calling environment");
  160. api_checknelems(L, 1);
  161. o = index2addr(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), "table expected");
  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. /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */
  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, index2addr(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 = index2addr(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 = index2addr(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 = index2addr(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 = index2addr(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 = index2addr(L, index1);
  214. StkId o2 = index2addr(L, index2);
  215. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  216. : luaO_rawequalObj(o1, o2);
  217. }
  218. LUA_API void lua_arith (lua_State *L, int op) {
  219. lua_lock(L);
  220. api_checknelems(L, 2);
  221. if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1))
  222. changenvalue(L->top - 2,
  223. luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
  224. else
  225. luaV_arith(L, L->top - 2, L->top - 2, L->top - 1, op - LUA_OPADD + TM_ADD);
  226. L->top--;
  227. lua_unlock(L);
  228. }
  229. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  230. StkId o1, o2;
  231. int i;
  232. lua_lock(L); /* may call tag method */
  233. o1 = index2addr(L, index1);
  234. o2 = index2addr(L, index2);
  235. if (o1 == luaO_nilobject || o2 == luaO_nilobject)
  236. i = 0;
  237. else switch (op) {
  238. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  239. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  240. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  241. default: api_check(L, 0, "invalid option"); i = 0;
  242. }
  243. lua_unlock(L);
  244. return i;
  245. }
  246. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  247. TValue n;
  248. const TValue *o = index2addr(L, idx);
  249. if (tonumber(o, &n))
  250. return nvalue(o);
  251. else
  252. return 0;
  253. }
  254. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  255. TValue n;
  256. const TValue *o = index2addr(L, idx);
  257. if (tonumber(o, &n)) {
  258. lua_Integer res;
  259. lua_Number num = nvalue(o);
  260. lua_number2integer(res, num);
  261. return res;
  262. }
  263. else
  264. return 0;
  265. }
  266. LUA_API int lua_toboolean (lua_State *L, int idx) {
  267. const TValue *o = index2addr(L, idx);
  268. return !l_isfalse(o);
  269. }
  270. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  271. StkId o = index2addr(L, idx);
  272. if (!ttisstring(o)) {
  273. lua_lock(L); /* `luaV_tostring' may create a new string */
  274. if (!luaV_tostring(L, o)) { /* conversion failed? */
  275. if (len != NULL) *len = 0;
  276. lua_unlock(L);
  277. return NULL;
  278. }
  279. luaC_checkGC(L);
  280. o = index2addr(L, idx); /* previous call may reallocate the stack */
  281. lua_unlock(L);
  282. }
  283. if (len != NULL) *len = tsvalue(o)->len;
  284. return svalue(o);
  285. }
  286. LUA_API size_t lua_objlen (lua_State *L, int idx) {
  287. StkId o = index2addr(L, idx);
  288. switch (ttype(o)) {
  289. case LUA_TSTRING: return tsvalue(o)->len;
  290. case LUA_TUSERDATA: return uvalue(o)->len;
  291. case LUA_TTABLE: return luaH_getn(hvalue(o));
  292. default: return 0;
  293. }
  294. }
  295. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  296. StkId o = index2addr(L, idx);
  297. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  298. }
  299. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  300. StkId o = index2addr(L, idx);
  301. switch (ttype(o)) {
  302. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  303. case LUA_TLIGHTUSERDATA: return pvalue(o);
  304. default: return NULL;
  305. }
  306. }
  307. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  308. StkId o = index2addr(L, idx);
  309. return (!ttisthread(o)) ? NULL : thvalue(o);
  310. }
  311. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  312. StkId o = index2addr(L, idx);
  313. switch (ttype(o)) {
  314. case LUA_TTABLE: return hvalue(o);
  315. case LUA_TFUNCTION: return clvalue(o);
  316. case LUA_TTHREAD: return thvalue(o);
  317. case LUA_TUSERDATA:
  318. case LUA_TLIGHTUSERDATA:
  319. return lua_touserdata(L, idx);
  320. default: return NULL;
  321. }
  322. }
  323. /*
  324. ** push functions (C -> stack)
  325. */
  326. LUA_API void lua_pushnil (lua_State *L) {
  327. lua_lock(L);
  328. setnilvalue(L->top);
  329. api_incr_top(L);
  330. lua_unlock(L);
  331. }
  332. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  333. lua_lock(L);
  334. setnvalue(L->top, n);
  335. api_incr_top(L);
  336. lua_unlock(L);
  337. }
  338. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  339. lua_lock(L);
  340. setnvalue(L->top, cast_num(n));
  341. api_incr_top(L);
  342. lua_unlock(L);
  343. }
  344. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  345. TString *ts;
  346. lua_lock(L);
  347. luaC_checkGC(L);
  348. ts = luaS_newlstr(L, s, len);
  349. setsvalue2s(L, L->top, ts);
  350. api_incr_top(L);
  351. lua_unlock(L);
  352. return getstr(ts);
  353. }
  354. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  355. if (s == NULL) {
  356. lua_pushnil(L);
  357. return NULL;
  358. }
  359. else
  360. return lua_pushlstring(L, s, strlen(s));
  361. }
  362. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  363. va_list argp) {
  364. const char *ret;
  365. lua_lock(L);
  366. luaC_checkGC(L);
  367. ret = luaO_pushvfstring(L, fmt, argp);
  368. lua_unlock(L);
  369. return ret;
  370. }
  371. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  372. const char *ret;
  373. va_list argp;
  374. lua_lock(L);
  375. luaC_checkGC(L);
  376. va_start(argp, fmt);
  377. ret = luaO_pushvfstring(L, fmt, argp);
  378. va_end(argp);
  379. lua_unlock(L);
  380. return ret;
  381. }
  382. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  383. Closure *cl;
  384. lua_lock(L);
  385. api_checknelems(L, n);
  386. api_check(L, n <= UCHAR_MAX, "upvalue index too large");
  387. luaC_checkGC(L);
  388. cl = luaF_newCclosure(L, n, getcurrenv(L));
  389. cl->c.f = fn;
  390. L->top -= n;
  391. while (n--)
  392. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  393. setclvalue(L, L->top, cl);
  394. lua_assert(iswhite(obj2gco(cl)));
  395. api_incr_top(L);
  396. lua_unlock(L);
  397. }
  398. LUA_API void lua_pushboolean (lua_State *L, int b) {
  399. lua_lock(L);
  400. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  401. api_incr_top(L);
  402. lua_unlock(L);
  403. }
  404. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  405. lua_lock(L);
  406. setpvalue(L->top, p);
  407. api_incr_top(L);
  408. lua_unlock(L);
  409. }
  410. LUA_API int lua_pushthread (lua_State *L) {
  411. lua_lock(L);
  412. setthvalue(L, L->top, L);
  413. api_incr_top(L);
  414. lua_unlock(L);
  415. return (G(L)->mainthread == L);
  416. }
  417. /*
  418. ** get functions (Lua -> stack)
  419. */
  420. LUA_API void lua_gettable (lua_State *L, int idx) {
  421. StkId t;
  422. lua_lock(L);
  423. t = index2addr(L, idx);
  424. api_checkvalidindex(L, t);
  425. luaV_gettable(L, t, L->top - 1, L->top - 1);
  426. lua_unlock(L);
  427. }
  428. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  429. StkId t;
  430. lua_lock(L);
  431. t = index2addr(L, idx);
  432. api_checkvalidindex(L, t);
  433. setsvalue2s(L, L->top, luaS_new(L, k));
  434. api_incr_top(L);
  435. luaV_gettable(L, t, L->top - 1, L->top - 1);
  436. lua_unlock(L);
  437. }
  438. LUA_API void lua_rawget (lua_State *L, int idx) {
  439. StkId t;
  440. lua_lock(L);
  441. t = index2addr(L, idx);
  442. api_check(L, ttistable(t), "table expected");
  443. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  444. lua_unlock(L);
  445. }
  446. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  447. StkId o;
  448. lua_lock(L);
  449. o = index2addr(L, idx);
  450. api_check(L, ttistable(o), "table expected");
  451. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  452. api_incr_top(L);
  453. lua_unlock(L);
  454. }
  455. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  456. Table *t;
  457. lua_lock(L);
  458. luaC_checkGC(L);
  459. t = luaH_new(L);
  460. sethvalue(L, L->top, t);
  461. api_incr_top(L);
  462. if (narray > 0 || nrec > 0)
  463. luaH_resize(L, t, narray, nrec);
  464. lua_unlock(L);
  465. }
  466. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  467. const TValue *obj;
  468. Table *mt = NULL;
  469. int res;
  470. lua_lock(L);
  471. obj = index2addr(L, objindex);
  472. switch (ttype(obj)) {
  473. case LUA_TTABLE:
  474. mt = hvalue(obj)->metatable;
  475. break;
  476. case LUA_TUSERDATA:
  477. mt = uvalue(obj)->metatable;
  478. break;
  479. default:
  480. mt = G(L)->mt[ttype(obj)];
  481. break;
  482. }
  483. if (mt == NULL)
  484. res = 0;
  485. else {
  486. sethvalue(L, L->top, mt);
  487. api_incr_top(L);
  488. res = 1;
  489. }
  490. lua_unlock(L);
  491. return res;
  492. }
  493. LUA_API void lua_getfenv (lua_State *L, int idx) {
  494. StkId o;
  495. lua_lock(L);
  496. o = index2addr(L, idx);
  497. api_checkvalidindex(L, o);
  498. switch (ttype(o)) {
  499. case LUA_TFUNCTION:
  500. sethvalue(L, L->top, clvalue(o)->c.env);
  501. break;
  502. case LUA_TUSERDATA:
  503. sethvalue(L, L->top, uvalue(o)->env);
  504. break;
  505. case LUA_TTHREAD:
  506. setobj2s(L, L->top, gt(thvalue(o)));
  507. break;
  508. default:
  509. setnilvalue(L->top);
  510. break;
  511. }
  512. api_incr_top(L);
  513. lua_unlock(L);
  514. }
  515. /*
  516. ** set functions (stack -> Lua)
  517. */
  518. LUA_API void lua_settable (lua_State *L, int idx) {
  519. StkId t;
  520. lua_lock(L);
  521. api_checknelems(L, 2);
  522. t = index2addr(L, idx);
  523. api_checkvalidindex(L, t);
  524. luaV_settable(L, t, L->top - 2, L->top - 1);
  525. L->top -= 2; /* pop index and value */
  526. lua_unlock(L);
  527. }
  528. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  529. StkId t;
  530. lua_lock(L);
  531. api_checknelems(L, 1);
  532. t = index2addr(L, idx);
  533. api_checkvalidindex(L, t);
  534. setsvalue2s(L, L->top++, luaS_new(L, k));
  535. luaV_settable(L, t, L->top - 1, L->top - 2);
  536. L->top -= 2; /* pop value and key */
  537. lua_unlock(L);
  538. }
  539. LUA_API void lua_rawset (lua_State *L, int idx) {
  540. StkId t;
  541. lua_lock(L);
  542. api_checknelems(L, 2);
  543. t = index2addr(L, idx);
  544. api_check(L, ttistable(t), "table expected");
  545. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  546. luaC_barriert(L, hvalue(t), L->top-1);
  547. L->top -= 2;
  548. lua_unlock(L);
  549. }
  550. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  551. StkId o;
  552. lua_lock(L);
  553. api_checknelems(L, 1);
  554. o = index2addr(L, idx);
  555. api_check(L, ttistable(o), "table expected");
  556. setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
  557. luaC_barriert(L, hvalue(o), L->top-1);
  558. L->top--;
  559. lua_unlock(L);
  560. }
  561. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  562. TValue *obj;
  563. Table *mt;
  564. lua_lock(L);
  565. api_checknelems(L, 1);
  566. obj = index2addr(L, objindex);
  567. api_checkvalidindex(L, obj);
  568. if (ttisnil(L->top - 1))
  569. mt = NULL;
  570. else {
  571. api_check(L, ttistable(L->top - 1), "table expected");
  572. mt = hvalue(L->top - 1);
  573. }
  574. switch (ttype(obj)) {
  575. case LUA_TTABLE: {
  576. hvalue(obj)->metatable = mt;
  577. if (mt)
  578. luaC_objbarriert(L, hvalue(obj), mt);
  579. break;
  580. }
  581. case LUA_TUSERDATA: {
  582. uvalue(obj)->metatable = mt;
  583. if (mt) {
  584. luaC_objbarrier(L, rawuvalue(obj), mt);
  585. luaC_checkfinalizer(L, rawuvalue(obj));
  586. }
  587. break;
  588. }
  589. default: {
  590. G(L)->mt[ttype(obj)] = mt;
  591. break;
  592. }
  593. }
  594. L->top--;
  595. lua_unlock(L);
  596. return 1;
  597. }
  598. LUA_API int lua_setfenv (lua_State *L, int idx) {
  599. StkId o;
  600. int res = 1;
  601. lua_lock(L);
  602. api_checknelems(L, 1);
  603. o = index2addr(L, idx);
  604. api_checkvalidindex(L, o);
  605. api_check(L, ttistable(L->top - 1), "table expected");
  606. switch (ttype(o)) {
  607. case LUA_TFUNCTION:
  608. clvalue(o)->c.env = hvalue(L->top - 1);
  609. break;
  610. case LUA_TUSERDATA:
  611. uvalue(o)->env = hvalue(L->top - 1);
  612. break;
  613. case LUA_TTHREAD:
  614. sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
  615. break;
  616. default:
  617. res = 0;
  618. break;
  619. }
  620. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  621. L->top--;
  622. lua_unlock(L);
  623. return res;
  624. }
  625. /*
  626. ** `load' and `call' functions (run Lua code)
  627. */
  628. #define checkresults(L,na,nr) \
  629. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  630. "results from function overflow current stack size")
  631. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  632. if (L->ci->callstatus & CIST_YIELDED) {
  633. if (ctx) *ctx = L->ci->u.c.ctx;
  634. return L->ci->u.c.status;
  635. }
  636. else return LUA_OK;
  637. }
  638. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  639. lua_CFunction k) {
  640. StkId func;
  641. lua_lock(L);
  642. api_check(L, k == NULL || !isLua(L->ci),
  643. "cannot use continuations inside hooks");
  644. api_checknelems(L, nargs+1);
  645. checkresults(L, nargs, nresults);
  646. func = L->top - (nargs+1);
  647. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  648. L->ci->u.c.k = k; /* save continuation */
  649. L->ci->u.c.ctx = ctx; /* save context */
  650. luaD_call(L, func, nresults, 1); /* do the call */
  651. }
  652. else /* no continuation or no yieldable */
  653. luaD_call(L, func, nresults, 0); /* just do the call */
  654. adjustresults(L, nresults);
  655. lua_unlock(L);
  656. }
  657. /*
  658. ** Execute a protected call.
  659. */
  660. struct CallS { /* data to `f_call' */
  661. StkId func;
  662. int nresults;
  663. };
  664. static void f_call (lua_State *L, void *ud) {
  665. struct CallS *c = cast(struct CallS *, ud);
  666. luaD_call(L, c->func, c->nresults, 0);
  667. }
  668. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  669. int ctx, lua_CFunction k) {
  670. struct CallS c;
  671. int status;
  672. ptrdiff_t func;
  673. lua_lock(L);
  674. api_checknelems(L, nargs+1);
  675. checkresults(L, nargs, nresults);
  676. if (errfunc == 0)
  677. func = 0;
  678. else {
  679. StkId o = index2addr(L, errfunc);
  680. api_checkvalidindex(L, o);
  681. func = savestack(L, o);
  682. }
  683. c.func = L->top - (nargs+1); /* function to be called */
  684. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  685. c.nresults = nresults; /* do a 'conventional' protected call */
  686. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  687. }
  688. else { /* prepare continuation (call is already protected by 'resume') */
  689. CallInfo *ci = L->ci;
  690. ci->u.c.k = k; /* save continuation */
  691. ci->u.c.ctx = ctx; /* save context */
  692. /* save information for error recovery */
  693. ci->u.c.oldtop = savestack(L, c.func);
  694. ci->u.c.old_allowhook = L->allowhook;
  695. ci->u.c.old_errfunc = L->errfunc;
  696. L->errfunc = func;
  697. /* mark that function may do error recovery */
  698. ci->callstatus |= CIST_YPCALL;
  699. luaD_call(L, c.func, nresults, 1); /* do the call */
  700. ci->callstatus &= ~CIST_YPCALL;
  701. L->errfunc = ci->u.c.old_errfunc;
  702. status = LUA_OK; /* if it is here, there were no errors */
  703. }
  704. adjustresults(L, nresults);
  705. lua_unlock(L);
  706. return status;
  707. }
  708. /*
  709. ** Execute a protected C call.
  710. */
  711. struct CCallS { /* data to `f_Ccall' */
  712. lua_CFunction func;
  713. void *ud;
  714. };
  715. static void f_Ccall (lua_State *L, void *ud) {
  716. struct CCallS *c = cast(struct CCallS *, ud);
  717. Closure *cl;
  718. cl = luaF_newCclosure(L, 0, getcurrenv(L));
  719. cl->c.f = c->func;
  720. setclvalue(L, L->top, cl); /* push function */
  721. api_incr_top(L);
  722. setpvalue(L->top, c->ud); /* push only argument */
  723. api_incr_top(L);
  724. luaD_call(L, L->top - 2, 0, 0);
  725. }
  726. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  727. struct CCallS c;
  728. int status;
  729. lua_lock(L);
  730. c.func = func;
  731. c.ud = ud;
  732. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  733. lua_unlock(L);
  734. return status;
  735. }
  736. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  737. const char *chunkname) {
  738. ZIO z;
  739. int status;
  740. lua_lock(L);
  741. if (!chunkname) chunkname = "?";
  742. luaZ_init(L, &z, reader, data);
  743. status = luaD_protectedparser(L, &z, chunkname);
  744. lua_unlock(L);
  745. return status;
  746. }
  747. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  748. int status;
  749. TValue *o;
  750. lua_lock(L);
  751. api_checknelems(L, 1);
  752. o = L->top - 1;
  753. if (isLfunction(o))
  754. status = luaU_dump(L, getproto(o), writer, data, 0);
  755. else
  756. status = 1;
  757. lua_unlock(L);
  758. return status;
  759. }
  760. LUA_API int lua_status (lua_State *L) {
  761. return L->status;
  762. }
  763. /*
  764. ** Garbage-collection function
  765. */
  766. LUA_API int lua_gc (lua_State *L, int what, int data) {
  767. int res = 0;
  768. global_State *g;
  769. lua_lock(L);
  770. g = G(L);
  771. switch (what) {
  772. case LUA_GCSTOP: {
  773. g->GCthreshold = MAX_LUMEM;
  774. break;
  775. }
  776. case LUA_GCRESTART: {
  777. g->GCthreshold = g->totalbytes;
  778. break;
  779. }
  780. case LUA_GCCOLLECT: {
  781. luaC_fullgc(L, 0);
  782. break;
  783. }
  784. case LUA_GCCOUNT: {
  785. /* GC values are expressed in Kbytes: #bytes/2^10 */
  786. res = cast_int(g->totalbytes >> 10);
  787. break;
  788. }
  789. case LUA_GCCOUNTB: {
  790. res = cast_int(g->totalbytes & 0x3ff);
  791. break;
  792. }
  793. case LUA_GCSTEP: {
  794. lu_mem oldts = g->GCthreshold;
  795. lu_mem a = (cast(lu_mem, data) << 10);
  796. g->GCthreshold = (a <= g->totalbytes) ? g->totalbytes - a : 0;
  797. while (g->GCthreshold <= g->totalbytes) {
  798. luaC_step(L);
  799. if (g->gcstate == GCSpause) { /* end of cycle? */
  800. res = 1; /* signal it */
  801. break;
  802. }
  803. }
  804. if (oldts == MAX_LUMEM) /* collector was stopped? */
  805. g->GCthreshold = oldts; /* keep it that way */
  806. break;
  807. }
  808. case LUA_GCSETPAUSE: {
  809. res = g->gcpause;
  810. g->gcpause = data;
  811. break;
  812. }
  813. case LUA_GCSETSTEPMUL: {
  814. res = g->gcstepmul;
  815. g->gcstepmul = data;
  816. break;
  817. }
  818. default: res = -1; /* invalid option */
  819. }
  820. lua_unlock(L);
  821. return res;
  822. }
  823. /*
  824. ** miscellaneous functions
  825. */
  826. LUA_API int lua_error (lua_State *L) {
  827. lua_lock(L);
  828. api_checknelems(L, 1);
  829. luaG_errormsg(L);
  830. lua_unlock(L);
  831. return 0; /* to avoid warnings */
  832. }
  833. LUA_API int lua_next (lua_State *L, int idx) {
  834. StkId t;
  835. int more;
  836. lua_lock(L);
  837. t = index2addr(L, idx);
  838. api_check(L, ttistable(t), "table expected");
  839. more = luaH_next(L, hvalue(t), L->top - 1);
  840. if (more) {
  841. api_incr_top(L);
  842. }
  843. else /* no more elements */
  844. L->top -= 1; /* remove key */
  845. lua_unlock(L);
  846. return more;
  847. }
  848. LUA_API void lua_concat (lua_State *L, int n) {
  849. lua_lock(L);
  850. api_checknelems(L, n);
  851. if (n >= 2) {
  852. luaC_checkGC(L);
  853. luaV_concat(L, n);
  854. }
  855. else if (n == 0) { /* push empty string */
  856. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  857. api_incr_top(L);
  858. }
  859. /* else n == 1; nothing to do */
  860. lua_unlock(L);
  861. }
  862. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  863. lua_Alloc f;
  864. lua_lock(L);
  865. if (ud) *ud = G(L)->ud;
  866. f = G(L)->frealloc;
  867. lua_unlock(L);
  868. return f;
  869. }
  870. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  871. lua_lock(L);
  872. G(L)->ud = ud;
  873. G(L)->frealloc = f;
  874. lua_unlock(L);
  875. }
  876. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  877. Udata *u;
  878. lua_lock(L);
  879. luaC_checkGC(L);
  880. u = luaS_newudata(L, size, getcurrenv(L));
  881. setuvalue(L, L->top, u);
  882. api_incr_top(L);
  883. lua_unlock(L);
  884. return u + 1;
  885. }
  886. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  887. Closure *f;
  888. if (!ttisfunction(fi)) return NULL;
  889. f = clvalue(fi);
  890. if (f->c.isC) {
  891. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  892. *val = &f->c.upvalue[n-1];
  893. return "";
  894. }
  895. else {
  896. Proto *p = f->l.p;
  897. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  898. *val = f->l.upvals[n-1]->v;
  899. return getstr(p->upvalues[n-1]);
  900. }
  901. }
  902. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  903. const char *name;
  904. TValue *val;
  905. lua_lock(L);
  906. name = aux_upvalue(index2addr(L, funcindex), n, &val);
  907. if (name) {
  908. setobj2s(L, L->top, val);
  909. api_incr_top(L);
  910. }
  911. lua_unlock(L);
  912. return name;
  913. }
  914. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  915. const char *name;
  916. TValue *val;
  917. StkId fi;
  918. lua_lock(L);
  919. fi = index2addr(L, funcindex);
  920. api_checknelems(L, 1);
  921. name = aux_upvalue(fi, n, &val);
  922. if (name) {
  923. L->top--;
  924. setobj(L, val, L->top);
  925. luaC_barrier(L, clvalue(fi), L->top);
  926. }
  927. lua_unlock(L);
  928. return name;
  929. }