lapi.c 25 KB

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