25 std::shared_ptr<ModuleInstance> instance)
26 :
pool_{std::move(pool)},
30 if (
auto pool =
pool_.lock()) {
41 return instance_->callExportFunction(name, encoded_args);
44 outcome::result<std::optional<WasmValue>>
getGlobal(
45 std::string_view name)
const override {
50 return instance_->forDataSegment(callback);
62 std::weak_ptr<RuntimeInstancesPool>
pool_;
67 outcome::result<std::shared_ptr<ModuleInstance>>
70 std::scoped_lock guard{mt_};
71 auto &pool = pools_[state];
73 if (not pool.empty()) {
74 auto top = std::move(pool.top());
76 return std::make_shared<BorrowedInstance>(
77 weak_from_this(), state, std::move(top));
80 auto opt_module = modules_.get(state);
81 BOOST_ASSERT(opt_module.has_value());
82 auto module = opt_module.value();
83 OUTCOME_TRY(instance, module.get()->instantiate());
85 return std::make_shared<BorrowedInstance>(
86 weak_from_this(), state, std::move(instance));
91 std::shared_ptr<ModuleInstance> &&instance) {
92 std::lock_guard guard{mt_};
93 auto &pool = pools_[state];
95 pool.emplace(std::move(instance));
100 std::lock_guard guard{mt_};
101 return modules_.get(state);
106 std::shared_ptr<Module> module) {
107 std::lock_guard guard{mt_};
108 modules_.put(state, std::move(module));
std::function< void(SegmentOffset, SegmentData)> DataSegmentProcessor
outcome::result< PtrSize > callExportFunction(std::string_view name, common::BufferView encoded_args) const override
InstanceEnvironment const & getEnvironment() const override
BorrowedInstance(std::weak_ptr< RuntimeInstancesPool > pool, const RuntimeInstancesPool::RootHash &state, std::shared_ptr< ModuleInstance > instance)
std::optional< std::shared_ptr< Module > > getModule(const RootHash &state)
Get the module for state from internal cache.
RuntimeInstancesPool::RootHash state_
void putModule(const RootHash &state, std::shared_ptr< Module > module)
Puts new module into internal cache.
outcome::result< std::shared_ptr< ModuleInstance > > tryAcquire(const RootHash &state)
Instantiate new or reuse existing ModuleInstance for the provided state.
Wrapper type over sptr<ModuleInstance>. Allows to return instance back to the ModuleInstancePool upon...
std::shared_ptr< ModuleInstance > instance_
void forDataSegment(DataSegmentProcessor const &callback) const override
std::weak_ptr< RuntimeInstancesPool > pool_
outcome::result< void > resetEnvironment() override
const common::Hash256 & getCodeHash() const override
void release(const RootHash &state, std::shared_ptr< ModuleInstance > &&instance)
Releases the module instance (returns it to the pool)
outcome::result< std::optional< WasmValue > > getGlobal(std::string_view name) const override