lapi.c 22 KB

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