lapi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. ** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
  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. #include "lrotable.h"
  27. const char lua_ident[] =
  28. "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
  29. "$Authors: " LUA_AUTHORS " $\n"
  30. "$URL: www.lua.org $\n";
  31. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
  32. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
  33. #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
  34. static TValue *index2adr(lua_State *L, int idx)
  35. {
  36. if (idx > 0)
  37. {
  38. TValue *o = L->base + (idx - 1);
  39. api_check(L, idx <= L->ci->top - L->base);
  40. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  41. else return o;
  42. }
  43. else if (idx > LUA_REGISTRYINDEX)
  44. {
  45. api_check(L, idx != 0 && -idx <= L->top - L->base);
  46. return L->top + idx;
  47. }
  48. else switch (idx) /* pseudo-indices */
  49. {
  50. case LUA_REGISTRYINDEX:
  51. return registry(L);
  52. case LUA_ENVIRONINDEX:
  53. {
  54. Closure *func = curr_func(L);
  55. sethvalue(L, &L->env, func ? func->c.env : hvalue(gt(L)));
  56. return &L->env;
  57. }
  58. case LUA_GLOBALSINDEX:
  59. return gt(L);
  60. default:
  61. {
  62. Closure *func = curr_func(L);
  63. if (!func) return cast(TValue *, luaO_nilobject);
  64. idx = LUA_GLOBALSINDEX - idx;
  65. return (idx <= func->c.nupvalues)
  66. ? &func->c.upvalue[idx - 1]
  67. : cast(TValue *, luaO_nilobject);
  68. }
  69. }
  70. }
  71. static Table *getcurrenv(lua_State *L)
  72. {
  73. if (L->ci == L->base_ci) /* no enclosing function? */
  74. return hvalue(gt(L)); /* use global table as environment */
  75. else
  76. {
  77. Closure *func = curr_func(L);
  78. return func ? func->c.env : hvalue(gt(L));
  79. }
  80. }
  81. void luaA_pushobject(lua_State *L, const TValue *o)
  82. {
  83. setobj2s(L, L->top, o);
  84. api_incr_top(L);
  85. }
  86. LUA_API int lua_checkstack(lua_State *L, int size)
  87. {
  88. int res = 1;
  89. lua_lock(L);
  90. if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
  91. res = 0; /* stack overflow */
  92. else if (size > 0)
  93. {
  94. luaD_checkstack(L, size);
  95. if (L->ci->top < L->top + size)
  96. L->ci->top = L->top + size;
  97. }
  98. lua_unlock(L);
  99. return res;
  100. }
  101. LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
  102. {
  103. int i;
  104. if (from == to) return;
  105. lua_lock(to);
  106. api_checknelems(from, n);
  107. api_check(from, G(from) == G(to));
  108. api_check(from, to->ci->top - to->top >= n);
  109. from->top -= n;
  110. for (i = 0; i < n; i++)
  111. {
  112. setobj2s(to, to->top++, from->top + i);
  113. }
  114. lua_unlock(to);
  115. }
  116. LUA_API void lua_setlevel(lua_State *from, lua_State *to)
  117. {
  118. to->nCcalls = from->nCcalls;
  119. }
  120. LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
  121. {
  122. lua_CFunction old;
  123. lua_lock(L);
  124. old = G(L)->panic;
  125. G(L)->panic = panicf;
  126. lua_unlock(L);
  127. return old;
  128. }
  129. LUA_API lua_State *lua_newthread(lua_State *L)
  130. {
  131. lua_State *L1;
  132. lua_lock(L);
  133. luaC_checkGC(L);
  134. L1 = luaE_newthread(L);
  135. setthvalue(L, L->top, L1);
  136. api_incr_top(L);
  137. lua_unlock(L);
  138. luai_userstatethread(L, L1);
  139. return L1;
  140. }
  141. /*
  142. ** basic stack manipulation
  143. */
  144. LUA_API int lua_gettop(lua_State *L)
  145. {
  146. return cast_int(L->top - L->base);
  147. }
  148. LUA_API void lua_settop(lua_State *L, int idx)
  149. {
  150. lua_lock(L);
  151. if (idx >= 0)
  152. {
  153. api_check(L, idx <= L->stack_last - L->base);
  154. while (L->top < L->base + idx)
  155. setnilvalue(L->top++);
  156. L->top = L->base + idx;
  157. }
  158. else
  159. {
  160. api_check(L, -(idx + 1) <= (L->top - L->base));
  161. L->top += idx + 1; /* `subtract' index (index is negative) */
  162. }
  163. lua_unlock(L);
  164. }
  165. LUA_API void lua_remove(lua_State *L, int idx)
  166. {
  167. StkId p;
  168. lua_lock(L);
  169. p = index2adr(L, idx);
  170. api_checkvalidindex(L, p);
  171. while (++p < L->top) setobjs2s(L, p - 1, p);
  172. L->top--;
  173. lua_unlock(L);
  174. }
  175. LUA_API void lua_insert(lua_State *L, int idx)
  176. {
  177. StkId p;
  178. StkId q;
  179. lua_lock(L);
  180. p = index2adr(L, idx);
  181. api_checkvalidindex(L, p);
  182. for (q = L->top; q > p; q--) setobjs2s(L, q, q - 1);
  183. setobjs2s(L, p, L->top);
  184. lua_unlock(L);
  185. }
  186. LUA_API void lua_replace(lua_State *L, int idx)
  187. {
  188. StkId o;
  189. lua_lock(L);
  190. /* explicit test for incompatible code */
  191. if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
  192. luaG_runerror(L, "no calling environment");
  193. api_checknelems(L, 1);
  194. o = index2adr(L, idx);
  195. api_checkvalidindex(L, o);
  196. if (idx == LUA_ENVIRONINDEX)
  197. {
  198. Closure *func = curr_func(L);
  199. if (!func)
  200. luaG_runerror(L, "attempt to set environment on lightfunction");
  201. else
  202. {
  203. api_check(L, ttistable(L->top - 1));
  204. func->c.env = hvalue(L->top - 1);
  205. luaC_barrier(L, func, L->top - 1);
  206. }
  207. }
  208. else
  209. {
  210. setobj(L, o, L->top - 1);
  211. if (curr_func(L) && idx < LUA_GLOBALSINDEX) /* function upvalue? */
  212. luaC_barrier(L, curr_func(L), L->top - 1);
  213. }
  214. L->top--;
  215. lua_unlock(L);
  216. }
  217. LUA_API void lua_pushvalue(lua_State *L, int idx)
  218. {
  219. lua_lock(L);
  220. setobj2s(L, L->top, index2adr(L, idx));
  221. api_incr_top(L);
  222. lua_unlock(L);
  223. }
  224. /*
  225. ** access functions (stack -> C)
  226. */
  227. LUA_API int lua_type(lua_State *L, int idx)
  228. {
  229. StkId o = index2adr(L, idx);
  230. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  231. }
  232. LUA_API const char *lua_typename(lua_State *L, int t)
  233. {
  234. UNUSED(L);
  235. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  236. }
  237. LUA_API int lua_iscfunction(lua_State *L, int idx)
  238. {
  239. StkId o = index2adr(L, idx);
  240. return iscfunction(o);
  241. }
  242. LUA_API int lua_isnumber(lua_State *L, int idx)
  243. {
  244. TValue n;
  245. const TValue *o = index2adr(L, idx);
  246. return tonumber(o, &n);
  247. }
  248. LUA_API int lua_isstring(lua_State *L, int idx)
  249. {
  250. int t = lua_type(L, idx);
  251. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  252. }
  253. LUA_API int lua_isuserdata(lua_State *L, int idx)
  254. {
  255. const TValue *o = index2adr(L, idx);
  256. return (ttisuserdata(o) || ttislightuserdata(o));
  257. }
  258. LUA_API int lua_rawequal(lua_State *L, int index1, int index2)
  259. {
  260. StkId o1 = index2adr(L, index1);
  261. StkId o2 = index2adr(L, index2);
  262. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  263. : luaO_rawequalObj(o1, o2);
  264. }
  265. LUA_API int lua_equal(lua_State *L, int index1, int index2)
  266. {
  267. StkId o1, o2;
  268. int i;
  269. lua_lock(L); /* may call tag method */
  270. o1 = index2adr(L, index1);
  271. o2 = index2adr(L, index2);
  272. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
  273. lua_unlock(L);
  274. return i;
  275. }
  276. LUA_API int lua_lessthan(lua_State *L, int index1, int index2)
  277. {
  278. StkId o1, o2;
  279. int i;
  280. lua_lock(L); /* may call tag method */
  281. o1 = index2adr(L, index1);
  282. o2 = index2adr(L, index2);
  283. i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  284. : luaV_lessthan(L, o1, o2);
  285. lua_unlock(L);
  286. return i;
  287. }
  288. LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
  289. {
  290. TValue n;
  291. const TValue *o = index2adr(L, idx);
  292. if (tonumber(o, &n))
  293. return nvalue(o);
  294. else
  295. return 0;
  296. }
  297. LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
  298. {
  299. TValue n;
  300. const TValue *o = index2adr(L, idx);
  301. if (tonumber(o, &n))
  302. {
  303. lua_Integer res;
  304. lua_Number num = nvalue(o);
  305. lua_number2integer(res, num);
  306. return res;
  307. }
  308. else
  309. return 0;
  310. }
  311. LUA_API int lua_toboolean(lua_State *L, int idx)
  312. {
  313. const TValue *o = index2adr(L, idx);
  314. return !l_isfalse(o);
  315. }
  316. LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
  317. {
  318. StkId o = index2adr(L, idx);
  319. if (!ttisstring(o))
  320. {
  321. lua_lock(L); /* `luaV_tostring' may create a new string */
  322. if (!luaV_tostring(L, o)) /* conversion failed? */
  323. {
  324. if (len != NULL) *len = 0;
  325. lua_unlock(L);
  326. return NULL;
  327. }
  328. luaC_checkGC(L);
  329. o = index2adr(L, idx); /* previous call may reallocate the stack */
  330. lua_unlock(L);
  331. }
  332. if (len != NULL) *len = tsvalue(o)->len;
  333. return svalue(o);
  334. }
  335. LUA_API size_t lua_objlen(lua_State *L, int idx)
  336. {
  337. StkId o = index2adr(L, idx);
  338. switch (ttype(o))
  339. {
  340. case LUA_TSTRING:
  341. return tsvalue(o)->len;
  342. case LUA_TUSERDATA:
  343. return uvalue(o)->len;
  344. case LUA_TTABLE:
  345. return luaH_getn(hvalue(o));
  346. case LUA_TROTABLE:
  347. return luaH_getn_ro(rvalue(o));
  348. case LUA_TNUMBER:
  349. {
  350. size_t l;
  351. lua_lock(L); /* `luaV_tostring' may create a new string */
  352. l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
  353. lua_unlock(L);
  354. return l;
  355. }
  356. default:
  357. return 0;
  358. }
  359. }
  360. LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
  361. {
  362. StkId o = index2adr(L, idx);
  363. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  364. }
  365. LUA_API void *lua_touserdata(lua_State *L, int idx)
  366. {
  367. StkId o = index2adr(L, idx);
  368. switch (ttype(o))
  369. {
  370. case LUA_TUSERDATA:
  371. return (rawuvalue(o) + 1);
  372. case LUA_TLIGHTUSERDATA:
  373. return pvalue(o);
  374. default:
  375. return NULL;
  376. }
  377. }
  378. LUA_API lua_State *lua_tothread(lua_State *L, int idx)
  379. {
  380. StkId o = index2adr(L, idx);
  381. return (!ttisthread(o)) ? NULL : thvalue(o);
  382. }
  383. LUA_API const void *lua_topointer(lua_State *L, int idx)
  384. {
  385. StkId o = index2adr(L, idx);
  386. switch (ttype(o))
  387. {
  388. case LUA_TTABLE:
  389. return hvalue(o);
  390. case LUA_TFUNCTION:
  391. return clvalue(o);
  392. case LUA_TTHREAD:
  393. return thvalue(o);
  394. case LUA_TUSERDATA:
  395. case LUA_TLIGHTUSERDATA:
  396. return lua_touserdata(L, idx);
  397. case LUA_TROTABLE:
  398. case LUA_TLIGHTFUNCTION:
  399. return pvalue(o);
  400. default:
  401. return NULL;
  402. }
  403. }
  404. /*
  405. ** push functions (C -> stack)
  406. */
  407. LUA_API void lua_pushnil(lua_State *L)
  408. {
  409. lua_lock(L);
  410. setnilvalue(L->top);
  411. api_incr_top(L);
  412. lua_unlock(L);
  413. }
  414. LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
  415. {
  416. lua_lock(L);
  417. setnvalue(L->top, n);
  418. api_incr_top(L);
  419. lua_unlock(L);
  420. }
  421. LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
  422. {
  423. lua_lock(L);
  424. setnvalue(L->top, cast_num(n));
  425. api_incr_top(L);
  426. lua_unlock(L);
  427. }
  428. LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)
  429. {
  430. lua_lock(L);
  431. luaC_checkGC(L);
  432. setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
  433. api_incr_top(L);
  434. lua_unlock(L);
  435. }
  436. LUA_API void lua_pushrolstring(lua_State *L, const char *s, size_t len)
  437. {
  438. lua_lock(L);
  439. luaC_checkGC(L);
  440. setsvalue2s(L, L->top, luaS_newrolstr(L, s, len));
  441. api_incr_top(L);
  442. lua_unlock(L);
  443. }
  444. LUA_API void lua_pushstring(lua_State *L, const char *s)
  445. {
  446. if (s == NULL)
  447. lua_pushnil(L);
  448. else
  449. lua_pushlstring(L, s, strlen(s));
  450. }
  451. LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
  452. va_list argp)
  453. {
  454. const char *ret;
  455. lua_lock(L);
  456. luaC_checkGC(L);
  457. ret = luaO_pushvfstring(L, fmt, argp);
  458. lua_unlock(L);
  459. return ret;
  460. }
  461. LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
  462. {
  463. const char *ret;
  464. va_list argp;
  465. lua_lock(L);
  466. luaC_checkGC(L);
  467. va_start(argp, fmt);
  468. ret = luaO_pushvfstring(L, fmt, argp);
  469. va_end(argp);
  470. lua_unlock(L);
  471. return ret;
  472. }
  473. LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
  474. {
  475. Closure *cl;
  476. lua_lock(L);
  477. luaC_checkGC(L);
  478. api_checknelems(L, n);
  479. cl = luaF_newCclosure(L, n, getcurrenv(L));
  480. cl->c.f = fn;
  481. L->top -= n;
  482. while (n--)
  483. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  484. setclvalue(L, L->top, cl);
  485. lua_assert(iswhite(obj2gco(cl)));
  486. api_incr_top(L);
  487. lua_unlock(L);
  488. }
  489. LUA_API void lua_pushboolean(lua_State *L, int b)
  490. {
  491. lua_lock(L);
  492. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  493. api_incr_top(L);
  494. lua_unlock(L);
  495. }
  496. LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
  497. {
  498. lua_lock(L);
  499. setpvalue(L->top, p);
  500. api_incr_top(L);
  501. lua_unlock(L);
  502. }
  503. LUA_API void lua_pushrotable(lua_State *L, void *p)
  504. {
  505. lua_lock(L);
  506. setrvalue(L->top, p);
  507. api_incr_top(L);
  508. lua_unlock(L);
  509. }
  510. LUA_API void lua_pushlightfunction(lua_State *L, void *p)
  511. {
  512. lua_lock(L);
  513. setfvalue(L->top, p);
  514. api_incr_top(L);
  515. lua_unlock(L);
  516. }
  517. LUA_API int lua_pushthread(lua_State *L)
  518. {
  519. lua_lock(L);
  520. setthvalue(L, L->top, L);
  521. api_incr_top(L);
  522. lua_unlock(L);
  523. return (G(L)->mainthread == L);
  524. }
  525. /*
  526. ** get functions (Lua -> stack)
  527. */
  528. LUA_API void lua_gettable(lua_State *L, int idx)
  529. {
  530. StkId t;
  531. lua_lock(L);
  532. t = index2adr(L, idx);
  533. api_checkvalidindex(L, t);
  534. luaV_gettable(L, t, L->top - 1, L->top - 1);
  535. lua_unlock(L);
  536. }
  537. LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
  538. {
  539. StkId t;
  540. TValue key;
  541. lua_lock(L);
  542. t = index2adr(L, idx);
  543. api_checkvalidindex(L, t);
  544. fixedstack(L);
  545. setsvalue(L, &key, luaS_new(L, k));
  546. unfixedstack(L);
  547. luaV_gettable(L, t, &key, L->top);
  548. api_incr_top(L);
  549. lua_unlock(L);
  550. }
  551. LUA_API void lua_rawget(lua_State *L, int idx)
  552. {
  553. StkId t;
  554. const TValue *res;
  555. lua_lock(L);
  556. t = index2adr(L, idx);
  557. api_check(L, ttistable(t) || ttisrotable(t));
  558. res = ttistable(t) ? luaH_get(hvalue(t), L->top - 1) : luaH_get_ro(rvalue(t), L->top - 1);
  559. setobj2s(L, L->top - 1, res);
  560. lua_unlock(L);
  561. }
  562. LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
  563. {
  564. StkId o;
  565. lua_lock(L);
  566. o = index2adr(L, idx);
  567. api_check(L, ttistable(o) || ttisrotable(o));
  568. setobj2s(L, L->top, ttistable(o) ? luaH_getnum(hvalue(o), n) : luaH_getnum_ro(rvalue(o), n))
  569. api_incr_top(L);
  570. lua_unlock(L);
  571. }
  572. LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
  573. {
  574. lua_lock(L);
  575. luaC_checkGC(L);
  576. sethvalue(L, L->top, luaH_new(L, narray, nrec));
  577. api_incr_top(L);
  578. lua_unlock(L);
  579. }
  580. LUA_API int lua_getmetatable(lua_State *L, int objindex)
  581. {
  582. const TValue *obj;
  583. Table *mt = NULL;
  584. int res;
  585. lua_lock(L);
  586. obj = index2adr(L, objindex);
  587. switch (ttype(obj))
  588. {
  589. case LUA_TTABLE:
  590. mt = hvalue(obj)->metatable;
  591. break;
  592. case LUA_TUSERDATA:
  593. mt = uvalue(obj)->metatable;
  594. break;
  595. case LUA_TROTABLE:
  596. mt = (Table *)luaR_getmeta(rvalue(obj));
  597. break;
  598. default:
  599. mt = G(L)->mt[ttype(obj)];
  600. break;
  601. }
  602. if (mt == NULL)
  603. res = 0;
  604. else
  605. {
  606. if (luaR_isrotable(mt))
  607. setrvalue(L->top, mt)
  608. else
  609. sethvalue(L, L->top, mt)
  610. api_incr_top(L);
  611. res = 1;
  612. }
  613. lua_unlock(L);
  614. return res;
  615. }
  616. LUA_API void lua_getfenv(lua_State *L, int idx)
  617. {
  618. StkId o;
  619. lua_lock(L);
  620. o = index2adr(L, idx);
  621. api_checkvalidindex(L, o);
  622. switch (ttype(o))
  623. {
  624. case LUA_TFUNCTION:
  625. sethvalue(L, L->top, clvalue(o)->c.env);
  626. break;
  627. case LUA_TUSERDATA:
  628. sethvalue(L, L->top, uvalue(o)->env);
  629. break;
  630. case LUA_TTHREAD:
  631. setobj2s(L, L->top, gt(thvalue(o)));
  632. break;
  633. default:
  634. setnilvalue(L->top);
  635. break;
  636. }
  637. api_incr_top(L);
  638. lua_unlock(L);
  639. }
  640. /*
  641. ** set functions (stack -> Lua)
  642. */
  643. LUA_API void lua_settable(lua_State *L, int idx)
  644. {
  645. StkId t;
  646. lua_lock(L);
  647. api_checknelems(L, 2);
  648. t = index2adr(L, idx);
  649. api_checkvalidindex(L, t);
  650. luaV_settable(L, t, L->top - 2, L->top - 1);
  651. L->top -= 2; /* pop index and value */
  652. lua_unlock(L);
  653. }
  654. LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
  655. {
  656. StkId t;
  657. lua_lock(L);
  658. api_checknelems(L, 1);
  659. t = index2adr(L, idx);
  660. api_checkvalidindex(L, t);
  661. setsvalue2s(L, L->top, luaS_new(L, k));
  662. api_incr_top(L);
  663. luaV_settable(L, t, L->top - 1, L->top - 2);
  664. L->top -= 2; /* pop key and value */
  665. lua_unlock(L);
  666. }
  667. LUA_API void lua_rawset(lua_State *L, int idx)
  668. {
  669. StkId t;
  670. lua_lock(L);
  671. api_checknelems(L, 2);
  672. t = index2adr(L, idx);
  673. api_check(L, ttistable(t));
  674. fixedstack(L);
  675. setobj2t(L, luaH_set(L, hvalue(t), L->top - 2), L->top - 1);
  676. unfixedstack(L);
  677. luaC_barriert(L, hvalue(t), L->top - 1);
  678. L->top -= 2;
  679. lua_unlock(L);
  680. }
  681. LUA_API void lua_rawseti(lua_State *L, int idx, int n)
  682. {
  683. StkId o;
  684. lua_lock(L);
  685. api_checknelems(L, 1);
  686. o = index2adr(L, idx);
  687. api_check(L, ttistable(o));
  688. fixedstack(L);
  689. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top - 1);
  690. unfixedstack(L);
  691. luaC_barriert(L, hvalue(o), L->top - 1);
  692. L->top--;
  693. lua_unlock(L);
  694. }
  695. LUA_API int lua_setmetatable(lua_State *L, int objindex)
  696. {
  697. TValue *obj;
  698. Table *mt;
  699. int isrometa = 0;
  700. lua_lock(L);
  701. api_checknelems(L, 1);
  702. obj = index2adr(L, objindex);
  703. api_checkvalidindex(L, obj);
  704. if (ttisnil(L->top - 1))
  705. mt = NULL;
  706. else
  707. {
  708. api_check(L, ttistable(L->top - 1) || ttisrotable(L->top - 1));
  709. if (ttistable(L->top - 1))
  710. mt = hvalue(L->top - 1);
  711. else
  712. {
  713. mt = (Table *)rvalue(L->top - 1);
  714. isrometa = 1;
  715. }
  716. }
  717. switch (ttype(obj))
  718. {
  719. case LUA_TTABLE:
  720. {
  721. hvalue(obj)->metatable = mt;
  722. if (mt && !isrometa)
  723. luaC_objbarriert(L, hvalue(obj), mt);
  724. break;
  725. }
  726. case LUA_TUSERDATA:
  727. {
  728. uvalue(obj)->metatable = mt;
  729. if (mt && !isrometa)
  730. luaC_objbarrier(L, rawuvalue(obj), mt);
  731. break;
  732. }
  733. default:
  734. {
  735. G(L)->mt[ttype(obj)] = mt;
  736. break;
  737. }
  738. }
  739. L->top--;
  740. lua_unlock(L);
  741. return 1;
  742. }
  743. LUA_API int lua_setfenv(lua_State *L, int idx)
  744. {
  745. StkId o;
  746. int res = 1;
  747. lua_lock(L);
  748. api_checknelems(L, 1);
  749. o = index2adr(L, idx);
  750. api_checkvalidindex(L, o);
  751. api_check(L, ttistable(L->top - 1));
  752. switch (ttype(o))
  753. {
  754. case LUA_TFUNCTION:
  755. clvalue(o)->c.env = hvalue(L->top - 1);
  756. break;
  757. case LUA_TUSERDATA:
  758. uvalue(o)->env = hvalue(L->top - 1);
  759. break;
  760. case LUA_TTHREAD:
  761. sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
  762. break;
  763. default:
  764. res = 0;
  765. break;
  766. }
  767. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  768. L->top--;
  769. lua_unlock(L);
  770. return res;
  771. }
  772. /*
  773. ** `load' and `call' functions (run Lua code)
  774. */
  775. #define adjustresults(L,nres) \
  776. { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
  777. #define checkresults(L,na,nr) \
  778. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
  779. LUA_API void lua_call(lua_State *L, int nargs, int nresults)
  780. {
  781. StkId func;
  782. lua_lock(L);
  783. api_checknelems(L, nargs + 1);
  784. checkresults(L, nargs, nresults);
  785. func = L->top - (nargs + 1);
  786. luaD_call(L, func, nresults);
  787. adjustresults(L, nresults);
  788. lua_unlock(L);
  789. }
  790. /*
  791. ** Execute a protected call.
  792. */
  793. struct CallS /* data to `f_call' */
  794. {
  795. StkId func;
  796. int nresults;
  797. };
  798. static void f_call(lua_State *L, void *ud)
  799. {
  800. struct CallS *c = cast(struct CallS *, ud);
  801. luaD_call(L, c->func, c->nresults);
  802. }
  803. LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
  804. {
  805. struct CallS c;
  806. int status;
  807. ptrdiff_t func;
  808. lua_lock(L);
  809. api_checknelems(L, nargs + 1);
  810. checkresults(L, nargs, nresults);
  811. if (errfunc == 0)
  812. func = 0;
  813. else
  814. {
  815. StkId o = index2adr(L, errfunc);
  816. api_checkvalidindex(L, o);
  817. func = savestack(L, o);
  818. }
  819. c.func = L->top - (nargs + 1); /* function to be called */
  820. c.nresults = nresults;
  821. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  822. adjustresults(L, nresults);
  823. lua_unlock(L);
  824. return status;
  825. }
  826. /*
  827. ** Execute a protected C call.
  828. */
  829. struct CCallS /* data to `f_Ccall' */
  830. {
  831. lua_CFunction func;
  832. void *ud;
  833. };
  834. static void f_Ccall(lua_State *L, void *ud)
  835. {
  836. struct CCallS *c = cast(struct CCallS *, ud);
  837. Closure *cl;
  838. cl = luaF_newCclosure(L, 0, getcurrenv(L));
  839. cl->c.f = c->func;
  840. setclvalue(L, L->top, cl); /* push function */
  841. api_incr_top(L);
  842. setpvalue(L->top, c->ud); /* push only argument */
  843. api_incr_top(L);
  844. luaD_call(L, L->top - 2, 0);
  845. }
  846. LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
  847. {
  848. struct CCallS c;
  849. int status;
  850. lua_lock(L);
  851. c.func = func;
  852. c.ud = ud;
  853. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  854. lua_unlock(L);
  855. return status;
  856. }
  857. LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
  858. const char *chunkname)
  859. {
  860. ZIO z;
  861. int status;
  862. lua_lock(L);
  863. if (!chunkname) chunkname = "?";
  864. luaZ_init(L, &z, reader, data);
  865. status = luaD_protectedparser(L, &z, chunkname);
  866. lua_unlock(L);
  867. return status;
  868. }
  869. LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
  870. {
  871. int status;
  872. TValue *o;
  873. lua_lock(L);
  874. api_checknelems(L, 1);
  875. o = L->top - 1;
  876. if (isLfunction(o))
  877. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  878. else
  879. status = 1;
  880. lua_unlock(L);
  881. return status;
  882. }
  883. LUA_API int lua_status(lua_State *L)
  884. {
  885. return L->status;
  886. }
  887. /*
  888. ** Garbage-collection function
  889. */
  890. LUA_API int lua_gc(lua_State *L, int what, int data)
  891. {
  892. int res = 0;
  893. global_State *g;
  894. lua_lock(L);
  895. g = G(L);
  896. switch (what)
  897. {
  898. case LUA_GCSTOP:
  899. {
  900. set_block_gc(L);
  901. break;
  902. }
  903. case LUA_GCRESTART:
  904. {
  905. unset_block_gc(L);
  906. break;
  907. }
  908. case LUA_GCCOLLECT:
  909. {
  910. luaC_fullgc(L);
  911. break;
  912. }
  913. case LUA_GCCOUNT:
  914. {
  915. /* GC values are expressed in Kbytes: #bytes/2^10 */
  916. res = cast_int(g->totalbytes >> 10);
  917. break;
  918. }
  919. case LUA_GCCOUNTB:
  920. {
  921. res = cast_int(g->totalbytes & 0x3ff);
  922. break;
  923. }
  924. case LUA_GCSTEP:
  925. {
  926. if (is_block_gc(L))
  927. {
  928. res = 1; /* gc is block so we need to pretend that the collection cycle finished. */
  929. break;
  930. }
  931. lu_mem a = (cast(lu_mem, data) << 10);
  932. if (a <= g->totalbytes)
  933. g->GCthreshold = g->totalbytes - a;
  934. else
  935. g->GCthreshold = 0;
  936. while (g->GCthreshold <= g->totalbytes)
  937. {
  938. luaC_step(L);
  939. if (g->gcstate == GCSpause) /* end of cycle? */
  940. {
  941. res = 1; /* signal it */
  942. break;
  943. }
  944. }
  945. break;
  946. }
  947. case LUA_GCSETPAUSE:
  948. {
  949. res = g->gcpause;
  950. g->gcpause = data;
  951. break;
  952. }
  953. case LUA_GCSETSTEPMUL:
  954. {
  955. res = g->gcstepmul;
  956. g->gcstepmul = data;
  957. break;
  958. }
  959. case LUA_GCSETMEMLIMIT:
  960. {
  961. /* GC values are expressed in Kbytes: #bytes/2^10 */
  962. lu_mem new_memlimit = (cast(lu_mem, data) << 10);
  963. if (new_memlimit > 0 && new_memlimit < g->totalbytes)
  964. {
  965. /* run a full GC to make totalbytes < the new limit. */
  966. luaC_fullgc(L);
  967. if (new_memlimit < g->totalbytes)
  968. new_memlimit = (g->totalbytes + 1024) & ~(1024 - 1); /* round up to next multiple of 1024 */
  969. }
  970. g->memlimit = new_memlimit;
  971. /* new memlimit might be > then requested memlimit. */
  972. res = cast_int(new_memlimit >> 10);
  973. break;
  974. }
  975. case LUA_GCGETMEMLIMIT:
  976. {
  977. res = cast_int(g->memlimit >> 10);
  978. break;
  979. }
  980. default:
  981. res = -1; /* invalid option */
  982. }
  983. lua_unlock(L);
  984. return res;
  985. }
  986. /*
  987. ** miscellaneous functions
  988. */
  989. LUA_API int lua_error(lua_State *L)
  990. {
  991. lua_lock(L);
  992. api_checknelems(L, 1);
  993. luaG_errormsg(L);
  994. lua_unlock(L);
  995. return 0; /* to avoid warnings */
  996. }
  997. LUA_API int lua_next(lua_State *L, int idx)
  998. {
  999. StkId t;
  1000. int more;
  1001. lua_lock(L);
  1002. t = index2adr(L, idx);
  1003. api_check(L, ttistable(t) || ttisrotable(t));
  1004. more = ttistable(t) ? luaH_next(L, hvalue(t), L->top - 1) : luaH_next_ro(L, rvalue(t), L->top - 1);
  1005. if (more)
  1006. {
  1007. api_incr_top(L);
  1008. }
  1009. else /* no more elements */
  1010. L->top -= 1; /* remove key */
  1011. lua_unlock(L);
  1012. return more;
  1013. }
  1014. LUA_API void lua_concat(lua_State *L, int n)
  1015. {
  1016. lua_lock(L);
  1017. api_checknelems(L, n);
  1018. if (n >= 2)
  1019. {
  1020. luaC_checkGC(L);
  1021. luaV_concat(L, n, cast_int(L->top - L->base) - 1);
  1022. L->top -= (n - 1);
  1023. }
  1024. else if (n == 0) /* push empty string */
  1025. {
  1026. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  1027. api_incr_top(L);
  1028. }
  1029. /* else n == 1; nothing to do */
  1030. lua_unlock(L);
  1031. }
  1032. LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
  1033. {
  1034. lua_Alloc f;
  1035. lua_lock(L);
  1036. if (ud) *ud = G(L)->ud;
  1037. f = G(L)->frealloc;
  1038. lua_unlock(L);
  1039. return f;
  1040. }
  1041. LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
  1042. {
  1043. lua_lock(L);
  1044. G(L)->ud = ud;
  1045. G(L)->frealloc = f;
  1046. lua_unlock(L);
  1047. }
  1048. LUA_API void *lua_newuserdata(lua_State *L, size_t size)
  1049. {
  1050. Udata *u;
  1051. lua_lock(L);
  1052. luaC_checkGC(L);
  1053. u = luaS_newudata(L, size, getcurrenv(L));
  1054. setuvalue(L, L->top, u);
  1055. api_incr_top(L);
  1056. lua_unlock(L);
  1057. return u + 1;
  1058. }
  1059. static const char *aux_upvalue(StkId fi, int n, TValue **val)
  1060. {
  1061. Closure *f;
  1062. if (!ttisfunction(fi)) return NULL;
  1063. f = clvalue(fi);
  1064. if (f->c.isC)
  1065. {
  1066. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  1067. *val = &f->c.upvalue[n - 1];
  1068. return "";
  1069. }
  1070. else
  1071. {
  1072. Proto *p = f->l.p;
  1073. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1074. *val = f->l.upvals[n - 1]->v;
  1075. return getstr(p->upvalues[n - 1]);
  1076. }
  1077. }
  1078. LUA_API const char *lua_getupvalue(lua_State *L, int funcindex, int n)
  1079. {
  1080. const char *name;
  1081. TValue *val;
  1082. lua_lock(L);
  1083. name = aux_upvalue(index2adr(L, funcindex), n, &val);
  1084. if (name)
  1085. {
  1086. setobj2s(L, L->top, val);
  1087. api_incr_top(L);
  1088. }
  1089. lua_unlock(L);
  1090. return name;
  1091. }
  1092. LUA_API const char *lua_setupvalue(lua_State *L, int funcindex, int n)
  1093. {
  1094. const char *name;
  1095. TValue *val;
  1096. StkId fi;
  1097. lua_lock(L);
  1098. fi = index2adr(L, funcindex);
  1099. api_checknelems(L, 1);
  1100. name = aux_upvalue(fi, n, &val);
  1101. if (name)
  1102. {
  1103. L->top--;
  1104. setobj(L, val, L->top);
  1105. luaC_barrier(L, clvalue(fi), L->top);
  1106. }
  1107. lua_unlock(L);
  1108. return name;
  1109. }