lapi.c 27 KB

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