lapi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. ** $Id: lapi.c,v 2.99 2009/11/09 18:55:17 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 and LUA_REGISTRYINDEX do not need gc barrier
  166. (collector revisits them before finishing collection) */
  167. }
  168. LUA_API void lua_replace (lua_State *L, int idx) {
  169. lua_lock(L);
  170. /* explicit test for incompatible code */
  171. if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
  172. luaG_runerror(L, "no calling environment");
  173. api_checknelems(L, 1);
  174. moveto(L, L->top - 1, idx);
  175. L->top--;
  176. lua_unlock(L);
  177. }
  178. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  179. TValue *fr;
  180. lua_lock(L);
  181. fr = index2addr(L, fromidx);
  182. api_checkvalidindex(L, fr);
  183. moveto(L, fr, toidx);
  184. lua_unlock(L);
  185. }
  186. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  187. lua_lock(L);
  188. setobj2s(L, L->top, index2addr(L, idx));
  189. api_incr_top(L);
  190. lua_unlock(L);
  191. }
  192. /*
  193. ** access functions (stack -> C)
  194. */
  195. LUA_API int lua_type (lua_State *L, int idx) {
  196. StkId o = index2addr(L, idx);
  197. return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  198. }
  199. LUA_API const char *lua_typename (lua_State *L, int t) {
  200. UNUSED(L);
  201. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  202. }
  203. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  204. StkId o = index2addr(L, idx);
  205. return iscfunction(o);
  206. }
  207. LUA_API int lua_isnumber (lua_State *L, int idx) {
  208. TValue n;
  209. const TValue *o = index2addr(L, idx);
  210. return tonumber(o, &n);
  211. }
  212. LUA_API int lua_isstring (lua_State *L, int idx) {
  213. int t = lua_type(L, idx);
  214. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  215. }
  216. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  217. const TValue *o = index2addr(L, idx);
  218. return (ttisuserdata(o) || ttislightuserdata(o));
  219. }
  220. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  221. StkId o1 = index2addr(L, index1);
  222. StkId o2 = index2addr(L, index2);
  223. return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
  224. : luaO_rawequalObj(o1, o2);
  225. }
  226. LUA_API void lua_arith (lua_State *L, int op) {
  227. lua_lock(L);
  228. api_checknelems(L, 2);
  229. if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1)) {
  230. changenvalue(L->top - 2,
  231. luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
  232. }
  233. else
  234. luaV_arith(L, L->top - 2, L->top - 2, L->top - 1, op - LUA_OPADD + TM_ADD);
  235. L->top--;
  236. lua_unlock(L);
  237. }
  238. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  239. StkId o1, o2;
  240. int i;
  241. lua_lock(L); /* may call tag method */
  242. o1 = index2addr(L, index1);
  243. o2 = index2addr(L, index2);
  244. if (o1 == luaO_nilobject || o2 == luaO_nilobject)
  245. i = 0;
  246. else switch (op) {
  247. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  248. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  249. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  250. default: api_check(L, 0, "invalid option"); i = 0;
  251. }
  252. lua_unlock(L);
  253. return i;
  254. }
  255. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  256. TValue n;
  257. const TValue *o = index2addr(L, idx);
  258. if (tonumber(o, &n))
  259. return nvalue(o);
  260. else
  261. return 0;
  262. }
  263. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  264. TValue n;
  265. const TValue *o = index2addr(L, idx);
  266. if (tonumber(o, &n)) {
  267. lua_Integer res;
  268. lua_Number num = nvalue(o);
  269. lua_number2integer(res, num);
  270. return res;
  271. }
  272. else
  273. return 0;
  274. }
  275. LUA_API int lua_toboolean (lua_State *L, int idx) {
  276. const TValue *o = index2addr(L, idx);
  277. return !l_isfalse(o);
  278. }
  279. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  280. StkId o = index2addr(L, idx);
  281. if (!ttisstring(o)) {
  282. lua_lock(L); /* `luaV_tostring' may create a new string */
  283. if (!luaV_tostring(L, o)) { /* conversion failed? */
  284. if (len != NULL) *len = 0;
  285. lua_unlock(L);
  286. return NULL;
  287. }
  288. luaC_checkGC(L);
  289. o = index2addr(L, idx); /* previous call may reallocate the stack */
  290. lua_unlock(L);
  291. }
  292. if (len != NULL) *len = tsvalue(o)->len;
  293. return svalue(o);
  294. }
  295. LUA_API size_t lua_objlen (lua_State *L, int idx) {
  296. StkId o = index2addr(L, idx);
  297. switch (ttype(o)) {
  298. case LUA_TSTRING: return tsvalue(o)->len;
  299. case LUA_TUSERDATA: return uvalue(o)->len;
  300. case LUA_TTABLE: return luaH_getn(hvalue(o));
  301. default: return 0;
  302. }
  303. }
  304. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  305. StkId o = index2addr(L, idx);
  306. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  307. }
  308. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  309. StkId o = index2addr(L, idx);
  310. switch (ttype(o)) {
  311. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  312. case LUA_TLIGHTUSERDATA: return pvalue(o);
  313. default: return NULL;
  314. }
  315. }
  316. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  317. StkId o = index2addr(L, idx);
  318. return (!ttisthread(o)) ? NULL : thvalue(o);
  319. }
  320. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  321. StkId o = index2addr(L, idx);
  322. switch (ttype(o)) {
  323. case LUA_TTABLE: return hvalue(o);
  324. case LUA_TFUNCTION: return clvalue(o);
  325. case LUA_TTHREAD: return thvalue(o);
  326. case LUA_TUSERDATA:
  327. case LUA_TLIGHTUSERDATA:
  328. return lua_touserdata(L, idx);
  329. default: return NULL;
  330. }
  331. }
  332. /*
  333. ** push functions (C -> stack)
  334. */
  335. LUA_API void lua_pushnil (lua_State *L) {
  336. lua_lock(L);
  337. setnilvalue(L->top);
  338. api_incr_top(L);
  339. lua_unlock(L);
  340. }
  341. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  342. lua_lock(L);
  343. setnvalue(L->top, n);
  344. api_incr_top(L);
  345. lua_unlock(L);
  346. }
  347. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  348. lua_lock(L);
  349. setnvalue(L->top, cast_num(n));
  350. api_incr_top(L);
  351. lua_unlock(L);
  352. }
  353. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  354. TString *ts;
  355. lua_lock(L);
  356. luaC_checkGC(L);
  357. ts = luaS_newlstr(L, s, len);
  358. setsvalue2s(L, L->top, ts);
  359. api_incr_top(L);
  360. lua_unlock(L);
  361. return getstr(ts);
  362. }
  363. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  364. if (s == NULL) {
  365. lua_pushnil(L);
  366. return NULL;
  367. }
  368. else
  369. return lua_pushlstring(L, s, strlen(s));
  370. }
  371. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  372. va_list argp) {
  373. const char *ret;
  374. lua_lock(L);
  375. luaC_checkGC(L);
  376. ret = luaO_pushvfstring(L, fmt, argp);
  377. lua_unlock(L);
  378. return ret;
  379. }
  380. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  381. const char *ret;
  382. va_list argp;
  383. lua_lock(L);
  384. luaC_checkGC(L);
  385. va_start(argp, fmt);
  386. ret = luaO_pushvfstring(L, fmt, argp);
  387. va_end(argp);
  388. lua_unlock(L);
  389. return ret;
  390. }
  391. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  392. Closure *cl;
  393. lua_lock(L);
  394. api_checknelems(L, n);
  395. api_check(L, n <= UCHAR_MAX, "upvalue index too large");
  396. luaC_checkGC(L);
  397. cl = luaF_newCclosure(L, n, getcurrenv(L));
  398. cl->c.f = fn;
  399. L->top -= n;
  400. while (n--)
  401. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  402. setclvalue(L, L->top, cl);
  403. lua_assert(iswhite(obj2gco(cl)));
  404. api_incr_top(L);
  405. lua_unlock(L);
  406. }
  407. LUA_API void lua_pushboolean (lua_State *L, int b) {
  408. lua_lock(L);
  409. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  410. api_incr_top(L);
  411. lua_unlock(L);
  412. }
  413. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  414. lua_lock(L);
  415. setpvalue(L->top, p);
  416. api_incr_top(L);
  417. lua_unlock(L);
  418. }
  419. LUA_API int lua_pushthread (lua_State *L) {
  420. lua_lock(L);
  421. setthvalue(L, L->top, L);
  422. api_incr_top(L);
  423. lua_unlock(L);
  424. return (G(L)->mainthread == L);
  425. }
  426. /*
  427. ** get functions (Lua -> stack)
  428. */
  429. LUA_API void lua_gettable (lua_State *L, int idx) {
  430. StkId t;
  431. lua_lock(L);
  432. t = index2addr(L, idx);
  433. api_checkvalidindex(L, t);
  434. luaV_gettable(L, t, L->top - 1, L->top - 1);
  435. lua_unlock(L);
  436. }
  437. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  438. StkId t;
  439. lua_lock(L);
  440. t = index2addr(L, idx);
  441. api_checkvalidindex(L, t);
  442. setsvalue2s(L, L->top, luaS_new(L, k));
  443. api_incr_top(L);
  444. luaV_gettable(L, t, L->top - 1, L->top - 1);
  445. lua_unlock(L);
  446. }
  447. LUA_API void lua_rawget (lua_State *L, int idx) {
  448. StkId t;
  449. lua_lock(L);
  450. t = index2addr(L, idx);
  451. api_check(L, ttistable(t), "table expected");
  452. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  453. lua_unlock(L);
  454. }
  455. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  456. StkId o;
  457. lua_lock(L);
  458. o = index2addr(L, idx);
  459. api_check(L, ttistable(o), "table expected");
  460. setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  461. api_incr_top(L);
  462. lua_unlock(L);
  463. }
  464. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  465. Table *t;
  466. lua_lock(L);
  467. luaC_checkGC(L);
  468. t = luaH_new(L);
  469. sethvalue(L, L->top, t);
  470. api_incr_top(L);
  471. if (narray > 0 || nrec > 0)
  472. luaH_resize(L, t, narray, nrec);
  473. lua_unlock(L);
  474. }
  475. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  476. const TValue *obj;
  477. Table *mt = NULL;
  478. int res;
  479. lua_lock(L);
  480. obj = index2addr(L, objindex);
  481. switch (ttype(obj)) {
  482. case LUA_TTABLE:
  483. mt = hvalue(obj)->metatable;
  484. break;
  485. case LUA_TUSERDATA:
  486. mt = uvalue(obj)->metatable;
  487. break;
  488. default:
  489. mt = G(L)->mt[ttype(obj)];
  490. break;
  491. }
  492. if (mt == NULL)
  493. res = 0;
  494. else {
  495. sethvalue(L, L->top, mt);
  496. api_incr_top(L);
  497. res = 1;
  498. }
  499. lua_unlock(L);
  500. return res;
  501. }
  502. LUA_API void lua_getfenv (lua_State *L, int idx) {
  503. StkId o;
  504. lua_lock(L);
  505. o = index2addr(L, idx);
  506. api_checkvalidindex(L, o);
  507. switch (ttype(o)) {
  508. case LUA_TFUNCTION:
  509. sethvalue(L, L->top, clvalue(o)->c.env);
  510. break;
  511. case LUA_TUSERDATA:
  512. sethvalue(L, L->top, uvalue(o)->env);
  513. break;
  514. default:
  515. setnilvalue(L->top);
  516. break;
  517. }
  518. api_incr_top(L);
  519. lua_unlock(L);
  520. }
  521. /*
  522. ** set functions (stack -> Lua)
  523. */
  524. LUA_API void lua_settable (lua_State *L, int idx) {
  525. StkId t;
  526. lua_lock(L);
  527. api_checknelems(L, 2);
  528. t = index2addr(L, idx);
  529. api_checkvalidindex(L, t);
  530. luaV_settable(L, t, L->top - 2, L->top - 1);
  531. L->top -= 2; /* pop index and value */
  532. lua_unlock(L);
  533. }
  534. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  535. StkId t;
  536. lua_lock(L);
  537. api_checknelems(L, 1);
  538. t = index2addr(L, idx);
  539. api_checkvalidindex(L, t);
  540. setsvalue2s(L, L->top++, luaS_new(L, k));
  541. luaV_settable(L, t, L->top - 1, L->top - 2);
  542. L->top -= 2; /* pop value and key */
  543. lua_unlock(L);
  544. }
  545. LUA_API void lua_rawset (lua_State *L, int idx) {
  546. StkId t;
  547. lua_lock(L);
  548. api_checknelems(L, 2);
  549. t = index2addr(L, idx);
  550. api_check(L, ttistable(t), "table expected");
  551. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  552. luaC_barriert(L, hvalue(t), L->top-1);
  553. L->top -= 2;
  554. lua_unlock(L);
  555. }
  556. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  557. StkId o;
  558. lua_lock(L);
  559. api_checknelems(L, 1);
  560. o = index2addr(L, idx);
  561. api_check(L, ttistable(o), "table expected");
  562. setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
  563. luaC_barriert(L, hvalue(o), L->top-1);
  564. L->top--;
  565. lua_unlock(L);
  566. }
  567. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  568. TValue *obj;
  569. Table *mt;
  570. lua_lock(L);
  571. api_checknelems(L, 1);
  572. obj = index2addr(L, objindex);
  573. api_checkvalidindex(L, obj);
  574. if (ttisnil(L->top - 1))
  575. mt = NULL;
  576. else {
  577. api_check(L, ttistable(L->top - 1), "table expected");
  578. mt = hvalue(L->top - 1);
  579. }
  580. switch (ttype(obj)) {
  581. case LUA_TTABLE: {
  582. hvalue(obj)->metatable = mt;
  583. if (mt)
  584. luaC_objbarriert(L, hvalue(obj), mt);
  585. break;
  586. }
  587. case LUA_TUSERDATA: {
  588. uvalue(obj)->metatable = mt;
  589. if (mt) {
  590. luaC_objbarrier(L, rawuvalue(obj), mt);
  591. luaC_checkfinalizer(L, rawuvalue(obj));
  592. }
  593. break;
  594. }
  595. default: {
  596. G(L)->mt[ttype(obj)] = mt;
  597. break;
  598. }
  599. }
  600. L->top--;
  601. lua_unlock(L);
  602. return 1;
  603. }
  604. LUA_API int lua_setfenv (lua_State *L, int idx) {
  605. StkId o;
  606. int res = 1;
  607. lua_lock(L);
  608. api_checknelems(L, 1);
  609. o = index2addr(L, idx);
  610. api_checkvalidindex(L, o);
  611. api_check(L, ttistable(L->top - 1), "table expected");
  612. switch (ttype(o)) {
  613. case LUA_TFUNCTION:
  614. clvalue(o)->c.env = hvalue(L->top - 1);
  615. break;
  616. case LUA_TUSERDATA:
  617. uvalue(o)->env = 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. "results from function overflow current stack size")
  634. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  635. if (L->ci->callstatus & CIST_YIELDED) {
  636. if (ctx) *ctx = L->ci->u.c.ctx;
  637. return L->ci->u.c.status;
  638. }
  639. else return LUA_OK;
  640. }
  641. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  642. lua_CFunction k) {
  643. StkId func;
  644. lua_lock(L);
  645. api_check(L, k == NULL || !isLua(L->ci),
  646. "cannot use continuations inside hooks");
  647. api_checknelems(L, nargs+1);
  648. checkresults(L, nargs, nresults);
  649. func = L->top - (nargs+1);
  650. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  651. L->ci->u.c.k = k; /* save continuation */
  652. L->ci->u.c.ctx = ctx; /* save context */
  653. luaD_call(L, func, nresults, 1); /* do the call */
  654. }
  655. else /* no continuation or no yieldable */
  656. luaD_call(L, func, nresults, 0); /* just do the call */
  657. adjustresults(L, nresults);
  658. lua_unlock(L);
  659. }
  660. /*
  661. ** Execute a protected call.
  662. */
  663. struct CallS { /* data to `f_call' */
  664. StkId func;
  665. int nresults;
  666. };
  667. static void f_call (lua_State *L, void *ud) {
  668. struct CallS *c = cast(struct CallS *, ud);
  669. luaD_call(L, c->func, c->nresults, 0);
  670. }
  671. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  672. int ctx, lua_CFunction k) {
  673. struct CallS c;
  674. int status;
  675. ptrdiff_t func;
  676. lua_lock(L);
  677. api_checknelems(L, nargs+1);
  678. checkresults(L, nargs, nresults);
  679. if (errfunc == 0)
  680. func = 0;
  681. else {
  682. StkId o = index2addr(L, errfunc);
  683. api_checkvalidindex(L, o);
  684. func = savestack(L, o);
  685. }
  686. c.func = L->top - (nargs+1); /* function to be called */
  687. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  688. c.nresults = nresults; /* do a 'conventional' protected call */
  689. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  690. }
  691. else { /* prepare continuation (call is already protected by 'resume') */
  692. CallInfo *ci = L->ci;
  693. ci->u.c.k = k; /* save continuation */
  694. ci->u.c.ctx = ctx; /* save context */
  695. /* save information for error recovery */
  696. ci->u.c.oldtop = savestack(L, c.func);
  697. ci->u.c.old_allowhook = L->allowhook;
  698. ci->u.c.old_errfunc = L->errfunc;
  699. L->errfunc = func;
  700. /* mark that function may do error recovery */
  701. ci->callstatus |= CIST_YPCALL;
  702. luaD_call(L, c.func, nresults, 1); /* do the call */
  703. ci->callstatus &= ~CIST_YPCALL;
  704. L->errfunc = ci->u.c.old_errfunc;
  705. status = LUA_OK; /* if it is here, there were no errors */
  706. }
  707. adjustresults(L, nresults);
  708. lua_unlock(L);
  709. return status;
  710. }
  711. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  712. const char *chunkname) {
  713. ZIO z;
  714. int status;
  715. lua_lock(L);
  716. if (!chunkname) chunkname = "?";
  717. luaZ_init(L, &z, reader, data);
  718. status = luaD_protectedparser(L, &z, chunkname);
  719. lua_unlock(L);
  720. return status;
  721. }
  722. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  723. int status;
  724. TValue *o;
  725. lua_lock(L);
  726. api_checknelems(L, 1);
  727. o = L->top - 1;
  728. if (isLfunction(o))
  729. status = luaU_dump(L, getproto(o), writer, data, 0);
  730. else
  731. status = 1;
  732. lua_unlock(L);
  733. return status;
  734. }
  735. LUA_API int lua_status (lua_State *L) {
  736. return L->status;
  737. }
  738. /*
  739. ** Garbage-collection function
  740. */
  741. LUA_API int lua_gc (lua_State *L, int what, int data) {
  742. int res = 0;
  743. global_State *g;
  744. lua_lock(L);
  745. g = G(L);
  746. switch (what) {
  747. case LUA_GCSTOP: {
  748. g->GCthreshold = MAX_LUMEM;
  749. break;
  750. }
  751. case LUA_GCRESTART: {
  752. g->GCthreshold = g->totalbytes;
  753. break;
  754. }
  755. case LUA_GCCOLLECT: {
  756. luaC_fullgc(L, 0);
  757. break;
  758. }
  759. case LUA_GCCOUNT: {
  760. /* GC values are expressed in Kbytes: #bytes/2^10 */
  761. res = cast_int(g->totalbytes >> 10);
  762. break;
  763. }
  764. case LUA_GCCOUNTB: {
  765. res = cast_int(g->totalbytes & 0x3ff);
  766. break;
  767. }
  768. case LUA_GCSTEP: {
  769. lu_mem oldts = g->GCthreshold;
  770. lu_mem a = (cast(lu_mem, data) << 10);
  771. g->GCthreshold = (a <= g->totalbytes) ? g->totalbytes - a : 0;
  772. while (g->GCthreshold <= g->totalbytes) {
  773. luaC_step(L);
  774. if (g->gcstate == GCSpause) { /* end of cycle? */
  775. res = 1; /* signal it */
  776. break;
  777. }
  778. }
  779. if (oldts == MAX_LUMEM) /* collector was stopped? */
  780. g->GCthreshold = oldts; /* keep it that way */
  781. break;
  782. }
  783. case LUA_GCSETPAUSE: {
  784. res = g->gcpause;
  785. g->gcpause = data;
  786. break;
  787. }
  788. case LUA_GCSETSTEPMUL: {
  789. res = g->gcstepmul;
  790. g->gcstepmul = data;
  791. break;
  792. }
  793. case LUA_GCISRUNNING: {
  794. res = (g->GCthreshold != MAX_LUMEM);
  795. break;
  796. }
  797. default: res = -1; /* invalid option */
  798. }
  799. lua_unlock(L);
  800. return res;
  801. }
  802. /*
  803. ** miscellaneous functions
  804. */
  805. LUA_API int lua_error (lua_State *L) {
  806. lua_lock(L);
  807. api_checknelems(L, 1);
  808. luaG_errormsg(L);
  809. lua_unlock(L);
  810. return 0; /* to avoid warnings */
  811. }
  812. LUA_API int lua_next (lua_State *L, int idx) {
  813. StkId t;
  814. int more;
  815. lua_lock(L);
  816. t = index2addr(L, idx);
  817. api_check(L, ttistable(t), "table expected");
  818. more = luaH_next(L, hvalue(t), L->top - 1);
  819. if (more) {
  820. api_incr_top(L);
  821. }
  822. else /* no more elements */
  823. L->top -= 1; /* remove key */
  824. lua_unlock(L);
  825. return more;
  826. }
  827. LUA_API void lua_concat (lua_State *L, int n) {
  828. lua_lock(L);
  829. api_checknelems(L, n);
  830. if (n >= 2) {
  831. luaC_checkGC(L);
  832. luaV_concat(L, n);
  833. }
  834. else if (n == 0) { /* push empty string */
  835. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  836. api_incr_top(L);
  837. }
  838. /* else n == 1; nothing to do */
  839. lua_unlock(L);
  840. }
  841. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  842. lua_Alloc f;
  843. lua_lock(L);
  844. if (ud) *ud = G(L)->ud;
  845. f = G(L)->frealloc;
  846. lua_unlock(L);
  847. return f;
  848. }
  849. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  850. lua_lock(L);
  851. G(L)->ud = ud;
  852. G(L)->frealloc = f;
  853. lua_unlock(L);
  854. }
  855. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  856. Udata *u;
  857. lua_lock(L);
  858. luaC_checkGC(L);
  859. u = luaS_newudata(L, size, getcurrenv(L));
  860. setuvalue(L, L->top, u);
  861. api_incr_top(L);
  862. lua_unlock(L);
  863. return u + 1;
  864. }
  865. static const char *aux_upvalue (StkId fi, int n, TValue **val) {
  866. Closure *f;
  867. if (!ttisfunction(fi)) return NULL;
  868. f = clvalue(fi);
  869. if (f->c.isC) {
  870. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  871. *val = &f->c.upvalue[n-1];
  872. return "";
  873. }
  874. else {
  875. Proto *p = f->l.p;
  876. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  877. *val = f->l.upvals[n-1]->v;
  878. return getstr(p->upvalues[n-1].name);
  879. }
  880. }
  881. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  882. const char *name;
  883. TValue *val;
  884. lua_lock(L);
  885. name = aux_upvalue(index2addr(L, funcindex), n, &val);
  886. if (name) {
  887. setobj2s(L, L->top, val);
  888. api_incr_top(L);
  889. }
  890. lua_unlock(L);
  891. return name;
  892. }
  893. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  894. const char *name;
  895. TValue *val;
  896. StkId fi;
  897. lua_lock(L);
  898. fi = index2addr(L, funcindex);
  899. api_checknelems(L, 1);
  900. name = aux_upvalue(fi, n, &val);
  901. if (name) {
  902. L->top--;
  903. setobj(L, val, L->top);
  904. luaC_barrier(L, clvalue(fi), L->top);
  905. }
  906. lua_unlock(L);
  907. return name;
  908. }
  909. static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
  910. Closure *f;
  911. Proto *p;
  912. StkId fi = index2addr(L, fidx);
  913. api_check(L, ttisfunction(fi), "function expected");
  914. f = clvalue(fi);
  915. api_check(L, !f->c.isC, "Lua function expected");
  916. p = f->l.p;
  917. api_check(L, (1 <= n && n <= p->sizeupvalues), "invalid upvalue index");
  918. if (pf) *pf = f;
  919. return &f->l.upvals[n - 1]; /* get its upvalue pointer */
  920. }
  921. LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n) {
  922. Closure *f;
  923. StkId fi = index2addr(L, fidx);
  924. api_check(L, ttisfunction(fi), "function expected");
  925. f = clvalue(fi);
  926. if (f->c.isC) {
  927. api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
  928. return &f->c.upvalue[n - 1];
  929. }
  930. else return *getupvalref(L, fidx, n, NULL);
  931. }
  932. LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1,
  933. int fidx2, int n2) {
  934. Closure *f1;
  935. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  936. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  937. *up1 = *up2;
  938. luaC_objbarrier(L, f1, *up2);
  939. }
  940. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  941. lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL);
  942. lua_pushlightuserdata(L, &func);
  943. lua_pushlightuserdata(L, ud);
  944. return lua_pcall(L, 2, 0, 0);
  945. }