lapi.c 26 KB

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