Kagome
Polkadot Runtime Engine in C++17
intrinsic_module.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_RUNTIME_WAVM_IMPL_INTRINSIC_MODULE_HPP
7 #define KAGOME_CORE_RUNTIME_WAVM_IMPL_INTRINSIC_MODULE_HPP
8 
10 
11 #include <boost/assert.hpp>
12 #include <unordered_map>
13 
16 
17 namespace kagome::runtime::wavm {
18 
19  class IntrinsicModule final {
20  public:
21  static constexpr std::string_view kIntrinsicMemoryName = "Runtime Memory";
22 
23  explicit IntrinsicModule(std::shared_ptr<CompartmentWrapper> compartment,
24  WAVM::IR::MemoryType intrinsic_memory_type)
25  : compartment_{compartment},
26  intrinsic_memory_type_{intrinsic_memory_type},
27  memory_{
28  &module_, kIntrinsicMemoryName.data(), intrinsic_memory_type} {}
29 
31  WAVM::IR::MemoryType intrinsic_memory_type)
32  : compartment_{module.compartment_},
33  intrinsic_memory_type_{intrinsic_memory_type},
34  memory_{
35  &module_, kIntrinsicMemoryName.data(), intrinsic_memory_type} {}
36 
37  std::unique_ptr<IntrinsicModuleInstance> instantiate() const {
38  BOOST_ASSERT_MSG(
39  !functions_.empty(),
40  "Host API methods are not registered within IntrinsicModule! See "
41  "runtime/wavm/intrinsics/intrinsic_functions.hpp");
42  return std::make_unique<IntrinsicModuleInstance>(
43  WAVM::Intrinsics::instantiateModule(compartment_->getCompartment(),
44  {&module_},
45  "Intrinsic Module Instance"),
48  }
49 
50  template <typename Ret, typename... Args>
51  void addFunction(std::string_view name,
52  Ret (*f)(WAVM::Runtime::ContextRuntimeData *, Args...),
53  WAVM::IR::FunctionType type) {
54  auto inferred_type = WAVM::Intrinsics::inferIntrinsicFunctionType(f);
55  BOOST_ASSERT(type.results() == inferred_type.results());
56  BOOST_ASSERT(type.params() == inferred_type.params());
57  auto intrinsic = std::make_unique<WAVM::Intrinsics::Function>(
58  &module_, name.data(), (void *)f, inferred_type);
59  functions_.insert(std::pair{name, std::move(intrinsic)});
60  }
61 
62  private:
63  std::shared_ptr<CompartmentWrapper> compartment_;
64  WAVM::IR::MemoryType intrinsic_memory_type_;
65  WAVM::Intrinsics::Module module_;
66 
67  // actually used, because added to the intrinsic module
68  WAVM::Intrinsics::Memory memory_;
69 
70  std::unordered_map<std::string, std::unique_ptr<WAVM::Intrinsics::Function>>
72  };
73 
74 } // namespace kagome::runtime::wavm
75 
76 #endif // KAGOME_CORE_RUNTIME_WAVM_IMPL_INTRINSIC_MODULE_HPP
std::unique_ptr< IntrinsicModuleInstance > instantiate() const
IntrinsicModule(IntrinsicModule &module, WAVM::IR::MemoryType intrinsic_memory_type)
IntrinsicModule(std::shared_ptr< CompartmentWrapper > compartment, WAVM::IR::MemoryType intrinsic_memory_type)
std::unordered_map< std::string, std::unique_ptr< WAVM::Intrinsics::Function > > functions_
void addFunction(std::string_view name, Ret(*f)(WAVM::Runtime::ContextRuntimeData *, Args...), WAVM::IR::FunctionType type)
std::shared_ptr< CompartmentWrapper > compartment_
static constexpr std::string_view kIntrinsicMemoryName