lapi.c 22 KB

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