lapi.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. ** $Id: lapi.c,v 2.94 2009/10/23 19:12:19 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 &G(L)->l_registry;
  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 &G(L)->l_gt;
  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(&G(L)->l_gt); /* 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. static void moveto (lua_State *L, TValue *fr, int idx) {
  152. TValue *to = index2addr(L, idx);
  153. api_checkvalidindex(L, to);
  154. if (idx == LUA_ENVIRONINDEX) {
  155. Closure *func = curr_func(L);
  156. api_check(L, ttistable(fr), "table expected");
  157. func->c.env = hvalue(fr);
  158. luaC_barrier(L, func, fr);
  159. }
  160. else {
  161. setobj(L, to, fr);
  162. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  163. luaC_barrier(L, curr_func(L), fr);
  164. }
  165. /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */
  166. }
  167. LUA_API void lua_replace (lua_State *L, int idx) {
  168. lua_lock(L);
  169. /* explicit test for incompatible code */
  170. if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
  171. luaG_runerror(L, "no calling environment");
  172. api_checknelems(L, 1);
  173. moveto(L, L->top - 1, idx);
  174. L->top--;
  175. lua_unlock(L);
  176. }
  177. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  178. TValue *fr;
  179. lua_lock(L);
  180. fr = index2addr(L, fromidx);
  181. api_checkvalidindex(L, fr);
  182. moveto(L, fr, toidx);
  183. lua_unlock(L);
  184. }
  185. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  186. lua_lock(L);
  187. setobj2s(L, L->top, index2addr(L, idx));
  188. api_incr_top(L);
  189. lua_unlock(L);
  190. }
  191. /*
  192. ** access functions (stack -> C)
  193. */
  194. LUA_API int lua_type (lua_State *L, int idx) {
  195. StkId o = index2addr(L, idx);
  196. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  197. }
  198. LUA_API const char *lua_typename (lua_State *L, int t) {
  199. UNUSED(L);
  200. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  201. }
  202. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  203. StkId o = index2addr(L, idx);
  204. return iscfunction(o);
  205. }
  206. LUA_API int lua_isnumber (lua_State *L, int idx) {
  207. TValue n;
  208. const TValue *o = index2addr(L, idx);
  209. return tonumber(o, &n);
  210. }
  211. LUA_API int lua_isstring (lua_State *L, int idx) {
  212. int t = lua_type(L, idx);
  213. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  214. }
  215. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  216. const TValue *o = index2addr(L, idx);
  217. return (ttisuserdata(o) || ttislightuserdata(o));
  218. }
  219. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  220. StkId o1 = index2addr(L, index1);
  221. StkId o2 = index2addr(L, index2);
  222. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  223. : luaO_rawequalObj(o1, o2);
  224. }
  225. LUA_API void lua_arith (lua_State *L, int op) {
  226. lua_lock(L);
  227. api_checknelems(L, 2);
  228. if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1))
  229. changenvalue(L->top - 2,
  230. luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
  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. default: res = -1; /* invalid option */
  792. }
  793. lua_unlock(L);
  794. return res;
  795. }
  796. /*
  797. ** miscellaneous functions
  798. */
  799. LUA_API int lua_error (lua_State *L) {
  800. lua_lock(L);
  801. api_checknelems(L, 1);
  802. luaG_errormsg(L);
  803. lua_unlock(L);
  804. return 0; /* to avoid warnings */
  805. }
  806. LUA_API int lua_next (lua_State *L, int idx) {
  807. StkId t;
  808. int more;
  809. lua_lock(L);
  810. t = index2addr(L, idx);
  811. api_check(L, ttistable(t), "table expected");
  812. more = luaH_next(L, hvalue(t), L->top - 1);
  813. if (more) {
  814. api_incr_top(L);
  815. }
  816. else /* no more elements */
  817. L->top -= 1; /* remove key */
  818. lua_unlock(L);
  819. return more;
  820. }
  821. LUA_API void lua_concat (lua_State *L, int n) {
  822. lua_lock(L);
  823. api_checknelems(L, n);
  824. if (n >= 2) {
  825. luaC_checkGC(L);
  826. luaV_concat(L, n);
  827. }
  828. else if (n == 0) { /* push empty string */
  829. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  830. api_incr_top(L);
  831. }
  832. /* else n == 1; nothing to do */
  833. lua_unlock(L);
  834. }
  835. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  836. lua_Alloc f;
  837. lua_lock(L);
  838. if (ud) *ud = G(L)->ud;
  839. f = G(L)->frealloc;
  840. lua_unlock(L);
  841. return f;
  842. }
  843. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  844. lua_lock(L);
  845. G(L)->ud = ud;
  846. G(L)->frealloc = f;
  847. lua_unlock(L);
  848. }
  849. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  850. Udata *u;
  851. lua_lock(L);
  852. luaC_checkGC(L);
  853. u = luaS_newudata(L, size, getcurrenv(L));
  854. setuvalue(L, L->top, u);
  855. api_incr_top(L);
  856. lua_unlock(L);
  857. return u + 1;
  858. }
  859. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  860. Closure *f;
  861. if (!ttisfunction(fi)) return NULL;
  862. f = clvalue(fi);
  863. if (f->c.isC) {
  864. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  865. *val = &f->c.upvalue[n-1];
  866. return "";
  867. }
  868. else {
  869. Proto *p = f->l.p;
  870. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  871. *val = f->l.upvals[n-1]->v;
  872. return getstr(p->upvalues[n-1].name);
  873. }
  874. }
  875. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  876. const char *name;
  877. TValue *val;
  878. lua_lock(L);
  879. name = aux_upvalue(index2addr(L, funcindex), n, &val);
  880. if (name) {
  881. setobj2s(L, L->top, val);
  882. api_incr_top(L);
  883. }
  884. lua_unlock(L);
  885. return name;
  886. }
  887. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  888. const char *name;
  889. TValue *val;
  890. StkId fi;
  891. lua_lock(L);
  892. fi = index2addr(L, funcindex);
  893. api_checknelems(L, 1);
  894. name = aux_upvalue(fi, n, &val);
  895. if (name) {
  896. L->top--;
  897. setobj(L, val, L->top);
  898. luaC_barrier(L, clvalue(fi), L->top);
  899. }
  900. lua_unlock(L);
  901. return name;
  902. }
  903. static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
  904. Closure *f;
  905. Proto *p;
  906. StkId fi = index2addr(L, fidx);
  907. if (!ttisfunction(fi)) return NULL; /* not a function? */
  908. f = clvalue(fi);
  909. if (f->c.isC) return NULL; /* not a Lua function? */
  910. p = f->l.p;
  911. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  912. else {
  913. if (pf) *pf = f;
  914. return &f->l.upvals[n - 1]; /* get its upvalue pointer */
  915. }
  916. }
  917. LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n) {
  918. Closure *f;
  919. StkId fi = index2addr(L, fidx);
  920. if (!ttisfunction(fi)) return NULL;
  921. f = clvalue(fi);
  922. if (f->c.isC) {
  923. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  924. else return &f->c.upvalue[n - 1];
  925. }
  926. else {
  927. UpVal **uv = getupvalref(L, fidx, n, NULL);
  928. return (uv == NULL) ? NULL : *uv;
  929. }
  930. }
  931. LUA_API int (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
  932. int fidx2, int n2) {
  933. Closure *f1;
  934. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  935. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  936. if (up1 == NULL || up2 == NULL) return 0;
  937. *up1 = *up2;
  938. luaC_objbarrier(L, f1, *up2);
  939. return 1;
  940. }
  941. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  942. lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL);
  943. lua_pushlightuserdata(L, &func);
  944. lua_pushlightuserdata(L, ud);
  945. return lua_pcall(L, 2, 0, 0);
  946. }