lapi.c 24 KB

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