lapi.c 29 KB

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