lapi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. ** $Id: lapi.c,v 2.95 2009/11/05 16:48: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. "not enough elements in the stack")
  29. #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject, \
  30. "invalid index")
  31. static TValue *index2addr (lua_State *L, int idx) {
  32. CallInfo *ci = L->ci;
  33. if (idx > 0) {
  34. TValue *o = ci->func + idx;
  35. api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
  36. if (o >= L->top) return cast(TValue *, luaO_nilobject);
  37. else return o;
  38. }
  39. else if (idx > LUA_REGISTRYINDEX) {
  40. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  41. return L->top + idx;
  42. }
  43. else switch (idx) { /* pseudo-indices */
  44. case LUA_REGISTRYINDEX: return &G(L)->l_registry;
  45. case LUA_ENVIRONINDEX: {
  46. Closure *func = curr_func(L);
  47. sethvalue(L, &L->env, func->c.env);
  48. return &L->env;
  49. }
  50. case LUA_GLOBALSINDEX: return &G(L)->l_gt;
  51. default: {
  52. Closure *func = curr_func(L);
  53. idx = LUA_GLOBALSINDEX - idx;
  54. api_check(L, idx <= UCHAR_MAX + 1, "upvalue index too large");
  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->previous == NULL) /* no enclosing function? */
  63. return hvalue(&G(L)->l_gt); /* use global table as environment */
  64. else {
  65. Closure *func = curr_func(L);
  66. return func->c.env;
  67. }
  68. }
  69. LUA_API int lua_checkstack (lua_State *L, int size) {
  70. int res = 1;
  71. CallInfo *ci = L->ci;
  72. lua_lock(L);
  73. if (L->stack_last - L->top <= size) { /* need to grow stack? */
  74. int inuse = L->top - L->stack + EXTRA_STACK;
  75. if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
  76. res = 0; /* no */
  77. else
  78. luaD_growstack(L, size);
  79. }
  80. if (res && ci->top < L->top + size)
  81. ci->top = L->top + size; /* adjust frame top */
  82. lua_unlock(L);
  83. return res;
  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), "moving among independent states");
  91. api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
  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 const lua_Number *lua_version (lua_State *L) {
  107. static const lua_Number version = LUA_VERSION_NUM;
  108. if (L == NULL) return &version;
  109. else return G(L)->version;
  110. }
  111. /*
  112. ** basic stack manipulation
  113. */
  114. LUA_API int lua_gettop (lua_State *L) {
  115. return cast_int(L->top - (L->ci->func + 1));
  116. }
  117. LUA_API void lua_settop (lua_State *L, int idx) {
  118. StkId func = L->ci->func;
  119. lua_lock(L);
  120. if (idx >= 0) {
  121. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  122. while (L->top < (func + 1) + idx)
  123. setnilvalue(L->top++);
  124. L->top = (func + 1) + idx;
  125. }
  126. else {
  127. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  128. L->top += idx+1; /* `subtract' index (index is negative) */
  129. }
  130. lua_unlock(L);
  131. }
  132. LUA_API void lua_remove (lua_State *L, int idx) {
  133. StkId p;
  134. lua_lock(L);
  135. p = index2addr(L, idx);
  136. api_checkvalidindex(L, p);
  137. while (++p < L->top) setobjs2s(L, p-1, p);
  138. L->top--;
  139. lua_unlock(L);
  140. }
  141. LUA_API void lua_insert (lua_State *L, int idx) {
  142. StkId p;
  143. StkId q;
  144. lua_lock(L);
  145. p = index2addr(L, idx);
  146. api_checkvalidindex(L, p);
  147. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  148. setobjs2s(L, p, L->top);
  149. lua_unlock(L);
  150. }
  151. static void moveto (lua_State *L, TValue *fr, int idx) {
  152. TValue *to = index2addr(L, idx);
  153. api_checkvalidindex(L, to);
  154. if (idx == LUA_ENVIRONINDEX) {
  155. Closure *func = curr_func(L);
  156. api_check(L, ttistable(fr), "table expected");
  157. func->c.env = hvalue(fr);
  158. luaC_barrier(L, func, fr);
  159. }
  160. else {
  161. setobj(L, to, fr);
  162. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  163. luaC_barrier(L, curr_func(L), fr);
  164. }
  165. /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */
  166. }
  167. LUA_API void lua_replace (lua_State *L, int idx) {
  168. lua_lock(L);
  169. /* explicit test for incompatible code */
  170. if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
  171. luaG_runerror(L, "no calling environment");
  172. api_checknelems(L, 1);
  173. moveto(L, L->top - 1, idx);
  174. L->top--;
  175. lua_unlock(L);
  176. }
  177. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  178. TValue *fr;
  179. lua_lock(L);
  180. fr = index2addr(L, fromidx);
  181. api_checkvalidindex(L, fr);
  182. moveto(L, fr, toidx);
  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, index2addr(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 = index2addr(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 = index2addr(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 = index2addr(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 = index2addr(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 = index2addr(L, index1);
  221. StkId o2 = index2addr(L, index2);
  222. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  223. : luaO_rawequalObj(o1, o2);
  224. }
  225. LUA_API void lua_arith (lua_State *L, int op) {
  226. lua_lock(L);
  227. api_checknelems(L, 2);
  228. if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1))
  229. changenvalue(L->top - 2,
  230. luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
  231. else
  232. luaV_arith(L, L->top - 2, L->top - 2, L->top - 1, op - LUA_OPADD + TM_ADD);
  233. L->top--;
  234. lua_unlock(L);
  235. }
  236. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  237. StkId o1, o2;
  238. int i;
  239. lua_lock(L); /* may call tag method */
  240. o1 = index2addr(L, index1);
  241. o2 = index2addr(L, index2);
  242. if (o1 == luaO_nilobject || o2 == luaO_nilobject)
  243. i = 0;
  244. else switch (op) {
  245. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  246. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  247. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  248. default: api_check(L, 0, "invalid option"); i = 0;
  249. }
  250. lua_unlock(L);
  251. return i;
  252. }
  253. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  254. TValue n;
  255. const TValue *o = index2addr(L, idx);
  256. if (tonumber(o, &n))
  257. return nvalue(o);
  258. else
  259. return 0;
  260. }
  261. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  262. TValue n;
  263. const TValue *o = index2addr(L, idx);
  264. if (tonumber(o, &n)) {
  265. lua_Integer res;
  266. lua_Number num = nvalue(o);
  267. lua_number2integer(res, num);
  268. return res;
  269. }
  270. else
  271. return 0;
  272. }
  273. LUA_API int lua_toboolean (lua_State *L, int idx) {
  274. const TValue *o = index2addr(L, idx);
  275. return !l_isfalse(o);
  276. }
  277. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  278. StkId o = index2addr(L, idx);
  279. if (!ttisstring(o)) {
  280. lua_lock(L); /* `luaV_tostring' may create a new string */
  281. if (!luaV_tostring(L, o)) { /* conversion failed? */
  282. if (len != NULL) *len = 0;
  283. lua_unlock(L);
  284. return NULL;
  285. }
  286. luaC_checkGC(L);
  287. o = index2addr(L, idx); /* previous call may reallocate the stack */
  288. lua_unlock(L);
  289. }
  290. if (len != NULL) *len = tsvalue(o)->len;
  291. return svalue(o);
  292. }
  293. LUA_API size_t lua_objlen (lua_State *L, int idx) {
  294. StkId o = index2addr(L, idx);
  295. switch (ttype(o)) {
  296. case LUA_TSTRING: return tsvalue(o)->len;
  297. case LUA_TUSERDATA: return uvalue(o)->len;
  298. case LUA_TTABLE: return luaH_getn(hvalue(o));
  299. default: return 0;
  300. }
  301. }
  302. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  303. StkId o = index2addr(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 = index2addr(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 = index2addr(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 = index2addr(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 const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  352. TString *ts;
  353. lua_lock(L);
  354. luaC_checkGC(L);
  355. ts = luaS_newlstr(L, s, len);
  356. setsvalue2s(L, L->top, ts);
  357. api_incr_top(L);
  358. lua_unlock(L);
  359. return getstr(ts);
  360. }
  361. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  362. if (s == NULL) {
  363. lua_pushnil(L);
  364. return NULL;
  365. }
  366. else
  367. return lua_pushlstring(L, s, strlen(s));
  368. }
  369. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  370. va_list argp) {
  371. const char *ret;
  372. lua_lock(L);
  373. luaC_checkGC(L);
  374. ret = luaO_pushvfstring(L, fmt, argp);
  375. lua_unlock(L);
  376. return ret;
  377. }
  378. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  379. const char *ret;
  380. va_list argp;
  381. lua_lock(L);
  382. luaC_checkGC(L);
  383. va_start(argp, fmt);
  384. ret = luaO_pushvfstring(L, fmt, argp);
  385. va_end(argp);
  386. lua_unlock(L);
  387. return ret;
  388. }
  389. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  390. Closure *cl;
  391. lua_lock(L);
  392. api_checknelems(L, n);
  393. api_check(L, n <= UCHAR_MAX, "upvalue index too large");
  394. luaC_checkGC(L);
  395. cl = luaF_newCclosure(L, n, getcurrenv(L));
  396. cl->c.f = fn;
  397. L->top -= n;
  398. while (n--)
  399. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  400. setclvalue(L, L->top, cl);
  401. lua_assert(iswhite(obj2gco(cl)));
  402. api_incr_top(L);
  403. lua_unlock(L);
  404. }
  405. LUA_API void lua_pushboolean (lua_State *L, int b) {
  406. lua_lock(L);
  407. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  408. api_incr_top(L);
  409. lua_unlock(L);
  410. }
  411. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  412. lua_lock(L);
  413. setpvalue(L->top, p);
  414. api_incr_top(L);
  415. lua_unlock(L);
  416. }
  417. LUA_API int lua_pushthread (lua_State *L) {
  418. lua_lock(L);
  419. setthvalue(L, L->top, L);
  420. api_incr_top(L);
  421. lua_unlock(L);
  422. return (G(L)->mainthread == L);
  423. }
  424. /*
  425. ** get functions (Lua -> stack)
  426. */
  427. LUA_API void lua_gettable (lua_State *L, int idx) {
  428. StkId t;
  429. lua_lock(L);
  430. t = index2addr(L, idx);
  431. api_checkvalidindex(L, t);
  432. luaV_gettable(L, t, L->top - 1, L->top - 1);
  433. lua_unlock(L);
  434. }
  435. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  436. StkId t;
  437. lua_lock(L);
  438. t = index2addr(L, idx);
  439. api_checkvalidindex(L, t);
  440. setsvalue2s(L, L->top, luaS_new(L, k));
  441. api_incr_top(L);
  442. luaV_gettable(L, t, L->top - 1, L->top - 1);
  443. lua_unlock(L);
  444. }
  445. LUA_API void lua_rawget (lua_State *L, int idx) {
  446. StkId t;
  447. lua_lock(L);
  448. t = index2addr(L, idx);
  449. api_check(L, ttistable(t), "table expected");
  450. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  451. lua_unlock(L);
  452. }
  453. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  454. StkId o;
  455. lua_lock(L);
  456. o = index2addr(L, idx);
  457. api_check(L, ttistable(o), "table expected");
  458. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  459. api_incr_top(L);
  460. lua_unlock(L);
  461. }
  462. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  463. Table *t;
  464. lua_lock(L);
  465. luaC_checkGC(L);
  466. t = luaH_new(L);
  467. sethvalue(L, L->top, t);
  468. api_incr_top(L);
  469. if (narray > 0 || nrec > 0)
  470. luaH_resize(L, t, narray, nrec);
  471. lua_unlock(L);
  472. }
  473. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  474. const TValue *obj;
  475. Table *mt = NULL;
  476. int res;
  477. lua_lock(L);
  478. obj = index2addr(L, objindex);
  479. switch (ttype(obj)) {
  480. case LUA_TTABLE:
  481. mt = hvalue(obj)->metatable;
  482. break;
  483. case LUA_TUSERDATA:
  484. mt = uvalue(obj)->metatable;
  485. break;
  486. default:
  487. mt = G(L)->mt[ttype(obj)];
  488. break;
  489. }
  490. if (mt == NULL)
  491. res = 0;
  492. else {
  493. sethvalue(L, L->top, mt);
  494. api_incr_top(L);
  495. res = 1;
  496. }
  497. lua_unlock(L);
  498. return res;
  499. }
  500. LUA_API void lua_getfenv (lua_State *L, int idx) {
  501. StkId o;
  502. lua_lock(L);
  503. o = index2addr(L, idx);
  504. api_checkvalidindex(L, o);
  505. switch (ttype(o)) {
  506. case LUA_TFUNCTION:
  507. sethvalue(L, L->top, clvalue(o)->c.env);
  508. break;
  509. case LUA_TUSERDATA:
  510. sethvalue(L, L->top, uvalue(o)->env);
  511. break;
  512. default:
  513. setnilvalue(L->top);
  514. break;
  515. }
  516. api_incr_top(L);
  517. lua_unlock(L);
  518. }
  519. /*
  520. ** set functions (stack -> Lua)
  521. */
  522. LUA_API void lua_settable (lua_State *L, int idx) {
  523. StkId t;
  524. lua_lock(L);
  525. api_checknelems(L, 2);
  526. t = index2addr(L, idx);
  527. api_checkvalidindex(L, t);
  528. luaV_settable(L, t, L->top - 2, L->top - 1);
  529. L->top -= 2; /* pop index and value */
  530. lua_unlock(L);
  531. }
  532. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  533. StkId t;
  534. lua_lock(L);
  535. api_checknelems(L, 1);
  536. t = index2addr(L, idx);
  537. api_checkvalidindex(L, t);
  538. setsvalue2s(L, L->top++, luaS_new(L, k));
  539. luaV_settable(L, t, L->top - 1, L->top - 2);
  540. L->top -= 2; /* pop value and key */
  541. lua_unlock(L);
  542. }
  543. LUA_API void lua_rawset (lua_State *L, int idx) {
  544. StkId t;
  545. lua_lock(L);
  546. api_checknelems(L, 2);
  547. t = index2addr(L, idx);
  548. api_check(L, ttistable(t), "table expected");
  549. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  550. luaC_barriert(L, hvalue(t), L->top-1);
  551. L->top -= 2;
  552. lua_unlock(L);
  553. }
  554. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  555. StkId o;
  556. lua_lock(L);
  557. api_checknelems(L, 1);
  558. o = index2addr(L, idx);
  559. api_check(L, ttistable(o), "table expected");
  560. setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
  561. luaC_barriert(L, hvalue(o), L->top-1);
  562. L->top--;
  563. lua_unlock(L);
  564. }
  565. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  566. TValue *obj;
  567. Table *mt;
  568. lua_lock(L);
  569. api_checknelems(L, 1);
  570. obj = index2addr(L, objindex);
  571. api_checkvalidindex(L, obj);
  572. if (ttisnil(L->top - 1))
  573. mt = NULL;
  574. else {
  575. api_check(L, ttistable(L->top - 1), "table expected");
  576. mt = hvalue(L->top - 1);
  577. }
  578. switch (ttype(obj)) {
  579. case LUA_TTABLE: {
  580. hvalue(obj)->metatable = mt;
  581. if (mt)
  582. luaC_objbarriert(L, hvalue(obj), mt);
  583. break;
  584. }
  585. case LUA_TUSERDATA: {
  586. uvalue(obj)->metatable = mt;
  587. if (mt) {
  588. luaC_objbarrier(L, rawuvalue(obj), mt);
  589. luaC_checkfinalizer(L, rawuvalue(obj));
  590. }
  591. break;
  592. }
  593. default: {
  594. G(L)->mt[ttype(obj)] = mt;
  595. break;
  596. }
  597. }
  598. L->top--;
  599. lua_unlock(L);
  600. return 1;
  601. }
  602. LUA_API int lua_setfenv (lua_State *L, int idx) {
  603. StkId o;
  604. int res = 1;
  605. lua_lock(L);
  606. api_checknelems(L, 1);
  607. o = index2addr(L, idx);
  608. api_checkvalidindex(L, o);
  609. api_check(L, ttistable(L->top - 1), "table expected");
  610. switch (ttype(o)) {
  611. case LUA_TFUNCTION:
  612. clvalue(o)->c.env = hvalue(L->top - 1);
  613. break;
  614. case LUA_TUSERDATA:
  615. uvalue(o)->env = hvalue(L->top - 1);
  616. break;
  617. default:
  618. res = 0;
  619. break;
  620. }
  621. if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  622. L->top--;
  623. lua_unlock(L);
  624. return res;
  625. }
  626. /*
  627. ** `load' and `call' functions (run Lua code)
  628. */
  629. #define checkresults(L,na,nr) \
  630. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  631. "results from function overflow current stack size")
  632. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  633. if (L->ci->callstatus & CIST_YIELDED) {
  634. if (ctx) *ctx = L->ci->u.c.ctx;
  635. return L->ci->u.c.status;
  636. }
  637. else return LUA_OK;
  638. }
  639. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  640. lua_CFunction k) {
  641. StkId func;
  642. lua_lock(L);
  643. api_check(L, k == NULL || !isLua(L->ci),
  644. "cannot use continuations inside hooks");
  645. api_checknelems(L, nargs+1);
  646. checkresults(L, nargs, nresults);
  647. func = L->top - (nargs+1);
  648. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  649. L->ci->u.c.k = k; /* save continuation */
  650. L->ci->u.c.ctx = ctx; /* save context */
  651. luaD_call(L, func, nresults, 1); /* do the call */
  652. }
  653. else /* no continuation or no yieldable */
  654. luaD_call(L, func, nresults, 0); /* just do the call */
  655. adjustresults(L, nresults);
  656. lua_unlock(L);
  657. }
  658. /*
  659. ** Execute a protected call.
  660. */
  661. struct CallS { /* data to `f_call' */
  662. StkId func;
  663. int nresults;
  664. };
  665. static void f_call (lua_State *L, void *ud) {
  666. struct CallS *c = cast(struct CallS *, ud);
  667. luaD_call(L, c->func, c->nresults, 0);
  668. }
  669. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  670. int ctx, lua_CFunction k) {
  671. struct CallS c;
  672. int status;
  673. ptrdiff_t func;
  674. lua_lock(L);
  675. api_checknelems(L, nargs+1);
  676. checkresults(L, nargs, nresults);
  677. if (errfunc == 0)
  678. func = 0;
  679. else {
  680. StkId o = index2addr(L, errfunc);
  681. api_checkvalidindex(L, o);
  682. func = savestack(L, o);
  683. }
  684. c.func = L->top - (nargs+1); /* function to be called */
  685. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  686. c.nresults = nresults; /* do a 'conventional' protected call */
  687. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  688. }
  689. else { /* prepare continuation (call is already protected by 'resume') */
  690. CallInfo *ci = L->ci;
  691. ci->u.c.k = k; /* save continuation */
  692. ci->u.c.ctx = ctx; /* save context */
  693. /* save information for error recovery */
  694. ci->u.c.oldtop = savestack(L, c.func);
  695. ci->u.c.old_allowhook = L->allowhook;
  696. ci->u.c.old_errfunc = L->errfunc;
  697. L->errfunc = func;
  698. /* mark that function may do error recovery */
  699. ci->callstatus |= CIST_YPCALL;
  700. luaD_call(L, c.func, nresults, 1); /* do the call */
  701. ci->callstatus &= ~CIST_YPCALL;
  702. L->errfunc = ci->u.c.old_errfunc;
  703. status = LUA_OK; /* if it is here, there were no errors */
  704. }
  705. adjustresults(L, nresults);
  706. lua_unlock(L);
  707. return status;
  708. }
  709. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  710. const char *chunkname) {
  711. ZIO z;
  712. int status;
  713. lua_lock(L);
  714. if (!chunkname) chunkname = "?";
  715. luaZ_init(L, &z, reader, data);
  716. status = luaD_protectedparser(L, &z, chunkname);
  717. lua_unlock(L);
  718. return status;
  719. }
  720. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  721. int status;
  722. TValue *o;
  723. lua_lock(L);
  724. api_checknelems(L, 1);
  725. o = L->top - 1;
  726. if (isLfunction(o))
  727. status = luaU_dump(L, getproto(o), writer, data, 0);
  728. else
  729. status = 1;
  730. lua_unlock(L);
  731. return status;
  732. }
  733. LUA_API int lua_status (lua_State *L) {
  734. return L->status;
  735. }
  736. /*
  737. ** Garbage-collection function
  738. */
  739. LUA_API int lua_gc (lua_State *L, int what, int data) {
  740. int res = 0;
  741. global_State *g;
  742. lua_lock(L);
  743. g = G(L);
  744. switch (what) {
  745. case LUA_GCSTOP: {
  746. g->GCthreshold = MAX_LUMEM;
  747. break;
  748. }
  749. case LUA_GCRESTART: {
  750. g->GCthreshold = g->totalbytes;
  751. break;
  752. }
  753. case LUA_GCCOLLECT: {
  754. luaC_fullgc(L, 0);
  755. break;
  756. }
  757. case LUA_GCCOUNT: {
  758. /* GC values are expressed in Kbytes: #bytes/2^10 */
  759. res = cast_int(g->totalbytes >> 10);
  760. break;
  761. }
  762. case LUA_GCCOUNTB: {
  763. res = cast_int(g->totalbytes & 0x3ff);
  764. break;
  765. }
  766. case LUA_GCSTEP: {
  767. lu_mem oldts = g->GCthreshold;
  768. lu_mem a = (cast(lu_mem, data) << 10);
  769. g->GCthreshold = (a <= g->totalbytes) ? g->totalbytes - a : 0;
  770. while (g->GCthreshold <= g->totalbytes) {
  771. luaC_step(L);
  772. if (g->gcstate == GCSpause) { /* end of cycle? */
  773. res = 1; /* signal it */
  774. break;
  775. }
  776. }
  777. if (oldts == MAX_LUMEM) /* collector was stopped? */
  778. g->GCthreshold = oldts; /* keep it that way */
  779. break;
  780. }
  781. case LUA_GCSETPAUSE: {
  782. res = g->gcpause;
  783. g->gcpause = data;
  784. break;
  785. }
  786. case LUA_GCSETSTEPMUL: {
  787. res = g->gcstepmul;
  788. g->gcstepmul = data;
  789. break;
  790. }
  791. default: res = -1; /* invalid option */
  792. }
  793. lua_unlock(L);
  794. return res;
  795. }
  796. /*
  797. ** miscellaneous functions
  798. */
  799. LUA_API int lua_error (lua_State *L) {
  800. lua_lock(L);
  801. api_checknelems(L, 1);
  802. luaG_errormsg(L);
  803. lua_unlock(L);
  804. return 0; /* to avoid warnings */
  805. }
  806. LUA_API int lua_next (lua_State *L, int idx) {
  807. StkId t;
  808. int more;
  809. lua_lock(L);
  810. t = index2addr(L, idx);
  811. api_check(L, ttistable(t), "table expected");
  812. more = luaH_next(L, hvalue(t), L->top - 1);
  813. if (more) {
  814. api_incr_top(L);
  815. }
  816. else /* no more elements */
  817. L->top -= 1; /* remove key */
  818. lua_unlock(L);
  819. return more;
  820. }
  821. LUA_API void lua_concat (lua_State *L, int n) {
  822. lua_lock(L);
  823. api_checknelems(L, n);
  824. if (n >= 2) {
  825. luaC_checkGC(L);
  826. luaV_concat(L, n);
  827. }
  828. else if (n == 0) { /* push empty string */
  829. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  830. api_incr_top(L);
  831. }
  832. /* else n == 1; nothing to do */
  833. lua_unlock(L);
  834. }
  835. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  836. lua_Alloc f;
  837. lua_lock(L);
  838. if (ud) *ud = G(L)->ud;
  839. f = G(L)->frealloc;
  840. lua_unlock(L);
  841. return f;
  842. }
  843. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  844. lua_lock(L);
  845. G(L)->ud = ud;
  846. G(L)->frealloc = f;
  847. lua_unlock(L);
  848. }
  849. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  850. Udata *u;
  851. lua_lock(L);
  852. luaC_checkGC(L);
  853. u = luaS_newudata(L, size, getcurrenv(L));
  854. setuvalue(L, L->top, u);
  855. api_incr_top(L);
  856. lua_unlock(L);
  857. return u + 1;
  858. }
  859. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  860. Closure *f;
  861. if (!ttisfunction(fi)) return NULL;
  862. f = clvalue(fi);
  863. if (f->c.isC) {
  864. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  865. *val = &f->c.upvalue[n-1];
  866. return "";
  867. }
  868. else {
  869. Proto *p = f->l.p;
  870. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  871. *val = f->l.upvals[n-1]->v;
  872. return getstr(p->upvalues[n-1].name);
  873. }
  874. }
  875. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  876. const char *name;
  877. TValue *val;
  878. lua_lock(L);
  879. name = aux_upvalue(index2addr(L, funcindex), n, &val);
  880. if (name) {
  881. setobj2s(L, L->top, val);
  882. api_incr_top(L);
  883. }
  884. lua_unlock(L);
  885. return name;
  886. }
  887. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  888. const char *name;
  889. TValue *val;
  890. StkId fi;
  891. lua_lock(L);
  892. fi = index2addr(L, funcindex);
  893. api_checknelems(L, 1);
  894. name = aux_upvalue(fi, n, &val);
  895. if (name) {
  896. L->top--;
  897. setobj(L, val, L->top);
  898. luaC_barrier(L, clvalue(fi), L->top);
  899. }
  900. lua_unlock(L);
  901. return name;
  902. }
  903. static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
  904. Closure *f;
  905. Proto *p;
  906. StkId fi = index2addr(L, fidx);
  907. api_check(L, ttisfunction(fi), "function expected");
  908. f = clvalue(fi);
  909. api_check(L, !f->c.isC, "Lua function expected");
  910. p = f->l.p;
  911. api_check(L, (1 <= n && n <= p->sizeupvalues), "invalid upvalue index");
  912. if (pf) *pf = f;
  913. return &f->l.upvals[n - 1]; /* get its upvalue pointer */
  914. }
  915. LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n) {
  916. Closure *f;
  917. StkId fi = index2addr(L, fidx);
  918. api_check(L, ttisfunction(fi), "function expected");
  919. f = clvalue(fi);
  920. if (f->c.isC) {
  921. api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
  922. return &f->c.upvalue[n - 1];
  923. }
  924. else return *getupvalref(L, fidx, n, NULL);
  925. }
  926. LUA_API void (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
  927. int fidx2, int n2) {
  928. Closure *f1;
  929. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  930. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  931. *up1 = *up2;
  932. luaC_objbarrier(L, f1, *up2);
  933. }
  934. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  935. lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL);
  936. lua_pushlightuserdata(L, &func);
  937. lua_pushlightuserdata(L, ud);
  938. return lua_pcall(L, 2, 0, 0);
  939. }