lj_meta.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. ** Metamethod handling.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** 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_meta_c
  9. #define LUA_CORE
  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_buf.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_meta.h"
  17. #include "lj_frame.h"
  18. #include "lj_bc.h"
  19. #include "lj_vm.h"
  20. #include "lj_strscan.h"
  21. #include "lj_strfmt.h"
  22. #include "lj_lib.h"
  23. /* -- Metamethod handling ------------------------------------------------- */
  24. /* String interning of metamethod names for fast indexing. */
  25. void lj_meta_init(lua_State *L)
  26. {
  27. #define MMNAME(name) "__" #name
  28. const char *metanames = MMDEF(MMNAME);
  29. #undef MMNAME
  30. global_State *g = G(L);
  31. const char *p, *q;
  32. uint32_t mm;
  33. for (mm = 0, p = metanames; *p; mm++, p = q) {
  34. GCstr *s;
  35. for (q = p+2; *q && *q != '_'; q++) ;
  36. s = lj_str_new(L, p, (size_t)(q-p));
  37. /* NOBARRIER: g->gcroot[] is a GC root. */
  38. setgcref(g->gcroot[GCROOT_MMNAME+mm], obj2gco(s));
  39. }
  40. }
  41. /* Negative caching of a few fast metamethods. See the lj_meta_fast() macro. */
  42. cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name)
  43. {
  44. cTValue *mo = lj_tab_getstr(mt, name);
  45. lj_assertX(mm <= MM_FAST, "bad metamethod %d", mm);
  46. if (!mo || tvisnil(mo)) { /* No metamethod? */
  47. mt->nomm |= (uint8_t)(1u<<mm); /* Set negative cache flag. */
  48. return NULL;
  49. }
  50. return mo;
  51. }
  52. /* Lookup metamethod for object. */
  53. cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm)
  54. {
  55. GCtab *mt;
  56. if (tvistab(o))
  57. mt = tabref(tabV(o)->metatable);
  58. else if (tvisudata(o))
  59. mt = tabref(udataV(o)->metatable);
  60. else
  61. mt = tabref(basemt_obj(G(L), o));
  62. if (mt) {
  63. cTValue *mo = lj_tab_getstr(mt, mmname_str(G(L), mm));
  64. if (mo)
  65. return mo;
  66. }
  67. return niltv(L);
  68. }
  69. #if LJ_HASFFI
  70. /* Tailcall from C function. */
  71. int lj_meta_tailcall(lua_State *L, cTValue *tv)
  72. {
  73. TValue *base = L->base;
  74. TValue *top = L->top;
  75. const BCIns *pc = frame_pc(base-1); /* Preserve old PC from frame. */
  76. copyTV(L, base-1-LJ_FR2, tv); /* Replace frame with new object. */
  77. if (LJ_FR2)
  78. (top++)->u64 = LJ_CONT_TAILCALL;
  79. else
  80. top->u32.lo = LJ_CONT_TAILCALL;
  81. setframe_pc(top++, pc);
  82. setframe_gc(top, obj2gco(L), LJ_TTHREAD); /* Dummy frame object. */
  83. if (LJ_FR2) top++;
  84. setframe_ftsz(top, ((char *)(top+1) - (char *)base) + FRAME_CONT);
  85. L->base = L->top = top+1;
  86. /*
  87. ** before: [old_mo|PC] [... ...]
  88. ** ^base ^top
  89. ** after: [new_mo|itype] [... ...] [NULL|PC] [dummy|delta]
  90. ** ^base/top
  91. ** tailcall: [new_mo|PC] [... ...]
  92. ** ^base ^top
  93. */
  94. return 0;
  95. }
  96. #endif
  97. /* Setup call to metamethod to be run by Assembler VM. */
  98. static TValue *mmcall(lua_State *L, ASMFunction cont, cTValue *mo,
  99. cTValue *a, cTValue *b)
  100. {
  101. /*
  102. ** |-- framesize -> top top+1 top+2 top+3
  103. ** before: [func slots ...]
  104. ** mm setup: [func slots ...] [cont|?] [mo|tmtype] [a] [b]
  105. ** in asm: [func slots ...] [cont|PC] [mo|delta] [a] [b]
  106. ** ^-- func base ^-- mm base
  107. ** after mm: [func slots ...] [result]
  108. ** ^-- copy to base[PC_RA] --/ for lj_cont_ra
  109. ** istruecond + branch for lj_cont_cond*
  110. ** ignore for lj_cont_nop
  111. ** next PC: [func slots ...]
  112. */
  113. TValue *top = L->top;
  114. if (curr_funcisL(L)) top = curr_topL(L);
  115. setcont(top++, cont); /* Assembler VM stores PC in upper word or FR2. */
  116. if (LJ_FR2) setnilV(top++);
  117. copyTV(L, top++, mo); /* Store metamethod and two arguments. */
  118. if (LJ_FR2) setnilV(top++);
  119. copyTV(L, top, a);
  120. copyTV(L, top+1, b);
  121. return top; /* Return new base. */
  122. }
  123. /* -- C helpers for some instructions, called from assembler VM ----------- */
  124. /* Helper for TGET*. __index chain and metamethod. */
  125. cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k)
  126. {
  127. int loop;
  128. for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) {
  129. cTValue *mo;
  130. if (LJ_LIKELY(tvistab(o))) {
  131. GCtab *t = tabV(o);
  132. cTValue *tv = lj_tab_get(L, t, k);
  133. if (!tvisnil(tv) ||
  134. !(mo = lj_meta_fast(L, tabref(t->metatable), MM_index)))
  135. return tv;
  136. } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_index))) {
  137. lj_err_optype(L, o, LJ_ERR_OPINDEX);
  138. return NULL; /* unreachable */
  139. }
  140. if (tvisfunc(mo)) {
  141. L->top = mmcall(L, lj_cont_ra, mo, o, k);
  142. return NULL; /* Trigger metamethod call. */
  143. }
  144. o = mo;
  145. }
  146. lj_err_msg(L, LJ_ERR_GETLOOP);
  147. return NULL; /* unreachable */
  148. }
  149. /* Helper for TSET*. __newindex chain and metamethod. */
  150. TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k)
  151. {
  152. TValue tmp;
  153. int loop;
  154. for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) {
  155. cTValue *mo;
  156. if (LJ_LIKELY(tvistab(o))) {
  157. GCtab *t = tabV(o);
  158. cTValue *tv = lj_tab_get(L, t, k);
  159. if (LJ_LIKELY(!tvisnil(tv))) {
  160. t->nomm = 0; /* Invalidate negative metamethod cache. */
  161. lj_gc_anybarriert(L, t);
  162. return (TValue *)tv;
  163. } else if (!(mo = lj_meta_fast(L, tabref(t->metatable), MM_newindex))) {
  164. t->nomm = 0; /* Invalidate negative metamethod cache. */
  165. lj_gc_anybarriert(L, t);
  166. if (tv != niltv(L))
  167. return (TValue *)tv;
  168. if (tvisnil(k)) lj_err_msg(L, LJ_ERR_NILIDX);
  169. else if (tvisint(k)) { setnumV(&tmp, (lua_Number)intV(k)); k = &tmp; }
  170. else if (tvisnum(k) && tvisnan(k)) lj_err_msg(L, LJ_ERR_NANIDX);
  171. return lj_tab_newkey(L, t, k);
  172. }
  173. } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_newindex))) {
  174. lj_err_optype(L, o, LJ_ERR_OPINDEX);
  175. return NULL; /* unreachable */
  176. }
  177. if (tvisfunc(mo)) {
  178. L->top = mmcall(L, lj_cont_nop, mo, o, k);
  179. /* L->top+2 = v filled in by caller. */
  180. return NULL; /* Trigger metamethod call. */
  181. }
  182. copyTV(L, &tmp, mo);
  183. o = &tmp;
  184. }
  185. lj_err_msg(L, LJ_ERR_SETLOOP);
  186. return NULL; /* unreachable */
  187. }
  188. static cTValue *str2num(cTValue *o, TValue *n)
  189. {
  190. if (tvisnum(o))
  191. return o;
  192. else if (tvisint(o))
  193. return (setnumV(n, (lua_Number)intV(o)), n);
  194. else if (tvisstr(o) && lj_strscan_num(strV(o), n))
  195. return n;
  196. else
  197. return NULL;
  198. }
  199. /* Helper for arithmetic instructions. Coercion, metamethod. */
  200. TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, cTValue *rc,
  201. BCReg op)
  202. {
  203. MMS mm = bcmode_mm(op);
  204. TValue tempb, tempc;
  205. cTValue *b, *c;
  206. if ((b = str2num(rb, &tempb)) != NULL &&
  207. (c = str2num(rc, &tempc)) != NULL) { /* Try coercion first. */
  208. setnumV(ra, lj_vm_foldarith(numV(b), numV(c), (int)mm-MM_add));
  209. return NULL;
  210. } else {
  211. cTValue *mo = lj_meta_lookup(L, rb, mm);
  212. if (tvisnil(mo)) {
  213. mo = lj_meta_lookup(L, rc, mm);
  214. if (tvisnil(mo)) {
  215. if (str2num(rb, &tempb) == NULL) rc = rb;
  216. lj_err_optype(L, rc, LJ_ERR_OPARITH);
  217. return NULL; /* unreachable */
  218. }
  219. }
  220. return mmcall(L, lj_cont_ra, mo, rb, rc);
  221. }
  222. }
  223. /* Helper for CAT. Coercion, iterative concat, __concat metamethod. */
  224. TValue *lj_meta_cat(lua_State *L, TValue *top, int left)
  225. {
  226. int fromc = 0;
  227. if (left < 0) { left = -left; fromc = 1; }
  228. do {
  229. if (!(tvisstr(top) || tvisnumber(top) || tvisbuf(top)) ||
  230. !(tvisstr(top-1) || tvisnumber(top-1) || tvisbuf(top-1))) {
  231. cTValue *mo = lj_meta_lookup(L, top-1, MM_concat);
  232. if (tvisnil(mo)) {
  233. mo = lj_meta_lookup(L, top, MM_concat);
  234. if (tvisnil(mo)) {
  235. if (tvisstr(top-1) || tvisnumber(top-1)) top++;
  236. lj_err_optype(L, top-1, LJ_ERR_OPCAT);
  237. return NULL; /* unreachable */
  238. }
  239. }
  240. /* One of the top two elements is not a string, call __cat metamethod:
  241. **
  242. ** before: [...][CAT stack .........................]
  243. ** top-1 top top+1 top+2
  244. ** pick two: [...][CAT stack ...] [o1] [o2]
  245. ** setup mm: [...][CAT stack ...] [cont|?] [mo|tmtype] [o1] [o2]
  246. ** in asm: [...][CAT stack ...] [cont|PC] [mo|delta] [o1] [o2]
  247. ** ^-- func base ^-- mm base
  248. ** after mm: [...][CAT stack ...] <--push-- [result]
  249. ** next step: [...][CAT stack .............]
  250. */
  251. copyTV(L, top+2*LJ_FR2+2, top); /* Carefully ordered stack copies! */
  252. copyTV(L, top+2*LJ_FR2+1, top-1);
  253. copyTV(L, top+LJ_FR2, mo);
  254. setcont(top-1, lj_cont_cat);
  255. if (LJ_FR2) { setnilV(top); setnilV(top+2); top += 2; }
  256. return top+1; /* Trigger metamethod call. */
  257. } else {
  258. /* Pick as many strings as possible from the top and concatenate them:
  259. **
  260. ** before: [...][CAT stack ...........................]
  261. ** pick str: [...][CAT stack ...] [...... strings ......]
  262. ** concat: [...][CAT stack ...] [result]
  263. ** next step: [...][CAT stack ............]
  264. */
  265. TValue *e, *o = top;
  266. uint64_t tlen = tvisstr(o) ? strV(o)->len :
  267. tvisbuf(o) ? sbufxlen(bufV(o)) : STRFMT_MAXBUF_NUM;
  268. SBuf *sb;
  269. do {
  270. o--; tlen += tvisstr(o) ? strV(o)->len :
  271. tvisbuf(o) ? sbufxlen(bufV(o)) : STRFMT_MAXBUF_NUM;
  272. } while (--left > 0 && (tvisstr(o-1) || tvisnumber(o-1)));
  273. if (tlen >= LJ_MAX_STR) lj_err_msg(L, LJ_ERR_STROV);
  274. sb = lj_buf_tmp_(L);
  275. lj_buf_more(sb, (MSize)tlen);
  276. for (e = top, top = o; o <= e; o++) {
  277. if (tvisstr(o)) {
  278. GCstr *s = strV(o);
  279. MSize len = s->len;
  280. lj_buf_putmem(sb, strdata(s), len);
  281. } else if (tvisbuf(o)) {
  282. SBufExt *sbx = bufV(o);
  283. lj_buf_putmem(sb, sbx->r, sbufxlen(sbx));
  284. } else if (tvisint(o)) {
  285. lj_strfmt_putint(sb, intV(o));
  286. } else {
  287. lj_strfmt_putfnum(sb, STRFMT_G14, numV(o));
  288. }
  289. }
  290. setstrV(L, top, lj_buf_str(L, sb));
  291. }
  292. } while (left >= 1);
  293. if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) {
  294. if (!fromc) L->top = curr_topL(L);
  295. lj_gc_step(L);
  296. }
  297. return NULL;
  298. }
  299. /* Helper for LEN. __len metamethod. */
  300. TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o)
  301. {
  302. cTValue *mo = lj_meta_lookup(L, o, MM_len);
  303. if (tvisnil(mo)) {
  304. if (LJ_52 && tvistab(o))
  305. tabref(tabV(o)->metatable)->nomm |= (uint8_t)(1u<<MM_len);
  306. else
  307. lj_err_optype(L, o, LJ_ERR_OPLEN);
  308. return NULL;
  309. }
  310. return mmcall(L, lj_cont_ra, mo, o, LJ_52 ? o : niltv(L));
  311. }
  312. /* Helper for equality comparisons. __eq metamethod. */
  313. TValue *lj_meta_equal(lua_State *L, GCobj *o1, GCobj *o2, int ne)
  314. {
  315. /* Field metatable must be at same offset for GCtab and GCudata! */
  316. cTValue *mo = lj_meta_fast(L, tabref(o1->gch.metatable), MM_eq);
  317. if (mo) {
  318. TValue *top;
  319. uint32_t it;
  320. if (tabref(o1->gch.metatable) != tabref(o2->gch.metatable)) {
  321. cTValue *mo2 = lj_meta_fast(L, tabref(o2->gch.metatable), MM_eq);
  322. if (mo2 == NULL || !lj_obj_equal(mo, mo2))
  323. return (TValue *)(intptr_t)ne;
  324. }
  325. top = curr_top(L);
  326. setcont(top++, ne ? lj_cont_condf : lj_cont_condt);
  327. if (LJ_FR2) setnilV(top++);
  328. copyTV(L, top++, mo);
  329. if (LJ_FR2) setnilV(top++);
  330. it = ~(uint32_t)o1->gch.gct;
  331. setgcV(L, top, o1, it);
  332. setgcV(L, top+1, o2, it);
  333. return top; /* Trigger metamethod call. */
  334. }
  335. return (TValue *)(intptr_t)ne;
  336. }
  337. #if LJ_HASFFI
  338. TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins)
  339. {
  340. ASMFunction cont = (bc_op(ins) & 1) ? lj_cont_condf : lj_cont_condt;
  341. int op = (int)bc_op(ins) & ~1;
  342. TValue tv;
  343. cTValue *mo, *o2, *o1 = &L->base[bc_a(ins)];
  344. cTValue *o1mm = o1;
  345. if (op == BC_ISEQV) {
  346. o2 = &L->base[bc_d(ins)];
  347. if (!tviscdata(o1mm)) o1mm = o2;
  348. } else if (op == BC_ISEQS) {
  349. setstrV(L, &tv, gco2str(proto_kgc(curr_proto(L), ~(ptrdiff_t)bc_d(ins))));
  350. o2 = &tv;
  351. } else if (op == BC_ISEQN) {
  352. o2 = &mref(curr_proto(L)->k, cTValue)[bc_d(ins)];
  353. } else {
  354. lj_assertL(op == BC_ISEQP, "bad bytecode op %d", op);
  355. setpriV(&tv, ~bc_d(ins));
  356. o2 = &tv;
  357. }
  358. mo = lj_meta_lookup(L, o1mm, MM_eq);
  359. if (LJ_LIKELY(!tvisnil(mo)))
  360. return mmcall(L, cont, mo, o1, o2);
  361. else
  362. return (TValue *)(intptr_t)(bc_op(ins) & 1);
  363. }
  364. #endif
  365. /* Helper for ordered comparisons. String compare, __lt/__le metamethods. */
  366. TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op)
  367. {
  368. if (LJ_HASFFI && (tviscdata(o1) || tviscdata(o2))) {
  369. ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt;
  370. MMS mm = (op & 2) ? MM_le : MM_lt;
  371. cTValue *mo = lj_meta_lookup(L, tviscdata(o1) ? o1 : o2, mm);
  372. if (LJ_UNLIKELY(tvisnil(mo))) goto err;
  373. return mmcall(L, cont, mo, o1, o2);
  374. } else if (LJ_52 || itype(o1) == itype(o2)) {
  375. /* Never called with two numbers. */
  376. if (tvisstr(o1) && tvisstr(o2)) {
  377. int32_t res = lj_str_cmp(strV(o1), strV(o2));
  378. return (TValue *)(intptr_t)(((op&2) ? res <= 0 : res < 0) ^ (op&1));
  379. } else {
  380. trymt:
  381. while (1) {
  382. ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt;
  383. MMS mm = (op & 2) ? MM_le : MM_lt;
  384. cTValue *mo = lj_meta_lookup(L, o1, mm);
  385. #if LJ_52
  386. if (tvisnil(mo) && tvisnil((mo = lj_meta_lookup(L, o2, mm))))
  387. #else
  388. cTValue *mo2 = lj_meta_lookup(L, o2, mm);
  389. if (tvisnil(mo) || !lj_obj_equal(mo, mo2))
  390. #endif
  391. {
  392. if (op & 2) { /* MM_le not found: retry with MM_lt. */
  393. cTValue *ot = o1; o1 = o2; o2 = ot; /* Swap operands. */
  394. op ^= 3; /* Use LT and flip condition. */
  395. continue;
  396. }
  397. goto err;
  398. }
  399. return mmcall(L, cont, mo, o1, o2);
  400. }
  401. }
  402. } else if (tvisbool(o1) && tvisbool(o2)) {
  403. goto trymt;
  404. } else {
  405. err:
  406. lj_err_comp(L, o1, o2);
  407. return NULL;
  408. }
  409. }
  410. /* Helper for ISTYPE and ISNUM. Implicit coercion or error. */
  411. void lj_meta_istype(lua_State *L, BCReg ra, BCReg tp)
  412. {
  413. L->top = curr_topL(L);
  414. ra++; tp--;
  415. lj_assertL(LJ_DUALNUM || tp != ~LJ_TNUMX, "bad type for ISTYPE");
  416. if (LJ_DUALNUM && tp == ~LJ_TNUMX) lj_lib_checkint(L, ra);
  417. else if (tp == ~LJ_TNUMX+1) lj_lib_checknum(L, ra);
  418. else if (tp == ~LJ_TSTR) lj_lib_checkstr(L, ra);
  419. else lj_err_argtype(L, ra, lj_obj_itypename[tp]);
  420. }
  421. /* Helper for calls. __call metamethod. */
  422. void lj_meta_call(lua_State *L, TValue *func, TValue *top)
  423. {
  424. cTValue *mo = lj_meta_lookup(L, func, MM_call);
  425. TValue *p;
  426. if (!tvisfunc(mo))
  427. lj_err_optype_call(L, func);
  428. for (p = top; p > func+2*LJ_FR2; p--) copyTV(L, p, p-1);
  429. if (LJ_FR2) copyTV(L, func+2, func);
  430. copyTV(L, func, mo);
  431. }
  432. /* Helper for FORI. Coercion. */
  433. void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o)
  434. {
  435. if (!lj_strscan_numberobj(o)) lj_err_msg(L, LJ_ERR_FORINIT);
  436. if (!lj_strscan_numberobj(o+1)) lj_err_msg(L, LJ_ERR_FORLIM);
  437. if (!lj_strscan_numberobj(o+2)) lj_err_msg(L, LJ_ERR_FORSTEP);
  438. if (LJ_DUALNUM) {
  439. /* Ensure all slots are integers or all slots are numbers. */
  440. int32_t k[3];
  441. int nint = 0;
  442. ptrdiff_t i;
  443. for (i = 0; i <= 2; i++) {
  444. if (tvisint(o+i)) {
  445. k[i] = intV(o+i); nint++;
  446. } else {
  447. k[i] = lj_num2int(numV(o+i)); nint += ((lua_Number)k[i] == numV(o+i));
  448. }
  449. }
  450. if (nint == 3) { /* Narrow to integers. */
  451. setintV(o, k[0]);
  452. setintV(o+1, k[1]);
  453. setintV(o+2, k[2]);
  454. } else if (nint != 0) { /* Widen to numbers. */
  455. if (tvisint(o)) setnumV(o, (lua_Number)intV(o));
  456. if (tvisint(o+1)) setnumV(o+1, (lua_Number)intV(o+1));
  457. if (tvisint(o+2)) setnumV(o+2, (lua_Number)intV(o+2));
  458. }
  459. }
  460. }