lapi.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*
  2. ** $Id: lapi.c,v 2.185 2013/07/05 14:29:51 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 (ttisuserdata(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) /* all other operations expect two operands */
  234. api_checknelems(L, 2);
  235. else { /* for unary minus, 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. setclCvalue(L, L->top, cl);
  478. }
  479. api_incr_top(L);
  480. lua_unlock(L);
  481. }
  482. LUA_API void lua_pushboolean (lua_State *L, int b) {
  483. lua_lock(L);
  484. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  485. api_incr_top(L);
  486. lua_unlock(L);
  487. }
  488. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  489. lua_lock(L);
  490. setpvalue(L->top, p);
  491. api_incr_top(L);
  492. lua_unlock(L);
  493. }
  494. LUA_API int lua_pushthread (lua_State *L) {
  495. lua_lock(L);
  496. setthvalue(L, L->top, L);
  497. api_incr_top(L);
  498. lua_unlock(L);
  499. return (G(L)->mainthread == L);
  500. }
  501. /*
  502. ** get functions (Lua -> stack)
  503. */
  504. LUA_API void lua_getglobal (lua_State *L, const char *var) {
  505. Table *reg = hvalue(&G(L)->l_registry);
  506. const TValue *gt; /* global table */
  507. lua_lock(L);
  508. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  509. setsvalue2s(L, L->top++, luaS_new(L, var));
  510. luaV_gettable(L, gt, L->top - 1, L->top - 1);
  511. lua_unlock(L);
  512. }
  513. LUA_API void lua_gettable (lua_State *L, int idx) {
  514. StkId t;
  515. lua_lock(L);
  516. t = index2addr(L, idx);
  517. luaV_gettable(L, t, L->top - 1, L->top - 1);
  518. lua_unlock(L);
  519. }
  520. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  521. StkId t;
  522. lua_lock(L);
  523. t = index2addr(L, idx);
  524. setsvalue2s(L, L->top, luaS_new(L, k));
  525. api_incr_top(L);
  526. luaV_gettable(L, t, L->top - 1, L->top - 1);
  527. lua_unlock(L);
  528. }
  529. LUA_API void lua_rawget (lua_State *L, int idx) {
  530. StkId t;
  531. lua_lock(L);
  532. t = index2addr(L, idx);
  533. api_check(L, ttistable(t), "table expected");
  534. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  535. lua_unlock(L);
  536. }
  537. LUA_API void lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  538. StkId t;
  539. lua_lock(L);
  540. t = index2addr(L, idx);
  541. api_check(L, ttistable(t), "table expected");
  542. setobj2s(L, L->top, luaH_getint(hvalue(t), n));
  543. api_incr_top(L);
  544. lua_unlock(L);
  545. }
  546. LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
  547. StkId t;
  548. TValue k;
  549. lua_lock(L);
  550. t = index2addr(L, idx);
  551. api_check(L, ttistable(t), "table expected");
  552. setpvalue(&k, cast(void *, p));
  553. setobj2s(L, L->top, luaH_get(hvalue(t), &k));
  554. api_incr_top(L);
  555. lua_unlock(L);
  556. }
  557. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  558. Table *t;
  559. lua_lock(L);
  560. luaC_checkGC(L);
  561. t = luaH_new(L);
  562. sethvalue(L, L->top, t);
  563. api_incr_top(L);
  564. if (narray > 0 || nrec > 0)
  565. luaH_resize(L, t, narray, nrec);
  566. lua_unlock(L);
  567. }
  568. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  569. const TValue *obj;
  570. Table *mt = NULL;
  571. int res;
  572. lua_lock(L);
  573. obj = index2addr(L, objindex);
  574. switch (ttnov(obj)) {
  575. case LUA_TTABLE:
  576. mt = hvalue(obj)->metatable;
  577. break;
  578. case LUA_TUSERDATA:
  579. mt = uvalue(obj)->metatable;
  580. break;
  581. default:
  582. mt = G(L)->mt[ttnov(obj)];
  583. break;
  584. }
  585. if (mt == NULL)
  586. res = 0;
  587. else {
  588. sethvalue(L, L->top, mt);
  589. api_incr_top(L);
  590. res = 1;
  591. }
  592. lua_unlock(L);
  593. return res;
  594. }
  595. LUA_API void lua_getuservalue (lua_State *L, int idx) {
  596. StkId o;
  597. lua_lock(L);
  598. o = index2addr(L, idx);
  599. api_check(L, ttisuserdata(o), "userdata expected");
  600. if (uvalue(o)->env) {
  601. sethvalue(L, L->top, uvalue(o)->env);
  602. } else
  603. setnilvalue(L->top);
  604. api_incr_top(L);
  605. lua_unlock(L);
  606. }
  607. /*
  608. ** set functions (stack -> Lua)
  609. */
  610. LUA_API void lua_setglobal (lua_State *L, const char *var) {
  611. Table *reg = hvalue(&G(L)->l_registry);
  612. const TValue *gt; /* global table */
  613. lua_lock(L);
  614. api_checknelems(L, 1);
  615. gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  616. setsvalue2s(L, L->top++, luaS_new(L, var));
  617. luaV_settable(L, gt, L->top - 1, L->top - 2);
  618. L->top -= 2; /* pop value and key */
  619. lua_unlock(L);
  620. }
  621. LUA_API void lua_settable (lua_State *L, int idx) {
  622. StkId t;
  623. lua_lock(L);
  624. api_checknelems(L, 2);
  625. t = index2addr(L, idx);
  626. luaV_settable(L, t, L->top - 2, L->top - 1);
  627. L->top -= 2; /* pop index and value */
  628. lua_unlock(L);
  629. }
  630. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  631. StkId t;
  632. lua_lock(L);
  633. api_checknelems(L, 1);
  634. t = index2addr(L, idx);
  635. setsvalue2s(L, L->top++, luaS_new(L, k));
  636. luaV_settable(L, t, L->top - 1, L->top - 2);
  637. L->top -= 2; /* pop value and key */
  638. lua_unlock(L);
  639. }
  640. LUA_API void lua_rawset (lua_State *L, int idx) {
  641. StkId t;
  642. lua_lock(L);
  643. api_checknelems(L, 2);
  644. t = index2addr(L, idx);
  645. api_check(L, ttistable(t), "table expected");
  646. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  647. invalidateTMcache(hvalue(t));
  648. luaC_barrierback(L, gcvalue(t), L->top-1);
  649. L->top -= 2;
  650. lua_unlock(L);
  651. }
  652. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  653. StkId t;
  654. lua_lock(L);
  655. api_checknelems(L, 1);
  656. t = index2addr(L, idx);
  657. api_check(L, ttistable(t), "table expected");
  658. luaH_setint(L, hvalue(t), n, L->top - 1);
  659. luaC_barrierback(L, gcvalue(t), L->top-1);
  660. L->top--;
  661. lua_unlock(L);
  662. }
  663. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  664. StkId t;
  665. TValue k;
  666. lua_lock(L);
  667. api_checknelems(L, 1);
  668. t = index2addr(L, idx);
  669. api_check(L, ttistable(t), "table expected");
  670. setpvalue(&k, cast(void *, p));
  671. setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
  672. luaC_barrierback(L, gcvalue(t), L->top - 1);
  673. L->top--;
  674. lua_unlock(L);
  675. }
  676. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  677. TValue *obj;
  678. Table *mt;
  679. lua_lock(L);
  680. api_checknelems(L, 1);
  681. obj = index2addr(L, objindex);
  682. if (ttisnil(L->top - 1))
  683. mt = NULL;
  684. else {
  685. api_check(L, ttistable(L->top - 1), "table expected");
  686. mt = hvalue(L->top - 1);
  687. }
  688. switch (ttnov(obj)) {
  689. case LUA_TTABLE: {
  690. hvalue(obj)->metatable = mt;
  691. if (mt) {
  692. luaC_objbarrierback(L, gcvalue(obj), mt);
  693. luaC_checkfinalizer(L, gcvalue(obj), mt);
  694. }
  695. break;
  696. }
  697. case LUA_TUSERDATA: {
  698. uvalue(obj)->metatable = mt;
  699. if (mt) {
  700. luaC_objbarrier(L, rawuvalue(obj), mt);
  701. luaC_checkfinalizer(L, gcvalue(obj), mt);
  702. }
  703. break;
  704. }
  705. default: {
  706. G(L)->mt[ttnov(obj)] = mt;
  707. break;
  708. }
  709. }
  710. L->top--;
  711. lua_unlock(L);
  712. return 1;
  713. }
  714. LUA_API void lua_setuservalue (lua_State *L, int idx) {
  715. StkId o;
  716. lua_lock(L);
  717. api_checknelems(L, 1);
  718. o = index2addr(L, idx);
  719. api_check(L, ttisuserdata(o), "userdata expected");
  720. if (ttisnil(L->top - 1))
  721. uvalue(o)->env = NULL;
  722. else {
  723. api_check(L, ttistable(L->top - 1), "table expected");
  724. uvalue(o)->env = hvalue(L->top - 1);
  725. luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
  726. }
  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) {
  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, 0);
  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. lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
  888. if (g->gcrunning)
  889. debt += g->GCdebt; /* include current debt */
  890. luaE_setdebt(g, debt);
  891. luaC_forcestep(L);
  892. if (g->gcstate == GCSpause) /* end of cycle? */
  893. res = 1; /* signal it */
  894. break;
  895. }
  896. case LUA_GCSETPAUSE: {
  897. res = g->gcpause;
  898. g->gcpause = data;
  899. break;
  900. }
  901. case LUA_GCSETSTEPMUL: {
  902. res = g->gcstepmul;
  903. g->gcstepmul = data;
  904. break;
  905. }
  906. case LUA_GCISRUNNING: {
  907. res = g->gcrunning;
  908. break;
  909. }
  910. default: res = -1; /* invalid option */
  911. }
  912. lua_unlock(L);
  913. return res;
  914. }
  915. /*
  916. ** miscellaneous functions
  917. */
  918. LUA_API int lua_error (lua_State *L) {
  919. lua_lock(L);
  920. api_checknelems(L, 1);
  921. luaG_errormsg(L);
  922. /* code unreachable; will unlock when control actually leaves the kernel */
  923. return 0; /* to avoid warnings */
  924. }
  925. LUA_API int lua_next (lua_State *L, int idx) {
  926. StkId t;
  927. int more;
  928. lua_lock(L);
  929. t = index2addr(L, idx);
  930. api_check(L, ttistable(t), "table expected");
  931. more = luaH_next(L, hvalue(t), L->top - 1);
  932. if (more) {
  933. api_incr_top(L);
  934. }
  935. else /* no more elements */
  936. L->top -= 1; /* remove key */
  937. lua_unlock(L);
  938. return more;
  939. }
  940. LUA_API void lua_concat (lua_State *L, int n) {
  941. lua_lock(L);
  942. api_checknelems(L, n);
  943. if (n >= 2) {
  944. luaC_checkGC(L);
  945. luaV_concat(L, n);
  946. }
  947. else if (n == 0) { /* push empty string */
  948. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  949. api_incr_top(L);
  950. }
  951. /* else n == 1; nothing to do */
  952. lua_unlock(L);
  953. }
  954. LUA_API void lua_len (lua_State *L, int idx) {
  955. StkId t;
  956. lua_lock(L);
  957. t = index2addr(L, idx);
  958. luaV_objlen(L, L->top, t);
  959. api_incr_top(L);
  960. lua_unlock(L);
  961. }
  962. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  963. lua_Alloc f;
  964. lua_lock(L);
  965. if (ud) *ud = G(L)->ud;
  966. f = G(L)->frealloc;
  967. lua_unlock(L);
  968. return f;
  969. }
  970. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  971. lua_lock(L);
  972. G(L)->ud = ud;
  973. G(L)->frealloc = f;
  974. lua_unlock(L);
  975. }
  976. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  977. Udata *u;
  978. lua_lock(L);
  979. luaC_checkGC(L);
  980. u = luaS_newudata(L, size, NULL);
  981. setuvalue(L, L->top, u);
  982. api_incr_top(L);
  983. lua_unlock(L);
  984. return u + 1;
  985. }
  986. static const char *aux_upvalue (StkId fi, int n, TValue **val,
  987. GCObject **owner) {
  988. switch (ttype(fi)) {
  989. case LUA_TCCL: { /* C closure */
  990. CClosure *f = clCvalue(fi);
  991. if (!(1 <= n && n <= f->nupvalues)) return NULL;
  992. *val = &f->upvalue[n-1];
  993. if (owner) *owner = obj2gco(f);
  994. return "";
  995. }
  996. case LUA_TLCL: { /* Lua closure */
  997. LClosure *f = clLvalue(fi);
  998. TString *name;
  999. Proto *p = f->p;
  1000. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  1001. *val = f->upvals[n-1]->v;
  1002. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1003. name = p->upvalues[n-1].name;
  1004. return (name == NULL) ? "" : getstr(name);
  1005. }
  1006. default: return NULL; /* not a closure */
  1007. }
  1008. }
  1009. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1010. const char *name;
  1011. TValue *val = NULL; /* to avoid warnings */
  1012. lua_lock(L);
  1013. name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
  1014. if (name) {
  1015. setobj2s(L, L->top, val);
  1016. api_incr_top(L);
  1017. }
  1018. lua_unlock(L);
  1019. return name;
  1020. }
  1021. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1022. const char *name;
  1023. TValue *val = NULL; /* to avoid warnings */
  1024. GCObject *owner = NULL; /* to avoid warnings */
  1025. StkId fi;
  1026. lua_lock(L);
  1027. fi = index2addr(L, funcindex);
  1028. api_checknelems(L, 1);
  1029. name = aux_upvalue(fi, n, &val, &owner);
  1030. if (name) {
  1031. L->top--;
  1032. setobj(L, val, L->top);
  1033. luaC_barrier(L, owner, L->top);
  1034. }
  1035. lua_unlock(L);
  1036. return name;
  1037. }
  1038. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1039. LClosure *f;
  1040. StkId fi = index2addr(L, fidx);
  1041. api_check(L, ttisLclosure(fi), "Lua function expected");
  1042. f = clLvalue(fi);
  1043. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1044. if (pf) *pf = f;
  1045. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1046. }
  1047. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1048. StkId fi = index2addr(L, fidx);
  1049. switch (ttype(fi)) {
  1050. case LUA_TLCL: { /* lua closure */
  1051. return *getupvalref(L, fidx, n, NULL);
  1052. }
  1053. case LUA_TCCL: { /* C closure */
  1054. CClosure *f = clCvalue(fi);
  1055. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1056. return &f->upvalue[n - 1];
  1057. }
  1058. default: {
  1059. api_check(L, 0, "closure expected");
  1060. return NULL;
  1061. }
  1062. }
  1063. }
  1064. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1065. int fidx2, int n2) {
  1066. LClosure *f1;
  1067. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1068. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1069. *up1 = *up2;
  1070. luaC_objbarrier(L, f1, *up2);
  1071. }