lj_api.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. /*
  2. ** Public Lua/C API.
  3. ** Copyright (C) 2005-2016 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. // Modified by Aster Jian for Urho3D
  9. #define lj_api_c
  10. #define LUA_CORE
  11. #include "lj_obj.h"
  12. #include "lj_gc.h"
  13. #include "lj_err.h"
  14. #include "lj_debug.h"
  15. #include "lj_str.h"
  16. #include "lj_tab.h"
  17. #include "lj_func.h"
  18. #include "lj_udata.h"
  19. #include "lj_meta.h"
  20. #include "lj_state.h"
  21. #include "lj_bc.h"
  22. #include "lj_frame.h"
  23. #include "lj_trace.h"
  24. #include "lj_vm.h"
  25. #include "lj_strscan.h"
  26. #include "lj_strfmt.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 && !LJ_GC64
  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_strscan_number(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 && !LJ_GC64
  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+LJ_FR2;
  259. return tvistruecond(L->top+1+LJ_FR2);
  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+LJ_FR2;
  281. return tvistruecond(L->top+1+LJ_FR2);
  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_strscan_num(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_strscan_num(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_strscan_num(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_strscan_number(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_strscan_number(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_strscan_number(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_strfmt_number(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_strfmt_number(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_strfmt_number(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_strfmt_number(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. return lj_obj_ptr(index2adr(L, idx));
  504. }
  505. /* -- Stack setters (object creation) ------------------------------------- */
  506. LUA_API void lua_pushnil(lua_State *L)
  507. {
  508. setnilV(L->top);
  509. incr_top(L);
  510. }
  511. LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
  512. {
  513. setnumV(L->top, n);
  514. if (LJ_UNLIKELY(tvisnan(L->top)))
  515. setnanV(L->top); /* Canonicalize injected NaNs. */
  516. incr_top(L);
  517. }
  518. LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
  519. {
  520. setintptrV(L->top, n);
  521. incr_top(L);
  522. }
  523. LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
  524. {
  525. GCstr *s;
  526. lj_gc_check(L);
  527. s = lj_str_new(L, str, len);
  528. setstrV(L, L->top, s);
  529. incr_top(L);
  530. }
  531. LUA_API void lua_pushstring(lua_State *L, const char *str)
  532. {
  533. if (str == NULL) {
  534. setnilV(L->top);
  535. } else {
  536. GCstr *s;
  537. lj_gc_check(L);
  538. s = lj_str_newz(L, str);
  539. setstrV(L, L->top, s);
  540. }
  541. incr_top(L);
  542. }
  543. LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
  544. va_list argp)
  545. {
  546. lj_gc_check(L);
  547. return lj_strfmt_pushvf(L, fmt, argp);
  548. }
  549. LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
  550. {
  551. const char *ret;
  552. va_list argp;
  553. lj_gc_check(L);
  554. va_start(argp, fmt);
  555. ret = lj_strfmt_pushvf(L, fmt, argp);
  556. va_end(argp);
  557. return ret;
  558. }
  559. LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
  560. {
  561. GCfunc *fn;
  562. lj_gc_check(L);
  563. api_checknelems(L, n);
  564. fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
  565. fn->c.f = f;
  566. L->top -= n;
  567. while (n--)
  568. copyTV(L, &fn->c.upvalue[n], L->top+n);
  569. setfuncV(L, L->top, fn);
  570. lua_assert(iswhite(obj2gco(fn)));
  571. incr_top(L);
  572. }
  573. LUA_API void lua_pushboolean(lua_State *L, int b)
  574. {
  575. setboolV(L->top, (b != 0));
  576. incr_top(L);
  577. }
  578. LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
  579. {
  580. setlightudV(L->top, checklightudptr(L, p));
  581. incr_top(L);
  582. }
  583. LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
  584. {
  585. lj_gc_check(L);
  586. settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));
  587. incr_top(L);
  588. }
  589. LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
  590. {
  591. GCtab *regt = tabV(registry(L));
  592. TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
  593. if (tvisnil(tv)) {
  594. GCtab *mt = lj_tab_new(L, 0, 1);
  595. settabV(L, tv, mt);
  596. settabV(L, L->top++, mt);
  597. lj_gc_anybarriert(L, regt);
  598. return 1;
  599. } else {
  600. copyTV(L, L->top++, tv);
  601. return 0;
  602. }
  603. }
  604. LUA_API int lua_pushthread(lua_State *L)
  605. {
  606. setthreadV(L, L->top, L);
  607. incr_top(L);
  608. return (mainthread(G(L)) == L);
  609. }
  610. LUA_API lua_State *lua_newthread(lua_State *L)
  611. {
  612. lua_State *L1;
  613. lj_gc_check(L);
  614. L1 = lj_state_new(L);
  615. setthreadV(L, L->top, L1);
  616. incr_top(L);
  617. return L1;
  618. }
  619. // Urho3D
  620. LUA_API lua_State *lua_getmainthread(lua_State *L)
  621. {
  622. lua_State *L1;
  623. L1 = mainthread(G(L));
  624. return L1;
  625. }
  626. LUA_API void *lua_newuserdata(lua_State *L, size_t size)
  627. {
  628. GCudata *ud;
  629. lj_gc_check(L);
  630. if (size > LJ_MAX_UDATA)
  631. lj_err_msg(L, LJ_ERR_UDATAOV);
  632. ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
  633. setudataV(L, L->top, ud);
  634. incr_top(L);
  635. return uddata(ud);
  636. }
  637. LUA_API void lua_concat(lua_State *L, int n)
  638. {
  639. api_checknelems(L, n);
  640. if (n >= 2) {
  641. n--;
  642. do {
  643. TValue *top = lj_meta_cat(L, L->top-1, -n);
  644. if (top == NULL) {
  645. L->top -= n;
  646. break;
  647. }
  648. n -= (int)(L->top - top);
  649. L->top = top+2;
  650. lj_vm_call(L, top, 1+1);
  651. L->top -= 1+LJ_FR2;
  652. copyTV(L, L->top-1, L->top+LJ_FR2);
  653. } while (--n > 0);
  654. } else if (n == 0) { /* Push empty string. */
  655. setstrV(L, L->top, &G(L)->strempty);
  656. incr_top(L);
  657. }
  658. /* else n == 1: nothing to do. */
  659. }
  660. /* -- Object getters ------------------------------------------------------ */
  661. LUA_API void lua_gettable(lua_State *L, int idx)
  662. {
  663. cTValue *v, *t = index2adr(L, idx);
  664. api_checkvalidindex(L, t);
  665. v = lj_meta_tget(L, t, L->top-1);
  666. if (v == NULL) {
  667. L->top += 2;
  668. lj_vm_call(L, L->top-2, 1+1);
  669. L->top -= 2+LJ_FR2;
  670. v = L->top+1+LJ_FR2;
  671. }
  672. copyTV(L, L->top-1, v);
  673. }
  674. LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
  675. {
  676. cTValue *v, *t = index2adr(L, idx);
  677. TValue key;
  678. api_checkvalidindex(L, t);
  679. setstrV(L, &key, lj_str_newz(L, k));
  680. v = lj_meta_tget(L, t, &key);
  681. if (v == NULL) {
  682. L->top += 2;
  683. lj_vm_call(L, L->top-2, 1+1);
  684. L->top -= 2+LJ_FR2;
  685. v = L->top+1+LJ_FR2;
  686. }
  687. copyTV(L, L->top, v);
  688. incr_top(L);
  689. }
  690. LUA_API void lua_rawget(lua_State *L, int idx)
  691. {
  692. cTValue *t = index2adr(L, idx);
  693. api_check(L, tvistab(t));
  694. copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
  695. }
  696. LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
  697. {
  698. cTValue *v, *t = index2adr(L, idx);
  699. api_check(L, tvistab(t));
  700. v = lj_tab_getint(tabV(t), n);
  701. if (v) {
  702. copyTV(L, L->top, v);
  703. } else {
  704. setnilV(L->top);
  705. }
  706. incr_top(L);
  707. }
  708. LUA_API int lua_getmetatable(lua_State *L, int idx)
  709. {
  710. cTValue *o = index2adr(L, idx);
  711. GCtab *mt = NULL;
  712. if (tvistab(o))
  713. mt = tabref(tabV(o)->metatable);
  714. else if (tvisudata(o))
  715. mt = tabref(udataV(o)->metatable);
  716. else
  717. mt = tabref(basemt_obj(G(L), o));
  718. if (mt == NULL)
  719. return 0;
  720. settabV(L, L->top, mt);
  721. incr_top(L);
  722. return 1;
  723. }
  724. LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
  725. {
  726. if (lua_getmetatable(L, idx)) {
  727. cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
  728. if (tv && !tvisnil(tv)) {
  729. copyTV(L, L->top-1, tv);
  730. return 1;
  731. }
  732. L->top--;
  733. }
  734. return 0;
  735. }
  736. LUA_API void lua_getfenv(lua_State *L, int idx)
  737. {
  738. cTValue *o = index2adr(L, idx);
  739. api_checkvalidindex(L, o);
  740. if (tvisfunc(o)) {
  741. settabV(L, L->top, tabref(funcV(o)->c.env));
  742. } else if (tvisudata(o)) {
  743. settabV(L, L->top, tabref(udataV(o)->env));
  744. } else if (tvisthread(o)) {
  745. settabV(L, L->top, tabref(threadV(o)->env));
  746. } else {
  747. setnilV(L->top);
  748. }
  749. incr_top(L);
  750. }
  751. LUA_API int lua_next(lua_State *L, int idx)
  752. {
  753. cTValue *t = index2adr(L, idx);
  754. int more;
  755. api_check(L, tvistab(t));
  756. more = lj_tab_next(L, tabV(t), L->top-1);
  757. if (more) {
  758. incr_top(L); /* Return new key and value slot. */
  759. } else { /* End of traversal. */
  760. L->top--; /* Remove key slot. */
  761. }
  762. return more;
  763. }
  764. LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
  765. {
  766. TValue *val;
  767. const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);
  768. if (name) {
  769. copyTV(L, L->top, val);
  770. incr_top(L);
  771. }
  772. return name;
  773. }
  774. LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
  775. {
  776. GCfunc *fn = funcV(index2adr(L, idx));
  777. n--;
  778. api_check(L, (uint32_t)n < fn->l.nupvalues);
  779. return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
  780. (void *)&fn->c.upvalue[n];
  781. }
  782. LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
  783. {
  784. GCfunc *fn1 = funcV(index2adr(L, idx1));
  785. GCfunc *fn2 = funcV(index2adr(L, idx2));
  786. n1--; n2--;
  787. api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues);
  788. api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues);
  789. setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
  790. lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
  791. }
  792. LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
  793. {
  794. cTValue *o = index2adr(L, idx);
  795. if (tvisudata(o)) {
  796. GCudata *ud = udataV(o);
  797. cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
  798. if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
  799. return uddata(ud);
  800. }
  801. lj_err_argtype(L, idx, tname);
  802. return NULL; /* unreachable */
  803. }
  804. /* -- Object setters ------------------------------------------------------ */
  805. LUA_API void lua_settable(lua_State *L, int idx)
  806. {
  807. TValue *o;
  808. cTValue *t = index2adr(L, idx);
  809. api_checknelems(L, 2);
  810. api_checkvalidindex(L, t);
  811. o = lj_meta_tset(L, t, L->top-2);
  812. if (o) {
  813. /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  814. L->top -= 2;
  815. copyTV(L, o, L->top+1);
  816. } else {
  817. TValue *base = L->top;
  818. copyTV(L, base+2, base-3-2*LJ_FR2);
  819. L->top = base+3;
  820. lj_vm_call(L, base, 0+1);
  821. L->top -= 3+LJ_FR2;
  822. }
  823. }
  824. LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
  825. {
  826. TValue *o;
  827. TValue key;
  828. cTValue *t = index2adr(L, idx);
  829. api_checknelems(L, 1);
  830. api_checkvalidindex(L, t);
  831. setstrV(L, &key, lj_str_newz(L, k));
  832. o = lj_meta_tset(L, t, &key);
  833. if (o) {
  834. /* NOBARRIER: lj_meta_tset ensures the table is not black. */
  835. copyTV(L, o, --L->top);
  836. } else {
  837. TValue *base = L->top;
  838. copyTV(L, base+2, base-3-2*LJ_FR2);
  839. L->top = base+3;
  840. lj_vm_call(L, base, 0+1);
  841. L->top -= 2+LJ_FR2;
  842. }
  843. }
  844. LUA_API void lua_rawset(lua_State *L, int idx)
  845. {
  846. GCtab *t = tabV(index2adr(L, idx));
  847. TValue *dst, *key;
  848. api_checknelems(L, 2);
  849. key = L->top-2;
  850. dst = lj_tab_set(L, t, key);
  851. copyTV(L, dst, key+1);
  852. lj_gc_anybarriert(L, t);
  853. L->top = key;
  854. }
  855. LUA_API void lua_rawseti(lua_State *L, int idx, int n)
  856. {
  857. GCtab *t = tabV(index2adr(L, idx));
  858. TValue *dst, *src;
  859. api_checknelems(L, 1);
  860. dst = lj_tab_setint(L, t, n);
  861. src = L->top-1;
  862. copyTV(L, dst, src);
  863. lj_gc_barriert(L, t, dst);
  864. L->top = src;
  865. }
  866. LUA_API int lua_setmetatable(lua_State *L, int idx)
  867. {
  868. global_State *g;
  869. GCtab *mt;
  870. cTValue *o = index2adr(L, idx);
  871. api_checknelems(L, 1);
  872. api_checkvalidindex(L, o);
  873. if (tvisnil(L->top-1)) {
  874. mt = NULL;
  875. } else {
  876. api_check(L, tvistab(L->top-1));
  877. mt = tabV(L->top-1);
  878. }
  879. g = G(L);
  880. if (tvistab(o)) {
  881. setgcref(tabV(o)->metatable, obj2gco(mt));
  882. if (mt)
  883. lj_gc_objbarriert(L, tabV(o), mt);
  884. } else if (tvisudata(o)) {
  885. setgcref(udataV(o)->metatable, obj2gco(mt));
  886. if (mt)
  887. lj_gc_objbarrier(L, udataV(o), mt);
  888. } else {
  889. /* Flush cache, since traces specialize to basemt. But not during __gc. */
  890. if (lj_trace_flushall(L))
  891. lj_err_caller(L, LJ_ERR_NOGCMM);
  892. if (tvisbool(o)) {
  893. /* NOBARRIER: basemt is a GC root. */
  894. setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
  895. setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
  896. } else {
  897. /* NOBARRIER: basemt is a GC root. */
  898. setgcref(basemt_obj(g, o), obj2gco(mt));
  899. }
  900. }
  901. L->top--;
  902. return 1;
  903. }
  904. LUA_API int lua_setfenv(lua_State *L, int idx)
  905. {
  906. cTValue *o = index2adr(L, idx);
  907. GCtab *t;
  908. api_checknelems(L, 1);
  909. api_checkvalidindex(L, o);
  910. api_check(L, tvistab(L->top-1));
  911. t = tabV(L->top-1);
  912. if (tvisfunc(o)) {
  913. setgcref(funcV(o)->c.env, obj2gco(t));
  914. } else if (tvisudata(o)) {
  915. setgcref(udataV(o)->env, obj2gco(t));
  916. } else if (tvisthread(o)) {
  917. setgcref(threadV(o)->env, obj2gco(t));
  918. } else {
  919. L->top--;
  920. return 0;
  921. }
  922. lj_gc_objbarrier(L, gcV(o), t);
  923. L->top--;
  924. return 1;
  925. }
  926. LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
  927. {
  928. cTValue *f = index2adr(L, idx);
  929. TValue *val;
  930. const char *name;
  931. api_checknelems(L, 1);
  932. name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val);
  933. if (name) {
  934. L->top--;
  935. copyTV(L, val, L->top);
  936. lj_gc_barrier(L, funcV(f), L->top);
  937. }
  938. return name;
  939. }
  940. /* -- Calls --------------------------------------------------------------- */
  941. #if LJ_FR2
  942. static TValue *api_call_base(lua_State *L, int nargs)
  943. {
  944. TValue *o = L->top, *base = o - nargs;
  945. L->top = o+1;
  946. for (; o > base; o--) copyTV(L, o, o-1);
  947. setnilV(o);
  948. return o+1;
  949. }
  950. #else
  951. #define api_call_base(L, nargs) (L->top - (nargs))
  952. #endif
  953. LUA_API void lua_call(lua_State *L, int nargs, int nresults)
  954. {
  955. api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  956. api_checknelems(L, nargs+1);
  957. lj_vm_call(L, api_call_base(L, nargs), nresults+1);
  958. }
  959. LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
  960. {
  961. global_State *g = G(L);
  962. uint8_t oldh = hook_save(g);
  963. ptrdiff_t ef;
  964. int status;
  965. api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  966. api_checknelems(L, nargs+1);
  967. if (errfunc == 0) {
  968. ef = 0;
  969. } else {
  970. cTValue *o = stkindex2adr(L, errfunc);
  971. api_checkvalidindex(L, o);
  972. ef = savestack(L, o);
  973. }
  974. status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);
  975. if (status) hook_restore(g, oldh);
  976. return status;
  977. }
  978. static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
  979. {
  980. GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
  981. TValue *top = L->top;
  982. fn->c.f = func;
  983. setfuncV(L, top++, fn);
  984. if (LJ_FR2) setnilV(top++);
  985. setlightudV(top++, checklightudptr(L, ud));
  986. cframe_nres(L->cframe) = 1+0; /* Zero results. */
  987. L->top = top;
  988. return top-1; /* Now call the newly allocated C function. */
  989. }
  990. LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
  991. {
  992. global_State *g = G(L);
  993. uint8_t oldh = hook_save(g);
  994. int status;
  995. api_check(L, L->status == 0 || L->status == LUA_ERRERR);
  996. status = lj_vm_cpcall(L, func, ud, cpcall);
  997. if (status) hook_restore(g, oldh);
  998. return status;
  999. }
  1000. LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
  1001. {
  1002. if (luaL_getmetafield(L, idx, field)) {
  1003. TValue *top = L->top--;
  1004. if (LJ_FR2) setnilV(top++);
  1005. copyTV(L, top++, index2adr(L, idx));
  1006. L->top = top;
  1007. lj_vm_call(L, top-1, 1+1);
  1008. return 1;
  1009. }
  1010. return 0;
  1011. }
  1012. /* -- Coroutine yield and resume ------------------------------------------ */
  1013. LUA_API int lua_yield(lua_State *L, int nresults)
  1014. {
  1015. void *cf = L->cframe;
  1016. global_State *g = G(L);
  1017. if (cframe_canyield(cf)) {
  1018. cf = cframe_raw(cf);
  1019. if (!hook_active(g)) { /* Regular yield: move results down if needed. */
  1020. cTValue *f = L->top - nresults;
  1021. if (f > L->base) {
  1022. TValue *t = L->base;
  1023. while (--nresults >= 0) copyTV(L, t++, f++);
  1024. L->top = t;
  1025. }
  1026. L->cframe = NULL;
  1027. L->status = LUA_YIELD;
  1028. return -1;
  1029. } else { /* Yield from hook: add a pseudo-frame. */
  1030. TValue *top = L->top;
  1031. hook_leave(g);
  1032. (top++)->u64 = cframe_multres(cf);
  1033. setcont(top, lj_cont_hook);
  1034. if (LJ_FR2) top++;
  1035. setframe_pc(top, cframe_pc(cf)-1);
  1036. if (LJ_FR2) top++;
  1037. setframe_gc(top, obj2gco(L), LJ_TTHREAD);
  1038. setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT);
  1039. L->top = L->base = top+1;
  1040. #if LJ_TARGET_X64
  1041. lj_err_throw(L, LUA_YIELD);
  1042. #else
  1043. L->cframe = NULL;
  1044. L->status = LUA_YIELD;
  1045. lj_vm_unwind_c(cf, LUA_YIELD);
  1046. #endif
  1047. }
  1048. }
  1049. lj_err_msg(L, LJ_ERR_CYIELD);
  1050. return 0; /* unreachable */
  1051. }
  1052. LUA_API int lua_resume(lua_State *L, int nargs)
  1053. {
  1054. if (L->cframe == NULL && L->status <= LUA_YIELD)
  1055. return lj_vm_resume(L,
  1056. L->status == 0 ? api_call_base(L, nargs) : L->top - nargs,
  1057. 0, 0);
  1058. L->top = L->base;
  1059. setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
  1060. incr_top(L);
  1061. return LUA_ERRRUN;
  1062. }
  1063. /* -- GC and memory management -------------------------------------------- */
  1064. LUA_API int lua_gc(lua_State *L, int what, int data)
  1065. {
  1066. global_State *g = G(L);
  1067. int res = 0;
  1068. switch (what) {
  1069. case LUA_GCSTOP:
  1070. g->gc.threshold = LJ_MAX_MEM;
  1071. break;
  1072. case LUA_GCRESTART:
  1073. g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
  1074. break;
  1075. case LUA_GCCOLLECT:
  1076. lj_gc_fullgc(L);
  1077. break;
  1078. case LUA_GCCOUNT:
  1079. res = (int)(g->gc.total >> 10);
  1080. break;
  1081. case LUA_GCCOUNTB:
  1082. res = (int)(g->gc.total & 0x3ff);
  1083. break;
  1084. case LUA_GCSTEP: {
  1085. GCSize a = (GCSize)data << 10;
  1086. g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
  1087. while (g->gc.total >= g->gc.threshold)
  1088. if (lj_gc_step(L) > 0) {
  1089. res = 1;
  1090. break;
  1091. }
  1092. break;
  1093. }
  1094. case LUA_GCSETPAUSE:
  1095. res = (int)(g->gc.pause);
  1096. g->gc.pause = (MSize)data;
  1097. break;
  1098. case LUA_GCSETSTEPMUL:
  1099. res = (int)(g->gc.stepmul);
  1100. g->gc.stepmul = (MSize)data;
  1101. break;
  1102. case LUA_GCISRUNNING:
  1103. res = (g->gc.threshold != LJ_MAX_MEM);
  1104. break;
  1105. default:
  1106. res = -1; /* Invalid option. */
  1107. }
  1108. return res;
  1109. }
  1110. LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
  1111. {
  1112. global_State *g = G(L);
  1113. if (ud) *ud = g->allocd;
  1114. return g->allocf;
  1115. }
  1116. LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
  1117. {
  1118. global_State *g = G(L);
  1119. g->allocd = ud;
  1120. g->allocf = f;
  1121. }