lapi.c 30 KB

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