diff --git a/codegen-luajit/runtime/runtime.lua b/codegen-luajit/runtime/runtime.lua index 8415c60..235dfdf 100644 --- a/codegen-luajit/runtime/runtime.lua +++ b/codegen-luajit/runtime/runtime.lua @@ -159,26 +159,11 @@ do end do - local band = {} - local bor = {} - local bxor = {} local bnot = {} - band.i32 = bit.band - band.i64 = bit.band - bnot.i32 = bit.bnot bnot.i64 = bit.bnot - bor.i32 = bit.bor - bor.i64 = bit.bor - - bxor.i32 = bit.bxor - bxor.i64 = bit.bxor - - module.band = band - module.bor = bor - module.bxor = bxor module.bnot = bnot end diff --git a/codegen-luajit/src/translator.rs b/codegen-luajit/src/translator.rs index 0c63b38..2f4618e 100644 --- a/codegen-luajit/src/translator.rs +++ b/codegen-luajit/src/translator.rs @@ -230,12 +230,21 @@ fn build_func_list(wasm: &Module, type_info: &TypeInfo) -> Vec { .collect() } +fn write_local_operation(head: &str, tail: &str, w: &mut dyn Write) -> Result<()> { + match (head, tail) { + ("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _) | ("band" | "bor" | "bxor", _) => { + write!(w, "local {head}_{tail} = math.{head} ") + } + _ => write!(w, "local {head}_{tail} = rt.{head}.{tail} "), + } +} + fn write_localize_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result<()> { let loc_set: BTreeSet<_> = func_list.iter().flat_map(localize::visit).collect(); loc_set .into_iter() - .try_for_each(|(a, b)| write!(w, "local {a}_{b} = rt.{a}.{b} ")) + .try_for_each(|(a, b)| write_local_operation(a, b, w)) } fn write_memory_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result> { diff --git a/codegen-luau/runtime/runtime.lua b/codegen-luau/runtime/runtime.lua index bc00b29..9a17354 100644 --- a/codegen-luau/runtime/runtime.lua +++ b/codegen-luau/runtime/runtime.lua @@ -180,16 +180,13 @@ do local bxor = {} local bnot = {} - band.i32 = bit32.band band.i64 = I64.bit_and bnot.i32 = bit32.bnot bnot.i64 = I64.bit_not - bor.i32 = bit32.bor bor.i64 = I64.bit_or - bxor.i32 = bit32.bxor bxor.i64 = I64.bit_xor module.band = band diff --git a/codegen-luau/src/translator.rs b/codegen-luau/src/translator.rs index 4c3f22b..2186719 100644 --- a/codegen-luau/src/translator.rs +++ b/codegen-luau/src/translator.rs @@ -229,12 +229,22 @@ fn build_func_list(wasm: &Module, type_info: &TypeInfo) -> Vec { .collect() } +fn write_local_operation(head: &str, tail: &str, w: &mut dyn Write) -> Result<()> { + match (head, tail) { + ("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _) + | ("band" | "bor" | "bxor", "i32") => { + write!(w, "local {head}_{tail} = math.{head} ") + } + _ => write!(w, "local {head}_{tail} = rt.{head}.{tail} "), + } +} + fn write_localize_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result<()> { let loc_set: BTreeSet<_> = func_list.iter().flat_map(localize::visit).collect(); loc_set .into_iter() - .try_for_each(|(a, b)| write!(w, "local {a}_{b} = rt.{a}.{b} ")) + .try_for_each(|(a, b)| write_local_operation(a, b, w)) } fn write_memory_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result> {