lapi.c 22 KB

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