lapi.c 25 KB

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