lapi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. ** $Id: lapi.c,v 2.117 2010/03/26 20:58:11 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. return lua_pushlstring(L, s, strlen(s));
  353. }
  354. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  355. va_list argp) {
  356. const char *ret;
  357. lua_lock(L);
  358. luaC_checkGC(L);
  359. ret = luaO_pushvfstring(L, fmt, argp);
  360. lua_unlock(L);
  361. return ret;
  362. }
  363. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  364. const char *ret;
  365. va_list argp;
  366. lua_lock(L);
  367. luaC_checkGC(L);
  368. va_start(argp, fmt);
  369. ret = luaO_pushvfstring(L, fmt, argp);
  370. va_end(argp);
  371. lua_unlock(L);
  372. return ret;
  373. }
  374. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  375. Closure *cl;
  376. lua_lock(L);
  377. api_checknelems(L, n);
  378. api_check(L, n <= UCHAR_MAX, "upvalue index too large");
  379. luaC_checkGC(L);
  380. cl = luaF_newCclosure(L, n);
  381. cl->c.f = fn;
  382. L->top -= n;
  383. while (n--)
  384. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  385. setclvalue(L, L->top, cl);
  386. lua_assert(iswhite(obj2gco(cl)));
  387. api_incr_top(L);
  388. lua_unlock(L);
  389. }
  390. LUA_API void lua_pushboolean (lua_State *L, int b) {
  391. lua_lock(L);
  392. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  393. api_incr_top(L);
  394. lua_unlock(L);
  395. }
  396. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  397. lua_lock(L);
  398. setpvalue(L->top, p);
  399. api_incr_top(L);
  400. lua_unlock(L);
  401. }
  402. LUA_API int lua_pushthread (lua_State *L) {
  403. lua_lock(L);
  404. setthvalue(L, L->top, L);
  405. api_incr_top(L);
  406. lua_unlock(L);
  407. return (G(L)->mainthread == L);
  408. }
  409. /*
  410. ** get functions (Lua -> stack)
  411. */
  412. LUA_API void lua_gettable (lua_State *L, int idx) {
  413. StkId t;
  414. lua_lock(L);
  415. t = index2addr(L, idx);
  416. api_checkvalidindex(L, t);
  417. luaV_gettable(L, t, L->top - 1, L->top - 1);
  418. lua_unlock(L);
  419. }
  420. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  421. StkId t;
  422. lua_lock(L);
  423. t = index2addr(L, idx);
  424. api_checkvalidindex(L, t);
  425. setsvalue2s(L, L->top, luaS_new(L, k));
  426. api_incr_top(L);
  427. luaV_gettable(L, t, L->top - 1, L->top - 1);
  428. lua_unlock(L);
  429. }
  430. LUA_API void lua_rawget (lua_State *L, int idx) {
  431. StkId t;
  432. lua_lock(L);
  433. t = index2addr(L, idx);
  434. api_check(L, ttistable(t), "table expected");
  435. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  436. lua_unlock(L);
  437. }
  438. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  439. StkId o;
  440. lua_lock(L);
  441. o = index2addr(L, idx);
  442. api_check(L, ttistable(o), "table expected");
  443. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  444. api_incr_top(L);
  445. lua_unlock(L);
  446. }
  447. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  448. Table *t;
  449. lua_lock(L);
  450. luaC_checkGC(L);
  451. t = luaH_new(L);
  452. sethvalue(L, L->top, t);
  453. api_incr_top(L);
  454. if (narray > 0 || nrec > 0)
  455. luaH_resize(L, t, narray, nrec);
  456. lua_unlock(L);
  457. }
  458. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  459. const TValue *obj;
  460. Table *mt = NULL;
  461. int res;
  462. lua_lock(L);
  463. obj = index2addr(L, objindex);
  464. switch (ttype(obj)) {
  465. case LUA_TTABLE:
  466. mt = hvalue(obj)->metatable;
  467. break;
  468. case LUA_TUSERDATA:
  469. mt = uvalue(obj)->metatable;
  470. break;
  471. default:
  472. mt = G(L)->mt[ttype(obj)];
  473. break;
  474. }
  475. if (mt == NULL)
  476. res = 0;
  477. else {
  478. sethvalue(L, L->top, mt);
  479. api_incr_top(L);
  480. res = 1;
  481. }
  482. lua_unlock(L);
  483. return res;
  484. }
  485. LUA_API void lua_getenv (lua_State *L, int idx) {
  486. StkId o;
  487. lua_lock(L);
  488. o = index2addr(L, idx);
  489. api_checkvalidindex(L, o);
  490. api_check(L, ttisuserdata(o), "userdata expected");
  491. if (uvalue(o)->env) {
  492. sethvalue(L, L->top, uvalue(o)->env);
  493. } else
  494. setnilvalue(L->top);
  495. api_incr_top(L);
  496. lua_unlock(L);
  497. }
  498. /*
  499. ** set functions (stack -> Lua)
  500. */
  501. LUA_API void lua_settable (lua_State *L, int idx) {
  502. StkId t;
  503. lua_lock(L);
  504. api_checknelems(L, 2);
  505. t = index2addr(L, idx);
  506. api_checkvalidindex(L, t);
  507. luaV_settable(L, t, L->top - 2, L->top - 1);
  508. L->top -= 2; /* pop index and value */
  509. lua_unlock(L);
  510. }
  511. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  512. StkId t;
  513. lua_lock(L);
  514. api_checknelems(L, 1);
  515. t = index2addr(L, idx);
  516. api_checkvalidindex(L, t);
  517. setsvalue2s(L, L->top++, luaS_new(L, k));
  518. luaV_settable(L, t, L->top - 1, L->top - 2);
  519. L->top -= 2; /* pop value and key */
  520. lua_unlock(L);
  521. }
  522. LUA_API void lua_rawset (lua_State *L, int idx) {
  523. StkId t;
  524. lua_lock(L);
  525. api_checknelems(L, 2);
  526. t = index2addr(L, idx);
  527. api_check(L, ttistable(t), "table expected");
  528. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  529. luaC_barriert(L, hvalue(t), L->top-1);
  530. L->top -= 2;
  531. lua_unlock(L);
  532. }
  533. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  534. StkId o;
  535. lua_lock(L);
  536. api_checknelems(L, 1);
  537. o = index2addr(L, idx);
  538. api_check(L, ttistable(o), "table expected");
  539. setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
  540. luaC_barriert(L, hvalue(o), L->top-1);
  541. L->top--;
  542. lua_unlock(L);
  543. }
  544. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  545. TValue *obj;
  546. Table *mt;
  547. lua_lock(L);
  548. api_checknelems(L, 1);
  549. obj = index2addr(L, objindex);
  550. api_checkvalidindex(L, obj);
  551. if (ttisnil(L->top - 1))
  552. mt = NULL;
  553. else {
  554. api_check(L, ttistable(L->top - 1), "table expected");
  555. mt = hvalue(L->top - 1);
  556. }
  557. switch (ttype(obj)) {
  558. case LUA_TTABLE: {
  559. hvalue(obj)->metatable = mt;
  560. if (mt)
  561. luaC_objbarriert(L, hvalue(obj), mt);
  562. break;
  563. }
  564. case LUA_TUSERDATA: {
  565. uvalue(obj)->metatable = mt;
  566. if (mt) {
  567. luaC_objbarrier(L, rawuvalue(obj), mt);
  568. luaC_checkfinalizer(L, rawuvalue(obj));
  569. }
  570. break;
  571. }
  572. default: {
  573. G(L)->mt[ttype(obj)] = mt;
  574. break;
  575. }
  576. }
  577. L->top--;
  578. lua_unlock(L);
  579. return 1;
  580. }
  581. LUA_API void lua_setenv (lua_State *L, int idx) {
  582. StkId o;
  583. lua_lock(L);
  584. api_checknelems(L, 1);
  585. o = index2addr(L, idx);
  586. api_checkvalidindex(L, o);
  587. api_check(L, ttisuserdata(o), "userdata expected");
  588. if (ttisnil(L->top - 1))
  589. uvalue(o)->env = NULL;
  590. else {
  591. api_check(L, ttistable(L->top - 1), "table expected");
  592. uvalue(o)->env = hvalue(L->top - 1);
  593. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  594. }
  595. L->top--;
  596. lua_unlock(L);
  597. }
  598. /*
  599. ** `load' and `call' functions (run Lua code)
  600. */
  601. #define checkresults(L,na,nr) \
  602. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  603. "results from function overflow current stack size")
  604. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  605. if (L->ci->callstatus & CIST_YIELDED) {
  606. if (ctx) *ctx = L->ci->u.c.ctx;
  607. return L->ci->u.c.status;
  608. }
  609. else return LUA_OK;
  610. }
  611. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  612. lua_CFunction k) {
  613. StkId func;
  614. lua_lock(L);
  615. api_check(L, k == NULL || !isLua(L->ci),
  616. "cannot use continuations inside hooks");
  617. api_checknelems(L, nargs+1);
  618. checkresults(L, nargs, nresults);
  619. func = L->top - (nargs+1);
  620. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  621. L->ci->u.c.k = k; /* save continuation */
  622. L->ci->u.c.ctx = ctx; /* save context */
  623. luaD_call(L, func, nresults, 1); /* do the call */
  624. }
  625. else /* no continuation or no yieldable */
  626. luaD_call(L, func, nresults, 0); /* just do the call */
  627. adjustresults(L, nresults);
  628. lua_unlock(L);
  629. }
  630. /*
  631. ** Execute a protected call.
  632. */
  633. struct CallS { /* data to `f_call' */
  634. StkId func;
  635. int nresults;
  636. };
  637. static void f_call (lua_State *L, void *ud) {
  638. struct CallS *c = cast(struct CallS *, ud);
  639. luaD_call(L, c->func, c->nresults, 0);
  640. }
  641. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  642. int ctx, lua_CFunction k) {
  643. struct CallS c;
  644. int status;
  645. ptrdiff_t func;
  646. lua_lock(L);
  647. api_check(L, k == NULL || !isLua(L->ci),
  648. "cannot use continuations inside hooks");
  649. api_checknelems(L, nargs+1);
  650. checkresults(L, nargs, nresults);
  651. if (errfunc == 0)
  652. func = 0;
  653. else {
  654. StkId o = index2addr(L, errfunc);
  655. api_checkvalidindex(L, o);
  656. func = savestack(L, o);
  657. }
  658. c.func = L->top - (nargs+1); /* function to be called */
  659. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  660. c.nresults = nresults; /* do a 'conventional' protected call */
  661. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  662. }
  663. else { /* prepare continuation (call is already protected by 'resume') */
  664. CallInfo *ci = L->ci;
  665. ci->u.c.k = k; /* save continuation */
  666. ci->u.c.ctx = ctx; /* save context */
  667. /* save information for error recovery */
  668. ci->u.c.extra = savestack(L, c.func);
  669. ci->u.c.old_allowhook = L->allowhook;
  670. ci->u.c.old_errfunc = L->errfunc;
  671. L->errfunc = func;
  672. /* mark that function may do error recovery */
  673. ci->callstatus |= CIST_YPCALL;
  674. luaD_call(L, c.func, nresults, 1); /* do the call */
  675. ci->callstatus &= ~CIST_YPCALL;
  676. L->errfunc = ci->u.c.old_errfunc;
  677. status = LUA_OK; /* if it is here, there were no errors */
  678. }
  679. adjustresults(L, nresults);
  680. lua_unlock(L);
  681. return status;
  682. }
  683. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  684. const char *chunkname) {
  685. ZIO z;
  686. int status;
  687. lua_lock(L);
  688. if (!chunkname) chunkname = "?";
  689. luaZ_init(L, &z, reader, data);
  690. status = luaD_protectedparser(L, &z, chunkname);
  691. if (status == LUA_OK) { /* no errors? */
  692. Closure *f = clvalue(L->top - 1); /* get newly created function */
  693. lua_assert(!f->c.isC);
  694. if (f->l.nupvalues == 1) { /* does it have one upvalue? */
  695. /* get global table from registry */
  696. Table *reg = hvalue(&G(L)->l_registry);
  697. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  698. /* set global table as 1st upvalue of 'f' (may be _ENV) */
  699. setobj(L, f->l.upvals[0]->v, gt);
  700. luaC_barrier(L, f, gt);
  701. }
  702. }
  703. lua_unlock(L);
  704. return status;
  705. }
  706. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  707. int status;
  708. TValue *o;
  709. lua_lock(L);
  710. api_checknelems(L, 1);
  711. o = L->top - 1;
  712. if (isLfunction(o))
  713. status = luaU_dump(L, getproto(o), writer, data, 0);
  714. else
  715. status = 1;
  716. lua_unlock(L);
  717. return status;
  718. }
  719. LUA_API int lua_status (lua_State *L) {
  720. return L->status;
  721. }
  722. /*
  723. ** Garbage-collection function
  724. */
  725. LUA_API int lua_gc (lua_State *L, int what, int data) {
  726. int res = 0;
  727. global_State *g;
  728. lua_lock(L);
  729. g = G(L);
  730. switch (what) {
  731. case LUA_GCSTOP: {
  732. g->GCthreshold = MAX_LUMEM;
  733. break;
  734. }
  735. case LUA_GCRESTART: {
  736. g->GCthreshold = g->totalbytes;
  737. break;
  738. }
  739. case LUA_GCCOLLECT: {
  740. luaC_fullgc(L, 0);
  741. break;
  742. }
  743. case LUA_GCCOUNT: {
  744. /* GC values are expressed in Kbytes: #bytes/2^10 */
  745. res = cast_int(g->totalbytes >> 10);
  746. break;
  747. }
  748. case LUA_GCCOUNTB: {
  749. res = cast_int(g->totalbytes & 0x3ff);
  750. break;
  751. }
  752. case LUA_GCSTEP: {
  753. lu_mem oldts = g->GCthreshold;
  754. if (g->gckind == KGC_GEN) { /* generational mode? */
  755. res = (g->lastmajormem == 0); /* 1 if will do major collection */
  756. luaC_step(L); /* do a single step */
  757. }
  758. else {
  759. lu_mem a = (cast(lu_mem, data) << 10);
  760. g->GCthreshold = (a <= g->totalbytes) ? g->totalbytes - a : 0;
  761. while (g->GCthreshold <= g->totalbytes) {
  762. luaC_step(L);
  763. if (g->gcstate == GCSpause) { /* end of cycle? */
  764. res = 1; /* signal it */
  765. break;
  766. }
  767. }
  768. }
  769. if (oldts == MAX_LUMEM) /* collector was stopped? */
  770. g->GCthreshold = oldts; /* keep it that way */
  771. break;
  772. }
  773. case LUA_GCSETPAUSE: {
  774. res = g->gcpause;
  775. g->gcpause = data;
  776. break;
  777. }
  778. case LUA_GCSETSTEPMUL: {
  779. res = g->gcstepmul;
  780. g->gcstepmul = data;
  781. break;
  782. }
  783. case LUA_GCISRUNNING: {
  784. res = (g->GCthreshold != MAX_LUMEM);
  785. break;
  786. }
  787. case LUA_GCGEN: { /* change collector to generational mode */
  788. luaC_runtilstate(L, bitmask(GCSpropagate));
  789. g->lastmajormem = g->totalbytes;
  790. g->gckind = KGC_GEN;
  791. break;
  792. }
  793. default: res = -1; /* invalid option */
  794. }
  795. lua_unlock(L);
  796. return res;
  797. }
  798. /*
  799. ** miscellaneous functions
  800. */
  801. LUA_API int lua_error (lua_State *L) {
  802. lua_lock(L);
  803. api_checknelems(L, 1);
  804. luaG_errormsg(L);
  805. lua_unlock(L);
  806. return 0; /* to avoid warnings */
  807. }
  808. LUA_API int lua_next (lua_State *L, int idx) {
  809. StkId t;
  810. int more;
  811. lua_lock(L);
  812. t = index2addr(L, idx);
  813. api_check(L, ttistable(t), "table expected");
  814. more = luaH_next(L, hvalue(t), L->top - 1);
  815. if (more) {
  816. api_incr_top(L);
  817. }
  818. else /* no more elements */
  819. L->top -= 1; /* remove key */
  820. lua_unlock(L);
  821. return more;
  822. }
  823. LUA_API void lua_concat (lua_State *L, int n) {
  824. lua_lock(L);
  825. api_checknelems(L, n);
  826. if (n >= 2) {
  827. luaC_checkGC(L);
  828. luaV_concat(L, n);
  829. }
  830. else if (n == 0) { /* push empty string */
  831. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  832. api_incr_top(L);
  833. }
  834. /* else n == 1; nothing to do */
  835. lua_unlock(L);
  836. }
  837. LUA_API void lua_len (lua_State *L, int idx) {
  838. StkId t;
  839. lua_lock(L);
  840. t = index2addr(L, idx);
  841. luaV_objlen(L, L->top, t);
  842. api_incr_top(L);
  843. lua_unlock(L);
  844. }
  845. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  846. lua_Alloc f;
  847. lua_lock(L);
  848. if (ud) *ud = G(L)->ud;
  849. f = G(L)->frealloc;
  850. lua_unlock(L);
  851. return f;
  852. }
  853. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  854. lua_lock(L);
  855. G(L)->ud = ud;
  856. G(L)->frealloc = f;
  857. lua_unlock(L);
  858. }
  859. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  860. Udata *u;
  861. lua_lock(L);
  862. luaC_checkGC(L);
  863. u = luaS_newudata(L, size, NULL);
  864. setuvalue(L, L->top, u);
  865. api_incr_top(L);
  866. lua_unlock(L);
  867. return u + 1;
  868. }
  869. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  870. Closure *f;
  871. if (!ttisfunction(fi)) return NULL;
  872. f = clvalue(fi);
  873. if (f->c.isC) {
  874. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  875. *val = &f->c.upvalue[n-1];
  876. return "";
  877. }
  878. else {
  879. Proto *p = f->l.p;
  880. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  881. *val = f->l.upvals[n-1]->v;
  882. return getstr(p->upvalues[n-1].name);
  883. }
  884. }
  885. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  886. const char *name;
  887. TValue *val;
  888. lua_lock(L);
  889. name = aux_upvalue(index2addr(L, funcindex), n, &val);
  890. if (name) {
  891. setobj2s(L, L->top, val);
  892. api_incr_top(L);
  893. }
  894. lua_unlock(L);
  895. return name;
  896. }
  897. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  898. const char *name;
  899. TValue *val;
  900. StkId fi;
  901. lua_lock(L);
  902. fi = index2addr(L, funcindex);
  903. api_checknelems(L, 1);
  904. name = aux_upvalue(fi, n, &val);
  905. if (name) {
  906. L->top--;
  907. setobj(L, val, L->top);
  908. luaC_barrier(L, clvalue(fi), L->top);
  909. }
  910. lua_unlock(L);
  911. return name;
  912. }
  913. static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
  914. Closure *f;
  915. Proto *p;
  916. StkId fi = index2addr(L, fidx);
  917. api_check(L, ttisfunction(fi), "function expected");
  918. f = clvalue(fi);
  919. api_check(L, !f->c.isC, "Lua function expected");
  920. p = f->l.p;
  921. api_check(L, (1 <= n && n <= p->sizeupvalues), "invalid upvalue index");
  922. if (pf) *pf = f;
  923. return &f->l.upvals[n - 1]; /* get its upvalue pointer */
  924. }
  925. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  926. Closure *f;
  927. StkId fi = index2addr(L, fidx);
  928. api_check(L, ttisfunction(fi), "function expected");
  929. f = clvalue(fi);
  930. if (f->c.isC) {
  931. api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
  932. return &f->c.upvalue[n - 1];
  933. }
  934. else return *getupvalref(L, fidx, n, NULL);
  935. }
  936. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  937. int fidx2, int n2) {
  938. Closure *f1;
  939. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  940. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  941. *up1 = *up2;
  942. luaC_objbarrier(L, f1, *up2);
  943. }