diff --git a/src/Makefile.dep b/src/Makefile.dep index 8e01865c..b2ced9c9 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -7,8 +7,9 @@ lib_base.o: lib_base.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h lj_strscan.h \ lj_lib.h lj_libdef.h lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cdata.h \ - lj_cconv.h lj_carith.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h + lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_strfmt.h \ + lj_ctype.h lj_cdata.h lj_cconv.h lj_carith.h lj_ff.h lj_ffdef.h lj_lib.h \ + lj_libdef.h lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \ lj_libdef.h diff --git a/src/lib_bit.c b/src/lib_bit.c index 85821b81..b1f0beb2 100644 --- a/src/lib_bit.c +++ b/src/lib_bit.c @@ -12,7 +12,9 @@ #include "lj_obj.h" #include "lj_err.h" +#include "lj_buf.h" #include "lj_str.h" +#include "lj_strfmt.h" #if LJ_HASFFI #include "lj_ctype.h" #include "lj_cdata.h" @@ -34,6 +36,20 @@ static int bit_result64(lua_State *L, CTypeID id, uint64_t x) setcdataV(L, L->base-1, cd); return FFH_RES(1); } +#else +static int32_t bit_checkbit(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && lj_strscan_numberobj(o))) + lj_err_argt(L, narg, LUA_TNUMBER); + if (LJ_LIKELY(tvisint(o))) { + return intV(o); + } else { + int32_t i = lj_num2bit(numV(o)); + if (LJ_DUALNUM) setintV(o, i); + return i; + } +} #endif LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit) @@ -86,7 +102,7 @@ LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) return FFH_RETRY; #else lj_lib_checknumber(L, 1); - lj_lib_checkbit(L, 2); + bit_checkbit(L, 2); return FFH_RETRY; #endif } @@ -131,20 +147,19 @@ LJLIB_CF(bit_tohex) #if LJ_HASFFI CTypeID id = 0, id2 = 0; uint64_t b = lj_carith_check64(L, 1, &id); - int32_t i, dig = id ? 16 : 8; - int32_t n = L->base+1>=L->top ? dig : (int32_t)lj_carith_check64(L, 2, &id2); - char buf[16]; + int32_t n = L->base+1>=L->top ? (id ? 16 : 8) : + (int32_t)lj_carith_check64(L, 2, &id2); #else - uint32_t b = (uint32_t)lj_lib_checkbit(L, 1); - int32_t i, dig = 8; - int32_t n = L->base+1>=L->top ? dig : lj_lib_checkbit(L, 2); - char buf[8]; + uint32_t b = (uint32_t)bit_checkbit(L, 1); + int32_t n = L->base+1>=L->top ? 8 : bit_checkbit(L, 2); #endif - const char *hexdigits = "0123456789abcdef"; - if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } - if (n > dig) n = dig; - for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } - lua_pushlstring(L, buf, (size_t)n); + SBuf *sb = lj_buf_tmp_(L); + SFormat sf = (STRFMT_UINT|STRFMT_T_HEX); + if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; } + sf |= ((SFormat)(n+1) << STRFMT_SH_PREC); + sb = lj_strfmt_putxint(sb, sf, b); + setstrV(L, L->top-1, lj_buf_str(L, sb)); + lj_gc_check(L); return 1; } diff --git a/src/lj_lib.c b/src/lj_lib.c index b6aa97a0..a4bde57a 100644 --- a/src/lj_lib.c +++ b/src/lj_lib.c @@ -223,20 +223,6 @@ int32_t lj_lib_optint(lua_State *L, int narg, int32_t def) return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def; } -int32_t lj_lib_checkbit(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && lj_strscan_numberobj(o))) - lj_err_argt(L, narg, LUA_TNUMBER); - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else { - int32_t i = lj_num2bit(numV(o)); - if (LJ_DUALNUM) setintV(o, i); - return i; - } -} - GCfunc *lj_lib_checkfunc(lua_State *L, int narg) { TValue *o = L->base + narg-1; diff --git a/src/lj_lib.h b/src/lj_lib.h index 05f90de5..2dd45adb 100644 --- a/src/lj_lib.h +++ b/src/lj_lib.h @@ -41,7 +41,6 @@ LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg); LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg); LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg); LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def); -LJ_FUNC int32_t lj_lib_checkbit(lua_State *L, int narg); LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg); LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg); LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg);