lapi.c 30 KB

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