2
0

lapi.c 29 KB

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