Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions test/utils/fizzy_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,6 @@ class FizzyEngine final : public WasmEngine

namespace
{
ValType translate_valtype(char input)
{
if (input == 'i')
return fizzy::ValType::i32;
else if (input == 'I')
return fizzy::ValType::i64;
else
throw std::runtime_error{"invalid type"};
}

FuncType translate_signature(std::string_view signature)
{
const auto delimiter_pos = signature.find(':');
assert(delimiter_pos != std::string_view::npos);
const auto inputs = signature.substr(0, delimiter_pos);
const auto outputs = signature.substr(delimiter_pos + 1);

FuncType func_type;
std::transform(std::begin(inputs), std::end(inputs), std::back_inserter(func_type.inputs),
translate_valtype);
std::transform(std::begin(outputs), std::end(outputs), std::back_inserter(func_type.outputs),
translate_valtype);
return func_type;
}

fizzy::ExecutionResult env_adler32(fizzy::Instance& instance, const fizzy::Value* args, int)
{
assert(instance.memory != nullptr);
Expand Down Expand Up @@ -124,7 +99,9 @@ std::optional<WasmEngine::FuncRef> FizzyEngine::find_function(
if (func_idx.has_value())
{
const auto func_type = m_instance->module->get_function_type(*func_idx);
const auto sig_type = translate_signature(signature);
FuncType sig_type;
std::tie(sig_type.inputs, sig_type.outputs) =
translate_function_signature<ValType, ValType::i32, ValType::i64>(signature);
if (sig_type != func_type)
return std::nullopt;
}
Expand Down
41 changes: 41 additions & 0 deletions test/utils/wasm_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#pragma once

#include "bytes.hpp"
#include <algorithm>
#include <cassert>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string_view>
#include <vector>

Expand Down Expand Up @@ -58,8 +61,46 @@ class WasmEngine
virtual Result execute(FuncRef func_ref, const std::vector<uint64_t>& args) = 0;
};

/// Throws exception if the signature is non-conformant.
///
/// A function signature consist of input and output types delimited by a colon. Zero number of
/// types is allowed. A type is represented with a single character, where `i` means i32, and
/// `I` means i64.
///
/// As an example `iI:i` translates to `(i32, i64) -> (i32)`, `I:` to `(i64) -> void`, etc.
void validate_function_signature(std::string_view signature);

/// Parses a validated signature and returns a pair of input and output type vectors of type
/// `ValueType`.
///
/// Note that calling `validate_function_signature` first is advised for better error reporting.
template <typename ValueType, ValueType i32_type, ValueType i64_type>
std::pair<std::vector<ValueType>, std::vector<ValueType>> translate_function_signature(
std::string_view signature)
{
constexpr auto translate_valtype = [](char input) {
if (input == 'i')
return i32_type;
else if (input == 'I')
return i64_type;
else
throw std::runtime_error{"invalid type"};
};

const auto delimiter_pos = signature.find(':');
assert(delimiter_pos != std::string_view::npos);
const auto inputs = signature.substr(0, delimiter_pos);
const auto outputs = signature.substr(delimiter_pos + 1);

std::vector<ValueType> input_types;
std::vector<ValueType> output_types;
std::transform(
std::begin(inputs), std::end(inputs), std::back_inserter(input_types), translate_valtype);
std::transform(std::begin(outputs), std::end(outputs), std::back_inserter(output_types),
translate_valtype);
return {std::move(input_types), std::move(output_types)};
}

std::unique_ptr<WasmEngine> create_fizzy_engine();
std::unique_ptr<WasmEngine> create_fizzy_c_engine();
std::unique_ptr<WasmEngine> create_wabt_engine();
Expand Down