lapi.c 22 KB

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