lapi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. ** $Id: lapi.c,v 2.79 2009/06/15 19:51:31 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->ci->func))
  28. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
  29. static TValue *index2adr (lua_State *L, int idx) {
  30. CallInfo *ci = L->ci;
  31. if (idx > 0) {
  32. TValue *o = ci->func + idx;
  33. api_check(L, idx <= ci->top - (ci->func + 1));
  34. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  35. else return o;
  36. }
  37. else if (idx > LUA_REGISTRYINDEX) {
  38. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1));
  39. return L->top + idx;
  40. }
  41. else switch (idx) { /* pseudo-indices */
  42. case LUA_REGISTRYINDEX: return registry(L);
  43. case LUA_ENVIRONINDEX: {
  44. Closure *func = curr_func(L);
  45. sethvalue(L, &L->env, func->c.env);
  46. return &L->env;
  47. }
  48. case LUA_GLOBALSINDEX: return gt(L);
  49. default: {
  50. Closure *func = curr_func(L);
  51. idx = LUA_GLOBALSINDEX - idx;
  52. api_check(L, idx <= UCHAR_MAX + 1);
  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->previous == NULL) /* 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 = 1;
  69. CallInfo *ci = L->ci;
  70. lua_lock(L);
  71. if (size > LUAI_MAXCSTACK ||
  72. (L->top - (ci->func + 1) + size) > LUAI_MAXCSTACK)
  73. res = 0; /* stack overflow */
  74. else if (size > 0) {
  75. luaD_checkstack(L, size);
  76. if (ci->top < L->top + size)
  77. ci->top = L->top + size;
  78. }
  79. lua_unlock(L);
  80. return res;
  81. }
  82. LUA_API lua_State *lua_mainthread (lua_State *L) {
  83. return G(L)->mainthread;
  84. }
  85. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  86. int i;
  87. if (from == to) return;
  88. lua_lock(to);
  89. api_checknelems(from, n);
  90. api_check(from, G(from) == G(to));
  91. api_check(from, to->ci->top - to->top >= n);
  92. from->top -= n;
  93. for (i = 0; i < n; i++) {
  94. setobj2s(to, to->top++, from->top + i);
  95. }
  96. lua_unlock(to);
  97. }
  98. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  99. lua_CFunction old;
  100. lua_lock(L);
  101. old = G(L)->panic;
  102. G(L)->panic = panicf;
  103. lua_unlock(L);
  104. return old;
  105. }
  106. LUA_API void lua_checkversion_ (lua_State *L, int version) {
  107. lua_lock(L);
  108. if (version != LUA_VERSION_NUM)
  109. luaG_runerror(L, "app./library not compiled with header " LUA_VERSION);
  110. if (G(L)->nilobjp != luaO_nilobject)
  111. luaG_runerror(L, "application using multiple Lua VMs");
  112. lua_unlock(L);
  113. }
  114. /*
  115. ** basic stack manipulation
  116. */
  117. LUA_API int lua_gettop (lua_State *L) {
  118. return cast_int(L->top - (L->ci->func + 1));
  119. }
  120. LUA_API void lua_settop (lua_State *L, int idx) {
  121. StkId func = L->ci->func;
  122. lua_lock(L);
  123. if (idx >= 0) {
  124. api_check(L, idx <= L->stack_last - (func + 1));
  125. while (L->top < (func + 1) + idx)
  126. setnilvalue(L->top++);
  127. L->top = (func + 1) + idx;
  128. }
  129. else {
  130. api_check(L, -(idx+1) <= (L->top - (func + 1)));
  131. L->top += idx+1; /* `subtract' index (index is negative) */
  132. }
  133. lua_unlock(L);
  134. }
  135. LUA_API void lua_remove (lua_State *L, int idx) {
  136. StkId p;
  137. lua_lock(L);
  138. p = index2adr(L, idx);
  139. api_checkvalidindex(L, p);
  140. while (++p < L->top) setobjs2s(L, p-1, p);
  141. L->top--;
  142. lua_unlock(L);
  143. }
  144. LUA_API void lua_insert (lua_State *L, int idx) {
  145. StkId p;
  146. StkId q;
  147. lua_lock(L);
  148. p = index2adr(L, idx);
  149. api_checkvalidindex(L, p);
  150. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  151. setobjs2s(L, p, L->top);
  152. lua_unlock(L);
  153. }
  154. LUA_API void lua_replace (lua_State *L, int idx) {
  155. StkId o;
  156. lua_lock(L);
  157. /* explicit test for incompatible code */
  158. if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
  159. luaG_runerror(L, "no calling environment");
  160. api_checknelems(L, 1);
  161. o = index2adr(L, idx);
  162. api_checkvalidindex(L, o);
  163. if (idx == LUA_ENVIRONINDEX) {
  164. Closure *func = curr_func(L);
  165. api_check(L, ttistable(L->top - 1));
  166. func->c.env = hvalue(L->top - 1);
  167. luaC_barrier(L, func, L->top - 1);
  168. }
  169. else {
  170. setobj(L, o, L->top - 1);
  171. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  172. luaC_barrier(L, curr_func(L), L->top - 1);
  173. }
  174. /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */
  175. L->top--;
  176. lua_unlock(L);
  177. }
  178. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  179. lua_lock(L);
  180. setobj2s(L, L->top, index2adr(L, idx));
  181. api_incr_top(L);
  182. lua_unlock(L);
  183. }
  184. /*
  185. ** access functions (stack -> C)
  186. */
  187. LUA_API int lua_type (lua_State *L, int idx) {
  188. StkId o = index2adr(L, idx);
  189. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  190. }
  191. LUA_API const char *lua_typename (lua_State *L, int t) {
  192. UNUSED(L);
  193. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  194. }
  195. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  196. StkId o = index2adr(L, idx);
  197. return iscfunction(o);
  198. }
  199. LUA_API int lua_isnumber (lua_State *L, int idx) {
  200. TValue n;
  201. const TValue *o = index2adr(L, idx);
  202. return tonumber(o, &n);
  203. }
  204. LUA_API int lua_isstring (lua_State *L, int idx) {
  205. int t = lua_type(L, idx);
  206. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  207. }
  208. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  209. const TValue *o = index2adr(L, idx);
  210. return (ttisuserdata(o) || ttislightuserdata(o));
  211. }
  212. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  213. StkId o1 = index2adr(L, index1);
  214. StkId o2 = index2adr(L, index2);
  215. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  216. : luaO_rawequalObj(o1, o2);
  217. }
  218. LUA_API void lua_arith (lua_State *L, int op) {
  219. lua_lock(L);
  220. api_checknelems(L, 2);
  221. luaV_arith(L, L->top - 2, L->top - 2, L->top - 1, op - LUA_OPADD + TM_ADD);
  222. L->top--;
  223. lua_unlock(L);
  224. }
  225. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  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. if (o1 == luaO_nilobject || o2 == luaO_nilobject)
  232. i = 0;
  233. else switch (op) {
  234. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  235. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  236. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  237. default: api_check(L, 0); i = 0;
  238. }
  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 const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  348. TString *ts;
  349. lua_lock(L);
  350. luaC_checkGC(L);
  351. ts = luaS_newlstr(L, s, len);
  352. setsvalue2s(L, L->top, ts);
  353. api_incr_top(L);
  354. lua_unlock(L);
  355. return getstr(ts);
  356. }
  357. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  358. if (s == NULL) {
  359. lua_pushnil(L);
  360. return NULL;
  361. }
  362. else
  363. return lua_pushlstring(L, s, strlen(s));
  364. }
  365. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  366. va_list argp) {
  367. const char *ret;
  368. lua_lock(L);
  369. luaC_checkGC(L);
  370. ret = luaO_pushvfstring(L, fmt, argp);
  371. lua_unlock(L);
  372. return ret;
  373. }
  374. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  375. const char *ret;
  376. va_list argp;
  377. lua_lock(L);
  378. luaC_checkGC(L);
  379. va_start(argp, fmt);
  380. ret = luaO_pushvfstring(L, fmt, argp);
  381. va_end(argp);
  382. lua_unlock(L);
  383. return ret;
  384. }
  385. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  386. Closure *cl;
  387. lua_lock(L);
  388. api_checknelems(L, n);
  389. api_check(L, n <= UCHAR_MAX);
  390. luaC_checkGC(L);
  391. cl = luaF_newCclosure(L, n, getcurrenv(L));
  392. cl->c.f = fn;
  393. L->top -= n;
  394. while (n--)
  395. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  396. setclvalue(L, L->top, cl);
  397. lua_assert(iswhite(obj2gco(cl)));
  398. api_incr_top(L);
  399. lua_unlock(L);
  400. }
  401. LUA_API void lua_pushboolean (lua_State *L, int b) {
  402. lua_lock(L);
  403. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  404. api_incr_top(L);
  405. lua_unlock(L);
  406. }
  407. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  408. lua_lock(L);
  409. setpvalue(L->top, p);
  410. api_incr_top(L);
  411. lua_unlock(L);
  412. }
  413. LUA_API int lua_pushthread (lua_State *L) {
  414. lua_lock(L);
  415. setthvalue(L, L->top, L);
  416. api_incr_top(L);
  417. lua_unlock(L);
  418. return (G(L)->mainthread == L);
  419. }
  420. /*
  421. ** get functions (Lua -> stack)
  422. */
  423. LUA_API void lua_gettable (lua_State *L, int idx) {
  424. StkId t;
  425. lua_lock(L);
  426. t = index2adr(L, idx);
  427. api_checkvalidindex(L, t);
  428. luaV_gettable(L, t, L->top - 1, L->top - 1);
  429. lua_unlock(L);
  430. }
  431. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  432. StkId t;
  433. lua_lock(L);
  434. t = index2adr(L, idx);
  435. api_checkvalidindex(L, t);
  436. setsvalue2s(L, L->top, luaS_new(L, k));
  437. api_incr_top(L);
  438. luaV_gettable(L, t, L->top - 1, L->top - 1);
  439. lua_unlock(L);
  440. }
  441. LUA_API void lua_rawget (lua_State *L, int idx) {
  442. StkId t;
  443. lua_lock(L);
  444. t = index2adr(L, idx);
  445. api_check(L, ttistable(t));
  446. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  447. lua_unlock(L);
  448. }
  449. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  450. StkId o;
  451. lua_lock(L);
  452. o = index2adr(L, idx);
  453. api_check(L, ttistable(o));
  454. setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  455. api_incr_top(L);
  456. lua_unlock(L);
  457. }
  458. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  459. Table *t;
  460. lua_lock(L);
  461. luaC_checkGC(L);
  462. t = luaH_new(L);
  463. sethvalue(L, L->top, t);
  464. api_incr_top(L);
  465. if (narray > 0 || nrec > 0)
  466. luaH_resize(L, t, narray, nrec);
  467. lua_unlock(L);
  468. }
  469. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  470. const TValue *obj;
  471. Table *mt = NULL;
  472. int res;
  473. lua_lock(L);
  474. obj = index2adr(L, objindex);
  475. switch (ttype(obj)) {
  476. case LUA_TTABLE:
  477. mt = hvalue(obj)->metatable;
  478. break;
  479. case LUA_TUSERDATA:
  480. mt = uvalue(obj)->metatable;
  481. break;
  482. default:
  483. mt = G(L)->mt[ttype(obj)];
  484. break;
  485. }
  486. if (mt == NULL)
  487. res = 0;
  488. else {
  489. sethvalue(L, L->top, mt);
  490. api_incr_top(L);
  491. res = 1;
  492. }
  493. lua_unlock(L);
  494. return res;
  495. }
  496. LUA_API void lua_getfenv (lua_State *L, int idx) {
  497. StkId o;
  498. lua_lock(L);
  499. o = index2adr(L, idx);
  500. api_checkvalidindex(L, o);
  501. switch (ttype(o)) {
  502. case LUA_TFUNCTION:
  503. sethvalue(L, L->top, clvalue(o)->c.env);
  504. break;
  505. case LUA_TUSERDATA:
  506. sethvalue(L, L->top, uvalue(o)->env);
  507. break;
  508. case LUA_TTHREAD:
  509. setobj2s(L, L->top, gt(thvalue(o)));
  510. break;
  511. default:
  512. setnilvalue(L->top);
  513. break;
  514. }
  515. api_incr_top(L);
  516. lua_unlock(L);
  517. }
  518. /*
  519. ** set functions (stack -> Lua)
  520. */
  521. LUA_API void lua_settable (lua_State *L, int idx) {
  522. StkId t;
  523. lua_lock(L);
  524. api_checknelems(L, 2);
  525. t = index2adr(L, idx);
  526. api_checkvalidindex(L, t);
  527. luaV_settable(L, t, L->top - 2, L->top - 1);
  528. L->top -= 2; /* pop index and value */
  529. lua_unlock(L);
  530. }
  531. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  532. StkId t;
  533. lua_lock(L);
  534. api_checknelems(L, 1);
  535. t = index2adr(L, idx);
  536. api_checkvalidindex(L, t);
  537. setsvalue2s(L, L->top++, luaS_new(L, k));
  538. luaV_settable(L, t, L->top - 1, L->top - 2);
  539. L->top -= 2; /* pop value and key */
  540. lua_unlock(L);
  541. }
  542. LUA_API void lua_rawset (lua_State *L, int idx) {
  543. StkId t;
  544. lua_lock(L);
  545. api_checknelems(L, 2);
  546. t = index2adr(L, idx);
  547. api_check(L, ttistable(t));
  548. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  549. luaC_barriert(L, hvalue(t), L->top-1);
  550. L->top -= 2;
  551. lua_unlock(L);
  552. }
  553. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  554. StkId o;
  555. lua_lock(L);
  556. api_checknelems(L, 1);
  557. o = index2adr(L, idx);
  558. api_check(L, ttistable(o));
  559. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  560. luaC_barriert(L, hvalue(o), L->top-1);
  561. L->top--;
  562. lua_unlock(L);
  563. }
  564. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  565. TValue *obj;
  566. Table *mt;
  567. lua_lock(L);
  568. api_checknelems(L, 1);
  569. obj = index2adr(L, objindex);
  570. api_checkvalidindex(L, obj);
  571. if (ttisnil(L->top - 1))
  572. mt = NULL;
  573. else {
  574. api_check(L, ttistable(L->top - 1));
  575. mt = hvalue(L->top - 1);
  576. }
  577. switch (ttype(obj)) {
  578. case LUA_TTABLE: {
  579. hvalue(obj)->metatable = mt;
  580. if (mt)
  581. luaC_objbarriert(L, hvalue(obj), mt);
  582. break;
  583. }
  584. case LUA_TUSERDATA: {
  585. uvalue(obj)->metatable = mt;
  586. if (mt) {
  587. luaC_objbarrier(L, rawuvalue(obj), mt);
  588. luaC_checkfinalizer(L, rawuvalue(obj));
  589. }
  590. break;
  591. }
  592. default: {
  593. G(L)->mt[ttype(obj)] = mt;
  594. break;
  595. }
  596. }
  597. L->top--;
  598. lua_unlock(L);
  599. return 1;
  600. }
  601. LUA_API int lua_setfenv (lua_State *L, int idx) {
  602. StkId o;
  603. int res = 1;
  604. lua_lock(L);
  605. api_checknelems(L, 1);
  606. o = index2adr(L, idx);
  607. api_checkvalidindex(L, o);
  608. api_check(L, ttistable(L->top - 1));
  609. switch (ttype(o)) {
  610. case LUA_TFUNCTION:
  611. clvalue(o)->c.env = hvalue(L->top - 1);
  612. break;
  613. case LUA_TUSERDATA:
  614. uvalue(o)->env = hvalue(L->top - 1);
  615. break;
  616. case LUA_TTHREAD:
  617. sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
  618. break;
  619. default:
  620. res = 0;
  621. break;
  622. }
  623. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  624. L->top--;
  625. lua_unlock(L);
  626. return res;
  627. }
  628. /*
  629. ** `load' and `call' functions (run Lua code)
  630. */
  631. #define checkresults(L,na,nr) \
  632. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
  633. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  634. if (L->ci->callstatus & CIST_YIELDED) {
  635. if (ctx) *ctx = L->ci->u.c.ctx;
  636. return L->ci->u.c.status;
  637. }
  638. else return LUA_OK;
  639. }
  640. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  641. lua_CFunction k) {
  642. StkId func;
  643. lua_lock(L);
  644. /* cannot use continuations inside hooks */
  645. api_check(L, k == NULL || !isLua(L->ci));
  646. api_checknelems(L, nargs+1);
  647. checkresults(L, nargs, nresults);
  648. func = L->top - (nargs+1);
  649. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  650. L->ci->u.c.k = k; /* save continuation */
  651. L->ci->u.c.ctx = ctx; /* save context */
  652. luaD_call(L, func, nresults, 1); /* do the call */
  653. }
  654. else /* no continuation or no yieldable */
  655. luaD_call(L, func, nresults, 0); /* just do the call */
  656. adjustresults(L, nresults);
  657. lua_unlock(L);
  658. }
  659. /*
  660. ** Execute a protected call.
  661. */
  662. struct CallS { /* data to `f_call' */
  663. StkId func;
  664. int nresults;
  665. };
  666. static void f_call (lua_State *L, void *ud) {
  667. struct CallS *c = cast(struct CallS *, ud);
  668. luaD_call(L, c->func, c->nresults, 0);
  669. }
  670. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  671. int ctx, lua_CFunction k) {
  672. struct CallS c;
  673. int status;
  674. ptrdiff_t func;
  675. lua_lock(L);
  676. api_checknelems(L, nargs+1);
  677. checkresults(L, nargs, nresults);
  678. if (errfunc == 0)
  679. func = 0;
  680. else {
  681. StkId o = index2adr(L, errfunc);
  682. api_checkvalidindex(L, o);
  683. func = savestack(L, o);
  684. }
  685. c.func = L->top - (nargs+1); /* function to be called */
  686. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  687. c.nresults = nresults; /* do a 'conventional' protected call */
  688. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  689. }
  690. else { /* prepare continuation (call is already protected by 'resume') */
  691. CallInfo *ci = L->ci;
  692. ci->u.c.k = k; /* save continuation */
  693. ci->u.c.ctx = ctx; /* save context */
  694. /* save information for error recovery */
  695. ci->u.c.oldtop = savestack(L, c.func);
  696. ci->u.c.old_allowhook = L->allowhook;
  697. ci->u.c.old_errfunc = L->errfunc;
  698. L->errfunc = func;
  699. /* mark that function may do error recovery */
  700. ci->callstatus |= CIST_YPCALL;
  701. luaD_call(L, c.func, nresults, 1); /* do the call */
  702. ci->callstatus &= ~CIST_YPCALL;
  703. L->errfunc = ci->u.c.old_errfunc;
  704. status = LUA_OK; /* if it is here, there were no errors */
  705. }
  706. adjustresults(L, nresults);
  707. lua_unlock(L);
  708. return status;
  709. }
  710. /*
  711. ** Execute a protected C call.
  712. */
  713. struct CCallS { /* data to `f_Ccall' */
  714. lua_CFunction func;
  715. void *ud;
  716. };
  717. static void f_Ccall (lua_State *L, void *ud) {
  718. struct CCallS *c = cast(struct CCallS *, ud);
  719. Closure *cl;
  720. cl = luaF_newCclosure(L, 0, getcurrenv(L));
  721. cl->c.f = c->func;
  722. setclvalue(L, L->top, cl); /* push function */
  723. api_incr_top(L);
  724. setpvalue(L->top, c->ud); /* push only argument */
  725. api_incr_top(L);
  726. luaD_call(L, L->top - 2, 0, 0);
  727. }
  728. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  729. struct CCallS c;
  730. int status;
  731. lua_lock(L);
  732. c.func = func;
  733. c.ud = ud;
  734. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  735. lua_unlock(L);
  736. return status;
  737. }
  738. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  739. const char *chunkname) {
  740. ZIO z;
  741. int status;
  742. lua_lock(L);
  743. if (!chunkname) chunkname = "?";
  744. luaZ_init(L, &z, reader, data);
  745. status = luaD_protectedparser(L, &z, chunkname);
  746. lua_unlock(L);
  747. return status;
  748. }
  749. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  750. int status;
  751. TValue *o;
  752. lua_lock(L);
  753. api_checknelems(L, 1);
  754. o = L->top - 1;
  755. if (isLfunction(o))
  756. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  757. else
  758. status = 1;
  759. lua_unlock(L);
  760. return status;
  761. }
  762. LUA_API int lua_status (lua_State *L) {
  763. return L->status;
  764. }
  765. /*
  766. ** Garbage-collection function
  767. */
  768. LUA_API int lua_gc (lua_State *L, int what, int data) {
  769. int res = 0;
  770. global_State *g;
  771. lua_lock(L);
  772. g = G(L);
  773. switch (what) {
  774. case LUA_GCSTOP: {
  775. g->GCthreshold = MAX_LUMEM;
  776. break;
  777. }
  778. case LUA_GCRESTART: {
  779. g->GCthreshold = g->totalbytes;
  780. break;
  781. }
  782. case LUA_GCCOLLECT: {
  783. luaC_fullgc(L, 0);
  784. break;
  785. }
  786. case LUA_GCCOUNT: {
  787. /* GC values are expressed in Kbytes: #bytes/2^10 */
  788. res = cast_int(g->totalbytes >> 10);
  789. break;
  790. }
  791. case LUA_GCCOUNTB: {
  792. res = cast_int(g->totalbytes & 0x3ff);
  793. break;
  794. }
  795. case LUA_GCSTEP: {
  796. lu_mem oldts = g->GCthreshold;
  797. lu_mem a = (cast(lu_mem, data) << 10);
  798. g->GCthreshold = (a <= g->totalbytes) ? g->totalbytes - a : 0;
  799. while (g->GCthreshold <= g->totalbytes) {
  800. luaC_step(L);
  801. if (g->gcstate == GCSpause) { /* end of cycle? */
  802. res = 1; /* signal it */
  803. break;
  804. }
  805. }
  806. if (oldts == MAX_LUMEM) /* collector was stopped? */
  807. g->GCthreshold = oldts; /* keep it that way */
  808. break;
  809. }
  810. case LUA_GCSETPAUSE: {
  811. res = g->gcpause;
  812. g->gcpause = data;
  813. break;
  814. }
  815. case LUA_GCSETSTEPMUL: {
  816. res = g->gcstepmul;
  817. g->gcstepmul = data;
  818. break;
  819. }
  820. default: res = -1; /* invalid option */
  821. }
  822. lua_unlock(L);
  823. return res;
  824. }
  825. /*
  826. ** miscellaneous functions
  827. */
  828. LUA_API int lua_error (lua_State *L) {
  829. lua_lock(L);
  830. api_checknelems(L, 1);
  831. luaG_errormsg(L);
  832. lua_unlock(L);
  833. return 0; /* to avoid warnings */
  834. }
  835. LUA_API int lua_next (lua_State *L, int idx) {
  836. StkId t;
  837. int more;
  838. lua_lock(L);
  839. t = index2adr(L, idx);
  840. api_check(L, ttistable(t));
  841. more = luaH_next(L, hvalue(t), L->top - 1);
  842. if (more) {
  843. api_incr_top(L);
  844. }
  845. else /* no more elements */
  846. L->top -= 1; /* remove key */
  847. lua_unlock(L);
  848. return more;
  849. }
  850. LUA_API void lua_concat (lua_State *L, int n) {
  851. lua_lock(L);
  852. api_checknelems(L, n);
  853. if (n >= 2) {
  854. luaC_checkGC(L);
  855. luaV_concat(L, n);
  856. }
  857. else if (n == 0) { /* push empty string */
  858. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  859. api_incr_top(L);
  860. }
  861. /* else n == 1; nothing to do */
  862. lua_unlock(L);
  863. }
  864. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  865. lua_Alloc f;
  866. lua_lock(L);
  867. if (ud) *ud = G(L)->ud;
  868. f = G(L)->frealloc;
  869. lua_unlock(L);
  870. return f;
  871. }
  872. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  873. lua_lock(L);
  874. G(L)->ud = ud;
  875. G(L)->frealloc = f;
  876. lua_unlock(L);
  877. }
  878. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  879. Udata *u;
  880. lua_lock(L);
  881. luaC_checkGC(L);
  882. u = luaS_newudata(L, size, getcurrenv(L));
  883. setuvalue(L, L->top, u);
  884. api_incr_top(L);
  885. lua_unlock(L);
  886. return u + 1;
  887. }
  888. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  889. Closure *f;
  890. if (!ttisfunction(fi)) return NULL;
  891. f = clvalue(fi);
  892. if (f->c.isC) {
  893. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  894. *val = &f->c.upvalue[n-1];
  895. return "";
  896. }
  897. else {
  898. Proto *p = f->l.p;
  899. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  900. *val = f->l.upvals[n-1]->v;
  901. return getstr(p->upvalues[n-1]);
  902. }
  903. }
  904. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  905. const char *name;
  906. TValue *val;
  907. lua_lock(L);
  908. name = aux_upvalue(index2adr(L, funcindex), n, &val);
  909. if (name) {
  910. setobj2s(L, L->top, val);
  911. api_incr_top(L);
  912. }
  913. lua_unlock(L);
  914. return name;
  915. }
  916. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  917. const char *name;
  918. TValue *val;
  919. StkId fi;
  920. lua_lock(L);
  921. fi = index2adr(L, funcindex);
  922. api_checknelems(L, 1);
  923. name = aux_upvalue(fi, n, &val);
  924. if (name) {
  925. L->top--;
  926. setobj(L, val, L->top);
  927. luaC_barrier(L, clvalue(fi), L->top);
  928. }
  929. lua_unlock(L);
  930. return name;
  931. }