lapi.c 22 KB

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