lapi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /*
  2. ** $Id: lapi.c,v 2.200 2014/02/26 12:39:30 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. if (L->stack_last - L->top > size) /* stack large enough? */
  77. res = 1; /* yes; check is OK */
  78. else { /* no; need to grow stack */
  79. int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
  80. if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
  81. res = 0; /* no */
  82. else /* try to grow stack */
  83. res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
  84. }
  85. if (res && ci->top < L->top + size)
  86. ci->top = L->top + size; /* adjust frame top */
  87. lua_unlock(L);
  88. return res;
  89. }
  90. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  91. int i;
  92. if (from == to) return;
  93. lua_lock(to);
  94. api_checknelems(from, n);
  95. api_check(from, G(from) == G(to), "moving among independent states");
  96. api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
  97. from->top -= n;
  98. for (i = 0; i < n; i++) {
  99. setobj2s(to, to->top++, from->top + i);
  100. }
  101. lua_unlock(to);
  102. }
  103. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  104. lua_CFunction old;
  105. lua_lock(L);
  106. old = G(L)->panic;
  107. G(L)->panic = panicf;
  108. lua_unlock(L);
  109. return old;
  110. }
  111. LUA_API const lua_Number *lua_version (lua_State *L) {
  112. static const lua_Number version = LUA_VERSION_NUM;
  113. if (L == NULL) return &version;
  114. else return G(L)->version;
  115. }
  116. /*
  117. ** basic stack manipulation
  118. */
  119. /*
  120. ** convert an acceptable stack index into an absolute index
  121. */
  122. LUA_API int lua_absindex (lua_State *L, int idx) {
  123. return (idx > 0 || ispseudo(idx))
  124. ? idx
  125. : cast_int(L->top - L->ci->func + idx);
  126. }
  127. LUA_API int lua_gettop (lua_State *L) {
  128. return cast_int(L->top - (L->ci->func + 1));
  129. }
  130. LUA_API void lua_settop (lua_State *L, int idx) {
  131. StkId func = L->ci->func;
  132. lua_lock(L);
  133. if (idx >= 0) {
  134. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  135. while (L->top < (func + 1) + idx)
  136. setnilvalue(L->top++);
  137. L->top = (func + 1) + idx;
  138. }
  139. else {
  140. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  141. L->top += idx+1; /* `subtract' index (index is negative) */
  142. }
  143. lua_unlock(L);
  144. }
  145. LUA_API void lua_remove (lua_State *L, int idx) {
  146. StkId p;
  147. lua_lock(L);
  148. p = index2addr(L, idx);
  149. api_checkstackindex(L, idx, p);
  150. while (++p < L->top) setobjs2s(L, p-1, p);
  151. L->top--;
  152. lua_unlock(L);
  153. }
  154. LUA_API void lua_insert (lua_State *L, int idx) {
  155. StkId p;
  156. StkId q;
  157. lua_lock(L);
  158. p = index2addr(L, idx);
  159. api_checkstackindex(L, idx, p);
  160. for (q = L->top; q > p; q--) /* use L->top as a temporary */
  161. setobjs2s(L, q, q - 1);
  162. setobjs2s(L, p, L->top);
  163. lua_unlock(L);
  164. }
  165. static void moveto (lua_State *L, TValue *fr, int idx) {
  166. TValue *to = index2addr(L, idx);
  167. api_checkvalidindex(L, to);
  168. setobj(L, to, fr);
  169. if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
  170. luaC_barrier(L, clCvalue(L->ci->func), fr);
  171. /* LUA_REGISTRYINDEX does not need gc barrier
  172. (collector revisits it before finishing collection) */
  173. }
  174. LUA_API void lua_replace (lua_State *L, int idx) {
  175. lua_lock(L);
  176. api_checknelems(L, 1);
  177. moveto(L, L->top - 1, idx);
  178. L->top--;
  179. lua_unlock(L);
  180. }
  181. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  182. TValue *fr;
  183. lua_lock(L);
  184. fr = index2addr(L, fromidx);
  185. moveto(L, fr, toidx);
  186. lua_unlock(L);
  187. }
  188. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  189. lua_lock(L);
  190. setobj2s(L, L->top, index2addr(L, idx));
  191. api_incr_top(L);
  192. lua_unlock(L);
  193. }
  194. /*
  195. ** access functions (stack -> C)
  196. */
  197. LUA_API int lua_type (lua_State *L, int idx) {
  198. StkId o = index2addr(L, idx);
  199. return (isvalid(o) ? ttnov(o) : LUA_TNONE);
  200. }
  201. LUA_API const char *lua_typename (lua_State *L, int t) {
  202. UNUSED(L);
  203. return ttypename(t);
  204. }
  205. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  206. StkId o = index2addr(L, idx);
  207. return (ttislcf(o) || (ttisCclosure(o)));
  208. }
  209. LUA_API int lua_isinteger (lua_State *L, int idx) {
  210. StkId o = index2addr(L, idx);
  211. return ttisinteger(o);
  212. }
  213. LUA_API int lua_isnumber (lua_State *L, int idx) {
  214. lua_Number n;
  215. const TValue *o = index2addr(L, idx);
  216. return tonumber(o, &n);
  217. }
  218. LUA_API int lua_isstring (lua_State *L, int idx) {
  219. int t = lua_type(L, idx);
  220. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  221. }
  222. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  223. const TValue *o = index2addr(L, idx);
  224. return (ttisfulluserdata(o) || ttislightuserdata(o));
  225. }
  226. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  227. StkId o1 = index2addr(L, index1);
  228. StkId o2 = index2addr(L, index2);
  229. return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
  230. }
  231. LUA_API void lua_arith (lua_State *L, int op) {
  232. lua_lock(L);
  233. if (op != LUA_OPUNM && op != LUA_OPBNOT)
  234. api_checknelems(L, 2); /* all other operations expect two operands */
  235. else { /* for unary operations, add fake 2nd operand */
  236. api_checknelems(L, 1);
  237. setobjs2s(L, L->top, L->top - 1);
  238. L->top++;
  239. }
  240. /* first operand at top - 2, second at top - 1; result go to top - 2 */
  241. luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
  242. L->top--; /* remove second operand */
  243. lua_unlock(L);
  244. }
  245. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  246. StkId o1, o2;
  247. int i = 0;
  248. lua_lock(L); /* may call tag method */
  249. o1 = index2addr(L, index1);
  250. o2 = index2addr(L, index2);
  251. if (isvalid(o1) && isvalid(o2)) {
  252. switch (op) {
  253. case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
  254. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  255. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  256. default: api_check(L, 0, "invalid option");
  257. }
  258. }
  259. lua_unlock(L);
  260. return i;
  261. }
  262. LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) {
  263. lua_Integer i; lua_Number n;
  264. if (luaO_str2int(s, len, &i)) { /* try as an integer */
  265. setivalue(L->top, i);
  266. }
  267. else if (luaO_str2d(s, len, &n)) { /* else try as a float */
  268. setnvalue(L->top, n);
  269. }
  270. else
  271. return 0; /* conversion failed */
  272. api_incr_top(L);
  273. return 1;
  274. }
  275. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  276. lua_Number n;
  277. const TValue *o = index2addr(L, idx);
  278. int isnum = tonumber(o, &n);
  279. if (!isnum)
  280. n = 0; /* call to 'tonumber' may change 'n' even if it fails */
  281. if (pisnum) *pisnum = isnum;
  282. return n;
  283. }
  284. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  285. lua_Integer res;
  286. const TValue *o = index2addr(L, idx);
  287. int isnum = tointeger(o, &res);
  288. if (!isnum)
  289. res = 0; /* call to 'tointeger' may change 'n' even if it fails */
  290. if (pisnum) *pisnum = isnum;
  291. return res;
  292. }
  293. LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) {
  294. lua_Unsigned res = 0;
  295. const TValue *o = index2addr(L, idx);
  296. int isnum = 0;
  297. switch (ttype(o)) {
  298. case LUA_TNUMINT: {
  299. res = cast_unsigned(ivalue(o));
  300. isnum = 1;
  301. break;
  302. }
  303. case LUA_TNUMFLT: { /* compute floor(n) % 2^(numbits in an integer) */
  304. const lua_Number twop = cast_num(MAX_UINTEGER) + cast_num(1); /* 2^n */
  305. lua_Number n = fltvalue(o); /* get value */
  306. int neg = 0;
  307. n = l_floor(n); /* get its floor */
  308. if (n < 0) {
  309. neg = 1;
  310. n = -n; /* make 'n' positive, so that 'fmod' is the same as '%' */
  311. }
  312. n = l_mathop(fmod)(n, twop); /* n = n % 2^(numbits in an integer) */
  313. if (luai_numisnan(n)) /* not a number? */
  314. break; /* not an integer, too */
  315. res = cast_unsigned(n); /* 'n' now must fit in an unsigned */
  316. if (neg) res = 0u - res; /* back to negative, if needed */
  317. isnum = 1;
  318. break;
  319. }
  320. default: break;
  321. }
  322. if (pisnum) *pisnum = isnum;
  323. return res;
  324. }
  325. LUA_API int lua_toboolean (lua_State *L, int idx) {
  326. const TValue *o = index2addr(L, idx);
  327. return !l_isfalse(o);
  328. }
  329. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  330. StkId o = index2addr(L, idx);
  331. if (!ttisstring(o)) {
  332. lua_lock(L); /* `luaV_tostring' may create a new string */
  333. if (!luaV_tostring(L, o)) { /* conversion failed? */
  334. if (len != NULL) *len = 0;
  335. lua_unlock(L);
  336. return NULL;
  337. }
  338. luaC_checkGC(L);
  339. o = index2addr(L, idx); /* previous call may reallocate the stack */
  340. lua_unlock(L);
  341. }
  342. if (len != NULL) *len = tsvalue(o)->len;
  343. return svalue(o);
  344. }
  345. LUA_API size_t lua_rawlen (lua_State *L, int idx) {
  346. StkId o = index2addr(L, idx);
  347. switch (ttnov(o)) {
  348. case LUA_TSTRING: return tsvalue(o)->len;
  349. case LUA_TUSERDATA: return uvalue(o)->len;
  350. case LUA_TTABLE: return luaH_getn(hvalue(o));
  351. default: return 0;
  352. }
  353. }
  354. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  355. StkId o = index2addr(L, idx);
  356. if (ttislcf(o)) return fvalue(o);
  357. else if (ttisCclosure(o))
  358. return clCvalue(o)->f;
  359. else return NULL; /* not a C function */
  360. }
  361. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  362. StkId o = index2addr(L, idx);
  363. switch (ttnov(o)) {
  364. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  365. case LUA_TLIGHTUSERDATA: return pvalue(o);
  366. default: return NULL;
  367. }
  368. }
  369. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  370. StkId o = index2addr(L, idx);
  371. return (!ttisthread(o)) ? NULL : thvalue(o);
  372. }
  373. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  374. StkId o = index2addr(L, idx);
  375. switch (ttype(o)) {
  376. case LUA_TTABLE: return hvalue(o);
  377. case LUA_TLCL: return clLvalue(o);
  378. case LUA_TCCL: return clCvalue(o);
  379. case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
  380. case LUA_TTHREAD: return thvalue(o);
  381. case LUA_TUSERDATA:
  382. case LUA_TLIGHTUSERDATA:
  383. return lua_touserdata(L, idx);
  384. default: return NULL;
  385. }
  386. }
  387. /*
  388. ** push functions (C -> stack)
  389. */
  390. LUA_API void lua_pushnil (lua_State *L) {
  391. lua_lock(L);
  392. setnilvalue(L->top);
  393. api_incr_top(L);
  394. lua_unlock(L);
  395. }
  396. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  397. lua_lock(L);
  398. setnvalue(L->top, n);
  399. luai_checknum(L, L->top,
  400. luaG_runerror(L, "C API - attempt to push a signaling NaN"));
  401. api_incr_top(L);
  402. lua_unlock(L);
  403. }
  404. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  405. lua_lock(L);
  406. setivalue(L->top, n);
  407. api_incr_top(L);
  408. lua_unlock(L);
  409. }
  410. LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
  411. lua_lock(L);
  412. setivalue(L->top, cast_integer(u));
  413. api_incr_top(L);
  414. lua_unlock(L);
  415. }
  416. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  417. TString *ts;
  418. lua_lock(L);
  419. luaC_checkGC(L);
  420. ts = luaS_newlstr(L, s, len);
  421. setsvalue2s(L, L->top, ts);
  422. api_incr_top(L);
  423. lua_unlock(L);
  424. return getstr(ts);
  425. }
  426. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  427. if (s == NULL) {
  428. lua_pushnil(L);
  429. return NULL;
  430. }
  431. else {
  432. TString *ts;
  433. lua_lock(L);
  434. luaC_checkGC(L);
  435. ts = luaS_new(L, s);
  436. setsvalue2s(L, L->top, ts);
  437. api_incr_top(L);
  438. lua_unlock(L);
  439. return getstr(ts);
  440. }
  441. }
  442. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  443. va_list argp) {
  444. const char *ret;
  445. lua_lock(L);
  446. luaC_checkGC(L);
  447. ret = luaO_pushvfstring(L, fmt, argp);
  448. lua_unlock(L);
  449. return ret;
  450. }
  451. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  452. const char *ret;
  453. va_list argp;
  454. lua_lock(L);
  455. luaC_checkGC(L);
  456. va_start(argp, fmt);
  457. ret = luaO_pushvfstring(L, fmt, argp);
  458. va_end(argp);
  459. lua_unlock(L);
  460. return ret;
  461. }
  462. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  463. lua_lock(L);
  464. if (n == 0) {
  465. setfvalue(L->top, fn);
  466. }
  467. else {
  468. Closure *cl;
  469. api_checknelems(L, n);
  470. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  471. luaC_checkGC(L);
  472. cl = luaF_newCclosure(L, n);
  473. cl->c.f = fn;
  474. L->top -= n;
  475. while (n--) {
  476. setobj2n(L, &cl->c.upvalue[n], L->top + n);
  477. /* does not need barrier because closure is white */
  478. }
  479. setclCvalue(L, L->top, cl);
  480. }
  481. api_incr_top(L);
  482. lua_unlock(L);
  483. }
  484. LUA_API void lua_pushboolean (lua_State *L, int b) {
  485. lua_lock(L);
  486. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  487. api_incr_top(L);
  488. lua_unlock(L);
  489. }
  490. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  491. lua_lock(L);
  492. setpvalue(L->top, p);
  493. api_incr_top(L);
  494. lua_unlock(L);
  495. }
  496. LUA_API int lua_pushthread (lua_State *L) {
  497. lua_lock(L);
  498. setthvalue(L, L->top, L);
  499. api_incr_top(L);
  500. lua_unlock(L);
  501. return (G(L)->mainthread == L);
  502. }
  503. /*
  504. ** get functions (Lua -> stack)
  505. */
  506. LUA_API int lua_getglobal (lua_State *L, const char *var) {
  507. Table *reg = hvalue(&G(L)->l_registry);
  508. const TValue *gt; /* global table */
  509. lua_lock(L);
  510. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  511. setsvalue2s(L, L->top++, luaS_new(L, var));
  512. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  513. lua_unlock(L);
  514. return ttnov(L->top - 1);
  515. }
  516. LUA_API int lua_gettable (lua_State *L, int idx) {
  517. StkId t;
  518. lua_lock(L);
  519. t = index2addr(L, idx);
  520. luaV_gettable(L, t, L->top - 1, L->top - 1);
  521. lua_unlock(L);
  522. return ttnov(L->top - 1);
  523. }
  524. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  525. StkId t;
  526. lua_lock(L);
  527. t = index2addr(L, idx);
  528. setsvalue2s(L, L->top, luaS_new(L, k));
  529. api_incr_top(L);
  530. luaV_gettable(L, t, L->top - 1, L->top - 1);
  531. lua_unlock(L);
  532. return ttnov(L->top - 1);
  533. }
  534. LUA_API int lua_rawget (lua_State *L, int idx) {
  535. StkId t;
  536. lua_lock(L);
  537. t = index2addr(L, idx);
  538. api_check(L, ttistable(t), "table expected");
  539. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  540. lua_unlock(L);
  541. return ttnov(L->top - 1);
  542. }
  543. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  544. StkId t;
  545. lua_lock(L);
  546. t = index2addr(L, idx);
  547. api_check(L, ttistable(t), "table expected");
  548. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  549. api_incr_top(L);
  550. lua_unlock(L);
  551. return ttnov(L->top - 1);
  552. }
  553. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  554. StkId t;
  555. TValue k;
  556. lua_lock(L);
  557. t = index2addr(L, idx);
  558. api_check(L, ttistable(t), "table expected");
  559. setpvalue(&k, cast(void *, p));
  560. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  561. api_incr_top(L);
  562. lua_unlock(L);
  563. return ttnov(L->top - 1);
  564. }
  565. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  566. Table *t;
  567. lua_lock(L);
  568. luaC_checkGC(L);
  569. t = luaH_new(L);
  570. sethvalue(L, L->top, t);
  571. api_incr_top(L);
  572. if (narray > 0 || nrec > 0)
  573. luaH_resize(L, t, narray, nrec);
  574. lua_unlock(L);
  575. }
  576. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  577. const TValue *obj;
  578. Table *mt = NULL;
  579. int res;
  580. lua_lock(L);
  581. obj = index2addr(L, objindex);
  582. switch (ttnov(obj)) {
  583. case LUA_TTABLE:
  584. mt = hvalue(obj)->metatable;
  585. break;
  586. case LUA_TUSERDATA:
  587. mt = uvalue(obj)->metatable;
  588. break;
  589. default:
  590. mt = G(L)->mt[ttnov(obj)];
  591. break;
  592. }
  593. if (mt == NULL)
  594. res = 0;
  595. else {
  596. sethvalue(L, L->top, mt);
  597. api_incr_top(L);
  598. res = 1;
  599. }
  600. lua_unlock(L);
  601. return res;
  602. }
  603. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  604. StkId o;
  605. lua_lock(L);
  606. o = index2addr(L, idx);
  607. api_check(L, ttisfulluserdata(o), "full userdata expected");
  608. getuservalue(L, rawuvalue(o), L->top);
  609. api_incr_top(L);
  610. lua_unlock(L);
  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. }