lapi.c 24 KB

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