]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
DUALNUM: Add missing type conversion for FORI slots. master v2.0
authorMike Pall <mike>
Sat, 6 Dec 2025 07:35:19 +0000 (08:35 +0100)
committerMike Pall <mike>
Sat, 6 Dec 2025 07:35:19 +0000 (08:35 +0100)
Reported by Sergey Kaplun. #1413

src/lj_record.c

index 1d535a229902d5836ec2cea698dd34486a395204..04a379250d683407c8f58d4fa14addf95a2188b4 100644 (file)
@@ -307,12 +307,27 @@ static TRef fori_load(jit_State *J, BCReg slot, IRType t, int mode)
                mode + conv);
 }
 
+/* Convert FORI argument to expected target type. */
+static TRef fori_conv(jit_State *J, TRef tr, IRType t)
+{
+  if (t == IRT_INT) {
+    if (!tref_isinteger(tr))
+      return emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_CHECK);
+  } else {
+    if (!tref_isnum(tr))
+      return emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);
+  }
+  return tr;
+}
+
 /* Peek before FORI to find a const initializer. Otherwise load from slot. */
 static TRef fori_arg(jit_State *J, const BCIns *fori, BCReg slot,
                     IRType t, int mode)
 {
   TRef tr = J->base[slot];
-  if (!tr) {
+  if (tr) {
+    tr = fori_conv(J, tr, t);
+  } else {
     tr = find_kinit(J, fori, slot, t);
     if (!tr)
       tr = fori_load(J, slot, t, mode);
@@ -458,13 +473,7 @@ static LoopEvent rec_for(jit_State *J, const BCIns *fori, int isforl)
       lua_assert(tref_isnumber_str(tr[i]));
       if (tref_isstr(tr[i]))
        tr[i] = emitir(IRTG(IR_STRTO, IRT_NUM), tr[i], 0);
-      if (t == IRT_INT) {
-       if (!tref_isinteger(tr[i]))
-         tr[i] = emitir(IRTGI(IR_CONV), tr[i], IRCONV_INT_NUM|IRCONV_CHECK);
-      } else {
-       if (!tref_isnum(tr[i]))
-         tr[i] = emitir(IRTN(IR_CONV), tr[i], IRCONV_NUM_INT);
-      }
+      tr[i] = fori_conv(J, tr[i], t);
     }
     tr[FORL_EXT] = tr[FORL_IDX];
     stop = tr[FORL_STOP];