lapi.c 31 KB

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