lapi.c 27 KB

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