lapi.c 26 KB

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