lapi.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. ** $Id: lapi.c,v 2.163 2012/05/23 15:42:27 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. /* value at a non-valid index */
  28. #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
  29. /* corresponding test */
  30. #define isvalid(o) ((o) != luaO_nilobject)
  31. #define api_checkvalidindex(L, i) api_check(L, isvalid(i), "invalid index")
  32. static TValue *index2addr (lua_State *L, int idx) {
  33. CallInfo *ci = L->ci;
  34. if (idx > 0) {
  35. TValue *o = ci->func + idx;
  36. api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
  37. if (o >= L->top) return NONVALIDVALUE;
  38. else return o;
  39. }
  40. else if (idx > LUA_REGISTRYINDEX) {
  41. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  42. return L->top + idx;
  43. }
  44. else if (idx == LUA_REGISTRYINDEX)
  45. return &G(L)->l_registry;
  46. else { /* upvalues */
  47. idx = LUA_REGISTRYINDEX - idx;
  48. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  49. if (ttislcf(ci->func)) /* light C function? */
  50. return NONVALIDVALUE; /* it has no upvalues */
  51. else {
  52. CClosure *func = clCvalue(ci->func);
  53. return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
  54. }
  55. }
  56. }
  57. /*
  58. ** to be called by 'lua_checkstack' in protected mode, to grow stack
  59. ** capturing memory errors
  60. */
  61. static void growstack (lua_State *L, void *ud) {
  62. int size = *(int *)ud;
  63. luaD_growstack(L, size);
  64. }
  65. LUA_API int lua_checkstack (lua_State *L, int size) {
  66. int res;
  67. CallInfo *ci = L->ci;
  68. lua_lock(L);
  69. if (L->stack_last - L->top > size) /* stack large enough? */
  70. res = 1; /* yes; check is OK */
  71. else { /* no; need to grow stack */
  72. int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
  73. if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
  74. res = 0; /* no */
  75. else /* try to grow stack */
  76. res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
  77. }
  78. if (res && ci->top < L->top + size)
  79. ci->top = L->top + size; /* adjust frame top */
  80. lua_unlock(L);
  81. return res;
  82. }
  83. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  84. int i;
  85. if (from == to) return;
  86. lua_lock(to);
  87. api_checknelems(from, n);
  88. api_check(from, G(from) == G(to), "moving among independent states");
  89. api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
  90. from->top -= n;
  91. for (i = 0; i < n; i++) {
  92. setobj2s(to, to->top++, from->top + i);
  93. }
  94. lua_unlock(to);
  95. }
  96. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  97. lua_CFunction old;
  98. lua_lock(L);
  99. old = G(L)->panic;
  100. G(L)->panic = panicf;
  101. lua_unlock(L);
  102. return old;
  103. }
  104. LUA_API const lua_Number *lua_version (lua_State *L) {
  105. static const lua_Number version = LUA_VERSION_NUM;
  106. if (L == NULL) return &version;
  107. else return G(L)->version;
  108. }
  109. /*
  110. ** basic stack manipulation
  111. */
  112. /*
  113. ** convert an acceptable stack index into an absolute index
  114. */
  115. LUA_API int lua_absindex (lua_State *L, int idx) {
  116. return (idx > 0 || idx <= LUA_REGISTRYINDEX)
  117. ? idx
  118. : cast_int(L->top - L->ci->func + idx);
  119. }
  120. LUA_API int lua_gettop (lua_State *L) {
  121. return cast_int(L->top - (L->ci->func + 1));
  122. }
  123. LUA_API void lua_settop (lua_State *L, int idx) {
  124. StkId func = L->ci->func;
  125. lua_lock(L);
  126. if (idx >= 0) {
  127. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  128. while (L->top < (func + 1) + idx)
  129. setnilvalue(L->top++);
  130. L->top = (func + 1) + idx;
  131. }
  132. else {
  133. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  134. L->top += idx+1; /* `subtract' index (index is negative) */
  135. }
  136. lua_unlock(L);
  137. }
  138. LUA_API void lua_remove (lua_State *L, int idx) {
  139. StkId p;
  140. lua_lock(L);
  141. p = index2addr(L, idx);
  142. api_checkvalidindex(L, p);
  143. while (++p < L->top) setobjs2s(L, p-1, p);
  144. L->top--;
  145. lua_unlock(L);
  146. }
  147. LUA_API void lua_insert (lua_State *L, int idx) {
  148. StkId p;
  149. StkId q;
  150. lua_lock(L);
  151. p = index2addr(L, idx);
  152. api_checkvalidindex(L, p);
  153. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  154. setobjs2s(L, p, L->top);
  155. lua_unlock(L);
  156. }
  157. static void moveto (lua_State *L, TValue *fr, int idx) {
  158. TValue *to = index2addr(L, idx);
  159. api_checkvalidindex(L, to);
  160. setobj(L, to, fr);
  161. if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
  162. luaC_barrier(L, clCvalue(L->ci->func), fr);
  163. /* LUA_REGISTRYINDEX does not need gc barrier
  164. (collector revisits it before finishing collection) */
  165. }
  166. LUA_API void lua_replace (lua_State *L, int idx) {
  167. lua_lock(L);
  168. api_checknelems(L, 1);
  169. moveto(L, L->top - 1, idx);
  170. L->top--;
  171. lua_unlock(L);
  172. }
  173. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  174. TValue *fr;
  175. lua_lock(L);
  176. fr = index2addr(L, fromidx);
  177. api_checkvalidindex(L, fr);
  178. moveto(L, fr, toidx);
  179. lua_unlock(L);
  180. }
  181. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  182. lua_lock(L);
  183. setobj2s(L, L->top, index2addr(L, idx));
  184. api_incr_top(L);
  185. lua_unlock(L);
  186. }
  187. /*
  188. ** access functions (stack -> C)
  189. */
  190. LUA_API int lua_type (lua_State *L, int idx) {
  191. StkId o = index2addr(L, idx);
  192. return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
  193. }
  194. LUA_API const char *lua_typename (lua_State *L, int t) {
  195. UNUSED(L);
  196. return ttypename(t);
  197. }
  198. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  199. StkId o = index2addr(L, idx);
  200. return (ttislcf(o) || (ttisCclosure(o)));
  201. }
  202. LUA_API int lua_isnumber (lua_State *L, int idx) {
  203. TValue n;
  204. const TValue *o = index2addr(L, idx);
  205. return tonumber(o, &n);
  206. }
  207. LUA_API int lua_isstring (lua_State *L, int idx) {
  208. int t = lua_type(L, idx);
  209. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  210. }
  211. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  212. const TValue *o = index2addr(L, idx);
  213. return (ttisuserdata(o) || ttislightuserdata(o));
  214. }
  215. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  216. StkId o1 = index2addr(L, index1);
  217. StkId o2 = index2addr(L, index2);
  218. return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
  219. }
  220. LUA_API void lua_arith (lua_State *L, int op) {
  221. StkId o1; /* 1st operand */
  222. StkId o2; /* 2nd operand */
  223. lua_lock(L);
  224. if (op != LUA_OPUNM) /* all other operations expect two operands */
  225. api_checknelems(L, 2);
  226. else { /* for unary minus, add fake 2nd operand */
  227. api_checknelems(L, 1);
  228. setobjs2s(L, L->top, L->top - 1);
  229. L->top++;
  230. }
  231. o1 = L->top - 2;
  232. o2 = L->top - 1;
  233. if (ttisnumber(o1) && ttisnumber(o2)) {
  234. changenvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
  235. }
  236. else
  237. luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
  238. L->top--;
  239. lua_unlock(L);
  240. }
  241. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  242. StkId o1, o2;
  243. int i = 0;
  244. lua_lock(L); /* may call tag method */
  245. o1 = index2addr(L, index1);
  246. o2 = index2addr(L, index2);
  247. if (isvalid(o1) && isvalid(o2)) {
  248. switch (op) {
  249. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  250. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  251. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  252. default: api_check(L, 0, "invalid option");
  253. }
  254. }
  255. lua_unlock(L);
  256. return i;
  257. }
  258. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
  259. TValue n;
  260. const TValue *o = index2addr(L, idx);
  261. if (tonumber(o, &n)) {
  262. if (isnum) *isnum = 1;
  263. return nvalue(o);
  264. }
  265. else {
  266. if (isnum) *isnum = 0;
  267. return 0;
  268. }
  269. }
  270. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
  271. TValue n;
  272. const TValue *o = index2addr(L, idx);
  273. if (tonumber(o, &n)) {
  274. lua_Integer res;
  275. lua_Number num = nvalue(o);
  276. lua_number2integer(res, num);
  277. if (isnum) *isnum = 1;
  278. return res;
  279. }
  280. else {
  281. if (isnum) *isnum = 0;
  282. return 0;
  283. }
  284. }
  285. LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
  286. TValue n;
  287. const TValue *o = index2addr(L, idx);
  288. if (tonumber(o, &n)) {
  289. lua_Unsigned res;
  290. lua_Number num = nvalue(o);
  291. lua_number2unsigned(res, num);
  292. if (isnum) *isnum = 1;
  293. return res;
  294. }
  295. else {
  296. if (isnum) *isnum = 0;
  297. return 0;
  298. }
  299. }
  300. LUA_API int lua_toboolean (lua_State *L, int idx) {
  301. const TValue *o = index2addr(L, idx);
  302. return !l_isfalse(o);
  303. }
  304. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  305. StkId o = index2addr(L, idx);
  306. if (!ttisstring(o)) {
  307. lua_lock(L); /* `luaV_tostring' may create a new string */
  308. if (!luaV_tostring(L, o)) { /* conversion failed? */
  309. if (len != NULL) *len = 0;
  310. lua_unlock(L);
  311. return NULL;
  312. }
  313. luaC_checkGC(L);
  314. o = index2addr(L, idx); /* previous call may reallocate the stack */
  315. lua_unlock(L);
  316. }
  317. if (len != NULL) *len = tsvalue(o)->len;
  318. return svalue(o);
  319. }
  320. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  321. StkId o = index2addr(L, idx);
  322. switch (ttypenv(o)) {
  323. case LUA_TSTRING: return tsvalue(o)->len;
  324. case LUA_TUSERDATA: return uvalue(o)->len;
  325. case LUA_TTABLE: return luaH_getn(hvalue(o));
  326. default: return 0;
  327. }
  328. }
  329. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  330. StkId o = index2addr(L, idx);
  331. if (ttislcf(o)) return fvalue(o);
  332. else if (ttisCclosure(o))
  333. return clCvalue(o)->f;
  334. else return NULL; /* not a C function */
  335. }
  336. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  337. StkId o = index2addr(L, idx);
  338. switch (ttypenv(o)) {
  339. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  340. case LUA_TLIGHTUSERDATA: return pvalue(o);
  341. default: return NULL;
  342. }
  343. }
  344. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  345. StkId o = index2addr(L, idx);
  346. return (!ttisthread(o)) ? NULL : thvalue(o);
  347. }
  348. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  349. StkId o = index2addr(L, idx);
  350. switch (ttype(o)) {
  351. case LUA_TTABLE: return hvalue(o);
  352. case LUA_TLCL: return clLvalue(o);
  353. case LUA_TCCL: return clCvalue(o);
  354. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  355. case LUA_TTHREAD: return thvalue(o);
  356. case LUA_TUSERDATA:
  357. case LUA_TLIGHTUSERDATA:
  358. return lua_touserdata(L, idx);
  359. default: return NULL;
  360. }
  361. }
  362. /*
  363. ** push functions (C -> stack)
  364. */
  365. LUA_API void lua_pushnil (lua_State *L) {
  366. lua_lock(L);
  367. setnilvalue(L->top);
  368. api_incr_top(L);
  369. lua_unlock(L);
  370. }
  371. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  372. lua_lock(L);
  373. setnvalue(L->top, n);
  374. luai_checknum(L, L->top,
  375. luaG_runerror(L, "C API - attempt to push a signaling NaN"));
  376. api_incr_top(L);
  377. lua_unlock(L);
  378. }
  379. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  380. lua_lock(L);
  381. setnvalue(L->top, cast_num(n));
  382. api_incr_top(L);
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
  386. lua_Number n;
  387. lua_lock(L);
  388. n = lua_unsigned2number(u);
  389. setnvalue(L->top, n);
  390. api_incr_top(L);
  391. lua_unlock(L);
  392. }
  393. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  394. TString *ts;
  395. lua_lock(L);
  396. luaC_checkGC(L);
  397. ts = luaS_newlstr(L, s, len);
  398. setsvalue2s(L, L->top, ts);
  399. api_incr_top(L);
  400. lua_unlock(L);
  401. return getstr(ts);
  402. }
  403. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  404. if (s == NULL) {
  405. lua_pushnil(L);
  406. return NULL;
  407. }
  408. else {
  409. TString *ts;
  410. lua_lock(L);
  411. luaC_checkGC(L);
  412. ts = luaS_new(L, s);
  413. setsvalue2s(L, L->top, ts);
  414. api_incr_top(L);
  415. lua_unlock(L);
  416. return getstr(ts);
  417. }
  418. }
  419. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  420. va_list argp) {
  421. const char *ret;
  422. lua_lock(L);
  423. luaC_checkGC(L);
  424. ret = luaO_pushvfstring(L, fmt, argp);
  425. lua_unlock(L);
  426. return ret;
  427. }
  428. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  429. const char *ret;
  430. va_list argp;
  431. lua_lock(L);
  432. luaC_checkGC(L);
  433. va_start(argp, fmt);
  434. ret = luaO_pushvfstring(L, fmt, argp);
  435. va_end(argp);
  436. lua_unlock(L);
  437. return ret;
  438. }
  439. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  440. lua_lock(L);
  441. if (n == 0) {
  442. setfvalue(L->top, fn);
  443. }
  444. else {
  445. Closure *cl;
  446. api_checknelems(L, n);
  447. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  448. luaC_checkGC(L);
  449. cl = luaF_newCclosure(L, n);
  450. cl->c.f = fn;
  451. L->top -= n;
  452. while (n--)
  453. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  454. setclCvalue(L, L->top, cl);
  455. }
  456. api_incr_top(L);
  457. lua_unlock(L);
  458. }
  459. LUA_API void lua_pushboolean (lua_State *L, int b) {
  460. lua_lock(L);
  461. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  462. api_incr_top(L);
  463. lua_unlock(L);
  464. }
  465. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  466. lua_lock(L);
  467. setpvalue(L->top, p);
  468. api_incr_top(L);
  469. lua_unlock(L);
  470. }
  471. LUA_API int lua_pushthread (lua_State *L) {
  472. lua_lock(L);
  473. setthvalue(L, L->top, L);
  474. api_incr_top(L);
  475. lua_unlock(L);
  476. return (G(L)->mainthread == L);
  477. }
  478. /*
  479. ** get functions (Lua -> stack)
  480. */
  481. LUA_API void lua_getglobal (lua_State *L, const char *var) {
  482. Table *reg = hvalue(&G(L)->l_registry);
  483. const TValue *gt; /* global table */
  484. lua_lock(L);
  485. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  486. setsvalue2s(L, L->top++, luaS_new(L, var));
  487. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  488. lua_unlock(L);
  489. }
  490. LUA_API void lua_gettable (lua_State *L, int idx) {
  491. StkId t;
  492. lua_lock(L);
  493. t = index2addr(L, idx);
  494. api_checkvalidindex(L, t);
  495. luaV_gettable(L, t, L->top - 1, L->top - 1);
  496. lua_unlock(L);
  497. }
  498. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  499. StkId t;
  500. lua_lock(L);
  501. t = index2addr(L, idx);
  502. api_checkvalidindex(L, t);
  503. setsvalue2s(L, L->top, luaS_new(L, k));
  504. api_incr_top(L);
  505. luaV_gettable(L, t, L->top - 1, L->top - 1);
  506. lua_unlock(L);
  507. }
  508. LUA_API void lua_rawget (lua_State *L, int idx) {
  509. StkId t;
  510. lua_lock(L);
  511. t = index2addr(L, idx);
  512. api_check(L, ttistable(t), "table expected");
  513. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  514. lua_unlock(L);
  515. }
  516. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  517. StkId t;
  518. lua_lock(L);
  519. t = index2addr(L, idx);
  520. api_check(L, ttistable(t), "table expected");
  521. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  522. api_incr_top(L);
  523. lua_unlock(L);
  524. }
  525. LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
  526. StkId t;
  527. TValue k;
  528. lua_lock(L);
  529. t = index2addr(L, idx);
  530. api_check(L, ttistable(t), "table expected");
  531. setpvalue(&k, cast(void *, p));
  532. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  533. api_incr_top(L);
  534. lua_unlock(L);
  535. }
  536. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  537. Table *t;
  538. lua_lock(L);
  539. luaC_checkGC(L);
  540. t = luaH_new(L);
  541. sethvalue(L, L->top, t);
  542. api_incr_top(L);
  543. if (narray > 0 || nrec > 0)
  544. luaH_resize(L, t, narray, nrec);
  545. lua_unlock(L);
  546. }
  547. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  548. const TValue *obj;
  549. Table *mt = NULL;
  550. int res;
  551. lua_lock(L);
  552. obj = index2addr(L, objindex);
  553. switch (ttypenv(obj)) {
  554. case LUA_TTABLE:
  555. mt = hvalue(obj)->metatable;
  556. break;
  557. case LUA_TUSERDATA:
  558. mt = uvalue(obj)->metatable;
  559. break;
  560. default:
  561. mt = G(L)->mt[ttypenv(obj)];
  562. break;
  563. }
  564. if (mt == NULL)
  565. res = 0;
  566. else {
  567. sethvalue(L, L->top, mt);
  568. api_incr_top(L);
  569. res = 1;
  570. }
  571. lua_unlock(L);
  572. return res;
  573. }
  574. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  575. StkId o;
  576. lua_lock(L);
  577. o = index2addr(L, idx);
  578. api_checkvalidindex(L, o);
  579. api_check(L, ttisuserdata(o), "userdata expected");
  580. if (uvalue(o)->env) {
  581. sethvalue(L, L->top, uvalue(o)->env);
  582. } else
  583. setnilvalue(L->top);
  584. api_incr_top(L);
  585. lua_unlock(L);
  586. }
  587. /*
  588. ** set functions (stack -> Lua)
  589. */
  590. LUA_API void lua_setglobal (lua_State *L, const char *var) {
  591. Table *reg = hvalue(&G(L)->l_registry);
  592. const TValue *gt; /* global table */
  593. lua_lock(L);
  594. api_checknelems(L, 1);
  595. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  596. setsvalue2s(L, L->top++, luaS_new(L, var));
  597. luaV_settable(L, gt, L->top - 1, L->top - 2);
  598. L->top -= 2; /* pop value and key */
  599. lua_unlock(L);
  600. }
  601. LUA_API void lua_settable (lua_State *L, int idx) {
  602. StkId t;
  603. lua_lock(L);
  604. api_checknelems(L, 2);
  605. t = index2addr(L, idx);
  606. api_checkvalidindex(L, t);
  607. luaV_settable(L, t, L->top - 2, L->top - 1);
  608. L->top -= 2; /* pop index and value */
  609. lua_unlock(L);
  610. }
  611. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  612. StkId t;
  613. lua_lock(L);
  614. api_checknelems(L, 1);
  615. t = index2addr(L, idx);
  616. api_checkvalidindex(L, t);
  617. setsvalue2s(L, L->top++, luaS_new(L, k));
  618. luaV_settable(L, t, L->top - 1, L->top - 2);
  619. L->top -= 2; /* pop value and key */
  620. lua_unlock(L);
  621. }
  622. LUA_API void lua_rawset (lua_State *L, int idx) {
  623. StkId t;
  624. lua_lock(L);
  625. api_checknelems(L, 2);
  626. t = index2addr(L, idx);
  627. api_check(L, ttistable(t), "table expected");
  628. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  629. invalidateTMcache(hvalue(t));
  630. luaC_barrierback(L, gcvalue(t), L->top-1);
  631. L->top -= 2;
  632. lua_unlock(L);
  633. }
  634. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  635. StkId t;
  636. lua_lock(L);
  637. api_checknelems(L, 1);
  638. t = index2addr(L, idx);
  639. api_check(L, ttistable(t), "table expected");
  640. luaH_setint(L, hvalue(t), n, L->top - 1);
  641. luaC_barrierback(L, gcvalue(t), L->top-1);
  642. L->top--;
  643. lua_unlock(L);
  644. }
  645. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  646. StkId t;
  647. TValue k;
  648. lua_lock(L);
  649. api_checknelems(L, 1);
  650. t = index2addr(L, idx);
  651. api_check(L, ttistable(t), "table expected");
  652. setpvalue(&k, cast(void *, p));
  653. setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
  654. luaC_barrierback(L, gcvalue(t), L->top - 1);
  655. L->top--;
  656. lua_unlock(L);
  657. }
  658. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  659. TValue *obj;
  660. Table *mt;
  661. lua_lock(L);
  662. api_checknelems(L, 1);
  663. obj = index2addr(L, objindex);
  664. api_checkvalidindex(L, obj);
  665. if (ttisnil(L->top - 1))
  666. mt = NULL;
  667. else {
  668. api_check(L, ttistable(L->top - 1), "table expected");
  669. mt = hvalue(L->top - 1);
  670. }
  671. switch (ttypenv(obj)) {
  672. case LUA_TTABLE: {
  673. hvalue(obj)->metatable = mt;
  674. if (mt)
  675. luaC_objbarrierback(L, gcvalue(obj), mt);
  676. luaC_checkfinalizer(L, gcvalue(obj), mt);
  677. break;
  678. }
  679. case LUA_TUSERDATA: {
  680. uvalue(obj)->metatable = mt;
  681. if (mt) {
  682. luaC_objbarrier(L, rawuvalue(obj), mt);
  683. luaC_checkfinalizer(L, gcvalue(obj), mt);
  684. }
  685. break;
  686. }
  687. default: {
  688. G(L)->mt[ttypenv(obj)] = mt;
  689. break;
  690. }
  691. }
  692. L->top--;
  693. lua_unlock(L);
  694. return 1;
  695. }
  696. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  697. StkId o;
  698. lua_lock(L);
  699. api_checknelems(L, 1);
  700. o = index2addr(L, idx);
  701. api_checkvalidindex(L, o);
  702. api_check(L, ttisuserdata(o), "userdata expected");
  703. if (ttisnil(L->top - 1))
  704. uvalue(o)->env = NULL;
  705. else {
  706. api_check(L, ttistable(L->top - 1), "table expected");
  707. uvalue(o)->env = hvalue(L->top - 1);
  708. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  709. }
  710. L->top--;
  711. lua_unlock(L);
  712. }
  713. /*
  714. ** `load' and `call' functions (run Lua code)
  715. */
  716. #define checkresults(L,na,nr) \
  717. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  718. "results from function overflow current stack size")
  719. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  720. if (L->ci->callstatus & CIST_YIELDED) {
  721. if (ctx) *ctx = L->ci->u.c.ctx;
  722. return L->ci->u.c.status;
  723. }
  724. else return LUA_OK;
  725. }
  726. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  727. lua_CFunction k) {
  728. StkId func;
  729. lua_lock(L);
  730. api_check(L, k == NULL || !isLua(L->ci),
  731. "cannot use continuations inside hooks");
  732. api_checknelems(L, nargs+1);
  733. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  734. checkresults(L, nargs, nresults);
  735. func = L->top - (nargs+1);
  736. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  737. L->ci->u.c.k = k; /* save continuation */
  738. L->ci->u.c.ctx = ctx; /* save context */
  739. luaD_call(L, func, nresults, 1); /* do the call */
  740. }
  741. else /* no continuation or no yieldable */
  742. luaD_call(L, func, nresults, 0); /* just do the call */
  743. adjustresults(L, nresults);
  744. lua_unlock(L);
  745. }
  746. /*
  747. ** Execute a protected call.
  748. */
  749. struct CallS { /* data to `f_call' */
  750. StkId func;
  751. int nresults;
  752. };
  753. static void f_call (lua_State *L, void *ud) {
  754. struct CallS *c = cast(struct CallS *, ud);
  755. luaD_call(L, c->func, c->nresults, 0);
  756. }
  757. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  758. int ctx, lua_CFunction k) {
  759. struct CallS c;
  760. int status;
  761. ptrdiff_t func;
  762. lua_lock(L);
  763. api_check(L, k == NULL || !isLua(L->ci),
  764. "cannot use continuations inside hooks");
  765. api_checknelems(L, nargs+1);
  766. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  767. checkresults(L, nargs, nresults);
  768. if (errfunc == 0)
  769. func = 0;
  770. else {
  771. StkId o = index2addr(L, errfunc);
  772. api_checkvalidindex(L, o);
  773. func = savestack(L, o);
  774. }
  775. c.func = L->top - (nargs+1); /* function to be called */
  776. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  777. c.nresults = nresults; /* do a 'conventional' protected call */
  778. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  779. }
  780. else { /* prepare continuation (call is already protected by 'resume') */
  781. CallInfo *ci = L->ci;
  782. ci->u.c.k = k; /* save continuation */
  783. ci->u.c.ctx = ctx; /* save context */
  784. /* save information for error recovery */
  785. ci->extra = savestack(L, c.func);
  786. ci->u.c.old_allowhook = L->allowhook;
  787. ci->u.c.old_errfunc = L->errfunc;
  788. L->errfunc = func;
  789. /* mark that function may do error recovery */
  790. ci->callstatus |= CIST_YPCALL;
  791. luaD_call(L, c.func, nresults, 1); /* do the call */
  792. ci->callstatus &= ~CIST_YPCALL;
  793. L->errfunc = ci->u.c.old_errfunc;
  794. status = LUA_OK; /* if it is here, there were no errors */
  795. }
  796. adjustresults(L, nresults);
  797. lua_unlock(L);
  798. return status;
  799. }
  800. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  801. const char *chunkname, const char *mode) {
  802. ZIO z;
  803. int status;
  804. lua_lock(L);
  805. if (!chunkname) chunkname = "?";
  806. luaZ_init(L, &z, reader, data);
  807. status = luaD_protectedparser(L, &z, chunkname, mode);
  808. if (status == LUA_OK) { /* no errors? */
  809. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  810. if (f->nupvalues == 1) { /* does it have one upvalue? */
  811. /* get global table from registry */
  812. Table *reg = hvalue(&G(L)->l_registry);
  813. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  814. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  815. setobj(L, f->upvals[0]->v, gt);
  816. luaC_barrier(L, f->upvals[0], gt);
  817. }
  818. }
  819. lua_unlock(L);
  820. return status;
  821. }
  822. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  823. int status;
  824. TValue *o;
  825. lua_lock(L);
  826. api_checknelems(L, 1);
  827. o = L->top - 1;
  828. if (isLfunction(o))
  829. status = luaU_dump(L, getproto(o), writer, data, 0);
  830. else
  831. status = 1;
  832. lua_unlock(L);
  833. return status;
  834. }
  835. LUA_API int lua_status (lua_State *L) {
  836. return L->status;
  837. }
  838. /*
  839. ** Garbage-collection function
  840. */
  841. LUA_API int lua_gc (lua_State *L, int what, int data) {
  842. int res = 0;
  843. global_State *g;
  844. lua_lock(L);
  845. g = G(L);
  846. switch (what) {
  847. case LUA_GCSTOP: {
  848. g->gcrunning = 0;
  849. break;
  850. }
  851. case LUA_GCRESTART: {
  852. luaE_setdebt(g, 0);
  853. g->gcrunning = 1;
  854. break;
  855. }
  856. case LUA_GCCOLLECT: {
  857. luaC_fullgc(L, 0);
  858. break;
  859. }
  860. case LUA_GCCOUNT: {
  861. /* GC values are expressed in Kbytes: #bytes/2^10 */
  862. res = cast_int(gettotalbytes(g) >> 10);
  863. break;
  864. }
  865. case LUA_GCCOUNTB: {
  866. res = cast_int(gettotalbytes(g) & 0x3ff);
  867. break;
  868. }
  869. case LUA_GCSTEP: {
  870. if (g->gckind == KGC_GEN) { /* generational mode? */
  871. res = (g->GCestimate == 0); /* true if it will do major collection */
  872. luaC_forcestep(L); /* do a single step */
  873. }
  874. else {
  875. lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
  876. if (g->gcrunning)
  877. debt += g->GCdebt; /* include current debt */
  878. luaE_setdebt(g, debt);
  879. luaC_forcestep(L);
  880. if (g->gcstate == GCSpause) /* end of cycle? */
  881. res = 1; /* signal it */
  882. }
  883. break;
  884. }
  885. case LUA_GCSETPAUSE: {
  886. res = g->gcpause;
  887. g->gcpause = data;
  888. break;
  889. }
  890. case LUA_GCSETMAJORINC: {
  891. res = g->gcmajorinc;
  892. g->gcmajorinc = data;
  893. break;
  894. }
  895. case LUA_GCSETSTEPMUL: {
  896. res = g->gcstepmul;
  897. g->gcstepmul = data;
  898. break;
  899. }
  900. case LUA_GCISRUNNING: {
  901. res = g->gcrunning;
  902. break;
  903. }
  904. case LUA_GCGEN: { /* change collector to generational mode */
  905. luaC_changemode(L, KGC_GEN);
  906. break;
  907. }
  908. case LUA_GCINC: { /* change collector to incremental mode */
  909. luaC_changemode(L, KGC_NORMAL);
  910. break;
  911. }
  912. default: res = -1; /* invalid option */
  913. }
  914. lua_unlock(L);
  915. return res;
  916. }
  917. /*
  918. ** miscellaneous functions
  919. */
  920. LUA_API int lua_error (lua_State *L) {
  921. lua_lock(L);
  922. api_checknelems(L, 1);
  923. luaG_errormsg(L);
  924. lua_unlock(L);
  925. return 0; /* to avoid warnings */
  926. }
  927. LUA_API int lua_next (lua_State *L, int idx) {
  928. StkId t;
  929. int more;
  930. lua_lock(L);
  931. t = index2addr(L, idx);
  932. api_check(L, ttistable(t), "table expected");
  933. more = luaH_next(L, hvalue(t), L->top - 1);
  934. if (more) {
  935. api_incr_top(L);
  936. }
  937. else /* no more elements */
  938. L->top -= 1; /* remove key */
  939. lua_unlock(L);
  940. return more;
  941. }
  942. LUA_API void lua_concat (lua_State *L, int n) {
  943. lua_lock(L);
  944. api_checknelems(L, n);
  945. if (n >= 2) {
  946. luaC_checkGC(L);
  947. luaV_concat(L, n);
  948. }
  949. else if (n == 0) { /* push empty string */
  950. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  951. api_incr_top(L);
  952. }
  953. /* else n == 1; nothing to do */
  954. lua_unlock(L);
  955. }
  956. LUA_API void lua_len (lua_State *L, int idx) {
  957. StkId t;
  958. lua_lock(L);
  959. t = index2addr(L, idx);
  960. luaV_objlen(L, L->top, t);
  961. api_incr_top(L);
  962. lua_unlock(L);
  963. }
  964. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  965. lua_Alloc f;
  966. lua_lock(L);
  967. if (ud) *ud = G(L)->ud;
  968. f = G(L)->frealloc;
  969. lua_unlock(L);
  970. return f;
  971. }
  972. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  973. lua_lock(L);
  974. G(L)->ud = ud;
  975. G(L)->frealloc = f;
  976. lua_unlock(L);
  977. }
  978. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  979. Udata *u;
  980. lua_lock(L);
  981. luaC_checkGC(L);
  982. u = luaS_newudata(L, size, NULL);
  983. setuvalue(L, L->top, u);
  984. api_incr_top(L);
  985. lua_unlock(L);
  986. return u + 1;
  987. }
  988. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  989. GCObject **owner) {
  990. switch (ttype(fi)) {
  991. case LUA_TCCL: { /* C closure */
  992. CClosure *f = clCvalue(fi);
  993. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  994. *val = &f->upvalue[n-1];
  995. if (owner) *owner = obj2gco(f);
  996. return "";
  997. }
  998. case LUA_TLCL: { /* Lua closure */
  999. LClosure *f = clLvalue(fi);
  1000. TString *name;
  1001. Proto *p = f->p;
  1002. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1003. *val = f->upvals[n-1]->v;
  1004. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1005. name = p->upvalues[n-1].name;
  1006. return (name == NULL) ? "" : getstr(name);
  1007. }
  1008. default: return NULL; /* not a closure */
  1009. }
  1010. }
  1011. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1012. const char *name;
  1013. TValue *val = NULL; /* to avoid warnings */
  1014. lua_lock(L);
  1015. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
  1016. if (name) {
  1017. setobj2s(L, L->top, val);
  1018. api_incr_top(L);
  1019. }
  1020. lua_unlock(L);
  1021. return name;
  1022. }
  1023. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1024. const char *name;
  1025. TValue *val = NULL; /* to avoid warnings */
  1026. GCObject *owner = NULL; /* to avoid warnings */
  1027. StkId fi;
  1028. lua_lock(L);
  1029. fi = index2addr(L, funcindex);
  1030. api_checknelems(L, 1);
  1031. name = aux_upvalue(fi, n, &val, &owner);
  1032. if (name) {
  1033. L->top--;
  1034. setobj(L, val, L->top);
  1035. luaC_barrier(L, owner, L->top);
  1036. }
  1037. lua_unlock(L);
  1038. return name;
  1039. }
  1040. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1041. LClosure *f;
  1042. StkId fi = index2addr(L, fidx);
  1043. api_check(L, ttisLclosure(fi), "Lua function expected");
  1044. f = clLvalue(fi);
  1045. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1046. if (pf) *pf = f;
  1047. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1048. }
  1049. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1050. StkId fi = index2addr(L, fidx);
  1051. switch (ttype(fi)) {
  1052. case LUA_TLCL: { /* lua closure */
  1053. return *getupvalref(L, fidx, n, NULL);
  1054. }
  1055. case LUA_TCCL: { /* C closure */
  1056. CClosure *f = clCvalue(fi);
  1057. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1058. return &f->upvalue[n - 1];
  1059. }
  1060. default: {
  1061. api_check(L, 0, "closure expected");
  1062. return NULL;
  1063. }
  1064. }
  1065. }
  1066. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1067. int fidx2, int n2) {
  1068. LClosure *f1;
  1069. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1070. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1071. *up1 = *up2;
  1072. luaC_objbarrier(L, f1, *up2);
  1073. }