lapi.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. ** $Id: lapi.c,v 2.64 2008/02/12 13:34:12 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. "$LuaVersion: " LUA_COPYRIGHT " $"
  28. "$LuaAuthors: " LUA_AUTHORS " $";
  29. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
  30. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
  31. static TValue *index2adr (lua_State *L, int idx) {
  32. if (idx > 0) {
  33. TValue *o = L->base + (idx - 1);
  34. api_check(L, idx <= L->ci->top - L->base);
  35. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  36. else return o;
  37. }
  38. else if (idx > LUA_REGISTRYINDEX) {
  39. api_check(L, idx != 0 && -idx <= L->top - L->base);
  40. return L->top + idx;
  41. }
  42. else switch (idx) { /* pseudo-indices */
  43. case LUA_REGISTRYINDEX: return registry(L);
  44. case LUA_ENVIRONINDEX: {
  45. Closure *func = curr_func(L);
  46. sethvalue(L, &L->env, func->c.env);
  47. return &L->env;
  48. }
  49. case LUA_GLOBALSINDEX: return gt(L);
  50. default: {
  51. Closure *func = curr_func(L);
  52. idx = LUA_GLOBALSINDEX - idx;
  53. api_check(L, idx <= UCHAR_MAX + 1);
  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 = 1;
  70. lua_lock(L);
  71. if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
  72. res = 0; /* stack overflow */
  73. else if (size > 0) {
  74. luaD_checkstack(L, size);
  75. if (L->ci->top < L->top + size)
  76. L->ci->top = L->top + size;
  77. }
  78. lua_unlock(L);
  79. return res;
  80. }
  81. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  82. int i;
  83. if (from == to) return;
  84. lua_lock(to);
  85. api_checknelems(from, n);
  86. api_check(from, G(from) == G(to));
  87. api_check(from, to->ci->top - to->top >= n);
  88. from->top -= n;
  89. for (i = 0; i < n; i++) {
  90. setobj2s(to, to->top++, from->top + i);
  91. }
  92. lua_unlock(to);
  93. }
  94. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  95. lua_CFunction old;
  96. lua_lock(L);
  97. old = G(L)->panic;
  98. G(L)->panic = panicf;
  99. lua_unlock(L);
  100. return old;
  101. }
  102. /*
  103. ** basic stack manipulation
  104. */
  105. LUA_API int lua_gettop (lua_State *L) {
  106. return cast_int(L->top - L->base);
  107. }
  108. LUA_API void lua_settop (lua_State *L, int idx) {
  109. lua_lock(L);
  110. if (idx >= 0) {
  111. api_check(L, idx <= L->stack_last - L->base);
  112. while (L->top < L->base + idx)
  113. setnilvalue(L->top++);
  114. L->top = L->base + idx;
  115. }
  116. else {
  117. api_check(L, -(idx+1) <= (L->top - L->base));
  118. L->top += idx+1; /* `subtract' index (index is negative) */
  119. }
  120. lua_unlock(L);
  121. }
  122. LUA_API void lua_remove (lua_State *L, int idx) {
  123. StkId p;
  124. lua_lock(L);
  125. p = index2adr(L, idx);
  126. api_checkvalidindex(L, p);
  127. while (++p < L->top) setobjs2s(L, p-1, p);
  128. L->top--;
  129. lua_unlock(L);
  130. }
  131. LUA_API void lua_insert (lua_State *L, int idx) {
  132. StkId p;
  133. StkId q;
  134. lua_lock(L);
  135. p = index2adr(L, idx);
  136. api_checkvalidindex(L, p);
  137. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  138. setobjs2s(L, p, L->top);
  139. lua_unlock(L);
  140. }
  141. LUA_API void lua_replace (lua_State *L, int idx) {
  142. StkId o;
  143. lua_lock(L);
  144. /* explicit test for incompatible code */
  145. if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
  146. luaG_runerror(L, "no calling environment");
  147. api_checknelems(L, 1);
  148. o = index2adr(L, idx);
  149. api_checkvalidindex(L, o);
  150. if (idx == LUA_ENVIRONINDEX) {
  151. Closure *func = curr_func(L);
  152. api_check(L, ttistable(L->top - 1));
  153. func->c.env = hvalue(L->top - 1);
  154. luaC_barrier(L, func, L->top - 1);
  155. }
  156. else {
  157. setobj(L, o, L->top - 1);
  158. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  159. luaC_barrier(L, curr_func(L), L->top - 1);
  160. }
  161. /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */
  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 const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  332. TString *ts;
  333. lua_lock(L);
  334. luaC_checkGC(L);
  335. ts = luaS_newlstr(L, s, len);
  336. setsvalue2s(L, L->top, ts);
  337. api_incr_top(L);
  338. lua_unlock(L);
  339. return getstr(ts);
  340. }
  341. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  342. if (s == NULL) {
  343. lua_pushnil(L);
  344. return NULL;
  345. }
  346. else
  347. return lua_pushlstring(L, s, strlen(s));
  348. }
  349. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  350. va_list argp) {
  351. const char *ret;
  352. lua_lock(L);
  353. luaC_checkGC(L);
  354. ret = luaO_pushvfstring(L, fmt, argp);
  355. lua_unlock(L);
  356. return ret;
  357. }
  358. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  359. const char *ret;
  360. va_list argp;
  361. lua_lock(L);
  362. luaC_checkGC(L);
  363. va_start(argp, fmt);
  364. ret = luaO_pushvfstring(L, fmt, argp);
  365. va_end(argp);
  366. lua_unlock(L);
  367. return ret;
  368. }
  369. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  370. Closure *cl;
  371. lua_lock(L);
  372. api_checknelems(L, n);
  373. api_check(L, n <= UCHAR_MAX);
  374. luaC_checkGC(L);
  375. cl = luaF_newCclosure(L, n, getcurrenv(L));
  376. cl->c.f = fn;
  377. L->top -= n;
  378. while (n--)
  379. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  380. setclvalue(L, L->top, cl);
  381. lua_assert(iswhite(obj2gco(cl)));
  382. api_incr_top(L);
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_pushboolean (lua_State *L, int b) {
  386. lua_lock(L);
  387. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  388. api_incr_top(L);
  389. lua_unlock(L);
  390. }
  391. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  392. lua_lock(L);
  393. setpvalue(L->top, p);
  394. api_incr_top(L);
  395. lua_unlock(L);
  396. }
  397. LUA_API int lua_pushthread (lua_State *L) {
  398. lua_lock(L);
  399. setthvalue(L, L->top, L);
  400. api_incr_top(L);
  401. lua_unlock(L);
  402. return (G(L)->mainthread == L);
  403. }
  404. /*
  405. ** get functions (Lua -> stack)
  406. */
  407. LUA_API void lua_gettable (lua_State *L, int idx) {
  408. StkId t;
  409. lua_lock(L);
  410. t = index2adr(L, idx);
  411. api_checkvalidindex(L, t);
  412. luaV_gettable(L, t, L->top - 1, L->top - 1);
  413. lua_unlock(L);
  414. }
  415. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  416. StkId t;
  417. lua_lock(L);
  418. t = index2adr(L, idx);
  419. api_checkvalidindex(L, t);
  420. setsvalue2s(L, L->top, luaS_new(L, k));
  421. api_incr_top(L);
  422. luaV_gettable(L, t, L->top - 1, L->top - 1);
  423. lua_unlock(L);
  424. }
  425. LUA_API void lua_rawget (lua_State *L, int idx) {
  426. StkId t;
  427. lua_lock(L);
  428. t = index2adr(L, idx);
  429. api_check(L, ttistable(t));
  430. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  431. lua_unlock(L);
  432. }
  433. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  434. StkId o;
  435. lua_lock(L);
  436. o = index2adr(L, idx);
  437. api_check(L, ttistable(o));
  438. setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  439. api_incr_top(L);
  440. lua_unlock(L);
  441. }
  442. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  443. Table *t;
  444. lua_lock(L);
  445. luaC_checkGC(L);
  446. t = luaH_new(L);
  447. sethvalue(L, L->top, t);
  448. api_incr_top(L);
  449. if (narray > 0 || nrec > 0)
  450. luaH_resize(L, t, narray, nrec);
  451. lua_unlock(L);
  452. }
  453. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  454. const TValue *obj;
  455. Table *mt = NULL;
  456. int res;
  457. lua_lock(L);
  458. obj = index2adr(L, objindex);
  459. switch (ttype(obj)) {
  460. case LUA_TTABLE:
  461. mt = hvalue(obj)->metatable;
  462. break;
  463. case LUA_TUSERDATA:
  464. mt = uvalue(obj)->metatable;
  465. break;
  466. default:
  467. mt = G(L)->mt[ttype(obj)];
  468. break;
  469. }
  470. if (mt == NULL)
  471. res = 0;
  472. else {
  473. sethvalue(L, L->top, mt);
  474. api_incr_top(L);
  475. res = 1;
  476. }
  477. lua_unlock(L);
  478. return res;
  479. }
  480. LUA_API void lua_getfenv (lua_State *L, int idx) {
  481. StkId o;
  482. lua_lock(L);
  483. o = index2adr(L, idx);
  484. api_checkvalidindex(L, o);
  485. switch (ttype(o)) {
  486. case LUA_TFUNCTION:
  487. sethvalue(L, L->top, clvalue(o)->c.env);
  488. break;
  489. case LUA_TUSERDATA:
  490. sethvalue(L, L->top, uvalue(o)->env);
  491. break;
  492. case LUA_TTHREAD:
  493. setobj2s(L, L->top, gt(thvalue(o)));
  494. break;
  495. default:
  496. setnilvalue(L->top);
  497. break;
  498. }
  499. api_incr_top(L);
  500. lua_unlock(L);
  501. }
  502. /*
  503. ** set functions (stack -> Lua)
  504. */
  505. LUA_API void lua_settable (lua_State *L, int idx) {
  506. StkId t;
  507. lua_lock(L);
  508. api_checknelems(L, 2);
  509. t = index2adr(L, idx);
  510. api_checkvalidindex(L, t);
  511. luaV_settable(L, t, L->top - 2, L->top - 1);
  512. L->top -= 2; /* pop index and value */
  513. lua_unlock(L);
  514. }
  515. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  516. StkId t;
  517. lua_lock(L);
  518. api_checknelems(L, 1);
  519. t = index2adr(L, idx);
  520. api_checkvalidindex(L, t);
  521. setsvalue2s(L, L->top++, luaS_new(L, k));
  522. luaV_settable(L, t, L->top - 1, L->top - 2);
  523. L->top -= 2; /* pop value and key */
  524. lua_unlock(L);
  525. }
  526. LUA_API void lua_rawset (lua_State *L, int idx) {
  527. StkId t;
  528. lua_lock(L);
  529. api_checknelems(L, 2);
  530. t = index2adr(L, idx);
  531. api_check(L, ttistable(t));
  532. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  533. luaC_barriert(L, hvalue(t), L->top-1);
  534. L->top -= 2;
  535. lua_unlock(L);
  536. }
  537. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  538. StkId o;
  539. lua_lock(L);
  540. api_checknelems(L, 1);
  541. o = index2adr(L, idx);
  542. api_check(L, ttistable(o));
  543. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  544. luaC_barriert(L, hvalue(o), L->top-1);
  545. L->top--;
  546. lua_unlock(L);
  547. }
  548. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  549. TValue *obj;
  550. Table *mt;
  551. lua_lock(L);
  552. api_checknelems(L, 1);
  553. obj = index2adr(L, objindex);
  554. api_checkvalidindex(L, obj);
  555. if (ttisnil(L->top - 1))
  556. mt = NULL;
  557. else {
  558. api_check(L, ttistable(L->top - 1));
  559. mt = hvalue(L->top - 1);
  560. }
  561. switch (ttype(obj)) {
  562. case LUA_TTABLE: {
  563. hvalue(obj)->metatable = mt;
  564. if (mt)
  565. luaC_objbarriert(L, hvalue(obj), mt);
  566. break;
  567. }
  568. case LUA_TUSERDATA: {
  569. uvalue(obj)->metatable = mt;
  570. if (mt)
  571. luaC_objbarrier(L, rawuvalue(obj), mt);
  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 a = (cast(lu_mem, data) << 10);
  746. if (a <= g->totalbytes)
  747. g->GCthreshold = g->totalbytes - a;
  748. else
  749. g->GCthreshold = 0;
  750. while (g->GCthreshold <= g->totalbytes)
  751. luaC_step(L);
  752. if (g->gcstate == GCSpause) /* end of cycle? */
  753. res = 1; /* signal it */
  754. break;
  755. }
  756. case LUA_GCSETPAUSE: {
  757. res = g->gcpause;
  758. g->gcpause = data;
  759. break;
  760. }
  761. case LUA_GCSETSTEPMUL: {
  762. res = g->gcstepmul;
  763. g->gcstepmul = data;
  764. break;
  765. }
  766. default: res = -1; /* invalid option */
  767. }
  768. lua_unlock(L);
  769. return res;
  770. }
  771. /*
  772. ** miscellaneous functions
  773. */
  774. LUA_API int lua_error (lua_State *L) {
  775. lua_lock(L);
  776. api_checknelems(L, 1);
  777. luaG_errormsg(L);
  778. lua_unlock(L);
  779. return 0; /* to avoid warnings */
  780. }
  781. LUA_API int lua_next (lua_State *L, int idx) {
  782. StkId t;
  783. int more;
  784. lua_lock(L);
  785. t = index2adr(L, idx);
  786. api_check(L, ttistable(t));
  787. more = luaH_next(L, hvalue(t), L->top - 1);
  788. if (more) {
  789. api_incr_top(L);
  790. }
  791. else /* no more elements */
  792. L->top -= 1; /* remove key */
  793. lua_unlock(L);
  794. return more;
  795. }
  796. LUA_API void lua_concat (lua_State *L, int n) {
  797. lua_lock(L);
  798. api_checknelems(L, n);
  799. if (n >= 2) {
  800. luaC_checkGC(L);
  801. luaV_concat(L, n, cast_int(L->top - L->base) - 1);
  802. L->top -= (n-1);
  803. }
  804. else if (n == 0) { /* push empty string */
  805. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  806. api_incr_top(L);
  807. }
  808. /* else n == 1; nothing to do */
  809. lua_unlock(L);
  810. }
  811. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  812. lua_Alloc f;
  813. lua_lock(L);
  814. if (ud) *ud = G(L)->ud;
  815. f = G(L)->frealloc;
  816. lua_unlock(L);
  817. return f;
  818. }
  819. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  820. lua_lock(L);
  821. G(L)->ud = ud;
  822. G(L)->frealloc = f;
  823. lua_unlock(L);
  824. }
  825. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  826. Udata *u;
  827. lua_lock(L);
  828. luaC_checkGC(L);
  829. u = luaS_newudata(L, size, getcurrenv(L));
  830. setuvalue(L, L->top, u);
  831. api_incr_top(L);
  832. lua_unlock(L);
  833. return u + 1;
  834. }
  835. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  836. Closure *f;
  837. if (!ttisfunction(fi)) return NULL;
  838. f = clvalue(fi);
  839. if (f->c.isC) {
  840. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  841. *val = &f->c.upvalue[n-1];
  842. return "";
  843. }
  844. else {
  845. Proto *p = f->l.p;
  846. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  847. *val = f->l.upvals[n-1]->v;
  848. return getstr(p->upvalues[n-1]);
  849. }
  850. }
  851. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  852. const char *name;
  853. TValue *val;
  854. lua_lock(L);
  855. name = aux_upvalue(index2adr(L, funcindex), n, &val);
  856. if (name) {
  857. setobj2s(L, L->top, val);
  858. api_incr_top(L);
  859. }
  860. lua_unlock(L);
  861. return name;
  862. }
  863. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  864. const char *name;
  865. TValue *val;
  866. StkId fi;
  867. lua_lock(L);
  868. fi = index2adr(L, funcindex);
  869. api_checknelems(L, 1);
  870. name = aux_upvalue(fi, n, &val);
  871. if (name) {
  872. L->top--;
  873. setobj(L, val, L->top);
  874. luaC_barrier(L, clvalue(fi), L->top);
  875. }
  876. lua_unlock(L);
  877. return name;
  878. }