lapi.c 30 KB

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