lapi.c 30 KB

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