lapi.c 28 KB

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