lapi.c 23 KB

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