lapi.c 23 KB

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