-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Fizzy: A fast WebAssembly interpreter | ||
// Copyright 2019-2020 The Fizzy Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <fizzy/fizzy.h> | ||
|
||
#include <test/utils/adler32.hpp> | ||
#include <test/utils/wasm_engine.hpp> | ||
#include <cassert> | ||
#include <cstring> | ||
|
||
namespace fizzy::test | ||
{ | ||
class FizzyCEngine final : public WasmEngine | ||
{ | ||
FizzyInstance* m_instance = nullptr; | ||
|
||
public: | ||
~FizzyCEngine(); | ||
|
||
bool parse(bytes_view input) const final; | ||
std::optional<FuncRef> find_function( | ||
std::string_view name, std::string_view signature) const final; | ||
bool instantiate(bytes_view wasm_binary) final; | ||
bool init_memory(bytes_view memory) final; | ||
bytes_view get_memory() const final; | ||
Result execute(FuncRef func_ref, const std::vector<uint64_t>& args) final; | ||
}; | ||
|
||
namespace | ||
{ | ||
FizzyExecutionResult env_adler32( | ||
void*, FizzyInstance* /*instance*/, const FizzyValue* /*args*/, size_t, int) | ||
{ | ||
// assert(instance.memory != nullptr); | ||
// const auto ret = fizzy::test::adler32( | ||
// bytes_view{*instance.memory}.substr(args[0].as<uint32_t>(), args[1].as<uint32_t>())); | ||
// return Value{ret}; | ||
return {false, true, {0}}; | ||
} | ||
} // namespace | ||
|
||
std::unique_ptr<WasmEngine> create_fizzy_c_engine() | ||
{ | ||
return std::make_unique<FizzyCEngine>(); | ||
} | ||
|
||
FizzyCEngine::~FizzyCEngine() | ||
{ | ||
if (m_instance) | ||
fizzy_free_instance(m_instance); | ||
} | ||
|
||
bool FizzyCEngine::parse(bytes_view input) const | ||
{ | ||
const auto module = fizzy_parse(input.data(), input.size()); | ||
if (!module) | ||
return false; | ||
|
||
fizzy_free_module(module); | ||
return true; | ||
} | ||
|
||
bool FizzyCEngine::instantiate(bytes_view wasm_binary) | ||
{ | ||
const auto module = fizzy_parse(wasm_binary.data(), wasm_binary.size()); | ||
if (!module) | ||
return false; | ||
|
||
// TODO resolve_imported_functions | ||
FizzyExternalFunction imports[] = {{env_adler32, nullptr}}; | ||
m_instance = fizzy_instantiate(module, imports, 1); | ||
|
||
assert(m_instance != nullptr); | ||
return true; | ||
} | ||
|
||
bool FizzyCEngine::init_memory(bytes_view /*memory*/) | ||
{ | ||
// if (m_instance->memory == nullptr || m_instance->memory->size() < memory.size()) | ||
// return false; | ||
// | ||
// std::memcpy(m_instance->memory->data(), memory.data(), memory.size()); | ||
// return true; | ||
return false; | ||
} | ||
|
||
bytes_view FizzyCEngine::get_memory() const | ||
{ | ||
// if (!m_instance->memory) | ||
// return {}; | ||
// | ||
// return {m_instance->memory->data(), m_instance->memory->size()}; | ||
return {}; | ||
} | ||
|
||
std::optional<WasmEngine::FuncRef> FizzyCEngine::find_function( | ||
std::string_view /*name*/, std::string_view /*signature*/) const | ||
{ | ||
// const auto func_idx = fizzy::find_exported_function(m_instance->module, name); | ||
// if (func_idx.has_value()) | ||
// { | ||
// const auto func_type = m_instance->module.get_function_type(*func_idx); | ||
// const auto sig_type = translate_signature(signature); | ||
// if (sig_type != func_type) | ||
// return std::nullopt; | ||
// } | ||
// return func_idx; | ||
return std::nullopt; | ||
} | ||
|
||
WasmEngine::Result FizzyCEngine::execute( | ||
WasmEngine::FuncRef func_ref, const std::vector<uint64_t>& args) | ||
{ | ||
static_assert(sizeof(uint64_t) == sizeof(FizzyValue)); | ||
const auto first_arg = reinterpret_cast<const FizzyValue*>(args.data()); | ||
const auto status = fizzy_execute( | ||
m_instance, static_cast<uint32_t>(func_ref), first_arg, args.size(), 0); | ||
if (status.trapped) | ||
return {true, std::nullopt}; | ||
else if (status.has_value) | ||
return {false, status.value.i64}; | ||
else | ||
return {false, std::nullopt}; | ||
} | ||
} // namespace fizzy::test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters