lapi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /*
  2. ** $Id: lapi.c,v 2.113 2010/02/09 11:55:37 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. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject, \
  28. "invalid index")
  29. static Table *getcurrenv (lua_State *L) {
  30. if (L->ci->previous == NULL) /* no enclosing function? */
  31. return G(L)->l_gt; /* use global table as environment */
  32. else {
  33. Closure *func = curr_func(L);
  34. return func->c.env;
  35. }
  36. }
  37. static TValue *index2addr (lua_State *L, int idx) {
  38. CallInfo *ci = L->ci;
  39. if (idx > 0) {
  40. TValue *o = ci->func + idx;
  41. api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
  42. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  43. else return o;
  44. }
  45. else if (idx > LUA_REGISTRYINDEX) {
  46. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  47. return L->top + idx;
  48. }
  49. else switch (idx) { /* pseudo-indices */
  50. case LUA_REGISTRYINDEX: return &G(L)->l_registry;
  51. case LUA_ENVIRONINDEX: {
  52. sethvalue(L, &L->env, getcurrenv(L));
  53. return &L->env;
  54. }
  55. default: {
  56. Closure *func = curr_func(L);
  57. idx = LUA_ENVIRONINDEX - idx;
  58. api_check(L, idx <= UCHAR_MAX + 1, "upvalue index too large");
  59. return (idx <= func->c.nupvalues)
  60. ? &func->c.upvalue[idx-1]
  61. : cast(TValue *, luaO_nilobject);
  62. }
  63. }
  64. }
  65. /*
  66. ** to be caled by 'lua_checkstack' in protected mode, to grow stack
  67. ** capturing memory errors
  68. */
  69. static void growstack (lua_State *L, void *ud) {
  70. int size = *(int *)ud;
  71. luaD_growstack(L, size);
  72. }
  73. LUA_API int lua_checkstack (lua_State *L, int size) {
  74. int res;
  75. CallInfo *ci = L->ci;
  76. lua_lock(L);
  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 = 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. LUA_API int lua_gettop (lua_State *L) {
  121. return cast_int(L->top - (L->ci->func + 1));
  122. }
  123. LUA_API void lua_settop (lua_State *L, int idx) {
  124. StkId func = L->ci->func;
  125. lua_lock(L);
  126. if (idx >= 0) {
  127. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  128. while (L->top < (func + 1) + idx)
  129. setnilvalue(L->top++);
  130. L->top = (func + 1) + idx;
  131. }
  132. else {
  133. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  134. L->top += idx+1; /* `subtract' index (index is negative) */
  135. }
  136. lua_unlock(L);
  137. }
  138. LUA_API void lua_remove (lua_State *L, int idx) {
  139. StkId p;
  140. lua_lock(L);
  141. p = index2addr(L, idx);
  142. api_checkvalidindex(L, p);
  143. while (++p < L->top) setobjs2s(L, p-1, p);
  144. L->top--;
  145. lua_unlock(L);
  146. }
  147. LUA_API void lua_insert (lua_State *L, int idx) {
  148. StkId p;
  149. StkId q;
  150. lua_lock(L);
  151. p = index2addr(L, idx);
  152. api_checkvalidindex(L, p);
  153. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  154. setobjs2s(L, p, L->top);
  155. lua_unlock(L);
  156. }
  157. static void moveto (lua_State *L, TValue *fr, int idx) {
  158. TValue *to = index2addr(L, idx);
  159. api_checkvalidindex(L, to);
  160. if (idx == LUA_ENVIRONINDEX) {
  161. Closure *func = curr_func(L);
  162. api_check(L, ttistable(fr), "table expected");
  163. func->c.env = hvalue(fr);
  164. luaC_barrier(L, func, fr);
  165. }
  166. else {
  167. setobj(L, to, fr);
  168. if (idx < LUA_ENVIRONINDEX) /* function upvalue? */
  169. luaC_barrier(L, curr_func(L), fr);
  170. }
  171. /* LUA_REGISTRYINDEX does not need gc barrier
  172. (collector revisits it before finishing collection) */
  173. }
  174. LUA_API void lua_replace (lua_State *L, int idx) {
  175. lua_lock(L);
  176. /* explicit test for incompatible code */
  177. if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
  178. luaG_runerror(L, "no calling environment");
  179. api_checknelems(L, 1);
  180. moveto(L, L->top - 1, idx);
  181. L->top--;
  182. lua_unlock(L);
  183. }
  184. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  185. TValue *fr;
  186. lua_lock(L);
  187. fr = index2addr(L, fromidx);
  188. api_checkvalidindex(L, fr);
  189. moveto(L, fr, toidx);
  190. lua_unlock(L);
  191. }
  192. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  193. lua_lock(L);
  194. setobj2s(L, L->top, index2addr(L, idx));
  195. api_incr_top(L);
  196. lua_unlock(L);
  197. }
  198. /*
  199. ** access functions (stack -> C)
  200. */
  201. LUA_API int lua_type (lua_State *L, int idx) {
  202. StkId o = index2addr(L, idx);
  203. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  204. }
  205. LUA_API const char *lua_typename (lua_State *L, int t) {
  206. UNUSED(L);
  207. return typename(t);
  208. }
  209. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  210. StkId o = index2addr(L, idx);
  211. return iscfunction(o);
  212. }
  213. LUA_API int lua_isnumber (lua_State *L, int idx) {
  214. TValue n;
  215. const TValue *o = index2addr(L, idx);
  216. return tonumber(o, &n);
  217. }
  218. LUA_API int lua_isstring (lua_State *L, int idx) {
  219. int t = lua_type(L, idx);
  220. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  221. }
  222. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  223. const TValue *o = index2addr(L, idx);
  224. return (ttisuserdata(o) || ttislightuserdata(o));
  225. }
  226. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  227. StkId o1 = index2addr(L, index1);
  228. StkId o2 = index2addr(L, index2);
  229. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  230. : luaO_rawequalObj(o1, o2);
  231. }
  232. LUA_API void lua_arith (lua_State *L, int op) {
  233. lua_lock(L);
  234. api_checknelems(L, 2);
  235. if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1)) {
  236. changenvalue(L->top - 2,
  237. luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
  238. }
  239. else
  240. luaV_arith(L, L->top - 2, L->top - 2, L->top - 1,
  241. cast(TMS, op - LUA_OPADD + TM_ADD));
  242. L->top--;
  243. lua_unlock(L);
  244. }
  245. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  246. StkId o1, o2;
  247. int i;
  248. lua_lock(L); /* may call tag method */
  249. o1 = index2addr(L, index1);
  250. o2 = index2addr(L, index2);
  251. if (o1 == luaO_nilobject || o2 == luaO_nilobject)
  252. i = 0;
  253. else switch (op) {
  254. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  255. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  256. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  257. default: api_check(L, 0, "invalid option"); i = 0;
  258. }
  259. lua_unlock(L);
  260. return i;
  261. }
  262. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  263. TValue n;
  264. const TValue *o = index2addr(L, idx);
  265. if (tonumber(o, &n))
  266. return nvalue(o);
  267. else
  268. return 0;
  269. }
  270. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  271. TValue n;
  272. const TValue *o = index2addr(L, idx);
  273. if (tonumber(o, &n)) {
  274. lua_Integer res;
  275. lua_Number num = nvalue(o);
  276. lua_number2integer(res, num);
  277. return res;
  278. }
  279. else
  280. return 0;
  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 (ttype(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. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  314. }
  315. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  316. StkId o = index2addr(L, idx);
  317. switch (ttype(o)) {
  318. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  319. case LUA_TLIGHTUSERDATA: return pvalue(o);
  320. default: return NULL;
  321. }
  322. }
  323. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  324. StkId o = index2addr(L, idx);
  325. return (!ttisthread(o)) ? NULL : thvalue(o);
  326. }
  327. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  328. StkId o = index2addr(L, idx);
  329. switch (ttype(o)) {
  330. case LUA_TTABLE: return hvalue(o);
  331. case LUA_TFUNCTION: return clvalue(o);
  332. case LUA_TTHREAD: return thvalue(o);
  333. case LUA_TUSERDATA:
  334. case LUA_TLIGHTUSERDATA:
  335. return lua_touserdata(L, idx);
  336. default: return NULL;
  337. }
  338. }
  339. /*
  340. ** push functions (C -> stack)
  341. */
  342. LUA_API void lua_pushnil (lua_State *L) {
  343. lua_lock(L);
  344. setnilvalue(L->top);
  345. api_incr_top(L);
  346. lua_unlock(L);
  347. }
  348. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  349. lua_lock(L);
  350. setnvalue(L->top, n);
  351. api_incr_top(L);
  352. lua_unlock(L);
  353. }
  354. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  355. lua_lock(L);
  356. setnvalue(L->top, cast_num(n));
  357. api_incr_top(L);
  358. lua_unlock(L);
  359. }
  360. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  361. TString *ts;
  362. lua_lock(L);
  363. luaC_checkGC(L);
  364. ts = luaS_newlstr(L, s, len);
  365. setsvalue2s(L, L->top, ts);
  366. api_incr_top(L);
  367. lua_unlock(L);
  368. return getstr(ts);
  369. }
  370. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  371. if (s == NULL) {
  372. lua_pushnil(L);
  373. return NULL;
  374. }
  375. else
  376. return lua_pushlstring(L, s, strlen(s));
  377. }
  378. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  379. va_list argp) {
  380. const char *ret;
  381. lua_lock(L);
  382. luaC_checkGC(L);
  383. ret = luaO_pushvfstring(L, fmt, argp);
  384. lua_unlock(L);
  385. return ret;
  386. }
  387. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  388. const char *ret;
  389. va_list argp;
  390. lua_lock(L);
  391. luaC_checkGC(L);
  392. va_start(argp, fmt);
  393. ret = luaO_pushvfstring(L, fmt, argp);
  394. va_end(argp);
  395. lua_unlock(L);
  396. return ret;
  397. }
  398. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  399. Closure *cl;
  400. lua_lock(L);
  401. api_checknelems(L, n);
  402. api_check(L, n <= UCHAR_MAX, "upvalue index too large");
  403. luaC_checkGC(L);
  404. cl = luaF_newCclosure(L, n, getcurrenv(L));
  405. cl->c.f = fn;
  406. L->top -= n;
  407. while (n--)
  408. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  409. setclvalue(L, L->top, cl);
  410. lua_assert(iswhite(obj2gco(cl)));
  411. api_incr_top(L);
  412. lua_unlock(L);
  413. }
  414. LUA_API void lua_pushboolean (lua_State *L, int b) {
  415. lua_lock(L);
  416. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  417. api_incr_top(L);
  418. lua_unlock(L);
  419. }
  420. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  421. lua_lock(L);
  422. setpvalue(L->top, p);
  423. api_incr_top(L);
  424. lua_unlock(L);
  425. }
  426. LUA_API int lua_pushthread (lua_State *L) {
  427. lua_lock(L);
  428. setthvalue(L, L->top, L);
  429. api_incr_top(L);
  430. lua_unlock(L);
  431. return (G(L)->mainthread == L);
  432. }
  433. /*
  434. ** get functions (Lua -> stack)
  435. */
  436. LUA_API void lua_gettable (lua_State *L, int idx) {
  437. StkId t;
  438. lua_lock(L);
  439. t = index2addr(L, idx);
  440. api_checkvalidindex(L, t);
  441. luaV_gettable(L, t, L->top - 1, L->top - 1);
  442. lua_unlock(L);
  443. }
  444. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  445. StkId t;
  446. lua_lock(L);
  447. t = index2addr(L, idx);
  448. api_checkvalidindex(L, t);
  449. setsvalue2s(L, L->top, luaS_new(L, k));
  450. api_incr_top(L);
  451. luaV_gettable(L, t, L->top - 1, L->top - 1);
  452. lua_unlock(L);
  453. }
  454. LUA_API void lua_rawget (lua_State *L, int idx) {
  455. StkId t;
  456. lua_lock(L);
  457. t = index2addr(L, idx);
  458. api_check(L, ttistable(t), "table expected");
  459. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  460. lua_unlock(L);
  461. }
  462. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  463. StkId o;
  464. lua_lock(L);
  465. o = index2addr(L, idx);
  466. api_check(L, ttistable(o), "table expected");
  467. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  468. api_incr_top(L);
  469. lua_unlock(L);
  470. }
  471. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  472. Table *t;
  473. lua_lock(L);
  474. luaC_checkGC(L);
  475. t = luaH_new(L);
  476. sethvalue(L, L->top, t);
  477. api_incr_top(L);
  478. if (narray > 0 || nrec > 0)
  479. luaH_resize(L, t, narray, nrec);
  480. lua_unlock(L);
  481. }
  482. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  483. const TValue *obj;
  484. Table *mt = NULL;
  485. int res;
  486. lua_lock(L);
  487. obj = index2addr(L, objindex);
  488. switch (ttype(obj)) {
  489. case LUA_TTABLE:
  490. mt = hvalue(obj)->metatable;
  491. break;
  492. case LUA_TUSERDATA:
  493. mt = uvalue(obj)->metatable;
  494. break;
  495. default:
  496. mt = G(L)->mt[ttype(obj)];
  497. break;
  498. }
  499. if (mt == NULL)
  500. res = 0;
  501. else {
  502. sethvalue(L, L->top, mt);
  503. api_incr_top(L);
  504. res = 1;
  505. }
  506. lua_unlock(L);
  507. return res;
  508. }
  509. LUA_API void lua_getfenv (lua_State *L, int idx) {
  510. StkId o;
  511. lua_lock(L);
  512. o = index2addr(L, idx);
  513. api_checkvalidindex(L, o);
  514. switch (ttype(o)) {
  515. case LUA_TFUNCTION:
  516. sethvalue(L, L->top, clvalue(o)->c.env);
  517. break;
  518. case LUA_TUSERDATA:
  519. sethvalue(L, L->top, uvalue(o)->env);
  520. break;
  521. default:
  522. setnilvalue(L->top);
  523. break;
  524. }
  525. api_incr_top(L);
  526. lua_unlock(L);
  527. }
  528. /*
  529. ** set functions (stack -> Lua)
  530. */
  531. LUA_API void lua_settable (lua_State *L, int idx) {
  532. StkId t;
  533. lua_lock(L);
  534. api_checknelems(L, 2);
  535. t = index2addr(L, idx);
  536. api_checkvalidindex(L, t);
  537. luaV_settable(L, t, L->top - 2, L->top - 1);
  538. L->top -= 2; /* pop index and value */
  539. lua_unlock(L);
  540. }
  541. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  542. StkId t;
  543. lua_lock(L);
  544. api_checknelems(L, 1);
  545. t = index2addr(L, idx);
  546. api_checkvalidindex(L, t);
  547. setsvalue2s(L, L->top++, luaS_new(L, k));
  548. luaV_settable(L, t, L->top - 1, L->top - 2);
  549. L->top -= 2; /* pop value and key */
  550. lua_unlock(L);
  551. }
  552. LUA_API void lua_rawset (lua_State *L, int idx) {
  553. StkId t;
  554. lua_lock(L);
  555. api_checknelems(L, 2);
  556. t = index2addr(L, idx);
  557. api_check(L, ttistable(t), "table expected");
  558. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  559. luaC_barriert(L, hvalue(t), L->top-1);
  560. L->top -= 2;
  561. lua_unlock(L);
  562. }
  563. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  564. StkId o;
  565. lua_lock(L);
  566. api_checknelems(L, 1);
  567. o = index2addr(L, idx);
  568. api_check(L, ttistable(o), "table expected");
  569. setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
  570. luaC_barriert(L, hvalue(o), L->top-1);
  571. L->top--;
  572. lua_unlock(L);
  573. }
  574. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  575. TValue *obj;
  576. Table *mt;
  577. lua_lock(L);
  578. api_checknelems(L, 1);
  579. obj = index2addr(L, objindex);
  580. api_checkvalidindex(L, obj);
  581. if (ttisnil(L->top - 1))
  582. mt = NULL;
  583. else {
  584. api_check(L, ttistable(L->top - 1), "table expected");
  585. mt = hvalue(L->top - 1);
  586. }
  587. switch (ttype(obj)) {
  588. case LUA_TTABLE: {
  589. hvalue(obj)->metatable = mt;
  590. if (mt)
  591. luaC_objbarriert(L, hvalue(obj), mt);
  592. break;
  593. }
  594. case LUA_TUSERDATA: {
  595. uvalue(obj)->metatable = mt;
  596. if (mt) {
  597. luaC_objbarrier(L, rawuvalue(obj), mt);
  598. luaC_checkfinalizer(L, rawuvalue(obj));
  599. }
  600. break;
  601. }
  602. default: {
  603. G(L)->mt[ttype(obj)] = mt;
  604. break;
  605. }
  606. }
  607. L->top--;
  608. lua_unlock(L);
  609. return 1;
  610. }
  611. LUA_API int lua_setfenv (lua_State *L, int idx) {
  612. StkId o;
  613. int res = 1;
  614. lua_lock(L);
  615. api_checknelems(L, 1);
  616. o = index2addr(L, idx);
  617. api_checkvalidindex(L, o);
  618. api_check(L, ttistable(L->top - 1), "table expected");
  619. switch (ttype(o)) {
  620. case LUA_TFUNCTION:
  621. clvalue(o)->c.env = hvalue(L->top - 1);
  622. break;
  623. case LUA_TUSERDATA:
  624. uvalue(o)->env = hvalue(L->top - 1);
  625. break;
  626. default:
  627. res = 0;
  628. break;
  629. }
  630. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  631. L->top--;
  632. lua_unlock(L);
  633. return res;
  634. }
  635. /*
  636. ** `load' and `call' functions (run Lua code)
  637. */
  638. #define checkresults(L,na,nr) \
  639. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  640. "results from function overflow current stack size")
  641. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  642. if (L->ci->callstatus & CIST_YIELDED) {
  643. if (ctx) *ctx = L->ci->u.c.ctx;
  644. return L->ci->u.c.status;
  645. }
  646. else return LUA_OK;
  647. }
  648. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  649. lua_CFunction k) {
  650. StkId func;
  651. lua_lock(L);
  652. api_check(L, k == NULL || !isLua(L->ci),
  653. "cannot use continuations inside hooks");
  654. api_checknelems(L, nargs+1);
  655. checkresults(L, nargs, nresults);
  656. func = L->top - (nargs+1);
  657. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  658. L->ci->u.c.k = k; /* save continuation */
  659. L->ci->u.c.ctx = ctx; /* save context */
  660. luaD_call(L, func, nresults, 1); /* do the call */
  661. }
  662. else /* no continuation or no yieldable */
  663. luaD_call(L, func, nresults, 0); /* just do the call */
  664. adjustresults(L, nresults);
  665. lua_unlock(L);
  666. }
  667. /*
  668. ** Execute a protected call.
  669. */
  670. struct CallS { /* data to `f_call' */
  671. StkId func;
  672. int nresults;
  673. };
  674. static void f_call (lua_State *L, void *ud) {
  675. struct CallS *c = cast(struct CallS *, ud);
  676. luaD_call(L, c->func, c->nresults, 0);
  677. }
  678. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  679. int ctx, lua_CFunction k) {
  680. struct CallS c;
  681. int status;
  682. ptrdiff_t func;
  683. lua_lock(L);
  684. api_check(L, k == NULL || !isLua(L->ci),
  685. "cannot use continuations inside hooks");
  686. api_checknelems(L, nargs+1);
  687. checkresults(L, nargs, nresults);
  688. if (errfunc == 0)
  689. func = 0;
  690. else {
  691. StkId o = index2addr(L, errfunc);
  692. api_checkvalidindex(L, o);
  693. func = savestack(L, o);
  694. }
  695. c.func = L->top - (nargs+1); /* function to be called */
  696. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  697. c.nresults = nresults; /* do a 'conventional' protected call */
  698. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  699. }
  700. else { /* prepare continuation (call is already protected by 'resume') */
  701. CallInfo *ci = L->ci;
  702. ci->u.c.k = k; /* save continuation */
  703. ci->u.c.ctx = ctx; /* save context */
  704. /* save information for error recovery */
  705. ci->u.c.extra = savestack(L, c.func);
  706. ci->u.c.old_allowhook = L->allowhook;
  707. ci->u.c.old_errfunc = L->errfunc;
  708. L->errfunc = func;
  709. /* mark that function may do error recovery */
  710. ci->callstatus |= CIST_YPCALL;
  711. luaD_call(L, c.func, nresults, 1); /* do the call */
  712. ci->callstatus &= ~CIST_YPCALL;
  713. L->errfunc = ci->u.c.old_errfunc;
  714. status = LUA_OK; /* if it is here, there were no errors */
  715. }
  716. adjustresults(L, nresults);
  717. lua_unlock(L);
  718. return status;
  719. }
  720. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  721. const char *chunkname) {
  722. ZIO z;
  723. int status;
  724. lua_lock(L);
  725. if (!chunkname) chunkname = "?";
  726. luaZ_init(L, &z, reader, data);
  727. status = luaD_protectedparser(L, &z, chunkname);
  728. if (status == LUA_OK) {
  729. Closure *f = clvalue(L->top - 1);
  730. lua_assert(!f->c.isC);
  731. if (f->l.nupvalues == 1)
  732. sethvalue(L, f->l.upvals[0]->v, G(L)->l_gt);
  733. }
  734. lua_unlock(L);
  735. return status;
  736. }
  737. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  738. int status;
  739. TValue *o;
  740. lua_lock(L);
  741. api_checknelems(L, 1);
  742. o = L->top - 1;
  743. if (isLfunction(o))
  744. status = luaU_dump(L, getproto(o), writer, data, 0);
  745. else
  746. status = 1;
  747. lua_unlock(L);
  748. return status;
  749. }
  750. LUA_API int lua_status (lua_State *L) {
  751. return L->status;
  752. }
  753. /*
  754. ** Garbage-collection function
  755. */
  756. LUA_API int lua_gc (lua_State *L, int what, int data) {
  757. int res = 0;
  758. global_State *g;
  759. lua_lock(L);
  760. g = G(L);
  761. switch (what) {
  762. case LUA_GCSTOP: {
  763. g->GCthreshold = MAX_LUMEM;
  764. break;
  765. }
  766. case LUA_GCRESTART: {
  767. g->GCthreshold = g->totalbytes;
  768. break;
  769. }
  770. case LUA_GCCOLLECT: {
  771. luaC_fullgc(L, 0);
  772. break;
  773. }
  774. case LUA_GCCOUNT: {
  775. /* GC values are expressed in Kbytes: #bytes/2^10 */
  776. res = cast_int(g->totalbytes >> 10);
  777. break;
  778. }
  779. case LUA_GCCOUNTB: {
  780. res = cast_int(g->totalbytes & 0x3ff);
  781. break;
  782. }
  783. case LUA_GCSTEP: {
  784. lu_mem oldts = g->GCthreshold;
  785. lu_mem a = (cast(lu_mem, data) << 10);
  786. g->GCthreshold = (a <= g->totalbytes) ? g->totalbytes - a : 0;
  787. while (g->GCthreshold <= g->totalbytes) {
  788. luaC_step(L);
  789. if (g->gcstate == GCSpause) { /* end of cycle? */
  790. res = 1; /* signal it */
  791. break;
  792. }
  793. }
  794. if (oldts == MAX_LUMEM) /* collector was stopped? */
  795. g->GCthreshold = oldts; /* keep it that way */
  796. break;
  797. }
  798. case LUA_GCSETPAUSE: {
  799. res = g->gcpause;
  800. g->gcpause = data;
  801. break;
  802. }
  803. case LUA_GCSETSTEPMUL: {
  804. res = g->gcstepmul;
  805. g->gcstepmul = data;
  806. break;
  807. }
  808. case LUA_GCISRUNNING: {
  809. res = (g->GCthreshold != MAX_LUMEM);
  810. break;
  811. }
  812. default: res = -1; /* invalid option */
  813. }
  814. lua_unlock(L);
  815. return res;
  816. }
  817. /*
  818. ** miscellaneous functions
  819. */
  820. LUA_API int lua_error (lua_State *L) {
  821. lua_lock(L);
  822. api_checknelems(L, 1);
  823. luaG_errormsg(L);
  824. lua_unlock(L);
  825. return 0; /* to avoid warnings */
  826. }
  827. LUA_API int lua_next (lua_State *L, int idx) {
  828. StkId t;
  829. int more;
  830. lua_lock(L);
  831. t = index2addr(L, idx);
  832. api_check(L, ttistable(t), "table expected");
  833. more = luaH_next(L, hvalue(t), L->top - 1);
  834. if (more) {
  835. api_incr_top(L);
  836. }
  837. else /* no more elements */
  838. L->top -= 1; /* remove key */
  839. lua_unlock(L);
  840. return more;
  841. }
  842. LUA_API void lua_concat (lua_State *L, int n) {
  843. lua_lock(L);
  844. api_checknelems(L, n);
  845. if (n >= 2) {
  846. luaC_checkGC(L);
  847. luaV_concat(L, n);
  848. }
  849. else if (n == 0) { /* push empty string */
  850. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  851. api_incr_top(L);
  852. }
  853. /* else n == 1; nothing to do */
  854. lua_unlock(L);
  855. }
  856. LUA_API void lua_len (lua_State *L, int idx) {
  857. StkId t;
  858. lua_lock(L);
  859. t = index2addr(L, idx);
  860. luaV_objlen(L, L->top, t);
  861. api_incr_top(L);
  862. lua_unlock(L);
  863. }
  864. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  865. lua_Alloc f;
  866. lua_lock(L);
  867. if (ud) *ud = G(L)->ud;
  868. f = G(L)->frealloc;
  869. lua_unlock(L);
  870. return f;
  871. }
  872. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  873. lua_lock(L);
  874. G(L)->ud = ud;
  875. G(L)->frealloc = f;
  876. lua_unlock(L);
  877. }
  878. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  879. Udata *u;
  880. lua_lock(L);
  881. luaC_checkGC(L);
  882. u = luaS_newudata(L, size, getcurrenv(L));
  883. setuvalue(L, L->top, u);
  884. api_incr_top(L);
  885. lua_unlock(L);
  886. return u + 1;
  887. }
  888. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  889. Closure *f;
  890. if (!ttisfunction(fi)) return NULL;
  891. f = clvalue(fi);
  892. if (f->c.isC) {
  893. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  894. *val = &f->c.upvalue[n-1];
  895. return "";
  896. }
  897. else {
  898. Proto *p = f->l.p;
  899. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  900. *val = f->l.upvals[n-1]->v;
  901. return getstr(p->upvalues[n-1].name);
  902. }
  903. }
  904. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  905. const char *name;
  906. TValue *val;
  907. lua_lock(L);
  908. name = aux_upvalue(index2addr(L, funcindex), n, &val);
  909. if (name) {
  910. setobj2s(L, L->top, val);
  911. api_incr_top(L);
  912. }
  913. lua_unlock(L);
  914. return name;
  915. }
  916. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  917. const char *name;
  918. TValue *val;
  919. StkId fi;
  920. lua_lock(L);
  921. fi = index2addr(L, funcindex);
  922. api_checknelems(L, 1);
  923. name = aux_upvalue(fi, n, &val);
  924. if (name) {
  925. L->top--;
  926. setobj(L, val, L->top);
  927. luaC_barrier(L, clvalue(fi), L->top);
  928. }
  929. lua_unlock(L);
  930. return name;
  931. }
  932. static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
  933. Closure *f;
  934. Proto *p;
  935. StkId fi = index2addr(L, fidx);
  936. api_check(L, ttisfunction(fi), "function expected");
  937. f = clvalue(fi);
  938. api_check(L, !f->c.isC, "Lua function expected");
  939. p = f->l.p;
  940. api_check(L, (1 <= n && n <= p->sizeupvalues), "invalid upvalue index");
  941. if (pf) *pf = f;
  942. return &f->l.upvals[n - 1]; /* get its upvalue pointer */
  943. }
  944. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  945. Closure *f;
  946. StkId fi = index2addr(L, fidx);
  947. api_check(L, ttisfunction(fi), "function expected");
  948. f = clvalue(fi);
  949. if (f->c.isC) {
  950. api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
  951. return &f->c.upvalue[n - 1];
  952. }
  953. else return *getupvalref(L, fidx, n, NULL);
  954. }
  955. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  956. int fidx2, int n2) {
  957. Closure *f1;
  958. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  959. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  960. *up1 = *up2;
  961. luaC_objbarrier(L, f1, *up2);
  962. }