lapi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. ** $Id: lapi.c,v 2.150 2011/08/09 20:58:29 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. luai_checknum(L, L->top,
  375. luaG_runerror(L, "C API - attempt to push a signaling NaN"));
  376. api_incr_top(L);
  377. lua_unlock(L);
  378. }
  379. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  380. lua_lock(L);
  381. setnvalue(L->top, cast_num(n));
  382. api_incr_top(L);
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
  386. lua_Number n;
  387. lua_lock(L);
  388. n = lua_unsigned2number(u);
  389. setnvalue(L->top, n);
  390. api_incr_top(L);
  391. lua_unlock(L);
  392. }
  393. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  394. TString *ts;
  395. lua_lock(L);
  396. luaC_checkGC(L);
  397. ts = luaS_newlstr(L, s, len);
  398. setsvalue2s(L, L->top, ts);
  399. api_incr_top(L);
  400. lua_unlock(L);
  401. return getstr(ts);
  402. }
  403. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  404. if (s == NULL) {
  405. lua_pushnil(L);
  406. return NULL;
  407. }
  408. else {
  409. TString *ts;
  410. lua_lock(L);
  411. luaC_checkGC(L);
  412. ts = luaS_new(L, s);
  413. setsvalue2s(L, L->top, ts);
  414. api_incr_top(L);
  415. lua_unlock(L);
  416. return getstr(ts);
  417. }
  418. }
  419. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  420. va_list argp) {
  421. const char *ret;
  422. lua_lock(L);
  423. luaC_checkGC(L);
  424. ret = luaO_pushvfstring(L, fmt, argp);
  425. lua_unlock(L);
  426. return ret;
  427. }
  428. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  429. const char *ret;
  430. va_list argp;
  431. lua_lock(L);
  432. luaC_checkGC(L);
  433. va_start(argp, fmt);
  434. ret = luaO_pushvfstring(L, fmt, argp);
  435. va_end(argp);
  436. lua_unlock(L);
  437. return ret;
  438. }
  439. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  440. lua_lock(L);
  441. if (n == 0) {
  442. setfvalue(L->top, fn);
  443. }
  444. else {
  445. Closure *cl;
  446. api_checknelems(L, n);
  447. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  448. luaC_checkGC(L);
  449. cl = luaF_newCclosure(L, n);
  450. cl->c.f = fn;
  451. L->top -= n;
  452. while (n--)
  453. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  454. setclCvalue(L, L->top, cl);
  455. }
  456. api_incr_top(L);
  457. lua_unlock(L);
  458. }
  459. LUA_API void lua_pushboolean (lua_State *L, int b) {
  460. lua_lock(L);
  461. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  462. api_incr_top(L);
  463. lua_unlock(L);
  464. }
  465. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  466. lua_lock(L);
  467. setpvalue(L->top, p);
  468. api_incr_top(L);
  469. lua_unlock(L);
  470. }
  471. LUA_API int lua_pushthread (lua_State *L) {
  472. lua_lock(L);
  473. setthvalue(L, L->top, L);
  474. api_incr_top(L);
  475. lua_unlock(L);
  476. return (G(L)->mainthread == L);
  477. }
  478. /*
  479. ** get functions (Lua -> stack)
  480. */
  481. LUA_API void lua_gettable (lua_State *L, int idx) {
  482. StkId t;
  483. lua_lock(L);
  484. t = index2addr(L, idx);
  485. api_checkvalidindex(L, t);
  486. luaV_gettable(L, t, L->top - 1, L->top - 1);
  487. lua_unlock(L);
  488. }
  489. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  490. StkId t;
  491. lua_lock(L);
  492. t = index2addr(L, idx);
  493. api_checkvalidindex(L, t);
  494. setsvalue2s(L, L->top, luaS_new(L, k));
  495. api_incr_top(L);
  496. luaV_gettable(L, t, L->top - 1, L->top - 1);
  497. lua_unlock(L);
  498. }
  499. LUA_API void lua_rawget (lua_State *L, int idx) {
  500. StkId t;
  501. lua_lock(L);
  502. t = index2addr(L, idx);
  503. api_check(L, ttistable(t), "table expected");
  504. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  505. lua_unlock(L);
  506. }
  507. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  508. StkId o;
  509. lua_lock(L);
  510. o = index2addr(L, idx);
  511. api_check(L, ttistable(o), "table expected");
  512. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  513. api_incr_top(L);
  514. lua_unlock(L);
  515. }
  516. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  517. Table *t;
  518. lua_lock(L);
  519. luaC_checkGC(L);
  520. t = luaH_new(L);
  521. sethvalue(L, L->top, t);
  522. api_incr_top(L);
  523. if (narray > 0 || nrec > 0)
  524. luaH_resize(L, t, narray, nrec);
  525. lua_unlock(L);
  526. }
  527. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  528. const TValue *obj;
  529. Table *mt = NULL;
  530. int res;
  531. lua_lock(L);
  532. obj = index2addr(L, objindex);
  533. switch (ttypenv(obj)) {
  534. case LUA_TTABLE:
  535. mt = hvalue(obj)->metatable;
  536. break;
  537. case LUA_TUSERDATA:
  538. mt = uvalue(obj)->metatable;
  539. break;
  540. default:
  541. mt = G(L)->mt[ttypenv(obj)];
  542. break;
  543. }
  544. if (mt == NULL)
  545. res = 0;
  546. else {
  547. sethvalue(L, L->top, mt);
  548. api_incr_top(L);
  549. res = 1;
  550. }
  551. lua_unlock(L);
  552. return res;
  553. }
  554. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  555. StkId o;
  556. lua_lock(L);
  557. o = index2addr(L, idx);
  558. api_checkvalidindex(L, o);
  559. api_check(L, ttisuserdata(o), "userdata expected");
  560. if (uvalue(o)->env) {
  561. sethvalue(L, L->top, uvalue(o)->env);
  562. } else
  563. setnilvalue(L->top);
  564. api_incr_top(L);
  565. lua_unlock(L);
  566. }
  567. /*
  568. ** set functions (stack -> Lua)
  569. */
  570. LUA_API void lua_settable (lua_State *L, int idx) {
  571. StkId t;
  572. lua_lock(L);
  573. api_checknelems(L, 2);
  574. t = index2addr(L, idx);
  575. api_checkvalidindex(L, t);
  576. luaV_settable(L, t, L->top - 2, L->top - 1);
  577. L->top -= 2; /* pop index and value */
  578. lua_unlock(L);
  579. }
  580. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  581. StkId t;
  582. lua_lock(L);
  583. api_checknelems(L, 1);
  584. t = index2addr(L, idx);
  585. api_checkvalidindex(L, t);
  586. setsvalue2s(L, L->top++, luaS_new(L, k));
  587. luaV_settable(L, t, L->top - 1, L->top - 2);
  588. L->top -= 2; /* pop value and key */
  589. lua_unlock(L);
  590. }
  591. LUA_API void lua_rawset (lua_State *L, int idx) {
  592. StkId t;
  593. lua_lock(L);
  594. api_checknelems(L, 2);
  595. t = index2addr(L, idx);
  596. api_check(L, ttistable(t), "table expected");
  597. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  598. invalidateTMcache(hvalue(t));
  599. luaC_barrierback(L, gcvalue(t), L->top-1);
  600. L->top -= 2;
  601. lua_unlock(L);
  602. }
  603. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  604. StkId o;
  605. lua_lock(L);
  606. api_checknelems(L, 1);
  607. o = index2addr(L, idx);
  608. api_check(L, ttistable(o), "table expected");
  609. luaH_setint(L, hvalue(o), n, L->top - 1);
  610. luaC_barrierback(L, gcvalue(o), L->top-1);
  611. L->top--;
  612. lua_unlock(L);
  613. }
  614. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  615. TValue *obj;
  616. Table *mt;
  617. lua_lock(L);
  618. api_checknelems(L, 1);
  619. obj = index2addr(L, objindex);
  620. api_checkvalidindex(L, obj);
  621. if (ttisnil(L->top - 1))
  622. mt = NULL;
  623. else {
  624. api_check(L, ttistable(L->top - 1), "table expected");
  625. mt = hvalue(L->top - 1);
  626. }
  627. switch (ttypenv(obj)) {
  628. case LUA_TTABLE: {
  629. hvalue(obj)->metatable = mt;
  630. if (mt)
  631. luaC_objbarrierback(L, gcvalue(obj), mt);
  632. luaC_checkfinalizer(L, gcvalue(obj), mt);
  633. break;
  634. }
  635. case LUA_TUSERDATA: {
  636. uvalue(obj)->metatable = mt;
  637. if (mt) {
  638. luaC_objbarrier(L, rawuvalue(obj), mt);
  639. luaC_checkfinalizer(L, gcvalue(obj), mt);
  640. }
  641. break;
  642. }
  643. default: {
  644. G(L)->mt[ttypenv(obj)] = mt;
  645. break;
  646. }
  647. }
  648. L->top--;
  649. lua_unlock(L);
  650. return 1;
  651. }
  652. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  653. StkId o;
  654. lua_lock(L);
  655. api_checknelems(L, 1);
  656. o = index2addr(L, idx);
  657. api_checkvalidindex(L, o);
  658. api_check(L, ttisuserdata(o), "userdata expected");
  659. if (ttisnil(L->top - 1))
  660. uvalue(o)->env = NULL;
  661. else {
  662. api_check(L, ttistable(L->top - 1), "table expected");
  663. uvalue(o)->env = hvalue(L->top - 1);
  664. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  665. }
  666. L->top--;
  667. lua_unlock(L);
  668. }
  669. /*
  670. ** `load' and `call' functions (run Lua code)
  671. */
  672. #define checkresults(L,na,nr) \
  673. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  674. "results from function overflow current stack size")
  675. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  676. if (L->ci->callstatus & CIST_YIELDED) {
  677. if (ctx) *ctx = L->ci->u.c.ctx;
  678. return L->ci->u.c.status;
  679. }
  680. else return LUA_OK;
  681. }
  682. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  683. lua_CFunction k) {
  684. StkId func;
  685. lua_lock(L);
  686. api_check(L, k == NULL || !isLua(L->ci),
  687. "cannot use continuations inside hooks");
  688. api_checknelems(L, nargs+1);
  689. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  690. checkresults(L, nargs, nresults);
  691. func = L->top - (nargs+1);
  692. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  693. L->ci->u.c.k = k; /* save continuation */
  694. L->ci->u.c.ctx = ctx; /* save context */
  695. luaD_call(L, func, nresults, 1); /* do the call */
  696. }
  697. else /* no continuation or no yieldable */
  698. luaD_call(L, func, nresults, 0); /* just do the call */
  699. adjustresults(L, nresults);
  700. lua_unlock(L);
  701. }
  702. /*
  703. ** Execute a protected call.
  704. */
  705. struct CallS { /* data to `f_call' */
  706. StkId func;
  707. int nresults;
  708. };
  709. static void f_call (lua_State *L, void *ud) {
  710. struct CallS *c = cast(struct CallS *, ud);
  711. luaD_call(L, c->func, c->nresults, 0);
  712. }
  713. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  714. int ctx, lua_CFunction k) {
  715. struct CallS c;
  716. int status;
  717. ptrdiff_t func;
  718. lua_lock(L);
  719. api_check(L, k == NULL || !isLua(L->ci),
  720. "cannot use continuations inside hooks");
  721. api_checknelems(L, nargs+1);
  722. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  723. checkresults(L, nargs, nresults);
  724. if (errfunc == 0)
  725. func = 0;
  726. else {
  727. StkId o = index2addr(L, errfunc);
  728. api_checkvalidindex(L, o);
  729. func = savestack(L, o);
  730. }
  731. c.func = L->top - (nargs+1); /* function to be called */
  732. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  733. c.nresults = nresults; /* do a 'conventional' protected call */
  734. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  735. }
  736. else { /* prepare continuation (call is already protected by 'resume') */
  737. CallInfo *ci = L->ci;
  738. ci->u.c.k = k; /* save continuation */
  739. ci->u.c.ctx = ctx; /* save context */
  740. /* save information for error recovery */
  741. ci->u.c.extra = savestack(L, c.func);
  742. ci->u.c.old_allowhook = L->allowhook;
  743. ci->u.c.old_errfunc = L->errfunc;
  744. L->errfunc = func;
  745. /* mark that function may do error recovery */
  746. ci->callstatus |= CIST_YPCALL;
  747. luaD_call(L, c.func, nresults, 1); /* do the call */
  748. ci->callstatus &= ~CIST_YPCALL;
  749. L->errfunc = ci->u.c.old_errfunc;
  750. status = LUA_OK; /* if it is here, there were no errors */
  751. }
  752. adjustresults(L, nresults);
  753. lua_unlock(L);
  754. return status;
  755. }
  756. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  757. const char *chunkname) {
  758. ZIO z;
  759. int status;
  760. lua_lock(L);
  761. if (!chunkname) chunkname = "?";
  762. luaZ_init(L, &z, reader, data);
  763. status = luaD_protectedparser(L, &z, chunkname);
  764. if (status == LUA_OK) { /* no errors? */
  765. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  766. if (f->nupvalues == 1) { /* does it have one upvalue? */
  767. /* get global table from registry */
  768. Table *reg = hvalue(&G(L)->l_registry);
  769. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  770. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  771. setobj(L, f->upvals[0]->v, gt);
  772. luaC_barrier(L, f->upvals[0], gt);
  773. }
  774. }
  775. lua_unlock(L);
  776. return status;
  777. }
  778. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  779. int status;
  780. TValue *o;
  781. lua_lock(L);
  782. api_checknelems(L, 1);
  783. o = L->top - 1;
  784. if (isLfunction(o))
  785. status = luaU_dump(L, getproto(o), writer, data, 0);
  786. else
  787. status = 1;
  788. lua_unlock(L);
  789. return status;
  790. }
  791. LUA_API int lua_status (lua_State *L) {
  792. return L->status;
  793. }
  794. /*
  795. ** Garbage-collection function
  796. */
  797. LUA_API int lua_gc (lua_State *L, int what, int data) {
  798. int res = 0;
  799. global_State *g;
  800. lua_lock(L);
  801. g = G(L);
  802. switch (what) {
  803. case LUA_GCSTOP: {
  804. g->gcrunning = 0;
  805. break;
  806. }
  807. case LUA_GCRESTART: {
  808. luaE_setdebt(g, 0);
  809. g->gcrunning = 1;
  810. break;
  811. }
  812. case LUA_GCCOLLECT: {
  813. luaC_fullgc(L, 0);
  814. break;
  815. }
  816. case LUA_GCCOUNT: {
  817. /* GC values are expressed in Kbytes: #bytes/2^10 */
  818. res = cast_int(gettotalbytes(g) >> 10);
  819. break;
  820. }
  821. case LUA_GCCOUNTB: {
  822. res = cast_int(gettotalbytes(g) & 0x3ff);
  823. break;
  824. }
  825. case LUA_GCSTEP: {
  826. if (g->gckind == KGC_GEN) { /* generational mode? */
  827. res = (g->lastmajormem == 0); /* 1 if will do major collection */
  828. luaC_forcestep(L); /* do a single step */
  829. }
  830. else {
  831. while (data-- >= 0) {
  832. luaC_forcestep(L);
  833. if (g->gcstate == GCSpause) { /* end of cycle? */
  834. res = 1; /* signal it */
  835. break;
  836. }
  837. }
  838. }
  839. break;
  840. }
  841. case LUA_GCSETPAUSE: {
  842. res = g->gcpause;
  843. g->gcpause = data;
  844. break;
  845. }
  846. case LUA_GCSETMAJORINC: {
  847. res = g->gcmajorinc;
  848. g->gcmajorinc = data;
  849. break;
  850. }
  851. case LUA_GCSETSTEPMUL: {
  852. res = g->gcstepmul;
  853. g->gcstepmul = data;
  854. break;
  855. }
  856. case LUA_GCISRUNNING: {
  857. res = g->gcrunning;
  858. break;
  859. }
  860. case LUA_GCGEN: { /* change collector to generational mode */
  861. luaC_changemode(L, KGC_GEN);
  862. break;
  863. }
  864. case LUA_GCINC: { /* change collector to incremental mode */
  865. luaC_changemode(L, KGC_NORMAL);
  866. break;
  867. }
  868. default: res = -1; /* invalid option */
  869. }
  870. lua_unlock(L);
  871. return res;
  872. }
  873. /*
  874. ** miscellaneous functions
  875. */
  876. LUA_API int lua_error (lua_State *L) {
  877. lua_lock(L);
  878. api_checknelems(L, 1);
  879. luaG_errormsg(L);
  880. lua_unlock(L);
  881. return 0; /* to avoid warnings */
  882. }
  883. LUA_API int lua_next (lua_State *L, int idx) {
  884. StkId t;
  885. int more;
  886. lua_lock(L);
  887. t = index2addr(L, idx);
  888. api_check(L, ttistable(t), "table expected");
  889. more = luaH_next(L, hvalue(t), L->top - 1);
  890. if (more) {
  891. api_incr_top(L);
  892. }
  893. else /* no more elements */
  894. L->top -= 1; /* remove key */
  895. lua_unlock(L);
  896. return more;
  897. }
  898. LUA_API void lua_concat (lua_State *L, int n) {
  899. lua_lock(L);
  900. api_checknelems(L, n);
  901. if (n >= 2) {
  902. luaC_checkGC(L);
  903. luaV_concat(L, n);
  904. }
  905. else if (n == 0) { /* push empty string */
  906. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  907. api_incr_top(L);
  908. }
  909. /* else n == 1; nothing to do */
  910. lua_unlock(L);
  911. }
  912. LUA_API void lua_len (lua_State *L, int idx) {
  913. StkId t;
  914. lua_lock(L);
  915. t = index2addr(L, idx);
  916. luaV_objlen(L, L->top, t);
  917. api_incr_top(L);
  918. lua_unlock(L);
  919. }
  920. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  921. lua_Alloc f;
  922. lua_lock(L);
  923. if (ud) *ud = G(L)->ud;
  924. f = G(L)->frealloc;
  925. lua_unlock(L);
  926. return f;
  927. }
  928. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  929. lua_lock(L);
  930. G(L)->ud = ud;
  931. G(L)->frealloc = f;
  932. lua_unlock(L);
  933. }
  934. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  935. Udata *u;
  936. lua_lock(L);
  937. luaC_checkGC(L);
  938. u = luaS_newudata(L, size, NULL);
  939. setuvalue(L, L->top, u);
  940. api_incr_top(L);
  941. lua_unlock(L);
  942. return u + 1;
  943. }
  944. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  945. GCObject **owner) {
  946. switch (ttype(fi)) {
  947. case LUA_TCCL: { /* C closure */
  948. CClosure *f = clCvalue(fi);
  949. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  950. *val = &f->upvalue[n-1];
  951. if (owner) *owner = obj2gco(f);
  952. return "";
  953. }
  954. case LUA_TLCL: { /* Lua closure */
  955. LClosure *f = clLvalue(fi);
  956. const char *name;
  957. Proto *p = f->p;
  958. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  959. *val = f->upvals[n-1]->v;
  960. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  961. name = getstr(p->upvalues[n-1].name);
  962. if (name == NULL) /* no debug information? */
  963. name = "";
  964. return name;
  965. }
  966. default: return NULL; /* not a closure */
  967. }
  968. }
  969. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  970. const char *name;
  971. TValue *val;
  972. lua_lock(L);
  973. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
  974. if (name) {
  975. setobj2s(L, L->top, val);
  976. api_incr_top(L);
  977. }
  978. lua_unlock(L);
  979. return name;
  980. }
  981. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  982. const char *name;
  983. TValue *val;
  984. GCObject *owner;
  985. StkId fi;
  986. lua_lock(L);
  987. fi = index2addr(L, funcindex);
  988. api_checknelems(L, 1);
  989. name = aux_upvalue(fi, n, &val, &owner);
  990. if (name) {
  991. L->top--;
  992. setobj(L, val, L->top);
  993. luaC_barrier(L, owner, L->top);
  994. }
  995. lua_unlock(L);
  996. return name;
  997. }
  998. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  999. LClosure *f;
  1000. StkId fi = index2addr(L, fidx);
  1001. api_check(L, ttisLclosure(fi), "Lua function expected");
  1002. f = clLvalue(fi);
  1003. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1004. if (pf) *pf = f;
  1005. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1006. }
  1007. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1008. StkId fi = index2addr(L, fidx);
  1009. switch (ttype(fi)) {
  1010. case LUA_TLCL: { /* lua closure */
  1011. return *getupvalref(L, fidx, n, NULL);
  1012. }
  1013. case LUA_TCCL: { /* C closure */
  1014. CClosure *f = clCvalue(fi);
  1015. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1016. return &f->upvalue[n - 1];
  1017. }
  1018. default: {
  1019. api_check(L, 0, "closure expected");
  1020. return NULL;
  1021. }
  1022. }
  1023. }
  1024. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1025. int fidx2, int n2) {
  1026. LClosure *f1;
  1027. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1028. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1029. *up1 = *up2;
  1030. luaC_objbarrier(L, f1, *up2);
  1031. }