lapi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. ** $Id: lapi.c,v 2.62 2007/11/28 18:27:38 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. return (idx <= func->c.nupvalues)
  54. ? &func->c.upvalue[idx-1]
  55. : cast(TValue *, luaO_nilobject);
  56. }
  57. }
  58. }
  59. static Table *getcurrenv (lua_State *L) {
  60. if (L->ci == L->base_ci) /* no enclosing function? */
  61. return hvalue(gt(L)); /* use global table as environment */
  62. else {
  63. Closure *func = curr_func(L);
  64. return func->c.env;
  65. }
  66. }
  67. LUA_API int lua_checkstack (lua_State *L, int size) {
  68. int res;
  69. lua_lock(L);
  70. if ((L->top - L->base + size) > LUAI_MAXCSTACK)
  71. res = 0; /* stack overflow */
  72. else {
  73. luaD_checkstack(L, size);
  74. if (L->ci->top < L->top + size)
  75. L->ci->top = L->top + size;
  76. res = 1;
  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. luaC_checkGC(L);
  373. api_checknelems(L, n);
  374. cl = luaF_newCclosure(L, n, getcurrenv(L));
  375. cl->c.f = fn;
  376. L->top -= n;
  377. while (n--)
  378. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  379. setclvalue(L, L->top, cl);
  380. lua_assert(iswhite(obj2gco(cl)));
  381. api_incr_top(L);
  382. lua_unlock(L);
  383. }
  384. LUA_API void lua_pushboolean (lua_State *L, int b) {
  385. lua_lock(L);
  386. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  387. api_incr_top(L);
  388. lua_unlock(L);
  389. }
  390. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  391. lua_lock(L);
  392. setpvalue(L->top, p);
  393. api_incr_top(L);
  394. lua_unlock(L);
  395. }
  396. LUA_API int lua_pushthread (lua_State *L) {
  397. lua_lock(L);
  398. setthvalue(L, L->top, L);
  399. api_incr_top(L);
  400. lua_unlock(L);
  401. return (G(L)->mainthread == L);
  402. }
  403. /*
  404. ** get functions (Lua -> stack)
  405. */
  406. LUA_API void lua_gettable (lua_State *L, int idx) {
  407. StkId t;
  408. lua_lock(L);
  409. t = index2adr(L, idx);
  410. api_checkvalidindex(L, t);
  411. luaV_gettable(L, t, L->top - 1, L->top - 1);
  412. lua_unlock(L);
  413. }
  414. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  415. StkId t;
  416. lua_lock(L);
  417. t = index2adr(L, idx);
  418. api_checkvalidindex(L, t);
  419. setsvalue2s(L, L->top, luaS_new(L, k));
  420. api_incr_top(L);
  421. luaV_gettable(L, t, L->top - 1, L->top - 1);
  422. lua_unlock(L);
  423. }
  424. LUA_API void lua_rawget (lua_State *L, int idx) {
  425. StkId t;
  426. lua_lock(L);
  427. t = index2adr(L, idx);
  428. api_check(L, ttistable(t));
  429. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  430. lua_unlock(L);
  431. }
  432. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  433. StkId o;
  434. lua_lock(L);
  435. o = index2adr(L, idx);
  436. api_check(L, ttistable(o));
  437. setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  438. api_incr_top(L);
  439. lua_unlock(L);
  440. }
  441. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  442. Table *t;
  443. lua_lock(L);
  444. luaC_checkGC(L);
  445. t = luaH_new(L);
  446. sethvalue(L, L->top, t);
  447. api_incr_top(L);
  448. if (narray > 0 || nrec > 0)
  449. luaH_resize(L, t, narray, nrec);
  450. lua_unlock(L);
  451. }
  452. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  453. const TValue *obj;
  454. Table *mt = NULL;
  455. int res;
  456. lua_lock(L);
  457. obj = index2adr(L, objindex);
  458. switch (ttype(obj)) {
  459. case LUA_TTABLE:
  460. mt = hvalue(obj)->metatable;
  461. break;
  462. case LUA_TUSERDATA:
  463. mt = uvalue(obj)->metatable;
  464. break;
  465. default:
  466. mt = G(L)->mt[ttype(obj)];
  467. break;
  468. }
  469. if (mt == NULL)
  470. res = 0;
  471. else {
  472. sethvalue(L, L->top, mt);
  473. api_incr_top(L);
  474. res = 1;
  475. }
  476. lua_unlock(L);
  477. return res;
  478. }
  479. LUA_API void lua_getfenv (lua_State *L, int idx) {
  480. StkId o;
  481. lua_lock(L);
  482. o = index2adr(L, idx);
  483. api_checkvalidindex(L, o);
  484. switch (ttype(o)) {
  485. case LUA_TFUNCTION:
  486. sethvalue(L, L->top, clvalue(o)->c.env);
  487. break;
  488. case LUA_TUSERDATA:
  489. sethvalue(L, L->top, uvalue(o)->env);
  490. break;
  491. case LUA_TTHREAD:
  492. setobj2s(L, L->top, gt(thvalue(o)));
  493. break;
  494. default:
  495. setnilvalue(L->top);
  496. break;
  497. }
  498. api_incr_top(L);
  499. lua_unlock(L);
  500. }
  501. /*
  502. ** set functions (stack -> Lua)
  503. */
  504. LUA_API void lua_settable (lua_State *L, int idx) {
  505. StkId t;
  506. lua_lock(L);
  507. api_checknelems(L, 2);
  508. t = index2adr(L, idx);
  509. api_checkvalidindex(L, t);
  510. luaV_settable(L, t, L->top - 2, L->top - 1);
  511. L->top -= 2; /* pop index and value */
  512. lua_unlock(L);
  513. }
  514. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  515. StkId t;
  516. lua_lock(L);
  517. api_checknelems(L, 1);
  518. t = index2adr(L, idx);
  519. api_checkvalidindex(L, t);
  520. setsvalue2s(L, L->top++, luaS_new(L, k));
  521. luaV_settable(L, t, L->top - 1, L->top - 2);
  522. L->top -= 2; /* pop value and key */
  523. lua_unlock(L);
  524. }
  525. LUA_API void lua_rawset (lua_State *L, int idx) {
  526. StkId t;
  527. lua_lock(L);
  528. api_checknelems(L, 2);
  529. t = index2adr(L, idx);
  530. api_check(L, ttistable(t));
  531. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  532. luaC_barriert(L, hvalue(t), L->top-1);
  533. L->top -= 2;
  534. lua_unlock(L);
  535. }
  536. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  537. StkId o;
  538. lua_lock(L);
  539. api_checknelems(L, 1);
  540. o = index2adr(L, idx);
  541. api_check(L, ttistable(o));
  542. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  543. luaC_barriert(L, hvalue(o), L->top-1);
  544. L->top--;
  545. lua_unlock(L);
  546. }
  547. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  548. TValue *obj;
  549. Table *mt;
  550. lua_lock(L);
  551. api_checknelems(L, 1);
  552. obj = index2adr(L, objindex);
  553. api_checkvalidindex(L, obj);
  554. if (ttisnil(L->top - 1))
  555. mt = NULL;
  556. else {
  557. api_check(L, ttistable(L->top - 1));
  558. mt = hvalue(L->top - 1);
  559. }
  560. switch (ttype(obj)) {
  561. case LUA_TTABLE: {
  562. hvalue(obj)->metatable = mt;
  563. if (mt)
  564. luaC_objbarriert(L, hvalue(obj), mt);
  565. break;
  566. }
  567. case LUA_TUSERDATA: {
  568. uvalue(obj)->metatable = mt;
  569. if (mt)
  570. luaC_objbarrier(L, rawuvalue(obj), mt);
  571. break;
  572. }
  573. default: {
  574. G(L)->mt[ttype(obj)] = mt;
  575. break;
  576. }
  577. }
  578. L->top--;
  579. lua_unlock(L);
  580. return 1;
  581. }
  582. LUA_API int lua_setfenv (lua_State *L, int idx) {
  583. StkId o;
  584. int res = 1;
  585. lua_lock(L);
  586. api_checknelems(L, 1);
  587. o = index2adr(L, idx);
  588. api_checkvalidindex(L, o);
  589. api_check(L, ttistable(L->top - 1));
  590. switch (ttype(o)) {
  591. case LUA_TFUNCTION:
  592. clvalue(o)->c.env = hvalue(L->top - 1);
  593. break;
  594. case LUA_TUSERDATA:
  595. uvalue(o)->env = hvalue(L->top - 1);
  596. break;
  597. case LUA_TTHREAD:
  598. sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
  599. break;
  600. default:
  601. res = 0;
  602. break;
  603. }
  604. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  605. L->top--;
  606. lua_unlock(L);
  607. return res;
  608. }
  609. /*
  610. ** `load' and `call' functions (run Lua code)
  611. */
  612. #define adjustresults(L,nres) \
  613. { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
  614. #define checkresults(L,na,nr) \
  615. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
  616. LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
  617. StkId func;
  618. lua_lock(L);
  619. api_checknelems(L, nargs+1);
  620. checkresults(L, nargs, nresults);
  621. func = L->top - (nargs+1);
  622. luaD_call(L, func, nresults);
  623. adjustresults(L, nresults);
  624. lua_unlock(L);
  625. }
  626. /*
  627. ** Execute a protected call.
  628. */
  629. struct CallS { /* data to `f_call' */
  630. StkId func;
  631. int nresults;
  632. };
  633. static void f_call (lua_State *L, void *ud) {
  634. struct CallS *c = cast(struct CallS *, ud);
  635. luaD_call(L, c->func, c->nresults);
  636. }
  637. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
  638. struct CallS c;
  639. int status;
  640. ptrdiff_t func;
  641. lua_lock(L);
  642. api_checknelems(L, nargs+1);
  643. checkresults(L, nargs, nresults);
  644. if (errfunc == 0)
  645. func = 0;
  646. else {
  647. StkId o = index2adr(L, errfunc);
  648. api_checkvalidindex(L, o);
  649. func = savestack(L, o);
  650. }
  651. c.func = L->top - (nargs+1); /* function to be called */
  652. c.nresults = nresults;
  653. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  654. adjustresults(L, nresults);
  655. lua_unlock(L);
  656. return status;
  657. }
  658. /*
  659. ** Execute a protected C call.
  660. */
  661. struct CCallS { /* data to `f_Ccall' */
  662. lua_CFunction func;
  663. void *ud;
  664. };
  665. static void f_Ccall (lua_State *L, void *ud) {
  666. struct CCallS *c = cast(struct CCallS *, ud);
  667. Closure *cl;
  668. cl = luaF_newCclosure(L, 0, getcurrenv(L));
  669. cl->c.f = c->func;
  670. setclvalue(L, L->top, cl); /* push function */
  671. api_incr_top(L);
  672. setpvalue(L->top, c->ud); /* push only argument */
  673. api_incr_top(L);
  674. luaD_call(L, L->top - 2, 0);
  675. }
  676. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  677. struct CCallS c;
  678. int status;
  679. lua_lock(L);
  680. c.func = func;
  681. c.ud = ud;
  682. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  683. lua_unlock(L);
  684. return status;
  685. }
  686. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  687. const char *chunkname) {
  688. ZIO z;
  689. int status;
  690. lua_lock(L);
  691. if (!chunkname) chunkname = "?";
  692. luaZ_init(L, &z, reader, data);
  693. status = luaD_protectedparser(L, &z, chunkname);
  694. lua_unlock(L);
  695. return status;
  696. }
  697. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  698. int status;
  699. TValue *o;
  700. lua_lock(L);
  701. api_checknelems(L, 1);
  702. o = L->top - 1;
  703. if (isLfunction(o))
  704. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  705. else
  706. status = 1;
  707. lua_unlock(L);
  708. return status;
  709. }
  710. LUA_API int lua_status (lua_State *L) {
  711. return L->status;
  712. }
  713. /*
  714. ** Garbage-collection function
  715. */
  716. LUA_API int lua_gc (lua_State *L, int what, int data) {
  717. int res = 0;
  718. global_State *g;
  719. lua_lock(L);
  720. g = G(L);
  721. switch (what) {
  722. case LUA_GCSTOP: {
  723. g->GCthreshold = MAX_LUMEM;
  724. break;
  725. }
  726. case LUA_GCRESTART: {
  727. g->GCthreshold = g->totalbytes;
  728. break;
  729. }
  730. case LUA_GCCOLLECT: {
  731. luaC_fullgc(L, 0);
  732. break;
  733. }
  734. case LUA_GCCOUNT: {
  735. /* GC values are expressed in Kbytes: #bytes/2^10 */
  736. res = cast_int(g->totalbytes >> 10);
  737. break;
  738. }
  739. case LUA_GCCOUNTB: {
  740. res = cast_int(g->totalbytes & 0x3ff);
  741. break;
  742. }
  743. case LUA_GCSTEP: {
  744. lu_mem a = (cast(lu_mem, data) << 10);
  745. if (a <= g->totalbytes)
  746. g->GCthreshold = g->totalbytes - a;
  747. else
  748. g->GCthreshold = 0;
  749. while (g->GCthreshold <= g->totalbytes)
  750. luaC_step(L);
  751. if (g->gcstate == GCSpause) /* end of cycle? */
  752. res = 1; /* signal it */
  753. break;
  754. }
  755. case LUA_GCSETPAUSE: {
  756. res = g->gcpause;
  757. g->gcpause = data;
  758. break;
  759. }
  760. case LUA_GCSETSTEPMUL: {
  761. res = g->gcstepmul;
  762. g->gcstepmul = data;
  763. break;
  764. }
  765. default: res = -1; /* invalid option */
  766. }
  767. lua_unlock(L);
  768. return res;
  769. }
  770. /*
  771. ** miscellaneous functions
  772. */
  773. LUA_API int lua_error (lua_State *L) {
  774. lua_lock(L);
  775. api_checknelems(L, 1);
  776. luaG_errormsg(L);
  777. lua_unlock(L);
  778. return 0; /* to avoid warnings */
  779. }
  780. LUA_API int lua_next (lua_State *L, int idx) {
  781. StkId t;
  782. int more;
  783. lua_lock(L);
  784. t = index2adr(L, idx);
  785. api_check(L, ttistable(t));
  786. more = luaH_next(L, hvalue(t), L->top - 1);
  787. if (more) {
  788. api_incr_top(L);
  789. }
  790. else /* no more elements */
  791. L->top -= 1; /* remove key */
  792. lua_unlock(L);
  793. return more;
  794. }
  795. LUA_API void lua_concat (lua_State *L, int n) {
  796. lua_lock(L);
  797. api_checknelems(L, n);
  798. if (n >= 2) {
  799. luaC_checkGC(L);
  800. luaV_concat(L, n, cast_int(L->top - L->base) - 1);
  801. L->top -= (n-1);
  802. }
  803. else if (n == 0) { /* push empty string */
  804. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  805. api_incr_top(L);
  806. }
  807. /* else n == 1; nothing to do */
  808. lua_unlock(L);
  809. }
  810. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  811. lua_Alloc f;
  812. lua_lock(L);
  813. if (ud) *ud = G(L)->ud;
  814. f = G(L)->frealloc;
  815. lua_unlock(L);
  816. return f;
  817. }
  818. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  819. lua_lock(L);
  820. G(L)->ud = ud;
  821. G(L)->frealloc = f;
  822. lua_unlock(L);
  823. }
  824. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  825. Udata *u;
  826. lua_lock(L);
  827. luaC_checkGC(L);
  828. u = luaS_newudata(L, size, getcurrenv(L));
  829. setuvalue(L, L->top, u);
  830. api_incr_top(L);
  831. lua_unlock(L);
  832. return u + 1;
  833. }
  834. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  835. Closure *f;
  836. if (!ttisfunction(fi)) return NULL;
  837. f = clvalue(fi);
  838. if (f->c.isC) {
  839. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  840. *val = &f->c.upvalue[n-1];
  841. return "";
  842. }
  843. else {
  844. Proto *p = f->l.p;
  845. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  846. *val = f->l.upvals[n-1]->v;
  847. return getstr(p->upvalues[n-1]);
  848. }
  849. }
  850. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  851. const char *name;
  852. TValue *val;
  853. lua_lock(L);
  854. name = aux_upvalue(index2adr(L, funcindex), n, &val);
  855. if (name) {
  856. setobj2s(L, L->top, val);
  857. api_incr_top(L);
  858. }
  859. lua_unlock(L);
  860. return name;
  861. }
  862. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  863. const char *name;
  864. TValue *val;
  865. StkId fi;
  866. lua_lock(L);
  867. fi = index2adr(L, funcindex);
  868. api_checknelems(L, 1);
  869. name = aux_upvalue(fi, n, &val);
  870. if (name) {
  871. L->top--;
  872. setobj(L, val, L->top);
  873. luaC_barrier(L, clvalue(fi), L->top);
  874. }
  875. lua_unlock(L);
  876. return name;
  877. }