lapi.c 29 KB

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