lapi.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*
  2. ** $Id: lapi.c,v 2.171 2013/03/16 21:10:18 roberto Exp $
  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. /* test for pseudo index */
  32. #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
  33. /* test for valid but not pseudo index */
  34. #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
  35. #define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
  36. #define api_checkstackindex(L, i, o) \
  37. api_check(L, isstackindex(i, o), "index not in the stack")
  38. static TValue *index2addr (lua_State *L, int idx) {
  39. CallInfo *ci = L->ci;
  40. if (idx > 0) {
  41. TValue *o = ci->func + idx;
  42. api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
  43. if (o >= L->top) return NONVALIDVALUE;
  44. else return o;
  45. }
  46. else if (!ispseudo(idx)) { /* negative index */
  47. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  48. return L->top + idx;
  49. }
  50. else if (idx == LUA_REGISTRYINDEX)
  51. return &G(L)->l_registry;
  52. else { /* upvalues */
  53. idx = LUA_REGISTRYINDEX - idx;
  54. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  55. if (ttislcf(ci->func)) /* light C function? */
  56. return NONVALIDVALUE; /* it has no upvalues */
  57. else {
  58. CClosure *func = clCvalue(ci->func);
  59. return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
  60. }
  61. }
  62. }
  63. /*
  64. ** to be called by 'lua_checkstack' in protected mode, to grow stack
  65. ** capturing memory errors
  66. */
  67. static void growstack (lua_State *L, void *ud) {
  68. int size = *(int *)ud;
  69. luaD_growstack(L, size);
  70. }
  71. LUA_API int lua_checkstack (lua_State *L, int size) {
  72. int res;
  73. CallInfo *ci = L->ci;
  74. lua_lock(L);
  75. if (L->stack_last - L->top > size) /* stack large enough? */
  76. res = 1; /* yes; check is OK */
  77. else { /* no; need to grow stack */
  78. int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
  79. if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
  80. res = 0; /* no */
  81. else /* try to grow stack */
  82. res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
  83. }
  84. if (res && ci->top < L->top + size)
  85. ci->top = L->top + size; /* adjust frame top */
  86. lua_unlock(L);
  87. return res;
  88. }
  89. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  90. int i;
  91. if (from == to) return;
  92. lua_lock(to);
  93. api_checknelems(from, n);
  94. api_check(from, G(from) == G(to), "moving among independent states");
  95. api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
  96. from->top -= n;
  97. for (i = 0; i < n; i++) {
  98. setobj2s(to, to->top++, from->top + i);
  99. }
  100. lua_unlock(to);
  101. }
  102. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  103. lua_CFunction old;
  104. lua_lock(L);
  105. old = G(L)->panic;
  106. G(L)->panic = panicf;
  107. lua_unlock(L);
  108. return old;
  109. }
  110. LUA_API const lua_Number *lua_version (lua_State *L) {
  111. static const lua_Number version = LUA_VERSION_NUM;
  112. if (L == NULL) return &version;
  113. else return G(L)->version;
  114. }
  115. /*
  116. ** basic stack manipulation
  117. */
  118. /*
  119. ** convert an acceptable stack index into an absolute index
  120. */
  121. LUA_API int lua_absindex (lua_State *L, int idx) {
  122. return (idx > 0 || ispseudo(idx))
  123. ? idx
  124. : cast_int(L->top - L->ci->func + idx);
  125. }
  126. LUA_API int lua_gettop (lua_State *L) {
  127. return cast_int(L->top - (L->ci->func + 1));
  128. }
  129. LUA_API void lua_settop (lua_State *L, int idx) {
  130. StkId func = L->ci->func;
  131. lua_lock(L);
  132. if (idx >= 0) {
  133. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  134. while (L->top < (func + 1) + idx)
  135. setnilvalue(L->top++);
  136. L->top = (func + 1) + idx;
  137. }
  138. else {
  139. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  140. L->top += idx+1; /* `subtract' index (index is negative) */
  141. }
  142. lua_unlock(L);
  143. }
  144. LUA_API void lua_remove (lua_State *L, int idx) {
  145. StkId p;
  146. lua_lock(L);
  147. p = index2addr(L, idx);
  148. api_checkstackindex(L, idx, p);
  149. while (++p < L->top) setobjs2s(L, p-1, p);
  150. L->top--;
  151. lua_unlock(L);
  152. }
  153. LUA_API void lua_insert (lua_State *L, int idx) {
  154. StkId p;
  155. StkId q;
  156. lua_lock(L);
  157. p = index2addr(L, idx);
  158. api_checkstackindex(L, idx, p);
  159. for (q = L->top; q > p; q--) /* use L->top as a temporary */
  160. setobjs2s(L, q, q - 1);
  161. setobjs2s(L, p, L->top);
  162. lua_unlock(L);
  163. }
  164. static void moveto (lua_State *L, TValue *fr, int idx) {
  165. TValue *to = index2addr(L, idx);
  166. api_checkvalidindex(L, to);
  167. setobj(L, to, fr);
  168. if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
  169. luaC_barrier(L, clCvalue(L->ci->func), fr);
  170. /* LUA_REGISTRYINDEX does not need gc barrier
  171. (collector revisits it before finishing collection) */
  172. }
  173. LUA_API void lua_replace (lua_State *L, int idx) {
  174. lua_lock(L);
  175. api_checknelems(L, 1);
  176. moveto(L, L->top - 1, idx);
  177. L->top--;
  178. lua_unlock(L);
  179. }
  180. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  181. TValue *fr;
  182. lua_lock(L);
  183. fr = index2addr(L, fromidx);
  184. moveto(L, fr, toidx);
  185. lua_unlock(L);
  186. }
  187. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  188. lua_lock(L);
  189. setobj2s(L, L->top, index2addr(L, idx));
  190. api_incr_top(L);
  191. lua_unlock(L);
  192. }
  193. /*
  194. ** access functions (stack -> C)
  195. */
  196. LUA_API int lua_type (lua_State *L, int idx) {
  197. StkId o = index2addr(L, idx);
  198. return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
  199. }
  200. LUA_API const char *lua_typename (lua_State *L, int t) {
  201. UNUSED(L);
  202. return ttypename(t);
  203. }
  204. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  205. StkId o = index2addr(L, idx);
  206. return (ttislcf(o) || (ttisCclosure(o)));
  207. }
  208. LUA_API int lua_isnumber (lua_State *L, int idx) {
  209. TValue n;
  210. const TValue *o = index2addr(L, idx);
  211. return tonumber(o, &n);
  212. }
  213. LUA_API int lua_isstring (lua_State *L, int idx) {
  214. int t = lua_type(L, idx);
  215. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  216. }
  217. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  218. const TValue *o = index2addr(L, idx);
  219. return (ttisuserdata(o) || ttislightuserdata(o));
  220. }
  221. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  222. StkId o1 = index2addr(L, index1);
  223. StkId o2 = index2addr(L, index2);
  224. return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
  225. }
  226. LUA_API void lua_arith (lua_State *L, int op) {
  227. StkId o1; /* 1st operand */
  228. StkId o2; /* 2nd operand */
  229. lua_lock(L);
  230. if (op != LUA_OPUNM) /* all other operations expect two operands */
  231. api_checknelems(L, 2);
  232. else { /* for unary minus, add fake 2nd operand */
  233. api_checknelems(L, 1);
  234. setobjs2s(L, L->top, L->top - 1);
  235. L->top++;
  236. }
  237. o1 = L->top - 2;
  238. o2 = L->top - 1;
  239. if (ttisnumber(o1) && ttisnumber(o2)) {
  240. setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
  241. }
  242. else
  243. luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
  244. L->top--;
  245. lua_unlock(L);
  246. }
  247. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  248. StkId o1, o2;
  249. int i = 0;
  250. lua_lock(L); /* may call tag method */
  251. o1 = index2addr(L, index1);
  252. o2 = index2addr(L, index2);
  253. if (isvalid(o1) && isvalid(o2)) {
  254. switch (op) {
  255. case LUA_OPEQ: i = equalobj(L, o1, o2); break;
  256. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  257. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  258. default: api_check(L, 0, "invalid option");
  259. }
  260. }
  261. lua_unlock(L);
  262. return i;
  263. }
  264. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
  265. TValue n;
  266. const TValue *o = index2addr(L, idx);
  267. if (tonumber(o, &n)) {
  268. if (isnum) *isnum = 1;
  269. return nvalue(o);
  270. }
  271. else {
  272. if (isnum) *isnum = 0;
  273. return 0;
  274. }
  275. }
  276. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
  277. TValue n;
  278. const TValue *o = index2addr(L, idx);
  279. if (tonumber(o, &n)) {
  280. lua_Integer res;
  281. lua_Number num = nvalue(o);
  282. lua_number2integer(res, num);
  283. if (isnum) *isnum = 1;
  284. return res;
  285. }
  286. else {
  287. if (isnum) *isnum = 0;
  288. return 0;
  289. }
  290. }
  291. LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
  292. TValue n;
  293. const TValue *o = index2addr(L, idx);
  294. if (tonumber(o, &n)) {
  295. lua_Unsigned res;
  296. lua_Number num = nvalue(o);
  297. lua_number2unsigned(res, num);
  298. if (isnum) *isnum = 1;
  299. return res;
  300. }
  301. else {
  302. if (isnum) *isnum = 0;
  303. return 0;
  304. }
  305. }
  306. LUA_API int lua_toboolean (lua_State *L, int idx) {
  307. const TValue *o = index2addr(L, idx);
  308. return !l_isfalse(o);
  309. }
  310. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  311. StkId o = index2addr(L, idx);
  312. if (!ttisstring(o)) {
  313. lua_lock(L); /* `luaV_tostring' may create a new string */
  314. if (!luaV_tostring(L, o)) { /* conversion failed? */
  315. if (len != NULL) *len = 0;
  316. lua_unlock(L);
  317. return NULL;
  318. }
  319. luaC_checkGC(L);
  320. o = index2addr(L, idx); /* previous call may reallocate the stack */
  321. lua_unlock(L);
  322. }
  323. if (len != NULL) *len = tsvalue(o)->len;
  324. return svalue(o);
  325. }
  326. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  327. StkId o = index2addr(L, idx);
  328. switch (ttypenv(o)) {
  329. case LUA_TSTRING: return tsvalue(o)->len;
  330. case LUA_TUSERDATA: return uvalue(o)->len;
  331. case LUA_TTABLE: return luaH_getn(hvalue(o));
  332. default: return 0;
  333. }
  334. }
  335. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  336. StkId o = index2addr(L, idx);
  337. if (ttislcf(o)) return fvalue(o);
  338. else if (ttisCclosure(o))
  339. return clCvalue(o)->f;
  340. else return NULL; /* not a C function */
  341. }
  342. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  343. StkId o = index2addr(L, idx);
  344. switch (ttypenv(o)) {
  345. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  346. case LUA_TLIGHTUSERDATA: return pvalue(o);
  347. default: return NULL;
  348. }
  349. }
  350. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  351. StkId o = index2addr(L, idx);
  352. return (!ttisthread(o)) ? NULL : thvalue(o);
  353. }
  354. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  355. StkId o = index2addr(L, idx);
  356. switch (ttype(o)) {
  357. case LUA_TTABLE: return hvalue(o);
  358. case LUA_TLCL: return clLvalue(o);
  359. case LUA_TCCL: return clCvalue(o);
  360. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  361. case LUA_TTHREAD: return thvalue(o);
  362. case LUA_TUSERDATA:
  363. case LUA_TLIGHTUSERDATA:
  364. return lua_touserdata(L, idx);
  365. default: return NULL;
  366. }
  367. }
  368. /*
  369. ** push functions (C -> stack)
  370. */
  371. LUA_API void lua_pushnil (lua_State *L) {
  372. lua_lock(L);
  373. setnilvalue(L->top);
  374. api_incr_top(L);
  375. lua_unlock(L);
  376. }
  377. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  378. lua_lock(L);
  379. setnvalue(L->top, n);
  380. luai_checknum(L, L->top,
  381. luaG_runerror(L, "C API - attempt to push a signaling NaN"));
  382. api_incr_top(L);
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  386. lua_lock(L);
  387. setnvalue(L->top, cast_num(n));
  388. api_incr_top(L);
  389. lua_unlock(L);
  390. }
  391. LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
  392. lua_Number n;
  393. lua_lock(L);
  394. n = lua_unsigned2number(u);
  395. setnvalue(L->top, n);
  396. api_incr_top(L);
  397. lua_unlock(L);
  398. }
  399. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  400. TString *ts;
  401. lua_lock(L);
  402. luaC_checkGC(L);
  403. ts = luaS_newlstr(L, s, len);
  404. setsvalue2s(L, L->top, ts);
  405. api_incr_top(L);
  406. lua_unlock(L);
  407. return getstr(ts);
  408. }
  409. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  410. if (s == NULL) {
  411. lua_pushnil(L);
  412. return NULL;
  413. }
  414. else {
  415. TString *ts;
  416. lua_lock(L);
  417. luaC_checkGC(L);
  418. ts = luaS_new(L, s);
  419. setsvalue2s(L, L->top, ts);
  420. api_incr_top(L);
  421. lua_unlock(L);
  422. return getstr(ts);
  423. }
  424. }
  425. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  426. va_list argp) {
  427. const char *ret;
  428. lua_lock(L);
  429. luaC_checkGC(L);
  430. ret = luaO_pushvfstring(L, fmt, argp);
  431. lua_unlock(L);
  432. return ret;
  433. }
  434. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  435. const char *ret;
  436. va_list argp;
  437. lua_lock(L);
  438. luaC_checkGC(L);
  439. va_start(argp, fmt);
  440. ret = luaO_pushvfstring(L, fmt, argp);
  441. va_end(argp);
  442. lua_unlock(L);
  443. return ret;
  444. }
  445. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  446. lua_lock(L);
  447. if (n == 0) {
  448. setfvalue(L->top, fn);
  449. }
  450. else {
  451. Closure *cl;
  452. api_checknelems(L, n);
  453. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  454. luaC_checkGC(L);
  455. cl = luaF_newCclosure(L, n);
  456. cl->c.f = fn;
  457. L->top -= n;
  458. while (n--)
  459. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  460. setclCvalue(L, L->top, cl);
  461. }
  462. api_incr_top(L);
  463. lua_unlock(L);
  464. }
  465. LUA_API void lua_pushboolean (lua_State *L, int b) {
  466. lua_lock(L);
  467. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  468. api_incr_top(L);
  469. lua_unlock(L);
  470. }
  471. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  472. lua_lock(L);
  473. setpvalue(L->top, p);
  474. api_incr_top(L);
  475. lua_unlock(L);
  476. }
  477. LUA_API int lua_pushthread (lua_State *L) {
  478. lua_lock(L);
  479. setthvalue(L, L->top, L);
  480. api_incr_top(L);
  481. lua_unlock(L);
  482. return (G(L)->mainthread == L);
  483. }
  484. /*
  485. ** get functions (Lua -> stack)
  486. */
  487. LUA_API void lua_getglobal (lua_State *L, const char *var) {
  488. Table *reg = hvalue(&G(L)->l_registry);
  489. const TValue *gt; /* global table */
  490. lua_lock(L);
  491. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  492. setsvalue2s(L, L->top++, luaS_new(L, var));
  493. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  494. lua_unlock(L);
  495. }
  496. LUA_API void lua_gettable (lua_State *L, int idx) {
  497. StkId t;
  498. lua_lock(L);
  499. t = index2addr(L, idx);
  500. luaV_gettable(L, t, L->top - 1, L->top - 1);
  501. lua_unlock(L);
  502. }
  503. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  504. StkId t;
  505. lua_lock(L);
  506. t = index2addr(L, idx);
  507. setsvalue2s(L, L->top, luaS_new(L, k));
  508. api_incr_top(L);
  509. luaV_gettable(L, t, L->top - 1, L->top - 1);
  510. lua_unlock(L);
  511. }
  512. LUA_API void lua_rawget (lua_State *L, int idx) {
  513. StkId t;
  514. lua_lock(L);
  515. t = index2addr(L, idx);
  516. api_check(L, ttistable(t), "table expected");
  517. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  518. lua_unlock(L);
  519. }
  520. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  521. StkId t;
  522. lua_lock(L);
  523. t = index2addr(L, idx);
  524. api_check(L, ttistable(t), "table expected");
  525. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  526. api_incr_top(L);
  527. lua_unlock(L);
  528. }
  529. LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
  530. StkId t;
  531. TValue k;
  532. lua_lock(L);
  533. t = index2addr(L, idx);
  534. api_check(L, ttistable(t), "table expected");
  535. setpvalue(&k, cast(void *, p));
  536. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  537. api_incr_top(L);
  538. lua_unlock(L);
  539. }
  540. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  541. Table *t;
  542. lua_lock(L);
  543. luaC_checkGC(L);
  544. t = luaH_new(L);
  545. sethvalue(L, L->top, t);
  546. api_incr_top(L);
  547. if (narray > 0 || nrec > 0)
  548. luaH_resize(L, t, narray, nrec);
  549. lua_unlock(L);
  550. }
  551. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  552. const TValue *obj;
  553. Table *mt = NULL;
  554. int res;
  555. lua_lock(L);
  556. obj = index2addr(L, objindex);
  557. switch (ttypenv(obj)) {
  558. case LUA_TTABLE:
  559. mt = hvalue(obj)->metatable;
  560. break;
  561. case LUA_TUSERDATA:
  562. mt = uvalue(obj)->metatable;
  563. break;
  564. default:
  565. mt = G(L)->mt[ttypenv(obj)];
  566. break;
  567. }
  568. if (mt == NULL)
  569. res = 0;
  570. else {
  571. sethvalue(L, L->top, mt);
  572. api_incr_top(L);
  573. res = 1;
  574. }
  575. lua_unlock(L);
  576. return res;
  577. }
  578. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  579. StkId o;
  580. lua_lock(L);
  581. o = index2addr(L, idx);
  582. api_check(L, ttisuserdata(o), "userdata expected");
  583. if (uvalue(o)->env) {
  584. sethvalue(L, L->top, uvalue(o)->env);
  585. } else
  586. setnilvalue(L->top);
  587. api_incr_top(L);
  588. lua_unlock(L);
  589. }
  590. /*
  591. ** set functions (stack -> Lua)
  592. */
  593. LUA_API void lua_setglobal (lua_State *L, const char *var) {
  594. Table *reg = hvalue(&G(L)->l_registry);
  595. const TValue *gt; /* global table */
  596. lua_lock(L);
  597. api_checknelems(L, 1);
  598. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  599. setsvalue2s(L, L->top++, luaS_new(L, var));
  600. luaV_settable(L, gt, L->top - 1, L->top - 2);
  601. L->top -= 2; /* pop value and key */
  602. lua_unlock(L);
  603. }
  604. LUA_API void lua_settable (lua_State *L, int idx) {
  605. StkId t;
  606. lua_lock(L);
  607. api_checknelems(L, 2);
  608. t = index2addr(L, idx);
  609. luaV_settable(L, t, L->top - 2, L->top - 1);
  610. L->top -= 2; /* pop index and value */
  611. lua_unlock(L);
  612. }
  613. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  614. StkId t;
  615. lua_lock(L);
  616. api_checknelems(L, 1);
  617. t = index2addr(L, idx);
  618. setsvalue2s(L, L->top++, luaS_new(L, k));
  619. luaV_settable(L, t, L->top - 1, L->top - 2);
  620. L->top -= 2; /* pop value and key */
  621. lua_unlock(L);
  622. }
  623. LUA_API void lua_rawset (lua_State *L, int idx) {
  624. StkId t;
  625. lua_lock(L);
  626. api_checknelems(L, 2);
  627. t = index2addr(L, idx);
  628. api_check(L, ttistable(t), "table expected");
  629. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  630. invalidateTMcache(hvalue(t));
  631. luaC_barrierback(L, gcvalue(t), L->top-1);
  632. L->top -= 2;
  633. lua_unlock(L);
  634. }
  635. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  636. StkId t;
  637. lua_lock(L);
  638. api_checknelems(L, 1);
  639. t = index2addr(L, idx);
  640. api_check(L, ttistable(t), "table expected");
  641. luaH_setint(L, hvalue(t), n, L->top - 1);
  642. luaC_barrierback(L, gcvalue(t), L->top-1);
  643. L->top--;
  644. lua_unlock(L);
  645. }
  646. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  647. StkId t;
  648. TValue k;
  649. lua_lock(L);
  650. api_checknelems(L, 1);
  651. t = index2addr(L, idx);
  652. api_check(L, ttistable(t), "table expected");
  653. setpvalue(&k, cast(void *, p));
  654. setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
  655. luaC_barrierback(L, gcvalue(t), L->top - 1);
  656. L->top--;
  657. lua_unlock(L);
  658. }
  659. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  660. TValue *obj;
  661. Table *mt;
  662. lua_lock(L);
  663. api_checknelems(L, 1);
  664. obj = index2addr(L, objindex);
  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. }
  678. break;
  679. }
  680. case LUA_TUSERDATA: {
  681. uvalue(obj)->metatable = mt;
  682. if (mt) {
  683. luaC_objbarrier(L, rawuvalue(obj), mt);
  684. luaC_checkfinalizer(L, gcvalue(obj), mt);
  685. }
  686. break;
  687. }
  688. default: {
  689. G(L)->mt[ttypenv(obj)] = mt;
  690. break;
  691. }
  692. }
  693. L->top--;
  694. lua_unlock(L);
  695. return 1;
  696. }
  697. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  698. StkId o;
  699. lua_lock(L);
  700. api_checknelems(L, 1);
  701. o = index2addr(L, idx);
  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_checkstackindex(L, errfunc, 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. /* code unreachable; will unlock when control actually leaves the kernel */
  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. }