lapi.c 24 KB

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