diff --git a/src/host/buildvm_libbc.h b/src/host/buildvm_libbc.h index 6b0e1d03..a71aa630 100644 --- a/src/host/buildvm_libbc.h +++ b/src/host/buildvm_libbc.h @@ -4,12 +4,20 @@ static const int libbc_endian = 0; static const uint8_t libbc_code[] = { 0,1,2,0,0,1,2,24,1,0,0,76,1,2,0,241,135,158,166,3,220,203,178,130,4,0,1,2,0, -0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0 +0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0,2,9,0,0,0,15, +16,0,12,0,16,1,9,0,41,2,1,0,21,3,0,0,41,4,1,0,77,2,8,128,18,6,1,0,18,7,5,0, +59,8,5,0,66,6,3,2,10,6,0,0,88,7,1,128,76,6,2,0,79,2,248,127,75,0,1,0,0,2,10, +0,0,0,16,16,0,12,0,16,1,9,0,43,2,0,0,18,3,0,0,41,4,0,0,88,5,7,128,18,7,1,0, +18,8,5,0,18,9,6,0,66,7,3,2,10,7,0,0,88,8,1,128,76,7,2,0,70,5,3,3,82,5,247,127, +75,0,1,0,0,1,2,0,0,0,3,16,0,12,0,21,1,0,0,76,1,2,0,0 }; static const struct { const char *name; int ofs; } libbc_map[] = { {"math_deg",0}, {"math_rad",25}, -{NULL,50} +{"table_foreachi",50}, +{"table_foreach",117}, +{"table_getn",188}, +{NULL,207} }; diff --git a/src/lib_table.c b/src/lib_table.c index 8d53a6cd..13aff24e 100644 --- a/src/lib_table.c +++ b/src/lib_table.c @@ -23,50 +23,34 @@ #define LJLIB_MODULE_table -LJLIB_CF(table_foreachi) -{ - GCtab *t = lj_lib_checktab(L, 1); - GCfunc *func = lj_lib_checkfunc(L, 2); - MSize i, n = lj_tab_len(t); - for (i = 1; i <= n; i++) { - cTValue *val; - setfuncV(L, L->top, func); - setintV(L->top+1, i); - val = lj_tab_getint(t, (int32_t)i); - if (val) { copyTV(L, L->top+2, val); } else { setnilV(L->top+2); } - L->top += 3; - lua_call(L, 2, 1); - if (!tvisnil(L->top-1)) - return 1; - L->top--; - } - return 0; -} +LJLIB_LUA(table_foreachi) /* + function(t, f) + CHECK_tab(t) + CHECK_func(f) + for i=1,#t do + local r = f(i, t[i]) + if r ~= nil then return r end + end + end +*/ -LJLIB_CF(table_foreach) -{ - GCtab *t = lj_lib_checktab(L, 1); - GCfunc *func = lj_lib_checkfunc(L, 2); - L->top = L->base+3; - setnilV(L->top-1); - while (lj_tab_next(L, t, L->top-1)) { - copyTV(L, L->top+2, L->top); - copyTV(L, L->top+1, L->top-1); - setfuncV(L, L->top, func); - L->top += 3; - lua_call(L, 2, 1); - if (!tvisnil(L->top-1)) - return 1; - L->top--; - } - return 0; -} +LJLIB_LUA(table_foreach) /* + function(t, f) + CHECK_tab(t) + CHECK_func(f) + for k, v in PAIRS(t) do + local r = f(k, v) + if r ~= nil then return r end + end + end +*/ -LJLIB_ASM(table_getn) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - return FFH_UNREACHABLE; -} +LJLIB_LUA(table_getn) /* + function(t) + CHECK_tab(t) + return #t + end +*/ LJLIB_CF(table_maxn) { diff --git a/src/lj_dispatch.h b/src/lj_dispatch.h index a662439b..a03804af 100644 --- a/src/lj_dispatch.h +++ b/src/lj_dispatch.h @@ -60,7 +60,7 @@ typedef uint16_t HotCount; #define HOTCOUNT_CALL 1 /* This solves a circular dependency problem -- bump as needed. Sigh. */ -#define GG_NUM_ASMFF 60 +#define GG_NUM_ASMFF 59 #define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF) #define GG_LEN_SDISP BC_FUNCF diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c index 30d5c44e..51981477 100644 --- a/src/lj_ffrecord.c +++ b/src/lj_ffrecord.c @@ -729,14 +729,6 @@ static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) /* -- Table library fast functions ---------------------------------------- */ -static void LJ_FASTCALL recff_table_getn(jit_State *J, RecordFFData *rd) -{ - if (tref_istab(J->base[0])) - J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, J->base[0]); - /* else: Interpreter will throw. */ - UNUSED(rd); -} - static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd) { TRef tab = J->base[0]; diff --git a/src/vm_arm.dasc b/src/vm_arm.dasc index d999d5ff..6928e03b 100644 --- a/src/vm_arm.dasc +++ b/src/vm_arm.dasc @@ -1861,17 +1861,6 @@ static void build_subroutines(BuildCtx *ctx) |ffstring_case string_lower, 65 |ffstring_case string_upper, 97 | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | checktab CARG2, ->fff_fallback - | .IOS mov RA, BASE - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | .IOS mov BASE, RA - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | |//-- Bit library -------------------------------------------------------- | |// FP number to bit conversion for soft-float. Clobbers r0-r3. diff --git a/src/vm_mips.dasc b/src/vm_mips.dasc index 6db5801f..5808e182 100644 --- a/src/vm_mips.dasc +++ b/src/vm_mips.dasc @@ -1812,18 +1812,6 @@ static void build_subroutines(BuildCtx *ctx) |ffstring_case string_lower, 65 |ffstring_case string_upper, 97 | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | li AT, LJ_TTAB - | bne CARG3, AT, ->fff_fallback - |. load_got lj_tab_len - | call_intern lj_tab_len // (GCtab *t) - |. nop - | // Returns uint32_t (but less than 2^31). - | b ->fff_resi - |. nop - | |//-- Bit library -------------------------------------------------------- | |.macro .ffunc_bit, name diff --git a/src/vm_ppc.dasc b/src/vm_ppc.dasc index d76e3a7b..bff50c59 100644 --- a/src/vm_ppc.dasc +++ b/src/vm_ppc.dasc @@ -2281,14 +2281,6 @@ static void build_subroutines(BuildCtx *ctx) |ffstring_case string_lower, 65 |ffstring_case string_upper, 97 | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | checktab CARG3; bne ->fff_fallback - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | b ->fff_resi - | |//-- Bit library -------------------------------------------------------- | |.macro .ffunc_bit, name diff --git a/src/vm_x86.dasc b/src/vm_x86.dasc index 0c4b0ce6..0a53ffde 100644 --- a/src/vm_x86.dasc +++ b/src/vm_x86.dasc @@ -2434,21 +2434,6 @@ static void build_subroutines(BuildCtx *ctx) |ffstring_case string_lower, 0x41, 0x5a |ffstring_case string_upper, 0x61, 0x7a | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | mov RB, BASE // Save BASE. - | mov TAB:FCARG1, [BASE] - | call extern lj_tab_len@4 // LJ_FASTCALL (GCtab *t) - | // Length of table returned in eax (RD). - | mov BASE, RB // Restore BASE. - |.if DUALNUM - | mov RB, RD; jmp ->fff_resi - |.else - | cvtsi2sd xmm0, RD; jmp ->fff_resxmm0 - |.endif - | |//-- Bit library -------------------------------------------------------- | |.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!).