lj_api.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. ** Public Lua/C API.
  3. ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #define lj_api_c
  9. #define LUA_CORE
  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_debug.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_func.h"
  17. #include "lj_udata.h"
  18. #include "lj_meta.h"
  19. #include "lj_state.h"
  20. #include "lj_bc.h"
  21. #include "lj_frame.h"
  22. #include "lj_trace.h"
  23. #include "lj_vm.h"
  24. #include "lj_lex.h"
  25. #include "lj_bcdump.h"
  26. #include "lj_parse.h"
  27. /* -- Common helper functions --------------------------------------------- */
  28. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
  29. #define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L))
  30. static TValue *index2adr(lua_State *L, int idx)
  31. {
  32. if (idx > 0) {
  33. TValue *o = L->base + (idx - 1);
  34. return o < L->top ? o : niltv(L);
  35. } else if (idx > LUA_REGISTRYINDEX) {
  36. api_check(L, idx != 0 && -idx <= L->top - L->base);
  37. return L->top + idx;
  38. } else if (idx == LUA_GLOBALSINDEX) {
  39. TValue *o = &G(L)->tmptv;
  40. settabV(L, o, tabref(L->env));
  41. return o;
  42. } else if (idx == LUA_REGISTRYINDEX) {
  43. return registry(L);
  44. } else {
  45. GCfunc *fn = curr_func(L);
  46. api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn));
  47. if (idx == LUA_ENVIRONINDEX) {
  48. TValue *o = &G(L)->tmptv;
  49. settabV(L, o, tabref(fn->c.env));
  50. return o;
  51. } else {
  52. idx = LUA_GLOBALSINDEX - idx;
  53. return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
  54. }
  55. }
  56. }
  57. static TValue *stkindex2adr(lua_State *L, int idx)
  58. {
  59. if (idx > 0) {
  60. TValue *o = L->base + (idx - 1);
  61. return o < L->top ? o : niltv(L);
  62. } else {
  63. api_check(L, idx != 0 && -idx <= L->top - L->base);
  64. return L->top + idx;
  65. }
  66. }
  67. static GCtab *getcurrenv(lua_State *L)
  68. {
  69. GCfunc *fn = curr_func(L);
  70. return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
  71. }
  72. /* -- Miscellaneous API functions ----------------------------------------- */
  73. LUA_API int lua_status(lua_State *L)
  74. {
  75. return L->status;
  76. }
  77. LUA_API int lua_checkstack(lua_State *L, int size)
  78. {
  79. if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
  80. return 0; /* Stack overflow. */
  81. } else if (size > 0) {
  82. lj_state_checkstack(L, (MSize)size);
  83. }
  84. return 1;
  85. }
  86. LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
  87. {
  88. if (!lua_checkstack(L, size))
  89. lj_err_callerv(L, LJ_ERR_STKOVM, msg);
  90. }
  91. LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
  92. {
  93. TValue *f, *t;
  94. if (from == to) return;
  95. api_checknelems(from, n);
  96. api_check(from, G(from) == G(to));
  97. lj_state_checkstack(to, (MSize)n);
  98. f = from->top;
  99. t = to->top = to->top + n;
  100. while (--n >= 0) copyTV(to, --t, --f);
  101. from->top = f;
  102. }
  103. /* -- Stack manipulation -------------------------------------------------- */
  104. LUA_API int lua_gettop(lua_State *L)
  105. {
  106. return (int)(L->top - L->base);
  107. }
  108. LUA_API void lua_settop(lua_State *L, int idx)
  109. {
  110. if (idx >= 0) {
  111. api_check(L, idx <= tvref(L->maxstack) - L->base);
  112. if (L->base + idx > L->top) {
  113. if (L->base + idx >= tvref(L->maxstack))
  114. lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
  115. do { setnilV(L->top++); } while (L->top < L->base + idx);
  116. } else {
  117. L->top = L->base + idx;
  118. }
  119. } else {
  120. api_check(L, -(idx+1) <= (L->top - L->base));
  121. L->top += idx+1; /* Shrinks top (idx < 0). */
  122. }
  123. }
  124. LUA_API void lua_remove(lua_State *L, int idx)
  125. {
  126. TValue *p = stkindex2adr(L, idx);
  127. api_checkvalidindex(L, p);
  128. while (++p < L->top) copyTV(L, p-1, p);
  129. L->top--;
  130. }
  131. LUA_API void lua_insert(lua_State *L, int idx)
  132. {
  133. TValue *q, *p = stkindex2adr(L, idx);
  134. api_checkvalidindex(L, p);
  135. for (q = L->top; q > p; q--) copyTV(L, q, q-1);
  136. copyTV(L, p, L->top);
  137. }
  138. LUA_API void lua_replace(lua_State *L, int idx)
  139. {
  140. api_checknelems(L, 1);
  141. if (idx == LUA_GLOBALSINDEX) {
  142. api_check(L, tvistab(L->top-1));
  143. /* NOBARRIER: A thread (i.e. L) is never black. */
  144. setgcref(L->env, obj2gco(tabV(L->top-1)));
  145. } else if (idx == LUA_ENVIRONINDEX) {
  146. GCfunc *fn = curr_func(L);
  147. if (fn->c.gct != ~LJ_TFUNC)
  148. lj_err_msg(L, LJ_ERR_NOENV);
  149. api_check(L, tvistab(L->top-1));
  150. setgcref(fn->c.env, obj2gco(tabV(L->top-1)));
  151. lj_gc_barrier(L, fn, L->top-1);
  152. } else {
  153. TValue *o = index2adr(L, idx);
  154. api_checkvalidindex(L, o);
  155. copyTV(L, o, L->top-1);
  156. if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */
  157. lj_gc_barrier(L, curr_func(L), L->top-1);
  158. }
  159. L->top--;
  160. }
  161. LUA_API void lua_pushvalue(lua_State *L, int idx)
  162. {
  163. copyTV(L, L->top, index2adr(L, idx));
  164. incr_top(L);
  165. }
  166. /* -- Stack getters ------------------------------------------------------- */
  167. LUA_API int lua_type(lua_State *L, int idx)
  168. {
  169. cTValue *o = index2adr(L, idx);
  170. if (tvisnumber(o)) {
  171. return LUA_TNUMBER;
  172. #if LJ_64
  173. } else if (tvislightud(o)) {
  174. return LUA_TLIGHTUSERDATA;
  175. #endif
  176. } else if (o == niltv(L)) {
  177. return LUA_TNONE;
  178. } else { /* Magic internal/external tag conversion. ORDER LJ_T */
  179. uint32_t t = ~itype(o);
  180. #if LJ_64
  181. int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
  182. #else
  183. int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
  184. #endif
  185. lua_assert(tt != LUA_TNIL || tvisnil(o));
  186. return tt;
  187. }
  188. }
  189. LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
  190. {
  191. if (lua_type(L, idx) != tt)
  192. lj_err_argt(L, idx, tt);
  193. }
  194. LUALIB_API void luaL_checkany(lua_State *L, int idx)
  195. {
  196. if (index2adr(L, idx) == niltv(L))
  197. lj_err_arg(L, idx, LJ_ERR_NOVAL);
  198. }
  199. LUA_API const char *lua_typename(lua_State *L, int t)
  200. {
  201. UNUSED(L);
  202. return lj_obj_typename[t+1];
  203. }
  204. LUA_API int lua_iscfunction(lua_State *L, int idx)
  205. {
  206. cTValue *o = index2adr(L, idx);
  207. return tvisfunc(o) && !isluafunc(funcV(o));
  208. }
  209. LUA_API int lua_isnumber(lua_State *L, int idx)
  210. {
  211. cTValue *o = index2adr(L, idx);
  212. TValue tmp;
  213. return (tvisnumber(o) || (tvisstr(o) && lj_str_tonumber(strV(o), &tmp)));
  214. }
  215. LUA_API int lua_isstring(lua_State *L, int idx)
  216. {
  217. cTValue *o = index2adr(L, idx);
  218. return (tvisstr(o) || tvisnumber(o));
  219. }
  220. LUA_API int lua_isuserdata(lua_State *L, int idx)
  221. {
  222. cTValue *o = index2adr(L, idx);
  223. return (tvisudata(o) || tvislightud(o));
  224. }
  225. LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
  226. {
  227. cTValue *o1 = index2adr(L, idx1);
  228. cTValue *o2 = index2adr(L, idx2);
  229. return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
  230. }
  231. LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
  232. {
  233. cTValue *o1 = index2adr(L, idx1);
  234. cTValue *o2 = index2adr(L, idx2);
  235. if (tvisint(o1) && tvisint(o2)) {
  236. return intV(o1) == intV(o2);
  237. } else if (tvisnumber(o1) && tvisnumber(o2)) {
  238. return numberVnum(o1) == numberVnum(o2);
  239. } else if (itype(o1) != itype(o2)) {
  240. return 0;
  241. } else if (tvispri(o1)) {
  242. return o1 != niltv(L) && o2 != niltv(L);
  243. #if LJ_64
  244. } else if (tvislightud(o1)) {
  245. return o1->u64 == o2->u64;
  246. #endif
  247. } else if (gcrefeq(o1->gcr, o2->gcr)) {
  248. return 1;
  249. } else if (!tvistabud(o1)) {
  250. return 0;
  251. } else {
  252. TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
  253. if ((uintptr_t)base <= 1) {
  254. return (int)(uintptr_t)base;
  255. } else {
  256. L->top = base+2;
  257. lj_vm_call(L, base, 1+1);
  258. L->top -= 2;
  259. return tvistruecond(L->top+1);
  260. }
  261. }
  262. }
  263. LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
  264. {
  265. cTValue *o1 = index2adr(L, idx1);
  266. cTValue *o2 = index2adr(L, idx2);
  267. if (o1 == niltv(L) || o2 == niltv(L)) {
  268. return 0;
  269. } else if (tvisint(o1) && tvisint(o2)) {
  270. return intV(o1) < intV(o2);
  271. } else if (tvisnumber(o1) && tvisnumber(o2)) {
  272. return numberVnum(o1) < numberVnum(o2);
  273. } else {
  274. TValue *base = lj_meta_comp(L, o1, o2, 0);
  275. if ((uintptr_t)base <= 1) {
  276. return (int)(uintptr_t)base;
  277. } else {
  278. L->top = base+2;
  279. lj_vm_call(L, base, 1+1);
  280. L->top -= 2;
  281. return tvistruecond(L->top+1);
  282. }
  283. }
  284. }
  285. LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
  286. {
  287. cTValue *o = index2adr(L, idx);
  288. TValue tmp;
  289. if (LJ_LIKELY(tvisnumber(o)))
  290. return numberVnum(o);
  291. else if (tvisstr(o) && lj_str_tonum(strV(o), &tmp))
  292. return numV(&tmp);
  293. else
  294. return 0;
  295. }
  296. LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
  297. {
  298. cTValue *o = index2adr(L, idx);
  299. TValue tmp;
  300. if (LJ_LIKELY(tvisnumber(o)))
  301. return numberVnum(o);
  302. else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp)))
  303. lj_err_argt(L, idx, LUA_TNUMBER);
  304. return numV(&tmp);
  305. }
  306. LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
  307. {
  308. cTValue *o = index2adr(L, idx);
  309. TValue tmp;
  310. if (LJ_LIKELY(tvisnumber(o)))
  311. return numberVnum(o);
  312. else if (tvisnil(o))
  313. return def;
  314. else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp)))
  315. lj_err_argt(L, idx, LUA_TNUMBER);
  316. return numV(&tmp);
  317. }
  318. LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
  319. {
  320. cTValue *o = index2adr(L, idx);
  321. TValue tmp;
  322. lua_Number n;
  323. if (LJ_LIKELY(tvisint(o))) {
  324. return intV(o);
  325. } else if (LJ_LIKELY(tvisnum(o))) {
  326. n = numV(o);
  327. } else {
  328. if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
  329. return 0;
  330. if (tvisint(&tmp))
  331. return (lua_Integer)intV(&tmp);
  332. n = numV(&tmp);
  333. }
  334. #if LJ_64
  335. return (lua_Integer)n;
  336. #else
  337. return lj_num2int(n);
  338. #endif
  339. }
  340. LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
  341. {
  342. cTValue *o = index2adr(L, idx);
  343. TValue tmp;
  344. lua_Number n;
  345. if (LJ_LIKELY(tvisint(o))) {
  346. return intV(o);
  347. } else if (LJ_LIKELY(tvisnum(o))) {
  348. n = numV(o);
  349. } else {
  350. if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
  351. lj_err_argt(L, idx, LUA_TNUMBER);
  352. if (tvisint(&tmp))
  353. return (lua_Integer)intV(&tmp);
  354. n = numV(&tmp);
  355. }
  356. #if LJ_64
  357. return (lua_Integer)n;
  358. #else
  359. return lj_num2int(n);
  360. #endif
  361. }
  362. LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
  363. {
  364. cTValue *o = index2adr(L, idx);
  365. TValue tmp;
  366. lua_Number n;
  367. if (LJ_LIKELY(tvisint(o))) {
  368. return intV(o);
  369. } else if (LJ_LIKELY(tvisnum(o))) {
  370. n = numV(o);
  371. } else if (tvisnil(o)) {
  372. return def;
  373. } else {
  374. if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
  375. lj_err_argt(L, idx, LUA_TNUMBER);
  376. if (tvisint(&tmp))
  377. return (lua_Integer)intV(&tmp);
  378. n = numV(&tmp);
  379. }
  380. #if LJ_64
  381. return (lua_Integer)n;
  382. #else
  383. return lj_num2int(n);
  384. #endif
  385. }
  386. LUA_API int lua_toboolean(lua_State *L, int idx)
  387. {
  388. cTValue *o = index2adr(L, idx);
  389. return tvistruecond(o);
  390. }
  391. LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
  392. {
  393. TValue *o = index2adr(L, idx);
  394. GCstr *s;
  395. if (LJ_LIKELY(tvisstr(o))) {
  396. s = strV(o);
  397. } else if (tvisnumber(o)) {
  398. lj_gc_check(L);
  399. o = index2adr(L, idx); /* GC may move the stack. */
  400. s = lj_str_fromnumber(L, o);
  401. setstrV(L, o, s);
  402. } else {
  403. if (len != NULL) *len = 0;
  404. return NULL;
  405. }
  406. if (len != NULL) *len = s->len;
  407. return strdata(s);
  408. }
  409. LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
  410. {
  411. TValue *o = index2adr(L, idx);
  412. GCstr *s;
  413. if (LJ_LIKELY(tvisstr(o))) {
  414. s = strV(o);
  415. } else if (tvisnumber(o)) {
  416. lj_gc_check(L);
  417. o = index2adr(L, idx); /* GC may move the stack. */
  418. s = lj_str_fromnumber(L, o);
  419. setstrV(L, o, s);
  420. } else {
  421. lj_err_argt(L, idx, LUA_TSTRING);
  422. }
  423. if (len != NULL) *len = s->len;
  424. return strdata(s);
  425. }
  426. LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
  427. const char *def, size_t *len)
  428. {
  429. TValue *o = index2adr(L, idx);
  430. GCstr *s;
  431. if (LJ_LIKELY(tvisstr(o))) {
  432. s = strV(o);
  433. } else if (tvisnil(o)) {
  434. if (len != NULL) *len = def ? strlen(def) : 0;
  435. return def;
  436. } else if (tvisnumber(o)) {
  437. lj_gc_check(L);
  438. o = index2adr(L, idx); /* GC may move the stack. */
  439. s = lj_str_fromnumber(L, o);
  440. setstrV(L, o, s);
  441. } else {
  442. lj_err_argt(L, idx, LUA_TSTRING);
  443. }
  444. if (len != NULL) *len = s->len;
  445. return strdata(s);
  446. }
  447. LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
  448. const char *const lst[])
  449. {
  450. ptrdiff_t i;
  451. const char *s = lua_tolstring(L, idx, NULL);
  452. if (s == NULL && (s = def) == NULL)
  453. lj_err_argt(L, idx, LUA_TSTRING);
  454. for (i = 0; lst[i]; i++)
  455. if (strcmp(lst[i], s) == 0)
  456. return (int)i;
  457. lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
  458. }
  459. LUA_API size_t lua_objlen(lua_State *L, int idx)
  460. {
  461. TValue *o = index2adr(L, idx);
  462. if (tvisstr(o)) {
  463. return strV(o)->len;
  464. } else if (tvistab(o)) {
  465. return (size_t)lj_tab_len(tabV(o));
  466. } else if (tvisudata(o)) {
  467. return udataV(o)->len;
  468. } else if (tvisnumber(o)) {
  469. GCstr *s = lj_str_fromnumber(L, o);
  470. setstrV(L, o, s);
  471. return s->len;
  472. } else {
  473. return 0;
  474. }
  475. }
  476. LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
  477. {
  478. cTValue *o = index2adr(L, idx);
  479. if (tvisfunc(o)) {
  480. BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
  481. if (op == BC_FUNCC || op == BC_FUNCCW)
  482. return funcV(o)->c.f;
  483. }
  484. return NULL;
  485. }
  486. LUA_API void *lua_touserdata(lua_State *L, int idx)
  487. {
  488. cTValue *o = index2adr(L, idx);
  489. if (tvisudata(o))
  490. return uddata(udataV(o));
  491. else if (tvislightud(o))
  492. return lightudV(o);
  493. else
  494. return NULL;
  495. }
  496. LUA_API lua_State *lua_tothread(lua_State *L, int idx)
  497. {
  498. cTValue *o = index2adr(L, idx);
  499. return (!tvisthread(o)) ? NULL : threadV(o);
  500. }
  501. LUA_API const void *lua_topointer(lua_State *L, int idx)
  502. {
  503. cTValue *o = index2adr(L, idx);
  504. if (tvisudata(o))
  505. return uddata(udataV(o));
  506. else if (tvislightud(o))
  507. return lightudV(o);
  508. else if (tviscdata(o))
  509. return cdataptr(cdataV(o));
  510. else if (tvisgcv(o))
  511. return gcV(o);
  512. else
  513. return NULL;
  514. }
  515. /* -- Stack setters (object creation) ------------------------------------- */
  516. LUA_API void lua_pushnil(lua_State *L)
  517. {
  518. setnilV(L->top);
  519. incr_top(L);
  520. }
  521. LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
  522. {
  523. setnumV(L->top, n);
  524. if (LJ_UNLIKELY(tvisnan(L->top)))
  525. setnanV(L->top); /* Canonicalize injected NaNs. */
  526. incr_top(L);
  527. }
  528. LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
  529. {
  530. setintptrV(L->top, n);
  531. incr_top(L);
  532. }
  533. LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
  534. {
  535. GCstr *s;
  536. lj_gc_check(L);
  537. s = lj_str_new(L, str, len);
  538. setstrV(L, L->top, s);
  539. incr_top(L);
  540. }
  541. LUA_API void lua_pushstring(lua_State *L, const char *str)
  542. {
  543. if (str == NULL) {
  544. setnilV(L->top);
  545. } else {
  546. GCstr *s;
  547. lj_gc_check(L);
  548. s = lj_str_newz(L, str);
  549. setstrV(L, L->top, s);
  550. }
  551. incr_top(L);
  552. }
  553. LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
  554. va_list argp)
  555. {
  556. lj_gc_check(L);
  557. return lj_str_pushvf(L, fmt, argp);
  558. }
  559. LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
  560. {
  561. const char *ret;
  562. va_list argp;
  563. lj_gc_check(L);
  564. va_start(argp, fmt);
  565. ret = lj_str_pushvf(L, fmt, argp);
  566. va_end(argp);
  567. return ret;
  568. }
  569. LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
  570. {
  571. GCfunc *fn;
  572. lj_gc_check(L);
  573. api_checknelems(L, n);
  574. fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
  575. fn->c.f = f;
  576. L->top -= n;
  577. while (n--)
  578. copyTV(L, &fn->c.upvalue[n], L->top+n);
  579. setfuncV(L, L->top, fn);
  580. lua_assert(iswhite(obj2gco(fn)));
  581. incr_top(L);
  582. }
  583. LUA_API void lua_pushboolean(lua_State *L, int b)
  584. {
  585. setboolV(L->top, (b != 0));
  586. incr_top(L);
  587. }
  588. LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
  589. {
  590. setlightudV(L->top, checklightudptr(L, p));
  591. incr_top(L);
  592. }
  593. LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
  594. {
  595. GCtab *t;
  596. lj_gc_check(L);
  597. t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec));
  598. settabV(L, L->top, t);
  599. incr_top(L);
  600. }
  601. LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
  602. {
  603. GCtab *regt = tabV(registry(L));
  604. TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
  605. if (tvisnil(tv)) {
  606. GCtab *mt = lj_tab_new(L, 0, 1);
  607. settabV(L, tv, mt);
  608. settabV(L, L->top++, mt);
  609. lj_gc_anybarriert(L, regt);
  610. return 1;
  611. } else {
  612. copyTV(L, L->top++, tv);
  613. return 0;
  614. }
  615. }
  616. LUA_API int lua_pushthread(lua_State *L)
  617. {
  618. setthreadV(L, L->top, L);
  619. incr_top(L);
  620. return (mainthread(G(L)) == L);
  621. }
  622. LUA_API lua_State *lua_newthread(lua_State *L)
  623. {
  624. lua_State *L1;
  625. lj_gc_check(L);
  626. L1 = lj_state_new(L);
  627. setthreadV(L, L->top, L1);
  628. incr_top(L);
  629. return L1;
  630. }
  631. LUA_API void *lua_newuserdata(lua_State *L, size_t size)
  632. {
  633. GCudata *ud;
  634. lj_gc_check(L);
  635. if (size > LJ_MAX_UDATA)
  636. lj_err_msg(L, LJ_ERR_UDATAOV);
  637. ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
  638. setudataV(L, L->top, ud);
  639. incr_top(L);
  640. return uddata(ud);
  641. }
  642. LUA_API void lua_concat(lua_State *L, int n)
  643. {
  644. api_checknelems(L, n);
  645. if (n >= 2) {
  646. n--;
  647. do {
  648. TValue *top = lj_meta_cat(L, L->top-1, -n);
  649. if (top == NULL) {
  650. L->top -= n;
  651. break;
  652. }
  653. n -= (int)(L->top - top);
  654. L->top = top+2;
  655. lj_vm_call(L, top, 1+1);
  656. L->top--;
  657. copyTV(L, L->top-1, L->top);
  658. } while (--n > 0);
  659. } else if (n == 0) { /* Push empty string. */
  660. setstrV(L, L->top, &G(L)->strempty);
  661. incr_top(L);
  662. }
  663. /* else n == 1: nothing to do. */
  664. }
  665. /* -- Object getters ------------------------------------------------------ */
  666. LUA_API void lua_gettable(lua_State *L, int idx)
  667. {
  668. cTValue *v, *t = index2adr(L, idx);
  669. api_checkvalidindex(L, t);
  670. v = lj_meta_tget(L, t, L->top-1);
  671. if (v == NULL) {
  672. L->top += 2;
  673. lj_vm_call(L, L->top-2, 1+1);
  674. L->top -= 2;
  675. v = L->top+1;
  676. }
  677. copyTV(L, L->top-1, v);
  678. }
  679. LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
  680. {
  681. cTValue *v, *t = index2adr(L, idx);
  682. TValue key;
  683. api_checkvalidindex(L, t);
  684. setstrV(L, &key, lj_str_newz(L, k));
  685. v = lj_meta_tget(L, t, &key);
  686. if (v == NULL) {
  687. L->top += 2;
  688. lj_vm_call(L, L->top-2, 1+1);
  689. L->top -= 2;
  690. v = L->top+1;
  691. }
  692. copyTV(L, L->top, v);
  693. incr_top(L);
  694. }
  695. LUA_API void lua_rawget(lua_State *L, int idx)
  696. {
  697. cTValue *t = index2adr(L, idx);
  698. api_check(L, tvistab(t));
  699. copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
  700. }
  701. LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
  702. {
  703. cTValue *v, *t = index2adr(L, idx);
  704. api_check(L, tvistab(t));
  705. v = lj_tab_getint(tabV(t), n);
  706. if (v) {
  707. copyTV(L, L->top, v);
  708. } else {
  709. setnilV(L->top);
  710. }
  711. incr_top(L);
  712. }
  713. LUA_API int lua_getmetatable(lua_State *L, int idx)
  714. {
  715. cTValue *o = index2adr(L, idx);
  716. GCtab *mt = NULL;
  717. if (tvistab(o))
  718. mt = tabref(tabV(o)->metatable);
  719. else if (tvisudata(o))
  720. mt = tabref(udataV(o)->metatable);
  721. else
  722. mt = tabref(basemt_obj(G(L), o));
  723. if (mt == NULL)
  724. return 0;
  725. settabV(L, L->top, mt);
  726. incr_top(L);
  727. return 1;
  728. }
  729. LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
  730. {
  731. if (lua_getmetatable(L, idx)) {
  732. cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
  733. if (tv && !tvisnil(tv)) {
  734. copyTV(L, L->top-1, tv);
  735. return 1;
  736. }
  737. L->top--;
  738. }
  739. return 0;
  740. }
  741. LUA_API void lua_getfenv(lua_State *L, int idx)
  742. {
  743. cTValue *o = index2adr(L, idx);
  744. api_checkvalidindex(L, o);
  745. if (tvisfunc(o)) {
  746. settabV(L, L->top, tabref(funcV(o)->c.env));
  747. } else if (tvisudata(o)) {
  748. settabV(L, L->top, tabref(udataV(o)->env));
  749. } else if (tvisthread(o)) {
  750. settabV(L, L->top, tabref(threadV(o)->env));
  751. } else {
  752. setnilV(L->top);
  753. }
  754. incr_top(L);
  755. }
  756. LUA_API int lua_next(lua_State *L, int idx)
  757. {
  758. cTValue *t = index2adr(L, idx);
  759. int more;
  760. api_check(L, tvistab(t));
  761. more = lj_tab_next(L, tabV(t), L->top-1);
  762. if (more) {
  763. incr_top(L); /* Return new key and value slot. */
  764. } else { /* End of traversal. */
  765. L->top--; /* Remove key slot. */
  766. }
  767. return more;
  768. }
  769. LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
  770. {
  771. TValue *val;
  772. const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);
  773. if (name) {
  774. copyTV(L, L->top, val);
  775. incr_top(L);
  776. }
  777. return name;
  778. }
  779. LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
  780. {
  781. cTValue *o = index2adr(L, idx);
  782. if (tvisudata(o)) {
  783. GCudata *ud = udataV(o);
  784. cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
  785. if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
  786. return uddata(ud);
  787. }
  788. lj_err_argtype(L, idx, tname);
  789. return NULL; /* unreachable */
  790. }
  791. /* -- Object setters ------------------------------------------------------ */
  792. LUA_API void lua_settable(lua_State *L, int idx)
  793. {
  794. TValue *o;
  795. cTValue *t = index2adr(L, idx);
  796. api_checknelems(L, 2);
  797. api_checkvalidindex(L, t);
  798. o = lj_meta_tset(L, t, L->top-2);
  799. if (o) {
  800. /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  801. copyTV(L, o, L->top-1);
  802. L->top -= 2;
  803. } else {
  804. L->top += 3;
  805. copyTV(L, L->top-1, L->top-6);
  806. lj_vm_call(L, L->top-3, 0+1);
  807. L->top -= 3;
  808. }
  809. }
  810. LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
  811. {
  812. TValue *o;
  813. TValue key;
  814. cTValue *t = index2adr(L, idx);
  815. api_checknelems(L, 1);
  816. api_checkvalidindex(L, t);
  817. setstrV(L, &key, lj_str_newz(L, k));
  818. o = lj_meta_tset(L, t, &key);
  819. if (o) {
  820. L->top--;
  821. /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  822. copyTV(L, o, L->top);
  823. } else {
  824. L->top += 3;
  825. copyTV(L, L->top-1, L->top-6);
  826. lj_vm_call(L, L->top-3, 0+1);
  827. L->top -= 2;
  828. }
  829. }
  830. LUA_API void lua_rawset(lua_State *L, int idx)
  831. {
  832. GCtab *t = tabV(index2adr(L, idx));
  833. TValue *dst, *key;
  834. api_checknelems(L, 2);
  835. key = L->top-2;
  836. dst = lj_tab_set(L, t, key);
  837. copyTV(L, dst, key+1);
  838. lj_gc_anybarriert(L, t);
  839. L->top = key;
  840. }
  841. LUA_API void lua_rawseti(lua_State *L, int idx, int n)
  842. {
  843. GCtab *t = tabV(index2adr(L, idx));
  844. TValue *dst, *src;
  845. api_checknelems(L, 1);
  846. dst = lj_tab_setint(L, t, n);
  847. src = L->top-1;
  848. copyTV(L, dst, src);
  849. lj_gc_barriert(L, t, dst);
  850. L->top = src;
  851. }
  852. LUA_API int lua_setmetatable(lua_State *L, int idx)
  853. {
  854. global_State *g;
  855. GCtab *mt;
  856. cTValue *o = index2adr(L, idx);
  857. api_checknelems(L, 1);
  858. api_checkvalidindex(L, o);
  859. if (tvisnil(L->top-1)) {
  860. mt = NULL;
  861. } else {
  862. api_check(L, tvistab(L->top-1));
  863. mt = tabV(L->top-1);
  864. }
  865. g = G(L);
  866. if (tvistab(o)) {
  867. setgcref(tabV(o)->metatable, obj2gco(mt));
  868. if (mt)
  869. lj_gc_objbarriert(L, tabV(o), mt);
  870. } else if (tvisudata(o)) {
  871. setgcref(udataV(o)->metatable, obj2gco(mt));
  872. if (mt)
  873. lj_gc_objbarrier(L, udataV(o), mt);
  874. } else {
  875. /* Flush cache, since traces specialize to basemt. But not during __gc. */
  876. if (lj_trace_flushall(L))
  877. lj_err_caller(L, LJ_ERR_NOGCMM);
  878. if (tvisbool(o)) {
  879. /* NOBARRIER: basemt is a GC root. */
  880. setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
  881. setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
  882. } else {
  883. /* NOBARRIER: basemt is a GC root. */
  884. setgcref(basemt_obj(g, o), obj2gco(mt));
  885. }
  886. }
  887. L->top--;
  888. return 1;
  889. }
  890. LUA_API int lua_setfenv(lua_State *L, int idx)
  891. {
  892. cTValue *o = index2adr(L, idx);
  893. GCtab *t;
  894. api_checknelems(L, 1);
  895. api_checkvalidindex(L, o);
  896. api_check(L, tvistab(L->top-1));
  897. t = tabV(L->top-1);
  898. if (tvisfunc(o)) {
  899. setgcref(funcV(o)->c.env, obj2gco(t));
  900. } else if (tvisudata(o)) {
  901. setgcref(udataV(o)->env, obj2gco(t));
  902. } else if (tvisthread(o)) {
  903. setgcref(threadV(o)->env, obj2gco(t));
  904. } else {
  905. L->top--;
  906. return 0;
  907. }
  908. lj_gc_objbarrier(L, gcV(o), t);
  909. L->top--;
  910. return 1;
  911. }
  912. LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
  913. {
  914. cTValue *f = index2adr(L, idx);
  915. TValue *val;
  916. const char *name;
  917. api_checknelems(L, 1);
  918. name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val);
  919. if (name) {
  920. L->top--;
  921. copyTV(L, val, L->top);
  922. lj_gc_barrier(L, funcV(f), L->top);
  923. }
  924. return name;
  925. }
  926. /* -- Calls --------------------------------------------------------------- */
  927. LUA_API void lua_call(lua_State *L, int nargs, int nresults)
  928. {
  929. api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  930. api_checknelems(L, nargs+1);
  931. lj_vm_call(L, L->top - nargs, nresults+1);
  932. }
  933. LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
  934. {
  935. global_State *g = G(L);
  936. uint8_t oldh = hook_save(g);
  937. ptrdiff_t ef;
  938. int status;
  939. api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  940. api_checknelems(L, nargs+1);
  941. if (errfunc == 0) {
  942. ef = 0;
  943. } else {
  944. cTValue *o = stkindex2adr(L, errfunc);
  945. api_checkvalidindex(L, o);
  946. ef = savestack(L, o);
  947. }
  948. status = lj_vm_pcall(L, L->top - nargs, nresults+1, ef);
  949. if (status) hook_restore(g, oldh);
  950. return status;
  951. }
  952. static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
  953. {
  954. GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
  955. fn->c.f = func;
  956. setfuncV(L, L->top, fn);
  957. setlightudV(L->top+1, checklightudptr(L, ud));
  958. cframe_nres(L->cframe) = 1+0; /* Zero results. */
  959. L->top += 2;
  960. return L->top-1; /* Now call the newly allocated C function. */
  961. }
  962. LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
  963. {
  964. global_State *g = G(L);
  965. uint8_t oldh = hook_save(g);
  966. int status;
  967. api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  968. status = lj_vm_cpcall(L, func, ud, cpcall);
  969. if (status) hook_restore(g, oldh);
  970. return status;
  971. }
  972. LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
  973. {
  974. if (luaL_getmetafield(L, idx, field)) {
  975. TValue *base = L->top--;
  976. copyTV(L, base, index2adr(L, idx));
  977. L->top = base+1;
  978. lj_vm_call(L, base, 1+1);
  979. return 1;
  980. }
  981. return 0;
  982. }
  983. /* -- Coroutine yield and resume ------------------------------------------ */
  984. LUA_API int lua_yield(lua_State *L, int nresults)
  985. {
  986. void *cf = L->cframe;
  987. global_State *g = G(L);
  988. if (cframe_canyield(cf)) {
  989. cf = cframe_raw(cf);
  990. if (!hook_active(g)) { /* Regular yield: move results down if needed. */
  991. cTValue *f = L->top - nresults;
  992. if (f > L->base) {
  993. TValue *t = L->base;
  994. while (--nresults >= 0) copyTV(L, t++, f++);
  995. L->top = t;
  996. }
  997. } else { /* Yield from hook: add a pseudo-frame. */
  998. TValue *top = L->top;
  999. hook_leave(g);
  1000. top->u64 = cframe_multres(cf);
  1001. setcont(top+1, lj_cont_hook);
  1002. setframe_pc(top+1, cframe_pc(cf)-1);
  1003. setframe_gc(top+2, obj2gco(L));
  1004. setframe_ftsz(top+2, (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT);
  1005. L->top = L->base = top+3;
  1006. }
  1007. #if LJ_TARGET_X64
  1008. lj_err_throw(L, LUA_YIELD);
  1009. #else
  1010. L->cframe = NULL;
  1011. L->status = LUA_YIELD;
  1012. lj_vm_unwind_c(cf, LUA_YIELD);
  1013. #endif
  1014. }
  1015. lj_err_msg(L, LJ_ERR_CYIELD);
  1016. return 0; /* unreachable */
  1017. }
  1018. LUA_API int lua_resume(lua_State *L, int nargs)
  1019. {
  1020. if (L->cframe == NULL && L->status <= LUA_YIELD)
  1021. return lj_vm_resume(L, L->top - nargs, 0, 0);
  1022. L->top = L->base;
  1023. setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
  1024. incr_top(L);
  1025. return LUA_ERRRUN;
  1026. }
  1027. /* -- Load and dump Lua code ---------------------------------------------- */
  1028. static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud)
  1029. {
  1030. LexState *ls = (LexState *)ud;
  1031. GCproto *pt;
  1032. GCfunc *fn;
  1033. UNUSED(dummy);
  1034. cframe_errfunc(L->cframe) = -1; /* Inherit error function. */
  1035. pt = lj_lex_setup(L, ls) ? lj_bcread(ls) : lj_parse(ls);
  1036. fn = lj_func_newL_empty(L, pt, tabref(L->env));
  1037. /* Don't combine above/below into one statement. */
  1038. setfuncV(L, L->top++, fn);
  1039. return NULL;
  1040. }
  1041. LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
  1042. const char *chunkname)
  1043. {
  1044. LexState ls;
  1045. int status;
  1046. ls.rfunc = reader;
  1047. ls.rdata = data;
  1048. ls.chunkarg = chunkname ? chunkname : "?";
  1049. lj_str_initbuf(&ls.sb);
  1050. status = lj_vm_cpcall(L, NULL, &ls, cpparser);
  1051. lj_lex_cleanup(L, &ls);
  1052. lj_gc_check(L);
  1053. return status;
  1054. }
  1055. LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
  1056. {
  1057. cTValue *o = L->top-1;
  1058. api_checknelems(L, 1);
  1059. if (tvisfunc(o) && isluafunc(funcV(o)))
  1060. return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0);
  1061. else
  1062. return 1;
  1063. }
  1064. /* -- GC and memory management -------------------------------------------- */
  1065. LUA_API int lua_gc(lua_State *L, int what, int data)
  1066. {
  1067. global_State *g = G(L);
  1068. int res = 0;
  1069. switch (what) {
  1070. case LUA_GCSTOP:
  1071. g->gc.threshold = LJ_MAX_MEM;
  1072. break;
  1073. case LUA_GCRESTART:
  1074. g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
  1075. break;
  1076. case LUA_GCCOLLECT:
  1077. lj_gc_fullgc(L);
  1078. break;
  1079. case LUA_GCCOUNT:
  1080. res = (int)(g->gc.total >> 10);
  1081. break;
  1082. case LUA_GCCOUNTB:
  1083. res = (int)(g->gc.total & 0x3ff);
  1084. break;
  1085. case LUA_GCSTEP: {
  1086. MSize a = (MSize)data << 10;
  1087. g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
  1088. while (g->gc.total >= g->gc.threshold)
  1089. if (lj_gc_step(L)) {
  1090. res = 1;
  1091. break;
  1092. }
  1093. break;
  1094. }
  1095. case LUA_GCSETPAUSE:
  1096. res = (int)(g->gc.pause);
  1097. g->gc.pause = (MSize)data;
  1098. break;
  1099. case LUA_GCSETSTEPMUL:
  1100. res = (int)(g->gc.stepmul);
  1101. g->gc.stepmul = (MSize)data;
  1102. break;
  1103. default:
  1104. res = -1; /* Invalid option. */
  1105. }
  1106. return res;
  1107. }
  1108. LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
  1109. {
  1110. global_State *g = G(L);
  1111. if (ud) *ud = g->allocd;
  1112. return g->allocf;
  1113. }
  1114. LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
  1115. {
  1116. global_State *g = G(L);
  1117. g->allocd = ud;
  1118. g->allocf = f;
  1119. }