lapi.c 26 KB

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