8 #include <WAVM/Runtime/Linker.h> 9 #include <WAVM/Runtime/Runtime.h> 10 #include <WAVM/WASM/WASM.h> 11 #include <boost/assert.hpp> 24 std::shared_ptr<CompartmentWrapper> compartment,
26 std::shared_ptr<IntrinsicModule> intrinsic_module,
27 std::shared_ptr<const InstanceEnvironmentFactory> env_factory,
28 gsl::span<const uint8_t> code,
30 std::shared_ptr<WAVM::Runtime::Module> module =
nullptr;
31 WAVM::WASM::LoadError loadError;
32 WAVM::IR::FeatureSpec featureSpec;
36 "Compiling WebAssembly module for Runtime (going to take a few dozens " 38 if (!WAVM::Runtime::loadBinaryModule(
39 code.data(), code.size(), module, featureSpec, &loadError)) {
40 logger->critical(
"Error loading WAVM binary module: {}",
45 auto imports = WAVM::Runtime::getModuleIR(module).memories.imports;
46 if (not imports.empty()) {
49 intrinsic_module = std::make_shared<IntrinsicModule>(
53 return std::unique_ptr<ModuleImpl>(
55 std::move(intrinsic_module),
56 std::move(env_factory),
62 std::shared_ptr<CompartmentWrapper> compartment,
63 std::shared_ptr<const IntrinsicModule> intrinsic_module,
64 std::shared_ptr<const InstanceEnvironmentFactory> env_factory,
65 std::shared_ptr<WAVM::Runtime::Module> module,
81 const auto &ir_module = WAVM::Runtime::getModuleIR(
module_);
83 std::find_if(ir_module.imports.cbegin(),
84 ir_module.imports.cend(),
86 return import.kind == WAVM::IR::ExternKind::memory;
88 != ir_module.imports.cend();
93 auto new_intrinsic_module_instance =
94 std::shared_ptr<IntrinsicModuleInstance>(
98 std::make_shared<IntrinsicResolverImpl>(new_intrinsic_module_instance);
100 auto internal_instance =
101 WAVM::Runtime::instantiateModule(
compartment_->getCompartment(),
107 memory_origin, internal_instance, new_intrinsic_module_instance);
109 auto instance = std::make_shared<ModuleInstanceImpl>(
117 auto &ir_module = WAVM::Runtime::getModuleIR(
module_);
119 auto link_result = WAVM::Runtime::linkModule(ir_module, resolver);
120 if (!link_result.success) {
121 logger_->error(
"Failed to link module:");
122 for (
auto &
import : link_result.missingImports) {
126 WAVM::IR::asString(
import.type));
128 throw std::runtime_error(
"Failed to link module");
130 return link_result.resolvedImports;
std::vector< Object * > ImportBindings
ModuleImpl(std::shared_ptr< CompartmentWrapper > compartment, std::shared_ptr< const IntrinsicModule > intrinsic_module, std::shared_ptr< const InstanceEnvironmentFactory > env_factory, std::shared_ptr< WAVM::Runtime::Module > module, const common::Hash256 &code_hash)
outcome::result< std::shared_ptr< ModuleInstance > > instantiate() const override
WAVM::Runtime::ImportBindings link(IntrinsicResolver &resolver) const
Global parameters for module instantiation. Currently contains only memory type that may be changed o...
WAVM::IR::MemoryType intrinsicMemoryType
static const wasm::Name env
std::shared_ptr< soralog::Logger > Logger
static std::unique_ptr< ModuleImpl > compileFrom(std::shared_ptr< CompartmentWrapper > compartment, ModuleParams &module_params, std::shared_ptr< IntrinsicModule > intrinsic_module, std::shared_ptr< const InstanceEnvironmentFactory > env_factory, gsl::span< const uint8_t > code, const common::Hash256 &code_hash)
std::shared_ptr< const InstanceEnvironmentFactory > env_factory_
std::shared_ptr< CompartmentWrapper > compartment_
Logger createLogger(const std::string &tag)
std::shared_ptr< const IntrinsicModule > intrinsic_module_
std::shared_ptr< WAVM::Runtime::Module > module_
void registerHostApiMethods(IntrinsicModule &module)
const common::Hash256 code_hash_