lapi.c 30 KB

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