2
0

lapi.c 22 KB

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