lapi.c 30 KB

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