lapi.c 30 KB

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