lapi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /*
  2. ** $Id: lapi.c,v 2.191 2013/12/04 12:15:22 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: {
  304. const lua_Number twop = (~(lua_Unsigned)0) + cast_num(1);
  305. lua_Number n = fltvalue(o);
  306. int neg = 0;
  307. n = l_floor(n);
  308. if (n < 0) {
  309. neg = 1;
  310. n = -n;
  311. }
  312. n = l_mathop(fmod)(n, twop);
  313. if (luai_numisnan(L,n)) /* not a number? */
  314. break; /* not an integer, too */
  315. res = cast_unsigned(n);
  316. if (neg) res = 0u - res;
  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. valnolocal(L->top + n); /* but needs 'local barrier' */
  479. }
  480. setclCvalue(L, L->top, cl);
  481. }
  482. api_incr_top(L);
  483. lua_unlock(L);
  484. }
  485. LUA_API void lua_pushboolean (lua_State *L, int b) {
  486. lua_lock(L);
  487. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  488. api_incr_top(L);
  489. lua_unlock(L);
  490. }
  491. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  492. lua_lock(L);
  493. setpvalue(L->top, p);
  494. api_incr_top(L);
  495. lua_unlock(L);
  496. }
  497. LUA_API int lua_pushthread (lua_State *L) {
  498. lua_lock(L);
  499. setthvalue(L, L->top, L);
  500. api_incr_top(L);
  501. lua_unlock(L);
  502. return (G(L)->mainthread == L);
  503. }
  504. /*
  505. ** get functions (Lua -> stack)
  506. */
  507. LUA_API void lua_getglobal (lua_State *L, const char *var) {
  508. Table *reg = hvalue(&G(L)->l_registry);
  509. const TValue *gt; /* global table */
  510. lua_lock(L);
  511. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  512. setsvalue2s(L, L->top++, luaS_new(L, var));
  513. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  514. lua_unlock(L);
  515. }
  516. LUA_API void 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. }
  523. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  524. StkId t;
  525. lua_lock(L);
  526. t = index2addr(L, idx);
  527. setsvalue2s(L, L->top, luaS_new(L, k));
  528. api_incr_top(L);
  529. luaV_gettable(L, t, L->top - 1, L->top - 1);
  530. lua_unlock(L);
  531. }
  532. LUA_API void lua_rawget (lua_State *L, int idx) {
  533. StkId t;
  534. lua_lock(L);
  535. t = index2addr(L, idx);
  536. api_check(L, ttistable(t), "table expected");
  537. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  538. lua_unlock(L);
  539. }
  540. LUA_API void lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  541. StkId t;
  542. lua_lock(L);
  543. t = index2addr(L, idx);
  544. api_check(L, ttistable(t), "table expected");
  545. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  546. api_incr_top(L);
  547. lua_unlock(L);
  548. }
  549. LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
  550. StkId t;
  551. TValue k;
  552. lua_lock(L);
  553. t = index2addr(L, idx);
  554. api_check(L, ttistable(t), "table expected");
  555. setpvalue(&k, cast(void *, p));
  556. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  557. api_incr_top(L);
  558. lua_unlock(L);
  559. }
  560. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  561. Table *t;
  562. lua_lock(L);
  563. luaC_checkGC(L);
  564. t = luaH_new(L);
  565. sethvalue(L, L->top, t);
  566. api_incr_top(L);
  567. if (narray > 0 || nrec > 0)
  568. luaH_resize(L, t, narray, nrec);
  569. lua_unlock(L);
  570. }
  571. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  572. const TValue *obj;
  573. Table *mt = NULL;
  574. int res;
  575. lua_lock(L);
  576. obj = index2addr(L, objindex);
  577. switch (ttnov(obj)) {
  578. case LUA_TTABLE:
  579. mt = hvalue(obj)->metatable;
  580. break;
  581. case LUA_TUSERDATA:
  582. mt = uvalue(obj)->metatable;
  583. break;
  584. default:
  585. mt = G(L)->mt[ttnov(obj)];
  586. break;
  587. }
  588. if (mt == NULL)
  589. res = 0;
  590. else {
  591. sethvalue(L, L->top, mt);
  592. api_incr_top(L);
  593. res = 1;
  594. }
  595. lua_unlock(L);
  596. return res;
  597. }
  598. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  599. StkId o;
  600. lua_lock(L);
  601. o = index2addr(L, idx);
  602. api_check(L, ttisfulluserdata(o), "full userdata expected");
  603. if (uvalue(o)->env) {
  604. sethvalue(L, L->top, uvalue(o)->env);
  605. } else
  606. setnilvalue(L->top);
  607. api_incr_top(L);
  608. lua_unlock(L);
  609. }
  610. /*
  611. ** set functions (stack -> Lua)
  612. */
  613. LUA_API void lua_setglobal (lua_State *L, const char *var) {
  614. Table *reg = hvalue(&G(L)->l_registry);
  615. const TValue *gt; /* global table */
  616. lua_lock(L);
  617. api_checknelems(L, 1);
  618. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  619. setsvalue2s(L, L->top++, luaS_new(L, var));
  620. luaV_settable(L, gt, L->top - 1, L->top - 2);
  621. L->top -= 2; /* pop value and key */
  622. lua_unlock(L);
  623. }
  624. LUA_API void lua_settable (lua_State *L, int idx) {
  625. StkId t;
  626. lua_lock(L);
  627. api_checknelems(L, 2);
  628. t = index2addr(L, idx);
  629. luaV_settable(L, t, L->top - 2, L->top - 1);
  630. L->top -= 2; /* pop index and value */
  631. lua_unlock(L);
  632. }
  633. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  634. StkId t;
  635. lua_lock(L);
  636. api_checknelems(L, 1);
  637. t = index2addr(L, idx);
  638. setsvalue2s(L, L->top++, luaS_new(L, k));
  639. luaV_settable(L, t, L->top - 1, L->top - 2);
  640. L->top -= 2; /* pop value and key */
  641. lua_unlock(L);
  642. }
  643. LUA_API void lua_rawset (lua_State *L, int idx) {
  644. StkId t;
  645. lua_lock(L);
  646. api_checknelems(L, 2);
  647. t = index2addr(L, idx);
  648. api_check(L, ttistable(t), "table expected");
  649. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  650. invalidateTMcache(hvalue(t));
  651. luaC_barrierback(L, gcvalue(t), L->top-1);
  652. L->top -= 2;
  653. lua_unlock(L);
  654. }
  655. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  656. StkId t;
  657. lua_lock(L);
  658. api_checknelems(L, 1);
  659. t = index2addr(L, idx);
  660. api_check(L, ttistable(t), "table expected");
  661. luaH_setint(L, hvalue(t), n, L->top - 1);
  662. luaC_barrierback(L, gcvalue(t), L->top-1);
  663. L->top--;
  664. lua_unlock(L);
  665. }
  666. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  667. StkId t;
  668. TValue k;
  669. lua_lock(L);
  670. api_checknelems(L, 1);
  671. t = index2addr(L, idx);
  672. api_check(L, ttistable(t), "table expected");
  673. setpvalue(&k, cast(void *, p));
  674. setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
  675. luaC_barrierback(L, gcvalue(t), L->top - 1);
  676. L->top--;
  677. lua_unlock(L);
  678. }
  679. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  680. TValue *obj;
  681. Table *mt;
  682. lua_lock(L);
  683. api_checknelems(L, 1);
  684. obj = index2addr(L, objindex);
  685. if (ttisnil(L->top - 1))
  686. mt = NULL;
  687. else {
  688. api_check(L, ttistable(L->top - 1), "table expected");
  689. mt = hvalue(L->top - 1);
  690. }
  691. switch (ttnov(obj)) {
  692. case LUA_TTABLE: {
  693. hvalue(obj)->metatable = mt;
  694. if (mt) {
  695. luaC_objbarrier(L, gcvalue(obj), mt);
  696. luaC_checkfinalizer(L, gcvalue(obj), mt);
  697. }
  698. break;
  699. }
  700. case LUA_TUSERDATA: {
  701. uvalue(obj)->metatable = mt;
  702. if (mt) {
  703. luaC_objbarrier(L, rawuvalue(obj), mt);
  704. luaC_checkfinalizer(L, gcvalue(obj), mt);
  705. }
  706. break;
  707. }
  708. default: {
  709. G(L)->mt[ttnov(obj)] = mt;
  710. if (mt) nolocal(obj2gco(mt));
  711. break;
  712. }
  713. }
  714. L->top--;
  715. lua_unlock(L);
  716. return 1;
  717. }
  718. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  719. StkId o;
  720. lua_lock(L);
  721. api_checknelems(L, 1);
  722. o = index2addr(L, idx);
  723. api_check(L, ttisfulluserdata(o), "full userdata expected");
  724. if (ttisnil(L->top - 1))
  725. uvalue(o)->env = NULL;
  726. else {
  727. api_check(L, ttistable(L->top - 1), "table expected");
  728. uvalue(o)->env = hvalue(L->top - 1);
  729. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  730. }
  731. L->top--;
  732. lua_unlock(L);
  733. }
  734. /*
  735. ** `load' and `call' functions (run Lua code)
  736. */
  737. #define checkresults(L,na,nr) \
  738. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  739. "results from function overflow current stack size")
  740. LUA_API int lua_getctx (lua_State *L, int *ctx) {
  741. if (L->ci->callstatus & CIST_YIELDED) {
  742. if (ctx) *ctx = L->ci->u.c.ctx;
  743. return L->ci->u.c.status;
  744. }
  745. else return LUA_OK;
  746. }
  747. LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
  748. lua_CFunction k) {
  749. StkId func;
  750. lua_lock(L);
  751. api_check(L, k == NULL || !isLua(L->ci),
  752. "cannot use continuations inside hooks");
  753. api_checknelems(L, nargs+1);
  754. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  755. checkresults(L, nargs, nresults);
  756. func = L->top - (nargs+1);
  757. if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
  758. L->ci->u.c.k = k; /* save continuation */
  759. L->ci->u.c.ctx = ctx; /* save context */
  760. luaD_call(L, func, nresults, 1); /* do the call */
  761. }
  762. else /* no continuation or no yieldable */
  763. luaD_call(L, func, nresults, 0); /* just do the call */
  764. adjustresults(L, nresults);
  765. lua_unlock(L);
  766. }
  767. /*
  768. ** Execute a protected call.
  769. */
  770. struct CallS { /* data to `f_call' */
  771. StkId func;
  772. int nresults;
  773. };
  774. static void f_call (lua_State *L, void *ud) {
  775. struct CallS *c = cast(struct CallS *, ud);
  776. luaD_call(L, c->func, c->nresults, 0);
  777. }
  778. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  779. int ctx, lua_CFunction k) {
  780. struct CallS c;
  781. int status;
  782. ptrdiff_t func;
  783. lua_lock(L);
  784. api_check(L, k == NULL || !isLua(L->ci),
  785. "cannot use continuations inside hooks");
  786. api_checknelems(L, nargs+1);
  787. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  788. checkresults(L, nargs, nresults);
  789. if (errfunc == 0)
  790. func = 0;
  791. else {
  792. StkId o = index2addr(L, errfunc);
  793. api_checkstackindex(L, errfunc, o);
  794. func = savestack(L, o);
  795. }
  796. c.func = L->top - (nargs+1); /* function to be called */
  797. if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
  798. c.nresults = nresults; /* do a 'conventional' protected call */
  799. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  800. }
  801. else { /* prepare continuation (call is already protected by 'resume') */
  802. CallInfo *ci = L->ci;
  803. ci->u.c.k = k; /* save continuation */
  804. ci->u.c.ctx = ctx; /* save context */
  805. /* save information for error recovery */
  806. ci->extra = savestack(L, c.func);
  807. ci->u.c.old_allowhook = L->allowhook;
  808. ci->u.c.old_errfunc = L->errfunc;
  809. L->errfunc = func;
  810. /* mark that function may do error recovery */
  811. ci->callstatus |= CIST_YPCALL;
  812. luaD_call(L, c.func, nresults, 1); /* do the call */
  813. ci->callstatus &= ~CIST_YPCALL;
  814. L->errfunc = ci->u.c.old_errfunc;
  815. status = LUA_OK; /* if it is here, there were no errors */
  816. }
  817. adjustresults(L, nresults);
  818. lua_unlock(L);
  819. return status;
  820. }
  821. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  822. const char *chunkname, const char *mode) {
  823. ZIO z;
  824. int status;
  825. lua_lock(L);
  826. if (!chunkname) chunkname = "?";
  827. luaZ_init(L, &z, reader, data);
  828. status = luaD_protectedparser(L, &z, chunkname, mode);
  829. if (status == LUA_OK) { /* no errors? */
  830. LClosure *f = clLvalue(L->top - 1); /* get newly created function */
  831. if (f->nupvalues == 1) { /* does it have one upvalue? */
  832. /* get global table from registry */
  833. Table *reg = hvalue(&G(L)->l_registry);
  834. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  835. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  836. setobj(L, f->upvals[0]->v, gt);
  837. luaC_barrier(L, f->upvals[0], gt);
  838. }
  839. }
  840. lua_unlock(L);
  841. return status;
  842. }
  843. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
  844. int status;
  845. TValue *o;
  846. lua_lock(L);
  847. api_checknelems(L, 1);
  848. o = L->top - 1;
  849. if (isLfunction(o))
  850. status = luaU_dump(L, getproto(o), writer, data, 0);
  851. else
  852. status = 1;
  853. lua_unlock(L);
  854. return status;
  855. }
  856. LUA_API int lua_status (lua_State *L) {
  857. return L->status;
  858. }
  859. /*
  860. ** Garbage-collection function
  861. */
  862. LUA_API int lua_gc (lua_State *L, int what, int data) {
  863. int res = 0;
  864. global_State *g;
  865. lua_lock(L);
  866. g = G(L);
  867. switch (what) {
  868. case LUA_GCSTOP: {
  869. g->gcrunning = 0;
  870. break;
  871. }
  872. case LUA_GCRESTART: {
  873. luaE_setdebt(g, 0);
  874. g->gcrunning = 1;
  875. break;
  876. }
  877. case LUA_GCCOLLECT: {
  878. luaC_fullgc(L, 0);
  879. break;
  880. }
  881. case LUA_GCCOUNT: {
  882. /* GC values are expressed in Kbytes: #bytes/2^10 */
  883. res = cast_int(gettotalbytes(g) >> 10);
  884. break;
  885. }
  886. case LUA_GCCOUNTB: {
  887. res = cast_int(gettotalbytes(g) & 0x3ff);
  888. break;
  889. }
  890. case LUA_GCSTEP: {
  891. lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
  892. if (g->gcrunning)
  893. debt += g->GCdebt; /* include current debt */
  894. luaE_setdebt(g, debt);
  895. luaC_forcestep(L);
  896. if (g->gcstate == GCSpause) /* end of cycle? */
  897. res = 1; /* signal it */
  898. break;
  899. }
  900. case LUA_GCSETPAUSE: {
  901. res = g->gcpause;
  902. g->gcpause = data;
  903. break;
  904. }
  905. case LUA_GCSETLOCALPAUSE: {
  906. res = g->gclocalpause;
  907. g->gclocalpause = data;
  908. break;
  909. }
  910. case LUA_GCSETSTEPMUL: {
  911. res = g->gcstepmul;
  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, NULL);
  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) ? "" : 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.op.touched = 1;
  1084. luaC_upvalbarrier(L, *up1);
  1085. }