lapi.c 24 KB

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