lapi.c 25 KB

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