lapi.c 29 KB

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