lapi.c 25 KB

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