-
Notifications
You must be signed in to change notification settings - Fork 34
/
module_factory.hpp
69 lines (55 loc) · 1.75 KB
/
module_factory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <filesystem>
#include "common/buffer_view.hpp"
#include "outcome/custom.hpp"
#include "runtime/runtime_context.hpp"
#include "runtime/types.hpp"
namespace kagome::runtime {
class Module;
struct CompilationError : std::runtime_error {
CompilationError(const std::string &message)
: std::runtime_error(message.c_str()) {}
CompilationError(const std::error_code &ec)
: CompilationError{ec.message()} {}
std::string_view message() const {
return what();
}
};
inline std::error_code make_error_code(CompilationError) {
return Error::COMPILATION_FAILED;
}
template <typename R>
using CompilationOutcome = CustomOutcome<R, CompilationError>;
inline void outcome_throw_as_system_error_with_payload(CompilationError e) {
throw e;
}
class ModuleFactory {
public:
virtual ~ModuleFactory() = default;
/**
* Used as part of filename to separate files of different incompatible
* compilers.
* `std::nullopt` means `path_compiled` stores raw WASM code for
* interpretation.
*/
virtual std::optional<std::string_view> compilerType() const = 0;
/**
* Compile `wasm` code to `path_compiled`.
*/
virtual CompilationOutcome<void> compile(
std::filesystem::path path_compiled,
BufferView wasm,
const RuntimeContext::ContextParams &config) const = 0;
/**
* Load compiled code from `path_compiled`.
*/
virtual CompilationOutcome<std::shared_ptr<Module>> loadCompiled(
std::filesystem::path path_compiled,
const RuntimeContext::ContextParams &config) const = 0;
};
} // namespace kagome::runtime