lapi.c 29 KB

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