lapi.c 30 KB

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