Skip to content

Commit 5f30ebd

Browse files
committed
test: Refactor translate_signature in wasm_engine
This replaces the implementation in fizzy_engine with a template.
1 parent 47401da commit 5f30ebd

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

test/utils/fizzy_engine.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,6 @@ class FizzyEngine final : public WasmEngine
2828

2929
namespace
3030
{
31-
ValType translate_valtype(char input)
32-
{
33-
if (input == 'i')
34-
return fizzy::ValType::i32;
35-
else if (input == 'I')
36-
return fizzy::ValType::i64;
37-
else
38-
throw std::runtime_error{"invalid type"};
39-
}
40-
41-
FuncType translate_signature(std::string_view signature)
42-
{
43-
const auto delimiter_pos = signature.find(':');
44-
assert(delimiter_pos != std::string_view::npos);
45-
const auto inputs = signature.substr(0, delimiter_pos);
46-
const auto outputs = signature.substr(delimiter_pos + 1);
47-
48-
FuncType func_type;
49-
std::transform(std::begin(inputs), std::end(inputs), std::back_inserter(func_type.inputs),
50-
translate_valtype);
51-
std::transform(std::begin(outputs), std::end(outputs), std::back_inserter(func_type.outputs),
52-
translate_valtype);
53-
return func_type;
54-
}
55-
5631
fizzy::ExecutionResult env_adler32(fizzy::Instance& instance, const fizzy::Value* args, int)
5732
{
5833
assert(instance.memory != nullptr);
@@ -124,7 +99,9 @@ std::optional<WasmEngine::FuncRef> FizzyEngine::find_function(
12499
if (func_idx.has_value())
125100
{
126101
const auto func_type = m_instance->module->get_function_type(*func_idx);
127-
const auto sig_type = translate_signature(signature);
102+
FuncType sig_type;
103+
std::tie(sig_type.inputs, sig_type.outputs) =
104+
translate_function_signature<ValType, ValType::i32, ValType::i64>(signature);
128105
if (sig_type != func_type)
129106
return std::nullopt;
130107
}

test/utils/wasm_engine.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
#pragma once
66

77
#include "bytes.hpp"
8+
#include <algorithm>
9+
#include <cassert>
810
#include <memory>
911
#include <optional>
12+
#include <stdexcept>
1013
#include <string_view>
1114
#include <vector>
1215

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

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

73+
/// Parses a validated signature and returns a pair of input and output type vectors of type
74+
/// `ValueType`.
75+
///
76+
/// Note that calling `validate_function_signature` first is advised for better error reporting.
77+
template <typename ValueType, ValueType i32_type, ValueType i64_type>
78+
std::pair<std::vector<ValueType>, std::vector<ValueType>> translate_function_signature(
79+
std::string_view signature)
80+
{
81+
constexpr auto translate_valtype = [](char input) {
82+
if (input == 'i')
83+
return i32_type;
84+
else if (input == 'I')
85+
return i64_type;
86+
else
87+
throw std::runtime_error{"invalid type"};
88+
};
89+
90+
const auto delimiter_pos = signature.find(':');
91+
assert(delimiter_pos != std::string_view::npos);
92+
const auto inputs = signature.substr(0, delimiter_pos);
93+
const auto outputs = signature.substr(delimiter_pos + 1);
94+
95+
std::vector<ValueType> input_types;
96+
std::vector<ValueType> output_types;
97+
std::transform(
98+
std::begin(inputs), std::end(inputs), std::back_inserter(input_types), translate_valtype);
99+
std::transform(std::begin(outputs), std::end(outputs), std::back_inserter(output_types),
100+
translate_valtype);
101+
return {std::move(input_types), std::move(output_types)};
102+
}
103+
63104
std::unique_ptr<WasmEngine> create_fizzy_engine();
64105
std::unique_ptr<WasmEngine> create_fizzy_c_engine();
65106
std::unique_ptr<WasmEngine> create_wabt_engine();

0 commit comments

Comments
 (0)