lapi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /*
  2. ** $Id: lapi.c,v 2.130 2010/05/31 16:08:55 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 TValue *index2addr (lua_State *L, int idx) {
  30. CallInfo *ci = L->ci;
  31. if (idx > 0) {
  32. TValue *o = ci->func + idx;
  33. api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
  34. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  35. else return o;
  36. }
  37. else if (idx > LUA_REGISTRYINDEX) {
  38. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  39. return L->top + idx;
  40. }
  41. else if (idx == LUA_REGISTRYINDEX)
  42. return &G(L)->l_registry;
  43. else { /* upvalues */
  44. idx = LUA_REGISTRYINDEX - idx;
  45. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  46. if (ttislcf(ci->func)) /* light C function? */
  47. return cast(TValue *, luaO_nilobject); /* it has no upvalues */
  48. else {
  49. Closure *func = clvalue(ci->func);
  50. return (idx <= func->c.nupvalues)
  51. ? &func->c.upvalue[idx-1]
  52. : cast(TValue *, luaO_nilobject);
  53. }
  54. }
  55. }
  56. /*
  57. ** to be called by 'lua_checkstack' in protected mode, to grow stack
  58. ** capturing memory errors
  59. */
  60. static void growstack (lua_State *L, void *ud) {
  61. int size = *(int *)ud;
  62. luaD_growstack(L, size);
  63. }
  64. LUA_API int lua_checkstack (lua_State *L, int size) {
  65. int res;
  66. CallInfo *ci = L->ci;
  67. lua_lock(L);
  68. if (L->stack_last - L->top > size) /* stack large enough? */
  69. res = 1; /* yes; check is OK */
  70. else { /* no; need to grow stack */
  71. int inuse = L->top - L->stack + EXTRA_STACK;
  72. if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
  73. res = 0; /* no */
  74. else /* try to grow stack */
  75. res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
  76. }
  77. if (res && ci->top < L->top + size)
  78. ci->top = L->top + size; /* adjust frame top */
  79. lua_unlock(L);
  80. return res;
  81. }
  82. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  83. int i;
  84. if (from == to) return;
  85. lua_lock(to);
  86. api_checknelems(from, n);
  87. api_check(from, G(from) == G(to), "moving among independent states");
  88. api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
  89. from->top -= n;
  90. for (i = 0; i < n; i++) {
  91. setobj2s(to, to->top++, from->top + i);
  92. }
  93. lua_unlock(to);
  94. }
  95. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  96. lua_CFunction old;
  97. lua_lock(L);
  98. old = G(L)->panic;
  99. G(L)->panic = panicf;
  100. lua_unlock(L);
  101. return old;
  102. }
  103. LUA_API const lua_Number *lua_version (lua_State *L) {
  104. static const lua_Number version = LUA_VERSION_NUM;
  105. if (L == NULL) return &version;
  106. else return G(L)->version;
  107. }
  108. /*
  109. ** basic stack manipulation
  110. */
  111. /*
  112. ** convert an acceptable stack index into an absolute index
  113. */
  114. LUA_API int lua_absindex (lua_State *L, int idx) {
  115. return (idx > 0 || idx <= LUA_REGISTRYINDEX)
  116. ? idx
  117. : cast_int(L->top - L->ci->func + idx);
  118. }
  119. LUA_API int lua_gettop (lua_State *L) {
  120. return cast_int(L->top - (L->ci->func + 1));
  121. }
  122. LUA_API void lua_settop (lua_State *L, int idx) {
  123. StkId func = L->ci->func;
  124. lua_lock(L);
  125. if (idx >= 0) {
  126. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  127. while (L->top < (func + 1) + idx)
  128. setnilvalue(L->top++);
  129. L->top = (func + 1) + idx;
  130. }
  131. else {
  132. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  133. L->top += idx+1; /* `subtract' index (index is negative) */
  134. }
  135. lua_unlock(L);
  136. }
  137. LUA_API void lua_remove (lua_State *L, int idx) {
  138. StkId p;
  139. lua_lock(L);
  140. p = index2addr(L, idx);
  141. api_checkvalidindex(L, p);
  142. while (++p < L->top) setobjs2s(L, p-1, p);
  143. L->top--;
  144. lua_unlock(L);
  145. }
  146. LUA_API void lua_insert (lua_State *L, int idx) {
  147. StkId p;
  148. StkId q;
  149. lua_lock(L);
  150. p = index2addr(L, idx);
  151. api_checkvalidindex(L, p);
  152. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  153. setobjs2s(L, p, L->top);
  154. lua_unlock(L);
  155. }
  156. static void moveto (lua_State *L, TValue *fr, int idx) {
  157. TValue *to = index2addr(L, idx);
  158. api_checkvalidindex(L, to);
  159. setobj(L, to, fr);
  160. if (idx < LUA_REGISTRYINDEX) { /* function upvalue? */
  161. lua_assert(ttisclosure(L->ci->func));
  162. luaC_barrier(L, clvalue(L->ci->func), fr);
  163. }
  164. /* LUA_REGISTRYINDEX does not need gc barrier
  165. (collector revisits it before finishing collection) */
  166. }
  167. LUA_API void lua_replace (lua_State *L, int idx) {
  168. lua_lock(L);
  169. api_checknelems(L, 1);
  170. moveto(L, L->top - 1, idx);
  171. L->top--;
  172. lua_unlock(L);
  173. }
  174. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  175. TValue *fr;
  176. lua_lock(L);
  177. fr = index2addr(L, fromidx);
  178. api_checkvalidindex(L, fr);
  179. moveto(L, fr, toidx);
  180. lua_unlock(L);
  181. }
  182. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  183. lua_lock(L);
  184. setobj2s(L, L->top, index2addr(L, idx));
  185. api_incr_top(L);
  186. lua_unlock(L);
  187. }
  188. /*
  189. ** access functions (stack -> C)
  190. */
  191. LUA_API int lua_type (lua_State *L, int idx) {
  192. StkId o = index2addr(L, idx);
  193. return (o == luaO_nilobject) ? LUA_TNONE : ttypenv(o);
  194. }
  195. LUA_API const char *lua_typename (lua_State *L, int t) {
  196. UNUSED(L);
  197. return ttypename(t);
  198. }
  199. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  200. StkId o = index2addr(L, idx);
  201. return (ttislcf(o) || (ttisclosure(o) && clvalue(o)->c.isC));
  202. }
  203. LUA_API int lua_isnumber (lua_State *L, int idx) {
  204. TValue n;
  205. const TValue *o = index2addr(L, idx);
  206. return tonumber(o, &n);
  207. }
  208. LUA_API int lua_isstring (lua_State *L, int idx) {
  209. int t = lua_type(L, idx);
  210. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  211. }
  212. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  213. const TValue *o = index2addr(L, idx);
  214. return (ttisuserdata(o) || ttislightuserdata(o));
  215. }
  216. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  217. StkId o1 = index2addr(L, index1);
  218. StkId o2 = index2addr(L, index2);
  219. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  220. : luaO_rawequalObj(o1, o2);
  221. }
  222. LUA_API void lua_arith (lua_State *L, int op) {
  223. lua_lock(L);
  224. api_checknelems(L, 2);
  225. if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1)) {
  226. changenvalue(L->top - 2,
  227. luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
  228. }
  229. else
  230. luaV_arith(L, L->top - 2, L->top - 2, L->top - 1,
  231. cast(TMS, op - LUA_OPADD + TM_ADD));
  232. L->top--;
  233. lua_unlock(L);
  234. }
  235. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  236. StkId o1, o2;
  237. int i;
  238. lua_lock(L); /* may call tag method */
  239. o1 = index2addr(L, index1);
  240. o2 = index2addr(L, index2);
  241. if (o1 == luaO_nilobject || o2 == luaO_nilobject)
  242. i = 0;
  243. else switch (op) {
  244. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  245. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  246. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  247. default: api_check(L, 0, "invalid option"); i = 0;
  248. }
  249. lua_unlock(L);
  250. return i;
  251. }
  252. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  253. TValue n;
  254. const TValue *o = index2addr(L, idx);
  255. if (tonumber(o, &n))
  256. return nvalue(o);
  257. else
  258. return 0;
  259. }
  260. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  261. TValue n;
  262. const TValue *o = index2addr(L, idx);
  263. if (tonumber(o, &n)) {
  264. lua_Integer res;
  265. lua_Number num = nvalue(o);
  266. lua_number2integer(res, num);
  267. return res;
  268. }
  269. else
  270. return 0;
  271. }
  272. LUA_API int lua_toboolean (lua_State *L, int idx) {
  273. const TValue *o = index2addr(L, idx);
  274. return !l_isfalse(o);
  275. }
  276. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  277. StkId o = index2addr(L, idx);
  278. if (!ttisstring(o)) {
  279. lua_lock(L); /* `luaV_tostring' may create a new string */
  280. if (!luaV_tostring(L, o)) { /* conversion failed? */
  281. if (len != NULL) *len = 0;
  282. lua_unlock(L);
  283. return NULL;
  284. }
  285. luaC_checkGC(L);
  286. o = index2addr(L, idx); /* previous call may reallocate the stack */
  287. lua_unlock(L);
  288. }
  289. if (len != NULL) *len = tsvalue(o)->len;
  290. return svalue(o);
  291. }
  292. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  293. StkId o = index2addr(L, idx);
  294. switch (ttype(o)) {
  295. case LUA_TSTRING: return tsvalue(o)->len;
  296. case LUA_TUSERDATA: return uvalue(o)->len;
  297. case LUA_TTABLE: return luaH_getn(hvalue(o));
  298. default: return 0;
  299. }
  300. }
  301. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  302. StkId o = index2addr(L, idx);
  303. if (ttislcf(o)) return fvalue(o);
  304. else if (ttisclosure(o) && clvalue(o)->c.isC)
  305. return clvalue(o)->c.f;
  306. else return NULL; /* not a C function */
  307. }
  308. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  309. StkId o = index2addr(L, idx);
  310. switch (ttype(o)) {
  311. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  312. case LUA_TLIGHTUSERDATA: return pvalue(o);
  313. default: return NULL;
  314. }
  315. }
  316. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  317. StkId o = index2addr(L, idx);
  318. return (!ttisthread(o)) ? NULL : thvalue(o);
  319. }
  320. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  321. StkId o = index2addr(L, idx);
  322. switch (ttype(o)) {
  323. case LUA_TTABLE: return hvalue(o);
  324. case LUA_TFUNCTION: return clvalue(o);
  325. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  326. case LUA_TTHREAD: return thvalue(o);
  327. case LUA_TUSERDATA:
  328. case LUA_TLIGHTUSERDATA:
  329. return lua_touserdata(L, idx);
  330. default: return NULL;
  331. }
  332. }
  333. /*
  334. ** push functions (C -> stack)
  335. */
  336. LUA_API void lua_pushnil (lua_State *L) {
  337. lua_lock(L);
  338. setnilvalue(L->top);
  339. api_incr_top(L);
  340. lua_unlock(L);
  341. }
  342. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  343. lua_lock(L);
  344. setnvalue(L->top, n);
  345. api_incr_top(L);
  346. lua_unlock(L);
  347. }
  348. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  349. lua_lock(L);
  350. setnvalue(L->top, cast_num(n));
  351. api_incr_top(L);
  352. lua_unlock(L);
  353. }
  354. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  355. TString *ts;
  356. lua_lock(L);
  357. luaC_checkGC(L);
  358. ts = luaS_newlstr(L, s, len);
  359. setsvalue2s(L, L->top, ts);
  360. api_incr_top(L);
  361. lua_unlock(L);
  362. return getstr(ts);
  363. }
  364. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  365. if (s == NULL) {
  366. lua_pushnil(L);
  367. return NULL;
  368. }
  369. else {
  370. TString *ts;
  371. lua_lock(L);
  372. luaC_checkGC(L);
  373. ts = luaS_new(L, s);
  374. setsvalue2s(L, L->top, ts);
  375. api_incr_top(L);
  376. lua_unlock(L);
  377. return getstr(ts);
  378. }
  379. }
  380. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  381. va_list argp) {
  382. const char *ret;
  383. lua_lock(L);
  384. luaC_checkGC(L);
  385. ret = luaO_pushvfstring(L, fmt, argp);
  386. lua_unlock(L);
  387. return ret;
  388. }
  389. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  390. const char *ret;
  391. va_list argp;
  392. lua_lock(L);
  393. luaC_checkGC(L);
  394. va_start(argp, fmt);
  395. ret = luaO_pushvfstring(L, fmt, argp);
  396. va_end(argp);
  397. lua_unlock(L);
  398. return ret;
  399. }
  400. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  401. lua_lock(L);
  402. if (n == 0) {
  403. setfvalue(L->top, fn);
  404. }
  405. else {
  406. Closure *cl;
  407. api_checknelems(L, n);
  408. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  409. luaC_checkGC(L);
  410. cl = luaF_newCclosure(L, n);
  411. cl->c.f = fn;
  412. L->top -= n;
  413. while (n--)
  414. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  415. setclvalue(L, L->top, cl);
  416. }
  417. api_incr_top(L);
  418. lua_unlock(L);
  419. }
  420. LUA_API void lua_pushboolean (lua_State *L, int b) {
  421. lua_lock(L);
  422. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  423. api_incr_top(L);
  424. lua_unlock(L);
  425. }
  426. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  427. lua_lock(L);
  428. setpvalue(L->top, p);
  429. api_incr_top(L);
  430. lua_unlock(L);
  431. }
  432. LUA_API int lua_pushthread (lua_State *L) {
  433. lua_lock(L);
  434. setthvalue(L, L->top, L);
  435. api_incr_top(L);
  436. lua_unlock(L);
  437. return (G(L)->mainthread == L);
  438. }
  439. /*
  440. ** get functions (Lua -> stack)
  441. */
  442. LUA_API void lua_gettable (lua_State *L, int idx) {
  443. StkId t;
  444. lua_lock(L);
  445. t = index2addr(L, idx);
  446. api_checkvalidindex(L, t);
  447. luaV_gettable(L, t, L->top - 1, L->top - 1);
  448. lua_unlock(L);
  449. }
  450. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  451. StkId t;
  452. lua_lock(L);
  453. t = index2addr(L, idx);
  454. api_checkvalidindex(L, t);
  455. setsvalue2s(L, L->top, luaS_new(L, k));
  456. api_incr_top(L);
  457. luaV_gettable(L, t, L->top - 1, L->top - 1);
  458. lua_unlock(L);
  459. }
  460. LUA_API void lua_rawget (lua_State *L, int idx) {
  461. StkId t;
  462. lua_lock(L);
  463. t = index2addr(L, idx);
  464. api_check(L, ttistable(t), "table expected");
  465. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  466. lua_unlock(L);
  467. }
  468. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  469. StkId o;
  470. lua_lock(L);
  471. o = index2addr(L, idx);
  472. api_check(L, ttistable(o), "table expected");
  473. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  474. api_incr_top(L);
  475. lua_unlock(L);
  476. }
  477. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  478. Table *t;
  479. lua_lock(L);
  480. luaC_checkGC(L);
  481. t = luaH_new(L);
  482. sethvalue(L, L->top, t);
  483. api_incr_top(L);
  484. if (narray > 0 || nrec > 0)
  485. luaH_resize(L, t, narray, nrec);
  486. lua_unlock(L);
  487. }
  488. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  489. const TValue *obj;
  490. Table *mt = NULL;
  491. int res;
  492. lua_lock(L);
  493. obj = index2addr(L, objindex);
  494. switch (ttype(obj)) {
  495. case LUA_TTABLE:
  496. mt = hvalue(obj)->metatable;
  497. break;
  498. case LUA_TUSERDATA:
  499. mt = uvalue(obj)->metatable;
  500. break;
  501. default:
  502. mt = G(L)->mt[ttypenv(obj)];
  503. break;
  504. }
  505. if (mt == NULL)
  506. res = 0;
  507. else {
  508. sethvalue(L, L->top, mt);
  509. api_incr_top(L);
  510. res = 1;
  511. }
  512. lua_unlock(L);
  513. return res;
  514. }
  515. LUA_API void lua_getenv (lua_State *L, int idx) {
  516. StkId o;
  517. lua_lock(L);
  518. o = index2addr(L, idx);
  519. api_checkvalidindex(L, o);
  520. api_check(L, ttisuserdata(o), "userdata expected");
  521. if (uvalue(o)->env) {
  522. sethvalue(L, L->top, uvalue(o)->env);
  523. } else
  524. setnilvalue(L->top);
  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_barrierback(L, gcvalue(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_barrierback(L, gcvalue(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_objbarrierback(L, gcvalue(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[ttypenv(obj)] = mt;
  604. break;
  605. }
  606. }
  607. L->top--;
  608. lua_unlock(L);
  609. return 1;
  610. }
  611. LUA_API void lua_setenv (lua_State *L, int idx) {
  612. StkId o;
  613. lua_lock(L);
  614. api_checknelems(L, 1);
  615. o = index2addr(L, idx);
  616. api_checkvalidindex(L, o);
  617. api_check(L, ttisuserdata(o), "userdata expected");
  618. if (ttisnil(L->top - 1))
  619. uvalue(o)->env = NULL;
  620. else {
  621. api_check(L, ttistable(L->top - 1), "table expected");
  622. uvalue(o)->env = hvalue(L->top - 1);
  623. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  624. }
  625. L->top--;
  626. lua_unlock(L);
  627. }
  628. /*
  629. ** `load' and `call' functions (run Lua code)
  630. */
  631. #define checkresults(L,na,nr) \
  632. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  633. "results from function overflow current stack size")
  634. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  635. if (L->ci->callstatus & CIST_YIELDED) {
  636. if (ctx) *ctx = L->ci->u.c.ctx;
  637. return L->ci->u.c.status;
  638. }
  639. else return LUA_OK;
  640. }
  641. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  642. lua_CFunction k) {
  643. StkId func;
  644. lua_lock(L);
  645. api_check(L, k == NULL || !isLua(L->ci),
  646. "cannot use continuations inside hooks");
  647. api_checknelems(L, nargs+1);
  648. checkresults(L, nargs, nresults);
  649. func = L->top - (nargs+1);
  650. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  651. L->ci->u.c.k = k; /* save continuation */
  652. L->ci->u.c.ctx = ctx; /* save context */
  653. luaD_call(L, func, nresults, 1); /* do the call */
  654. }
  655. else /* no continuation or no yieldable */
  656. luaD_call(L, func, nresults, 0); /* just do the call */
  657. adjustresults(L, nresults);
  658. lua_unlock(L);
  659. }
  660. /*
  661. ** Execute a protected call.
  662. */
  663. struct CallS { /* data to `f_call' */
  664. StkId func;
  665. int nresults;
  666. };
  667. static void f_call (lua_State *L, void *ud) {
  668. struct CallS *c = cast(struct CallS *, ud);
  669. luaD_call(L, c->func, c->nresults, 0);
  670. }
  671. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  672. int ctx, lua_CFunction k) {
  673. struct CallS c;
  674. int status;
  675. ptrdiff_t func;
  676. lua_lock(L);
  677. api_check(L, k == NULL || !isLua(L->ci),
  678. "cannot use continuations inside hooks");
  679. api_checknelems(L, nargs+1);
  680. checkresults(L, nargs, nresults);
  681. if (errfunc == 0)
  682. func = 0;
  683. else {
  684. StkId o = index2addr(L, errfunc);
  685. api_checkvalidindex(L, o);
  686. func = savestack(L, o);
  687. }
  688. c.func = L->top - (nargs+1); /* function to be called */
  689. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  690. c.nresults = nresults; /* do a 'conventional' protected call */
  691. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  692. }
  693. else { /* prepare continuation (call is already protected by 'resume') */
  694. CallInfo *ci = L->ci;
  695. ci->u.c.k = k; /* save continuation */
  696. ci->u.c.ctx = ctx; /* save context */
  697. /* save information for error recovery */
  698. ci->u.c.extra = savestack(L, c.func);
  699. ci->u.c.old_allowhook = L->allowhook;
  700. ci->u.c.old_errfunc = L->errfunc;
  701. L->errfunc = func;
  702. /* mark that function may do error recovery */
  703. ci->callstatus |= CIST_YPCALL;
  704. luaD_call(L, c.func, nresults, 1); /* do the call */
  705. ci->callstatus &= ~CIST_YPCALL;
  706. L->errfunc = ci->u.c.old_errfunc;
  707. status = LUA_OK; /* if it is here, there were no errors */
  708. }
  709. adjustresults(L, nresults);
  710. lua_unlock(L);
  711. return status;
  712. }
  713. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  714. const char *chunkname) {
  715. ZIO z;
  716. int status;
  717. lua_lock(L);
  718. if (!chunkname) chunkname = "?";
  719. luaZ_init(L, &z, reader, data);
  720. status = luaD_protectedparser(L, &z, chunkname);
  721. if (status == LUA_OK) { /* no errors? */
  722. Closure *f = clvalue(L->top - 1); /* get newly created function */
  723. lua_assert(!f->c.isC);
  724. if (f->l.nupvalues == 1) { /* does it have one upvalue? */
  725. /* get global table from registry */
  726. Table *reg = hvalue(&G(L)->l_registry);
  727. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  728. /* set global table as 1st upvalue of 'f' (may be _ENV) */
  729. setobj(L, f->l.upvals[0]->v, gt);
  730. luaC_barrier(L, f->l.upvals[0], gt);
  731. }
  732. }
  733. lua_unlock(L);
  734. return status;
  735. }
  736. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  737. int status;
  738. TValue *o;
  739. lua_lock(L);
  740. api_checknelems(L, 1);
  741. o = L->top - 1;
  742. if (isLfunction(o))
  743. status = luaU_dump(L, getproto(o), writer, data, 0);
  744. else
  745. status = 1;
  746. lua_unlock(L);
  747. return status;
  748. }
  749. LUA_API int lua_status (lua_State *L) {
  750. return L->status;
  751. }
  752. /*
  753. ** Garbage-collection function
  754. */
  755. LUA_API int lua_gc (lua_State *L, int what, int data) {
  756. int res = 0;
  757. global_State *g;
  758. lua_lock(L);
  759. g = G(L);
  760. switch (what) {
  761. case LUA_GCSTOP: {
  762. stopgc(g);
  763. break;
  764. }
  765. case LUA_GCRESTART: {
  766. g->GCdebt = 0;
  767. break;
  768. }
  769. case LUA_GCCOLLECT: {
  770. luaC_fullgc(L, 0);
  771. break;
  772. }
  773. case LUA_GCCOUNT: {
  774. /* GC values are expressed in Kbytes: #bytes/2^10 */
  775. res = cast_int(g->totalbytes >> 10);
  776. break;
  777. }
  778. case LUA_GCCOUNTB: {
  779. res = cast_int(g->totalbytes & 0x3ff);
  780. break;
  781. }
  782. case LUA_GCSTEP: {
  783. int stopped = gcstopped(g);
  784. if (g->gckind == KGC_GEN) { /* generational mode? */
  785. res = (g->lastmajormem == 0); /* 1 if will do major collection */
  786. luaC_step(L); /* do a single step */
  787. }
  788. else {
  789. while (data-- >= 0) {
  790. luaC_step(L);
  791. if (g->gcstate == GCSpause) { /* end of cycle? */
  792. res = 1; /* signal it */
  793. break;
  794. }
  795. }
  796. }
  797. if (stopped) /* collector was stopped? */
  798. stopgc(g); /* keep it that way */
  799. break;
  800. }
  801. case LUA_GCSETPAUSE: {
  802. res = g->gcpause;
  803. g->gcpause = data;
  804. break;
  805. }
  806. case LUA_GCSETSTEPMUL: {
  807. res = g->gcstepmul;
  808. g->gcstepmul = data;
  809. break;
  810. }
  811. case LUA_GCISRUNNING: {
  812. res = !gcstopped(g);
  813. break;
  814. }
  815. case LUA_GCGEN: { /* change collector to generational mode */
  816. luaC_changemode(L, KGC_GEN);
  817. break;
  818. }
  819. case LUA_GCINC: { /* change collector to incremental mode */
  820. luaC_changemode(L, KGC_NORMAL);
  821. break;
  822. }
  823. default: res = -1; /* invalid option */
  824. }
  825. lua_unlock(L);
  826. return res;
  827. }
  828. /*
  829. ** miscellaneous functions
  830. */
  831. LUA_API int lua_error (lua_State *L) {
  832. lua_lock(L);
  833. api_checknelems(L, 1);
  834. luaG_errormsg(L);
  835. lua_unlock(L);
  836. return 0; /* to avoid warnings */
  837. }
  838. LUA_API int lua_next (lua_State *L, int idx) {
  839. StkId t;
  840. int more;
  841. lua_lock(L);
  842. t = index2addr(L, idx);
  843. api_check(L, ttistable(t), "table expected");
  844. more = luaH_next(L, hvalue(t), L->top - 1);
  845. if (more) {
  846. api_incr_top(L);
  847. }
  848. else /* no more elements */
  849. L->top -= 1; /* remove key */
  850. lua_unlock(L);
  851. return more;
  852. }
  853. LUA_API void lua_concat (lua_State *L, int n) {
  854. lua_lock(L);
  855. api_checknelems(L, n);
  856. if (n >= 2) {
  857. luaC_checkGC(L);
  858. luaV_concat(L, n);
  859. }
  860. else if (n == 0) { /* push empty string */
  861. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  862. api_incr_top(L);
  863. }
  864. /* else n == 1; nothing to do */
  865. lua_unlock(L);
  866. }
  867. LUA_API void lua_len (lua_State *L, int idx) {
  868. StkId t;
  869. lua_lock(L);
  870. t = index2addr(L, idx);
  871. luaV_objlen(L, L->top, t);
  872. api_incr_top(L);
  873. lua_unlock(L);
  874. }
  875. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  876. lua_Alloc f;
  877. lua_lock(L);
  878. if (ud) *ud = G(L)->ud;
  879. f = G(L)->frealloc;
  880. lua_unlock(L);
  881. return f;
  882. }
  883. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  884. lua_lock(L);
  885. G(L)->ud = ud;
  886. G(L)->frealloc = f;
  887. lua_unlock(L);
  888. }
  889. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  890. Udata *u;
  891. lua_lock(L);
  892. luaC_checkGC(L);
  893. u = luaS_newudata(L, size, NULL);
  894. setuvalue(L, L->top, u);
  895. api_incr_top(L);
  896. lua_unlock(L);
  897. return u + 1;
  898. }
  899. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  900. GCObject **owner) {
  901. Closure *f;
  902. if (!ttisclosure(fi)) return NULL;
  903. f = clvalue(fi);
  904. if (f->c.isC) {
  905. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  906. *val = &f->c.upvalue[n-1];
  907. if (owner) *owner = obj2gco(f);
  908. return "";
  909. }
  910. else {
  911. Proto *p = f->l.p;
  912. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  913. *val = f->l.upvals[n-1]->v;
  914. if (owner) *owner = obj2gco(f->l.upvals[n - 1]);
  915. return getstr(p->upvalues[n-1].name);
  916. }
  917. }
  918. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  919. const char *name;
  920. TValue *val;
  921. lua_lock(L);
  922. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
  923. if (name) {
  924. setobj2s(L, L->top, val);
  925. api_incr_top(L);
  926. }
  927. lua_unlock(L);
  928. return name;
  929. }
  930. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  931. const char *name;
  932. TValue *val;
  933. GCObject *owner;
  934. StkId fi;
  935. lua_lock(L);
  936. fi = index2addr(L, funcindex);
  937. api_checknelems(L, 1);
  938. name = aux_upvalue(fi, n, &val, &owner);
  939. if (name) {
  940. L->top--;
  941. setobj(L, val, L->top);
  942. luaC_barrier(L, owner, L->top);
  943. }
  944. lua_unlock(L);
  945. return name;
  946. }
  947. static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
  948. Closure *f;
  949. Proto *p;
  950. StkId fi = index2addr(L, fidx);
  951. api_check(L, ttisclosure(fi), "Lua function expected");
  952. f = clvalue(fi);
  953. api_check(L, !f->c.isC, "Lua function expected");
  954. p = f->l.p;
  955. api_check(L, (1 <= n && n <= p->sizeupvalues), "invalid upvalue index");
  956. if (pf) *pf = f;
  957. return &f->l.upvals[n - 1]; /* get its upvalue pointer */
  958. }
  959. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  960. Closure *f;
  961. StkId fi = index2addr(L, fidx);
  962. api_check(L, ttisclosure(fi), "function expected");
  963. f = clvalue(fi);
  964. if (f->c.isC) {
  965. api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
  966. return &f->c.upvalue[n - 1];
  967. }
  968. else return *getupvalref(L, fidx, n, NULL);
  969. }
  970. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  971. int fidx2, int n2) {
  972. Closure *f1;
  973. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  974. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  975. *up1 = *up2;
  976. luaC_objbarrier(L, f1, *up2);
  977. }