lapi.c 27 KB

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