diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index f90d5d264..02e4aeebd 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -489,6 +489,15 @@ std::optional find_export(const Module& module, ExternalKind kind, std return (it != module.exportsec.end() ? std::make_optional(it->index) : std::nullopt); } +inline uint64_t& get_local( + std::vector& args, std::vector& locals, uint32_t idx) noexcept +{ + if (idx < args.size()) + return args[idx]; + assert((idx - args.size()) <= locals.size()); + return locals[idx - args.size()]; +} + } // namespace std::unique_ptr instantiate(Module module, @@ -641,8 +650,7 @@ execution_result execute( const auto& code = instance.module.codesec[code_idx]; auto* const memory = instance.memory.get(); - std::vector locals = std::move(args); - locals.resize(locals.size() + code.local_count); + std::vector locals(code.local_count, 0); OperandStack stack(static_cast(code.max_stack_height)); @@ -842,23 +850,17 @@ execution_result execute( } case Instr::local_get: { - const auto idx = read(immediates); - assert(idx <= locals.size()); - stack.push(locals[idx]); + stack.push(get_local(args, locals, read(immediates))); break; } case Instr::local_set: { - const auto idx = read(immediates); - assert(idx <= locals.size()); - locals[idx] = stack.pop(); + get_local(args, locals, read(immediates)) = stack.pop(); break; } case Instr::local_tee: { - const auto idx = read(immediates); - assert(idx <= locals.size()); - locals[idx] = stack.top(); + get_local(args, locals, read(immediates)) = stack.top(); break; } case Instr::global_get: