lapi.c 25 KB

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