Skip to content

Commit

Permalink
test: update wasm3 engine with new API
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Feb 22, 2021
1 parent bc00683 commit 46c1a3f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions test/utils/wasm3_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Wasm3Engine final : public WasmEngine

namespace
{
const void* env_adler32(IM3Runtime /*runtime*/, uint64_t* stack, void* mem)
const void* env_adler32(IM3Runtime /*runtime*/, uint64_t* stack, void* mem, void* /*userdata*/)
{
const uint32_t offset = static_cast<uint32_t>(stack[0]);
const uint32_t length = static_cast<uint32_t>(stack[1]);
Expand Down Expand Up @@ -115,20 +115,31 @@ std::optional<WasmEngine::FuncRef> Wasm3Engine::find_function(
{
IM3Function function;
if (m3_FindFunction(&function, m_runtime, name.data()) == m3Err_none)
// TODO: validate input/output types (m3_GetArgCount/m3_GetArgType/m3_GetRetCount/m3_GetRetType)
return reinterpret_cast<WasmEngine::FuncRef>(function);
return std::nullopt;
}

WasmEngine::Result Wasm3Engine::execute(
WasmEngine::FuncRef func_ref, const std::vector<uint64_t>& args)
{
unsigned ret_valid;
uint64_t ret_value;
IM3Function function = reinterpret_cast<IM3Function>(func_ref);
auto const result = m3_CallProper(
function, static_cast<uint32_t>(args.size()), args.data(), &ret_valid, &ret_value);

std::vector<const void*> argPtrs;
for (auto const& arg : args)
argPtrs.push_back(&arg);

// This ensures input count/type matches. For the return value we assume find_function did the validation.
auto const result = m3_Call(function, static_cast<uint32_t>(args.size()), argPtrs.data());
if (result == m3Err_none)
return {false, ret_valid ? ret_value : std::optional<uint64_t>{}};
{
if (m3_GetRetCount(function) == 0)
return {false, std::nullopt};

uint64_t ret_value = 0;
assert(m3_GetResultsV(function, &ret_value) == m3Err_none);
return {false, ret_value};
}
return {true, std::nullopt};
}
} // namespace fizzy::test

0 comments on commit 46c1a3f

Please sign in to comment.