lapi.c 24 KB

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