lapi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*
  2. ** $Id: lapi.c,v 2.196 2014/02/14 16:43:14 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 (ttisfulluserdata(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 && op != LUA_OPBNOT)
  234. api_checknelems(L, 2); /* all other operations expect two operands */
  235. else { /* for unary operations, 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_strtonum (lua_State *L, const char *s, size_t len) {
  263. lua_Integer i; lua_Number n;
  264. if (luaO_str2int(s, len, &i)) { /* try as an integer */
  265. setivalue(L->top, i);
  266. }
  267. else if (luaO_str2d(s, len, &n)) { /* else try as a float */
  268. setnvalue(L->top, n);
  269. }
  270. else
  271. return 0; /* conversion failed */
  272. api_incr_top(L);
  273. return 1;
  274. }
  275. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  276. lua_Number n;
  277. const TValue *o = index2addr(L, idx);
  278. int isnum = tonumber(o, &n);
  279. if (!isnum)
  280. n = 0; /* call to 'tonumber' may change 'n' even if it fails */
  281. if (pisnum) *pisnum = isnum;
  282. return n;
  283. }
  284. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  285. lua_Integer res;
  286. const TValue *o = index2addr(L, idx);
  287. int isnum = tointeger(o, &res);
  288. if (!isnum)
  289. res = 0; /* call to 'tointeger' may change 'n' even if it fails */
  290. if (pisnum) *pisnum = isnum;
  291. return res;
  292. }
  293. LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) {
  294. lua_Unsigned res = 0;
  295. const TValue *o = index2addr(L, idx);
  296. int isnum = 0;
  297. switch (ttype(o)) {
  298. case LUA_TNUMINT: {
  299. res = cast_unsigned(ivalue(o));
  300. isnum = 1;
  301. break;
  302. }
  303. case LUA_TNUMFLT: {
  304. const lua_Number twop = (~(lua_Unsigned)0) + cast_num(1);
  305. lua_Number n = fltvalue(o);
  306. int neg = 0;
  307. n = l_floor(n);
  308. if (n < 0) {
  309. neg = 1;
  310. n = -n;
  311. }
  312. n = l_mathop(fmod)(n, twop);
  313. if (luai_numisnan(n)) /* not a number? */
  314. break; /* not an integer, too */
  315. res = cast_unsigned(n);
  316. if (neg) res = 0u - res;
  317. isnum = 1;
  318. break;
  319. }
  320. default: break;
  321. }
  322. if (pisnum) *pisnum = isnum;
  323. return res;
  324. }
  325. LUA_API int lua_toboolean (lua_State *L, int idx) {
  326. const TValue *o = index2addr(L, idx);
  327. return !l_isfalse(o);
  328. }
  329. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  330. StkId o = index2addr(L, idx);
  331. if (!ttisstring(o)) {
  332. lua_lock(L); /* `luaV_tostring' may create a new string */
  333. if (!luaV_tostring(L, o)) { /* conversion failed? */
  334. if (len != NULL) *len = 0;
  335. lua_unlock(L);
  336. return NULL;
  337. }
  338. luaC_checkGC(L);
  339. o = index2addr(L, idx); /* previous call may reallocate the stack */
  340. lua_unlock(L);
  341. }
  342. if (len != NULL) *len = tsvalue(o)->len;
  343. return svalue(o);
  344. }
  345. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  346. StkId o = index2addr(L, idx);
  347. switch (ttnov(o)) {
  348. case LUA_TSTRING: return tsvalue(o)->len;
  349. case LUA_TUSERDATA: return uvalue(o)->len;
  350. case LUA_TTABLE: return luaH_getn(hvalue(o));
  351. default: return 0;
  352. }
  353. }
  354. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  355. StkId o = index2addr(L, idx);
  356. if (ttislcf(o)) return fvalue(o);
  357. else if (ttisCclosure(o))
  358. return clCvalue(o)->f;
  359. else return NULL; /* not a C function */
  360. }
  361. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  362. StkId o = index2addr(L, idx);
  363. switch (ttnov(o)) {
  364. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  365. case LUA_TLIGHTUSERDATA: return pvalue(o);
  366. default: return NULL;
  367. }
  368. }
  369. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  370. StkId o = index2addr(L, idx);
  371. return (!ttisthread(o)) ? NULL : thvalue(o);
  372. }
  373. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  374. StkId o = index2addr(L, idx);
  375. switch (ttype(o)) {
  376. case LUA_TTABLE: return hvalue(o);
  377. case LUA_TLCL: return clLvalue(o);
  378. case LUA_TCCL: return clCvalue(o);
  379. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  380. case LUA_TTHREAD: return thvalue(o);
  381. case LUA_TUSERDATA:
  382. case LUA_TLIGHTUSERDATA:
  383. return lua_touserdata(L, idx);
  384. default: return NULL;
  385. }
  386. }
  387. /*
  388. ** push functions (C -> stack)
  389. */
  390. LUA_API void lua_pushnil (lua_State *L) {
  391. lua_lock(L);
  392. setnilvalue(L->top);
  393. api_incr_top(L);
  394. lua_unlock(L);
  395. }
  396. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  397. lua_lock(L);
  398. setnvalue(L->top, n);
  399. luai_checknum(L, L->top,
  400. luaG_runerror(L, "C API - attempt to push a signaling NaN"));
  401. api_incr_top(L);
  402. lua_unlock(L);
  403. }
  404. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  405. lua_lock(L);
  406. setivalue(L->top, n);
  407. api_incr_top(L);
  408. lua_unlock(L);
  409. }
  410. LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
  411. lua_lock(L);
  412. setivalue(L->top, cast_integer(u));
  413. api_incr_top(L);
  414. lua_unlock(L);
  415. }
  416. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  417. TString *ts;
  418. lua_lock(L);
  419. luaC_checkGC(L);
  420. ts = luaS_newlstr(L, s, len);
  421. setsvalue2s(L, L->top, ts);
  422. api_incr_top(L);
  423. lua_unlock(L);
  424. return getstr(ts);
  425. }
  426. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  427. if (s == NULL) {
  428. lua_pushnil(L);
  429. return NULL;
  430. }
  431. else {
  432. TString *ts;
  433. lua_lock(L);
  434. luaC_checkGC(L);
  435. ts = luaS_new(L, s);
  436. setsvalue2s(L, L->top, ts);
  437. api_incr_top(L);
  438. lua_unlock(L);
  439. return getstr(ts);
  440. }
  441. }
  442. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  443. va_list argp) {
  444. const char *ret;
  445. lua_lock(L);
  446. luaC_checkGC(L);
  447. ret = luaO_pushvfstring(L, fmt, argp);
  448. lua_unlock(L);
  449. return ret;
  450. }
  451. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  452. const char *ret;
  453. va_list argp;
  454. lua_lock(L);
  455. luaC_checkGC(L);
  456. va_start(argp, fmt);
  457. ret = luaO_pushvfstring(L, fmt, argp);
  458. va_end(argp);
  459. lua_unlock(L);
  460. return ret;
  461. }
  462. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  463. lua_lock(L);
  464. if (n == 0) {
  465. setfvalue(L->top, fn);
  466. }
  467. else {
  468. Closure *cl;
  469. api_checknelems(L, n);
  470. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  471. luaC_checkGC(L);
  472. cl = luaF_newCclosure(L, n);
  473. cl->c.f = fn;
  474. L->top -= n;
  475. while (n--) {
  476. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  477. /* does not need barrier because closure is white */
  478. }
  479. setclCvalue(L, L->top, cl);
  480. }
  481. api_incr_top(L);
  482. lua_unlock(L);
  483. }
  484. LUA_API void lua_pushboolean (lua_State *L, int b) {
  485. lua_lock(L);
  486. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  487. api_incr_top(L);
  488. lua_unlock(L);
  489. }
  490. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  491. lua_lock(L);
  492. setpvalue(L->top, p);
  493. api_incr_top(L);
  494. lua_unlock(L);
  495. }
  496. LUA_API int lua_pushthread (lua_State *L) {
  497. lua_lock(L);
  498. setthvalue(L, L->top, L);
  499. api_incr_top(L);
  500. lua_unlock(L);
  501. return (G(L)->mainthread == L);
  502. }
  503. /*
  504. ** get functions (Lua -> stack)
  505. */
  506. LUA_API void lua_getglobal (lua_State *L, const char *var) {
  507. Table *reg = hvalue(&G(L)->l_registry);
  508. const TValue *gt; /* global table */
  509. lua_lock(L);
  510. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  511. setsvalue2s(L, L->top++, luaS_new(L, var));
  512. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  513. lua_unlock(L);
  514. }
  515. LUA_API void lua_gettable (lua_State *L, int idx) {
  516. StkId t;
  517. lua_lock(L);
  518. t = index2addr(L, idx);
  519. luaV_gettable(L, t, L->top - 1, L->top - 1);
  520. lua_unlock(L);
  521. }
  522. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  523. StkId t;
  524. lua_lock(L);
  525. t = index2addr(L, idx);
  526. setsvalue2s(L, L->top, luaS_new(L, k));
  527. api_incr_top(L);
  528. luaV_gettable(L, t, L->top - 1, L->top - 1);
  529. lua_unlock(L);
  530. }
  531. LUA_API void lua_rawget (lua_State *L, int idx) {
  532. StkId t;
  533. lua_lock(L);
  534. t = index2addr(L, idx);
  535. api_check(L, ttistable(t), "table expected");
  536. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  537. lua_unlock(L);
  538. }
  539. LUA_API void lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  540. StkId t;
  541. lua_lock(L);
  542. t = index2addr(L, idx);
  543. api_check(L, ttistable(t), "table expected");
  544. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  545. api_incr_top(L);
  546. lua_unlock(L);
  547. }
  548. LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
  549. StkId t;
  550. TValue k;
  551. lua_lock(L);
  552. t = index2addr(L, idx);
  553. api_check(L, ttistable(t), "table expected");
  554. setpvalue(&k, cast(void *, p));
  555. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  556. api_incr_top(L);
  557. lua_unlock(L);
  558. }
  559. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  560. Table *t;
  561. lua_lock(L);
  562. luaC_checkGC(L);
  563. t = luaH_new(L);
  564. sethvalue(L, L->top, t);
  565. api_incr_top(L);
  566. if (narray > 0 || nrec > 0)
  567. luaH_resize(L, t, narray, nrec);
  568. lua_unlock(L);
  569. }
  570. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  571. const TValue *obj;
  572. Table *mt = NULL;
  573. int res;
  574. lua_lock(L);
  575. obj = index2addr(L, objindex);
  576. switch (ttnov(obj)) {
  577. case LUA_TTABLE:
  578. mt = hvalue(obj)->metatable;
  579. break;
  580. case LUA_TUSERDATA:
  581. mt = uvalue(obj)->metatable;
  582. break;
  583. default:
  584. mt = G(L)->mt[ttnov(obj)];
  585. break;
  586. }
  587. if (mt == NULL)
  588. res = 0;
  589. else {
  590. sethvalue(L, L->top, mt);
  591. api_incr_top(L);
  592. res = 1;
  593. }
  594. lua_unlock(L);
  595. return res;
  596. }
  597. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  598. StkId o;
  599. lua_lock(L);
  600. o = index2addr(L, idx);
  601. api_check(L, ttisfulluserdata(o), "full userdata expected");
  602. if (uvalue(o)->env) {
  603. sethvalue(L, L->top, uvalue(o)->env);
  604. } else
  605. setnilvalue(L->top);
  606. api_incr_top(L);
  607. lua_unlock(L);
  608. }
  609. /*
  610. ** set functions (stack -> Lua)
  611. */
  612. LUA_API void lua_setglobal (lua_State *L, const char *var) {
  613. Table *reg = hvalue(&G(L)->l_registry);
  614. const TValue *gt; /* global table */
  615. lua_lock(L);
  616. api_checknelems(L, 1);
  617. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  618. setsvalue2s(L, L->top++, luaS_new(L, var));
  619. luaV_settable(L, gt, L->top - 1, L->top - 2);
  620. L->top -= 2; /* pop value and key */
  621. lua_unlock(L);
  622. }
  623. LUA_API void lua_settable (lua_State *L, int idx) {
  624. StkId t;
  625. lua_lock(L);
  626. api_checknelems(L, 2);
  627. t = index2addr(L, idx);
  628. luaV_settable(L, t, L->top - 2, L->top - 1);
  629. L->top -= 2; /* pop index and value */
  630. lua_unlock(L);
  631. }
  632. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  633. StkId t;
  634. lua_lock(L);
  635. api_checknelems(L, 1);
  636. t = index2addr(L, idx);
  637. setsvalue2s(L, L->top++, luaS_new(L, k));
  638. luaV_settable(L, t, L->top - 1, L->top - 2);
  639. L->top -= 2; /* pop value and key */
  640. lua_unlock(L);
  641. }
  642. LUA_API void lua_rawset (lua_State *L, int idx) {
  643. StkId t;
  644. lua_lock(L);
  645. api_checknelems(L, 2);
  646. t = index2addr(L, idx);
  647. api_check(L, ttistable(t), "table expected");
  648. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  649. invalidateTMcache(hvalue(t));
  650. luaC_barrierback(L, gcvalue(t), L->top-1);
  651. L->top -= 2;
  652. lua_unlock(L);
  653. }
  654. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  655. StkId t;
  656. lua_lock(L);
  657. api_checknelems(L, 1);
  658. t = index2addr(L, idx);
  659. api_check(L, ttistable(t), "table expected");
  660. luaH_setint(L, hvalue(t), n, L->top - 1);
  661. luaC_barrierback(L, gcvalue(t), L->top-1);
  662. L->top--;
  663. lua_unlock(L);
  664. }
  665. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  666. StkId t;
  667. TValue k;
  668. lua_lock(L);
  669. api_checknelems(L, 1);
  670. t = index2addr(L, idx);
  671. api_check(L, ttistable(t), "table expected");
  672. setpvalue(&k, cast(void *, p));
  673. setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
  674. luaC_barrierback(L, gcvalue(t), L->top - 1);
  675. L->top--;
  676. lua_unlock(L);
  677. }
  678. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  679. TValue *obj;
  680. Table *mt;
  681. lua_lock(L);
  682. api_checknelems(L, 1);
  683. obj = index2addr(L, objindex);
  684. if (ttisnil(L->top - 1))
  685. mt = NULL;
  686. else {
  687. api_check(L, ttistable(L->top - 1), "table expected");
  688. mt = hvalue(L->top - 1);
  689. }
  690. switch (ttnov(obj)) {
  691. case LUA_TTABLE: {
  692. hvalue(obj)->metatable = mt;
  693. if (mt) {
  694. luaC_objbarrier(L, gcvalue(obj), mt);
  695. luaC_checkfinalizer(L, gcvalue(obj), mt);
  696. }
  697. break;
  698. }
  699. case LUA_TUSERDATA: {
  700. uvalue(obj)->metatable = mt;
  701. if (mt) {
  702. luaC_objbarrier(L, rawuvalue(obj), mt);
  703. luaC_checkfinalizer(L, gcvalue(obj), mt);
  704. }
  705. break;
  706. }
  707. default: {
  708. G(L)->mt[ttnov(obj)] = mt;
  709. break;
  710. }
  711. }
  712. L->top--;
  713. lua_unlock(L);
  714. return 1;
  715. }
  716. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  717. StkId o;
  718. lua_lock(L);
  719. api_checknelems(L, 1);
  720. o = index2addr(L, idx);
  721. api_check(L, ttisfulluserdata(o), "full userdata expected");
  722. if (ttisnil(L->top - 1))
  723. uvalue(o)->env = NULL;
  724. else {
  725. api_check(L, ttistable(L->top - 1), "table expected");
  726. uvalue(o)->env = hvalue(L->top - 1);
  727. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  728. }
  729. L->top--;
  730. lua_unlock(L);
  731. }
  732. /*
  733. ** `load' and `call' functions (run Lua code)
  734. */
  735. #define checkresults(L,na,nr) \
  736. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  737. "results from function overflow current stack size")
  738. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  739. if (L->ci->callstatus & CIST_YIELDED) {
  740. if (ctx) *ctx = L->ci->u.c.ctx;
  741. return L->ci->u.c.status;
  742. }
  743. else return LUA_OK;
  744. }
  745. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  746. lua_CFunction k) {
  747. StkId func;
  748. lua_lock(L);
  749. api_check(L, k == NULL || !isLua(L->ci),
  750. "cannot use continuations inside hooks");
  751. api_checknelems(L, nargs+1);
  752. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  753. checkresults(L, nargs, nresults);
  754. func = L->top - (nargs+1);
  755. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  756. L->ci->u.c.k = k; /* save continuation */
  757. L->ci->u.c.ctx = ctx; /* save context */
  758. luaD_call(L, func, nresults, 1); /* do the call */
  759. }
  760. else /* no continuation or no yieldable */
  761. luaD_call(L, func, nresults, 0); /* just do the call */
  762. adjustresults(L, nresults);
  763. lua_unlock(L);
  764. }
  765. /*
  766. ** Execute a protected call.
  767. */
  768. struct CallS { /* data to `f_call' */
  769. StkId func;
  770. int nresults;
  771. };
  772. static void f_call (lua_State *L, void *ud) {
  773. struct CallS *c = cast(struct CallS *, ud);
  774. luaD_call(L, c->func, c->nresults, 0);
  775. }
  776. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  777. int ctx, lua_CFunction k) {
  778. struct CallS c;
  779. int status;
  780. ptrdiff_t func;
  781. lua_lock(L);
  782. api_check(L, k == NULL || !isLua(L->ci),
  783. "cannot use continuations inside hooks");
  784. api_checknelems(L, nargs+1);
  785. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  786. checkresults(L, nargs, nresults);
  787. if (errfunc == 0)
  788. func = 0;
  789. else {
  790. StkId o = index2addr(L, errfunc);
  791. api_checkstackindex(L, errfunc, o);
  792. func = savestack(L, o);
  793. }
  794. c.func = L->top - (nargs+1); /* function to be called */
  795. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  796. c.nresults = nresults; /* do a 'conventional' protected call */
  797. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  798. }
  799. else { /* prepare continuation (call is already protected by 'resume') */
  800. CallInfo *ci = L->ci;
  801. ci->u.c.k = k; /* save continuation */
  802. ci->u.c.ctx = ctx; /* save context */
  803. /* save information for error recovery */
  804. ci->extra = savestack(L, c.func);
  805. ci->u.c.old_allowhook = L->allowhook;
  806. ci->u.c.old_errfunc = L->errfunc;
  807. L->errfunc = func;
  808. /* mark that function may do error recovery */
  809. ci->callstatus |= CIST_YPCALL;
  810. luaD_call(L, c.func, nresults, 1); /* do the call */
  811. ci->callstatus &= ~CIST_YPCALL;
  812. L->errfunc = ci->u.c.old_errfunc;
  813. status = LUA_OK; /* if it is here, there were no errors */
  814. }
  815. adjustresults(L, nresults);
  816. lua_unlock(L);
  817. return status;
  818. }
  819. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  820. const char *chunkname, const char *mode) {
  821. ZIO z;
  822. int status;
  823. lua_lock(L);
  824. if (!chunkname) chunkname = "?";
  825. luaZ_init(L, &z, reader, data);
  826. status = luaD_protectedparser(L, &z, chunkname, mode);
  827. if (status == LUA_OK) { /* no errors? */
  828. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  829. if (f->nupvalues == 1) { /* does it have one upvalue? */
  830. /* get global table from registry */
  831. Table *reg = hvalue(&G(L)->l_registry);
  832. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  833. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  834. setobj(L, f->upvals[0]->v, gt);
  835. luaC_barrier(L, f->upvals[0], gt);
  836. }
  837. }
  838. lua_unlock(L);
  839. return status;
  840. }
  841. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  842. int status;
  843. TValue *o;
  844. lua_lock(L);
  845. api_checknelems(L, 1);
  846. o = L->top - 1;
  847. if (isLfunction(o))
  848. status = luaU_dump(L, getproto(o), writer, data, 0);
  849. else
  850. status = 1;
  851. lua_unlock(L);
  852. return status;
  853. }
  854. LUA_API int lua_status (lua_State *L) {
  855. return L->status;
  856. }
  857. /*
  858. ** Garbage-collection function
  859. */
  860. LUA_API int lua_gc (lua_State *L, int what, int data) {
  861. int res = 0;
  862. global_State *g;
  863. lua_lock(L);
  864. g = G(L);
  865. switch (what) {
  866. case LUA_GCSTOP: {
  867. g->gcrunning = 0;
  868. break;
  869. }
  870. case LUA_GCRESTART: {
  871. luaE_setdebt(g, 0);
  872. g->gcrunning = 1;
  873. break;
  874. }
  875. case LUA_GCCOLLECT: {
  876. luaC_fullgc(L, 0);
  877. break;
  878. }
  879. case LUA_GCCOUNT: {
  880. /* GC values are expressed in Kbytes: #bytes/2^10 */
  881. res = cast_int(gettotalbytes(g) >> 10);
  882. break;
  883. }
  884. case LUA_GCCOUNTB: {
  885. res = cast_int(gettotalbytes(g) & 0x3ff);
  886. break;
  887. }
  888. case LUA_GCSTEP: {
  889. l_mem debt = 1; /* =1 to signal that it did an actual step */
  890. int oldrunning = g->gcrunning;
  891. g->gcrunning = 1; /* force GC to run */
  892. if (data == 0) {
  893. luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
  894. luaC_step(L);
  895. }
  896. else { /* add 'data' to total debt */
  897. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  898. luaE_setdebt(g, debt);
  899. luaC_checkGC(L);
  900. }
  901. g->gcrunning = oldrunning; /* restore previous state */
  902. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  903. res = 1; /* signal it */
  904. break;
  905. }
  906. case LUA_GCSETPAUSE: {
  907. res = g->gcpause;
  908. g->gcpause = data;
  909. break;
  910. }
  911. case LUA_GCSETSTEPMUL: {
  912. res = g->gcstepmul;
  913. if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
  914. g->gcstepmul = data;
  915. break;
  916. }
  917. case LUA_GCISRUNNING: {
  918. res = g->gcrunning;
  919. break;
  920. }
  921. default: res = -1; /* invalid option */
  922. }
  923. lua_unlock(L);
  924. return res;
  925. }
  926. /*
  927. ** miscellaneous functions
  928. */
  929. LUA_API int lua_error (lua_State *L) {
  930. lua_lock(L);
  931. api_checknelems(L, 1);
  932. luaG_errormsg(L);
  933. /* code unreachable; will unlock when control actually leaves the kernel */
  934. return 0; /* to avoid warnings */
  935. }
  936. LUA_API int lua_next (lua_State *L, int idx) {
  937. StkId t;
  938. int more;
  939. lua_lock(L);
  940. t = index2addr(L, idx);
  941. api_check(L, ttistable(t), "table expected");
  942. more = luaH_next(L, hvalue(t), L->top - 1);
  943. if (more) {
  944. api_incr_top(L);
  945. }
  946. else /* no more elements */
  947. L->top -= 1; /* remove key */
  948. lua_unlock(L);
  949. return more;
  950. }
  951. LUA_API void lua_concat (lua_State *L, int n) {
  952. lua_lock(L);
  953. api_checknelems(L, n);
  954. if (n >= 2) {
  955. luaC_checkGC(L);
  956. luaV_concat(L, n);
  957. }
  958. else if (n == 0) { /* push empty string */
  959. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  960. api_incr_top(L);
  961. }
  962. /* else n == 1; nothing to do */
  963. lua_unlock(L);
  964. }
  965. LUA_API void lua_len (lua_State *L, int idx) {
  966. StkId t;
  967. lua_lock(L);
  968. t = index2addr(L, idx);
  969. luaV_objlen(L, L->top, t);
  970. api_incr_top(L);
  971. lua_unlock(L);
  972. }
  973. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  974. lua_Alloc f;
  975. lua_lock(L);
  976. if (ud) *ud = G(L)->ud;
  977. f = G(L)->frealloc;
  978. lua_unlock(L);
  979. return f;
  980. }
  981. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  982. lua_lock(L);
  983. G(L)->ud = ud;
  984. G(L)->frealloc = f;
  985. lua_unlock(L);
  986. }
  987. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  988. Udata *u;
  989. lua_lock(L);
  990. luaC_checkGC(L);
  991. u = luaS_newudata(L, size, NULL);
  992. setuvalue(L, L->top, u);
  993. api_incr_top(L);
  994. lua_unlock(L);
  995. return u + 1;
  996. }
  997. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  998. GCObject **owner, UpVal **uv) {
  999. switch (ttype(fi)) {
  1000. case LUA_TCCL: { /* C closure */
  1001. CClosure *f = clCvalue(fi);
  1002. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  1003. *val = &f->upvalue[n-1];
  1004. if (owner) *owner = obj2gco(f);
  1005. return "";
  1006. }
  1007. case LUA_TLCL: { /* Lua closure */
  1008. LClosure *f = clLvalue(fi);
  1009. TString *name;
  1010. Proto *p = f->p;
  1011. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1012. *val = f->upvals[n-1]->v;
  1013. if (uv) *uv = f->upvals[n - 1];
  1014. name = p->upvalues[n-1].name;
  1015. return (name == NULL) ? "" : getstr(name);
  1016. }
  1017. default: return NULL; /* not a closure */
  1018. }
  1019. }
  1020. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1021. const char *name;
  1022. TValue *val = NULL; /* to avoid warnings */
  1023. lua_lock(L);
  1024. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL);
  1025. if (name) {
  1026. setobj2s(L, L->top, val);
  1027. api_incr_top(L);
  1028. }
  1029. lua_unlock(L);
  1030. return name;
  1031. }
  1032. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1033. const char *name;
  1034. TValue *val = NULL; /* to avoid warnings */
  1035. GCObject *owner = NULL;
  1036. UpVal *uv = NULL;
  1037. StkId fi;
  1038. lua_lock(L);
  1039. fi = index2addr(L, funcindex);
  1040. api_checknelems(L, 1);
  1041. name = aux_upvalue(fi, n, &val, &owner, &uv);
  1042. if (name) {
  1043. L->top--;
  1044. setobj(L, val, L->top);
  1045. if (owner) { luaC_barrier(L, owner, L->top); }
  1046. else if (uv) { luaC_upvalbarrier(L, uv); }
  1047. }
  1048. lua_unlock(L);
  1049. return name;
  1050. }
  1051. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1052. LClosure *f;
  1053. StkId fi = index2addr(L, fidx);
  1054. api_check(L, ttisLclosure(fi), "Lua function expected");
  1055. f = clLvalue(fi);
  1056. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1057. if (pf) *pf = f;
  1058. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1059. }
  1060. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1061. StkId fi = index2addr(L, fidx);
  1062. switch (ttype(fi)) {
  1063. case LUA_TLCL: { /* lua closure */
  1064. return *getupvalref(L, fidx, n, NULL);
  1065. }
  1066. case LUA_TCCL: { /* C closure */
  1067. CClosure *f = clCvalue(fi);
  1068. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1069. return &f->upvalue[n - 1];
  1070. }
  1071. default: {
  1072. api_check(L, 0, "closure expected");
  1073. return NULL;
  1074. }
  1075. }
  1076. }
  1077. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1078. int fidx2, int n2) {
  1079. LClosure *f1;
  1080. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1081. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1082. luaC_upvdeccount(L, *up1);
  1083. *up1 = *up2;
  1084. (*up1)->refcount++;
  1085. if (upisopen(*up1)) (*up1)->u.open.touched = 1;
  1086. luaC_upvalbarrier(L, *up1);
  1087. }