lapi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. /*
  2. ** $Id: lapi.c,v 2.208 2014/05/01 18:18:06 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 size_t lua_strtonum (lua_State *L, const char *s) {
  264. size_t sz = luaO_str2num(s, L->top);
  265. if (sz != 0)
  266. api_incr_top(L);
  267. return sz;
  268. }
  269. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  270. lua_Number n;
  271. const TValue *o = index2addr(L, idx);
  272. int isnum = tonumber(o, &n);
  273. if (!isnum)
  274. n = 0; /* call to 'tonumber' may change 'n' even if it fails */
  275. if (pisnum) *pisnum = isnum;
  276. return n;
  277. }
  278. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  279. lua_Integer res;
  280. const TValue *o = index2addr(L, idx);
  281. int isnum = tointeger(o, &res);
  282. if (!isnum)
  283. res = 0; /* call to 'tointeger' may change 'n' even if it fails */
  284. if (pisnum) *pisnum = isnum;
  285. return res;
  286. }
  287. LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) {
  288. lua_Unsigned res = 0;
  289. const TValue *o = index2addr(L, idx);
  290. int isnum = 0;
  291. switch (ttype(o)) {
  292. case LUA_TNUMINT: {
  293. res = l_castS2U(ivalue(o));
  294. isnum = 1;
  295. break;
  296. }
  297. case LUA_TNUMFLT: { /* compute floor(n) % 2^(numbits in an integer) */
  298. const lua_Number two2n = cast_num(LUA_MAXUNSIGNED) + cast_num(1);
  299. lua_Number n = fltvalue(o); /* get value */
  300. int neg = 0;
  301. n = l_floor(n); /* get its floor */
  302. if (n < 0) {
  303. neg = 1;
  304. n = -n; /* make 'n' positive, so that 'fmod' is the same as '%' */
  305. }
  306. n = l_mathop(fmod)(n, two2n); /* n = n % 2^(numbits in an integer) */
  307. if (luai_numisnan(n)) /* not a number? */
  308. break; /* not an integer, too */
  309. res = cast(lua_Unsigned, n); /* 'n' now must fit in an unsigned */
  310. if (neg) res = 0u - res; /* back to negative, if needed */
  311. isnum = 1;
  312. break;
  313. }
  314. default: break;
  315. }
  316. if (pisnum) *pisnum = isnum;
  317. return res;
  318. }
  319. LUA_API int lua_toboolean (lua_State *L, int idx) {
  320. const TValue *o = index2addr(L, idx);
  321. return !l_isfalse(o);
  322. }
  323. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  324. StkId o = index2addr(L, idx);
  325. if (!ttisstring(o)) {
  326. lua_lock(L); /* `luaV_tostring' may create a new string */
  327. if (!luaV_tostring(L, o)) { /* conversion failed? */
  328. if (len != NULL) *len = 0;
  329. lua_unlock(L);
  330. return NULL;
  331. }
  332. luaC_checkGC(L);
  333. o = index2addr(L, idx); /* previous call may reallocate the stack */
  334. lua_unlock(L);
  335. }
  336. if (len != NULL) *len = tsvalue(o)->len;
  337. return svalue(o);
  338. }
  339. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  340. StkId o = index2addr(L, idx);
  341. switch (ttnov(o)) {
  342. case LUA_TSTRING: return tsvalue(o)->len;
  343. case LUA_TUSERDATA: return uvalue(o)->len;
  344. case LUA_TTABLE: return luaH_getn(hvalue(o));
  345. default: return 0;
  346. }
  347. }
  348. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  349. StkId o = index2addr(L, idx);
  350. if (ttislcf(o)) return fvalue(o);
  351. else if (ttisCclosure(o))
  352. return clCvalue(o)->f;
  353. else return NULL; /* not a C function */
  354. }
  355. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  356. StkId o = index2addr(L, idx);
  357. switch (ttnov(o)) {
  358. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  359. case LUA_TLIGHTUSERDATA: return pvalue(o);
  360. default: return NULL;
  361. }
  362. }
  363. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  364. StkId o = index2addr(L, idx);
  365. return (!ttisthread(o)) ? NULL : thvalue(o);
  366. }
  367. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  368. StkId o = index2addr(L, idx);
  369. switch (ttype(o)) {
  370. case LUA_TTABLE: return hvalue(o);
  371. case LUA_TLCL: return clLvalue(o);
  372. case LUA_TCCL: return clCvalue(o);
  373. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  374. case LUA_TTHREAD: return thvalue(o);
  375. case LUA_TUSERDATA:
  376. case LUA_TLIGHTUSERDATA:
  377. return lua_touserdata(L, idx);
  378. default: return NULL;
  379. }
  380. }
  381. /*
  382. ** push functions (C -> stack)
  383. */
  384. LUA_API void lua_pushnil (lua_State *L) {
  385. lua_lock(L);
  386. setnilvalue(L->top);
  387. api_incr_top(L);
  388. lua_unlock(L);
  389. }
  390. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  391. lua_lock(L);
  392. setfltvalue(L->top, n);
  393. luai_checknum(L, L->top,
  394. luaG_runerror(L, "C API - attempt to push a signaling NaN"));
  395. api_incr_top(L);
  396. lua_unlock(L);
  397. }
  398. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  399. lua_lock(L);
  400. setivalue(L->top, n);
  401. api_incr_top(L);
  402. lua_unlock(L);
  403. }
  404. LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
  405. lua_lock(L);
  406. setivalue(L->top, l_castU2S(u));
  407. api_incr_top(L);
  408. lua_unlock(L);
  409. }
  410. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  411. TString *ts;
  412. lua_lock(L);
  413. luaC_checkGC(L);
  414. ts = luaS_newlstr(L, s, len);
  415. setsvalue2s(L, L->top, ts);
  416. api_incr_top(L);
  417. lua_unlock(L);
  418. return getstr(ts);
  419. }
  420. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  421. if (s == NULL) {
  422. lua_pushnil(L);
  423. return NULL;
  424. }
  425. else {
  426. TString *ts;
  427. lua_lock(L);
  428. luaC_checkGC(L);
  429. ts = luaS_new(L, s);
  430. setsvalue2s(L, L->top, ts);
  431. api_incr_top(L);
  432. lua_unlock(L);
  433. return getstr(ts);
  434. }
  435. }
  436. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  437. va_list argp) {
  438. const char *ret;
  439. lua_lock(L);
  440. luaC_checkGC(L);
  441. ret = luaO_pushvfstring(L, fmt, argp);
  442. lua_unlock(L);
  443. return ret;
  444. }
  445. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  446. const char *ret;
  447. va_list argp;
  448. lua_lock(L);
  449. luaC_checkGC(L);
  450. va_start(argp, fmt);
  451. ret = luaO_pushvfstring(L, fmt, argp);
  452. va_end(argp);
  453. lua_unlock(L);
  454. return ret;
  455. }
  456. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  457. lua_lock(L);
  458. if (n == 0) {
  459. setfvalue(L->top, fn);
  460. }
  461. else {
  462. Closure *cl;
  463. api_checknelems(L, n);
  464. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  465. luaC_checkGC(L);
  466. cl = luaF_newCclosure(L, n);
  467. cl->c.f = fn;
  468. L->top -= n;
  469. while (n--) {
  470. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  471. /* does not need barrier because closure is white */
  472. }
  473. setclCvalue(L, L->top, cl);
  474. }
  475. api_incr_top(L);
  476. lua_unlock(L);
  477. }
  478. LUA_API void lua_pushboolean (lua_State *L, int b) {
  479. lua_lock(L);
  480. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  481. api_incr_top(L);
  482. lua_unlock(L);
  483. }
  484. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  485. lua_lock(L);
  486. setpvalue(L->top, p);
  487. api_incr_top(L);
  488. lua_unlock(L);
  489. }
  490. LUA_API int lua_pushthread (lua_State *L) {
  491. lua_lock(L);
  492. setthvalue(L, L->top, L);
  493. api_incr_top(L);
  494. lua_unlock(L);
  495. return (G(L)->mainthread == L);
  496. }
  497. /*
  498. ** get functions (Lua -> stack)
  499. */
  500. LUA_API int lua_getglobal (lua_State *L, const char *var) {
  501. Table *reg = hvalue(&G(L)->l_registry);
  502. const TValue *gt; /* global table */
  503. lua_lock(L);
  504. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  505. setsvalue2s(L, L->top++, luaS_new(L, var));
  506. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  507. lua_unlock(L);
  508. return ttnov(L->top - 1);
  509. }
  510. LUA_API int lua_gettable (lua_State *L, int idx) {
  511. StkId t;
  512. lua_lock(L);
  513. t = index2addr(L, idx);
  514. luaV_gettable(L, t, L->top - 1, L->top - 1);
  515. lua_unlock(L);
  516. return ttnov(L->top - 1);
  517. }
  518. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  519. StkId t;
  520. lua_lock(L);
  521. t = index2addr(L, idx);
  522. setsvalue2s(L, L->top, luaS_new(L, k));
  523. api_incr_top(L);
  524. luaV_gettable(L, t, L->top - 1, L->top - 1);
  525. lua_unlock(L);
  526. return ttnov(L->top - 1);
  527. }
  528. LUA_API int lua_rawget (lua_State *L, int idx) {
  529. StkId t;
  530. lua_lock(L);
  531. t = index2addr(L, idx);
  532. api_check(L, ttistable(t), "table expected");
  533. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  534. lua_unlock(L);
  535. return ttnov(L->top - 1);
  536. }
  537. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  538. StkId t;
  539. lua_lock(L);
  540. t = index2addr(L, idx);
  541. api_check(L, ttistable(t), "table expected");
  542. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  543. api_incr_top(L);
  544. lua_unlock(L);
  545. return ttnov(L->top - 1);
  546. }
  547. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  548. StkId t;
  549. TValue k;
  550. lua_lock(L);
  551. t = index2addr(L, idx);
  552. api_check(L, ttistable(t), "table expected");
  553. setpvalue(&k, cast(void *, p));
  554. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  555. api_incr_top(L);
  556. lua_unlock(L);
  557. return ttnov(L->top - 1);
  558. }
  559. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  560. Table *t;
  561. lua_lock(L);
  562. luaC_checkGC(L);
  563. t = luaH_new(L);
  564. sethvalue(L, L->top, t);
  565. api_incr_top(L);
  566. if (narray > 0 || nrec > 0)
  567. luaH_resize(L, t, narray, nrec);
  568. lua_unlock(L);
  569. }
  570. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  571. const TValue *obj;
  572. Table *mt = NULL;
  573. int res;
  574. lua_lock(L);
  575. obj = index2addr(L, objindex);
  576. switch (ttnov(obj)) {
  577. case LUA_TTABLE:
  578. mt = hvalue(obj)->metatable;
  579. break;
  580. case LUA_TUSERDATA:
  581. mt = uvalue(obj)->metatable;
  582. break;
  583. default:
  584. mt = G(L)->mt[ttnov(obj)];
  585. break;
  586. }
  587. if (mt == NULL)
  588. res = 0;
  589. else {
  590. sethvalue(L, L->top, mt);
  591. api_incr_top(L);
  592. res = 1;
  593. }
  594. lua_unlock(L);
  595. return res;
  596. }
  597. LUA_API int lua_getuservalue (lua_State *L, int idx) {
  598. StkId o;
  599. lua_lock(L);
  600. o = index2addr(L, idx);
  601. api_check(L, ttisfulluserdata(o), "full userdata expected");
  602. getuservalue(L, rawuvalue(o), L->top);
  603. api_incr_top(L);
  604. lua_unlock(L);
  605. return ttnov(L->top - 1);
  606. }
  607. /*
  608. ** set functions (stack -> Lua)
  609. */
  610. LUA_API void lua_setglobal (lua_State *L, const char *var) {
  611. Table *reg = hvalue(&G(L)->l_registry);
  612. const TValue *gt; /* global table */
  613. lua_lock(L);
  614. api_checknelems(L, 1);
  615. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  616. setsvalue2s(L, L->top++, luaS_new(L, var));
  617. luaV_settable(L, gt, L->top - 1, L->top - 2);
  618. L->top -= 2; /* pop value and key */
  619. lua_unlock(L);
  620. }
  621. LUA_API void lua_settable (lua_State *L, int idx) {
  622. StkId t;
  623. lua_lock(L);
  624. api_checknelems(L, 2);
  625. t = index2addr(L, idx);
  626. luaV_settable(L, t, L->top - 2, L->top - 1);
  627. L->top -= 2; /* pop index and value */
  628. lua_unlock(L);
  629. }
  630. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  631. StkId t;
  632. lua_lock(L);
  633. api_checknelems(L, 1);
  634. t = index2addr(L, idx);
  635. setsvalue2s(L, L->top++, luaS_new(L, k));
  636. luaV_settable(L, t, L->top - 1, L->top - 2);
  637. L->top -= 2; /* pop value and key */
  638. lua_unlock(L);
  639. }
  640. LUA_API void lua_rawset (lua_State *L, int idx) {
  641. StkId t;
  642. lua_lock(L);
  643. api_checknelems(L, 2);
  644. t = index2addr(L, idx);
  645. api_check(L, ttistable(t), "table expected");
  646. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  647. invalidateTMcache(hvalue(t));
  648. luaC_barrierback(L, gcvalue(t), L->top-1);
  649. L->top -= 2;
  650. lua_unlock(L);
  651. }
  652. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  653. StkId t;
  654. lua_lock(L);
  655. api_checknelems(L, 1);
  656. t = index2addr(L, idx);
  657. api_check(L, ttistable(t), "table expected");
  658. luaH_setint(L, hvalue(t), n, L->top - 1);
  659. luaC_barrierback(L, gcvalue(t), L->top-1);
  660. L->top--;
  661. lua_unlock(L);
  662. }
  663. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  664. StkId t;
  665. TValue k;
  666. lua_lock(L);
  667. api_checknelems(L, 1);
  668. t = index2addr(L, idx);
  669. api_check(L, ttistable(t), "table expected");
  670. setpvalue(&k, cast(void *, p));
  671. setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
  672. luaC_barrierback(L, gcvalue(t), L->top - 1);
  673. L->top--;
  674. lua_unlock(L);
  675. }
  676. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  677. TValue *obj;
  678. Table *mt;
  679. lua_lock(L);
  680. api_checknelems(L, 1);
  681. obj = index2addr(L, objindex);
  682. if (ttisnil(L->top - 1))
  683. mt = NULL;
  684. else {
  685. api_check(L, ttistable(L->top - 1), "table expected");
  686. mt = hvalue(L->top - 1);
  687. }
  688. switch (ttnov(obj)) {
  689. case LUA_TTABLE: {
  690. hvalue(obj)->metatable = mt;
  691. if (mt) {
  692. luaC_objbarrier(L, gcvalue(obj), mt);
  693. luaC_checkfinalizer(L, gcvalue(obj), mt);
  694. }
  695. break;
  696. }
  697. case LUA_TUSERDATA: {
  698. uvalue(obj)->metatable = mt;
  699. if (mt) {
  700. luaC_objbarrier(L, rawuvalue(obj), mt);
  701. luaC_checkfinalizer(L, gcvalue(obj), mt);
  702. }
  703. break;
  704. }
  705. default: {
  706. G(L)->mt[ttnov(obj)] = mt;
  707. break;
  708. }
  709. }
  710. L->top--;
  711. lua_unlock(L);
  712. return 1;
  713. }
  714. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  715. StkId o;
  716. lua_lock(L);
  717. api_checknelems(L, 1);
  718. o = index2addr(L, idx);
  719. api_check(L, ttisfulluserdata(o), "full userdata expected");
  720. setuservalue(L, rawuvalue(o), L->top - 1);
  721. luaC_barrier(L, gcvalue(o), L->top - 1);
  722. L->top--;
  723. lua_unlock(L);
  724. }
  725. /*
  726. ** `load' and `call' functions (run Lua code)
  727. */
  728. #define checkresults(L,na,nr) \
  729. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  730. "results from function overflow current stack size")
  731. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  732. if (L->ci->callstatus & CIST_YIELDED) {
  733. if (ctx) *ctx = L->ci->u.c.ctx;
  734. return L->ci->u.c.status;
  735. }
  736. else return LUA_OK;
  737. }
  738. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  739. lua_CFunction k) {
  740. StkId func;
  741. lua_lock(L);
  742. api_check(L, k == NULL || !isLua(L->ci),
  743. "cannot use continuations inside hooks");
  744. api_checknelems(L, nargs+1);
  745. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  746. checkresults(L, nargs, nresults);
  747. func = L->top - (nargs+1);
  748. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  749. L->ci->u.c.k = k; /* save continuation */
  750. L->ci->u.c.ctx = ctx; /* save context */
  751. luaD_call(L, func, nresults, 1); /* do the call */
  752. }
  753. else /* no continuation or no yieldable */
  754. luaD_call(L, func, nresults, 0); /* just do the call */
  755. adjustresults(L, nresults);
  756. lua_unlock(L);
  757. }
  758. /*
  759. ** Execute a protected call.
  760. */
  761. struct CallS { /* data to `f_call' */
  762. StkId func;
  763. int nresults;
  764. };
  765. static void f_call (lua_State *L, void *ud) {
  766. struct CallS *c = cast(struct CallS *, ud);
  767. luaD_call(L, c->func, c->nresults, 0);
  768. }
  769. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  770. int ctx, lua_CFunction k) {
  771. struct CallS c;
  772. int status;
  773. ptrdiff_t func;
  774. lua_lock(L);
  775. api_check(L, k == NULL || !isLua(L->ci),
  776. "cannot use continuations inside hooks");
  777. api_checknelems(L, nargs+1);
  778. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  779. checkresults(L, nargs, nresults);
  780. if (errfunc == 0)
  781. func = 0;
  782. else {
  783. StkId o = index2addr(L, errfunc);
  784. api_checkstackindex(L, errfunc, o);
  785. func = savestack(L, o);
  786. }
  787. c.func = L->top - (nargs+1); /* function to be called */
  788. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  789. c.nresults = nresults; /* do a 'conventional' protected call */
  790. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  791. }
  792. else { /* prepare continuation (call is already protected by 'resume') */
  793. CallInfo *ci = L->ci;
  794. ci->u.c.k = k; /* save continuation */
  795. ci->u.c.ctx = ctx; /* save context */
  796. /* save information for error recovery */
  797. ci->extra = savestack(L, c.func);
  798. ci->u.c.old_allowhook = L->allowhook;
  799. ci->u.c.old_errfunc = L->errfunc;
  800. L->errfunc = func;
  801. /* mark that function may do error recovery */
  802. ci->callstatus |= CIST_YPCALL;
  803. luaD_call(L, c.func, nresults, 1); /* do the call */
  804. ci->callstatus &= ~CIST_YPCALL;
  805. L->errfunc = ci->u.c.old_errfunc;
  806. status = LUA_OK; /* if it is here, there were no errors */
  807. }
  808. adjustresults(L, nresults);
  809. lua_unlock(L);
  810. return status;
  811. }
  812. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  813. const char *chunkname, const char *mode) {
  814. ZIO z;
  815. int status;
  816. lua_lock(L);
  817. if (!chunkname) chunkname = "?";
  818. luaZ_init(L, &z, reader, data);
  819. status = luaD_protectedparser(L, &z, chunkname, mode);
  820. if (status == LUA_OK) { /* no errors? */
  821. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  822. if (f->nupvalues == 1) { /* does it have one upvalue? */
  823. /* get global table from registry */
  824. Table *reg = hvalue(&G(L)->l_registry);
  825. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  826. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  827. setobj(L, f->upvals[0]->v, gt);
  828. luaC_barrier(L, f->upvals[0], gt);
  829. }
  830. }
  831. lua_unlock(L);
  832. return status;
  833. }
  834. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  835. int status;
  836. TValue *o;
  837. lua_lock(L);
  838. api_checknelems(L, 1);
  839. o = L->top - 1;
  840. if (isLfunction(o))
  841. status = luaU_dump(L, getproto(o), writer, data, strip);
  842. else
  843. status = 1;
  844. lua_unlock(L);
  845. return status;
  846. }
  847. LUA_API int lua_status (lua_State *L) {
  848. return L->status;
  849. }
  850. /*
  851. ** Garbage-collection function
  852. */
  853. LUA_API int lua_gc (lua_State *L, int what, int data) {
  854. int res = 0;
  855. global_State *g;
  856. lua_lock(L);
  857. g = G(L);
  858. switch (what) {
  859. case LUA_GCSTOP: {
  860. g->gcrunning = 0;
  861. break;
  862. }
  863. case LUA_GCRESTART: {
  864. luaE_setdebt(g, 0);
  865. g->gcrunning = 1;
  866. break;
  867. }
  868. case LUA_GCCOLLECT: {
  869. luaC_fullgc(L, 0);
  870. break;
  871. }
  872. case LUA_GCCOUNT: {
  873. /* GC values are expressed in Kbytes: #bytes/2^10 */
  874. res = cast_int(gettotalbytes(g) >> 10);
  875. break;
  876. }
  877. case LUA_GCCOUNTB: {
  878. res = cast_int(gettotalbytes(g) & 0x3ff);
  879. break;
  880. }
  881. case LUA_GCSTEP: {
  882. l_mem debt = 1; /* =1 to signal that it did an actual step */
  883. int oldrunning = g->gcrunning;
  884. g->gcrunning = 1; /* force GC to run */
  885. if (data == 0) {
  886. luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
  887. luaC_step(L);
  888. }
  889. else { /* add 'data' to total debt */
  890. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  891. luaE_setdebt(g, debt);
  892. luaC_checkGC(L);
  893. }
  894. g->gcrunning = oldrunning; /* restore previous state */
  895. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  896. res = 1; /* signal it */
  897. break;
  898. }
  899. case LUA_GCSETPAUSE: {
  900. res = g->gcpause;
  901. g->gcpause = data;
  902. break;
  903. }
  904. case LUA_GCSETSTEPMUL: {
  905. res = g->gcstepmul;
  906. if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
  907. g->gcstepmul = data;
  908. break;
  909. }
  910. case LUA_GCISRUNNING: {
  911. res = g->gcrunning;
  912. break;
  913. }
  914. default: res = -1; /* invalid option */
  915. }
  916. lua_unlock(L);
  917. return res;
  918. }
  919. /*
  920. ** miscellaneous functions
  921. */
  922. LUA_API int lua_error (lua_State *L) {
  923. lua_lock(L);
  924. api_checknelems(L, 1);
  925. luaG_errormsg(L);
  926. /* code unreachable; will unlock when control actually leaves the kernel */
  927. return 0; /* to avoid warnings */
  928. }
  929. LUA_API int lua_next (lua_State *L, int idx) {
  930. StkId t;
  931. int more;
  932. lua_lock(L);
  933. t = index2addr(L, idx);
  934. api_check(L, ttistable(t), "table expected");
  935. more = luaH_next(L, hvalue(t), L->top - 1);
  936. if (more) {
  937. api_incr_top(L);
  938. }
  939. else /* no more elements */
  940. L->top -= 1; /* remove key */
  941. lua_unlock(L);
  942. return more;
  943. }
  944. LUA_API void lua_concat (lua_State *L, int n) {
  945. lua_lock(L);
  946. api_checknelems(L, n);
  947. if (n >= 2) {
  948. luaC_checkGC(L);
  949. luaV_concat(L, n);
  950. }
  951. else if (n == 0) { /* push empty string */
  952. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  953. api_incr_top(L);
  954. }
  955. /* else n == 1; nothing to do */
  956. lua_unlock(L);
  957. }
  958. LUA_API void lua_len (lua_State *L, int idx) {
  959. StkId t;
  960. lua_lock(L);
  961. t = index2addr(L, idx);
  962. luaV_objlen(L, L->top, t);
  963. api_incr_top(L);
  964. lua_unlock(L);
  965. }
  966. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  967. lua_Alloc f;
  968. lua_lock(L);
  969. if (ud) *ud = G(L)->ud;
  970. f = G(L)->frealloc;
  971. lua_unlock(L);
  972. return f;
  973. }
  974. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  975. lua_lock(L);
  976. G(L)->ud = ud;
  977. G(L)->frealloc = f;
  978. lua_unlock(L);
  979. }
  980. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  981. Udata *u;
  982. lua_lock(L);
  983. luaC_checkGC(L);
  984. u = luaS_newudata(L, size);
  985. setuvalue(L, L->top, u);
  986. api_incr_top(L);
  987. lua_unlock(L);
  988. return u + 1;
  989. }
  990. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  991. GCObject **owner, UpVal **uv) {
  992. switch (ttype(fi)) {
  993. case LUA_TCCL: { /* C closure */
  994. CClosure *f = clCvalue(fi);
  995. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  996. *val = &f->upvalue[n-1];
  997. if (owner) *owner = obj2gco(f);
  998. return "";
  999. }
  1000. case LUA_TLCL: { /* Lua closure */
  1001. LClosure *f = clLvalue(fi);
  1002. TString *name;
  1003. Proto *p = f->p;
  1004. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1005. *val = f->upvals[n-1]->v;
  1006. if (uv) *uv = f->upvals[n - 1];
  1007. name = p->upvalues[n-1].name;
  1008. return (name == NULL) ? "(*no name)" : getstr(name);
  1009. }
  1010. default: return NULL; /* not a closure */
  1011. }
  1012. }
  1013. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1014. const char *name;
  1015. TValue *val = NULL; /* to avoid warnings */
  1016. lua_lock(L);
  1017. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL);
  1018. if (name) {
  1019. setobj2s(L, L->top, val);
  1020. api_incr_top(L);
  1021. }
  1022. lua_unlock(L);
  1023. return name;
  1024. }
  1025. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1026. const char *name;
  1027. TValue *val = NULL; /* to avoid warnings */
  1028. GCObject *owner = NULL;
  1029. UpVal *uv = NULL;
  1030. StkId fi;
  1031. lua_lock(L);
  1032. fi = index2addr(L, funcindex);
  1033. api_checknelems(L, 1);
  1034. name = aux_upvalue(fi, n, &val, &owner, &uv);
  1035. if (name) {
  1036. L->top--;
  1037. setobj(L, val, L->top);
  1038. if (owner) { luaC_barrier(L, owner, L->top); }
  1039. else if (uv) { luaC_upvalbarrier(L, uv); }
  1040. }
  1041. lua_unlock(L);
  1042. return name;
  1043. }
  1044. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1045. LClosure *f;
  1046. StkId fi = index2addr(L, fidx);
  1047. api_check(L, ttisLclosure(fi), "Lua function expected");
  1048. f = clLvalue(fi);
  1049. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1050. if (pf) *pf = f;
  1051. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1052. }
  1053. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1054. StkId fi = index2addr(L, fidx);
  1055. switch (ttype(fi)) {
  1056. case LUA_TLCL: { /* lua closure */
  1057. return *getupvalref(L, fidx, n, NULL);
  1058. }
  1059. case LUA_TCCL: { /* C closure */
  1060. CClosure *f = clCvalue(fi);
  1061. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1062. return &f->upvalue[n - 1];
  1063. }
  1064. default: {
  1065. api_check(L, 0, "closure expected");
  1066. return NULL;
  1067. }
  1068. }
  1069. }
  1070. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1071. int fidx2, int n2) {
  1072. LClosure *f1;
  1073. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1074. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1075. luaC_upvdeccount(L, *up1);
  1076. *up1 = *up2;
  1077. (*up1)->refcount++;
  1078. if (upisopen(*up1)) (*up1)->u.open.touched = 1;
  1079. luaC_upvalbarrier(L, *up1);
  1080. }