lapi.c 30 KB

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