Ver código fonte

From Lua 5.2: Add rawlen(). Needs -DLUAJIT_ENABLE_LUA52COMPAT.

Mike Pall 13 anos atrás
pai
commit
ca0bb4881f
2 arquivos alterados com 27 adições e 0 exclusões
  1. 14 0
      src/lib_base.c
  2. 13 0
      src/lj_ffrecord.c

+ 14 - 0
src/lib_base.c

@@ -199,6 +199,20 @@ LJLIB_CF(rawequal)		LJLIB_REC(.)
   return 1;
 }
 
+#if LJ_52
+LJLIB_CF(rawlen)		LJLIB_REC(.)
+{
+  cTValue *o = L->base;
+  int32_t len;
+  if (L->top > o && tvisstr(o))
+    len = (int32_t)strV(o)->len;
+  else
+    len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1));
+  setintV(L->top-1, len);
+  return 1;
+}
+#endif
+
 LJLIB_CF(unpack)
 {
   GCtab *t = lj_lib_checktab(L, 1);

+ 13 - 0
src/lj_ffrecord.c

@@ -213,6 +213,19 @@ static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd)
   }  /* else: Interpreter will throw. */
 }
 
+#if LJ_52
+static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd)
+{
+  TRef tr = J->base[0];
+  if (tref_isstr(tr))
+    J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN);
+  else if (tref_istab(tr))
+    J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, tr);
+  /* else: Interpreter will throw. */
+  UNUSED(rd);
+}
+#endif
+
 /* Determine mode of select() call. */
 int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv)
 {