lapi.c 31 KB

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