lapi.c 22 KB

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