lapi.c 30 KB

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