lapi.c 29 KB

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