lapi.c 27 KB

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