lapi.c 24 KB

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