lapi.c 26 KB

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