mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 15:47:58 +00:00
49af986ad4
* Migrate wasmtime backend to wasmtime-api * Port to a newer version of wasmtime * Update to the latest changes. * Rejig the sandbox module a bit * Materialze * Fixes. * executor wasm_runtime fix * Refactor everything * More refactoring * Even more refactorings * More cleaning. * Update to the latest wasmtime * Reformat * Renames * Refactoring and comments. * Docs * Rename FunctionExecutor to host. * Imrpove docs. * fmt * Remove panic * Assert the number of arguments are equal between wasmtime and hostfunc. * Comment a possible panic if there is no corresponding value variant. * Check signature of the entrypoint. * Use git version of wasmtime * Refine and doc the sandbox code. * Comment RefCells. * Update wasmtime to the latest-ish master. This may solve a problem with segfaults. * Apply suggestions from code review Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Use full SHA1 hash of wasmtime commit. * Add a panic message. * Add some documentation * Update wasmtime version to include SIGSEGV fix * Update to crates.io version of wasmtime * Make it work. * Move the creation of memory into `InstanceWrapper::new` * Make `InstanceWrapper` !Send & !Sync * Avoid using `take_mut` * Update client/executor/wasmtime/Cargo.toml Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Limit maximum size of memory. * Rename `init_state` to `with_initialized_state` Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
78 lines
2.5 KiB
Rust
78 lines
2.5 KiB
Rust
// Copyright 2020 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Substrate is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
use crate::host::{HostContext, HostState};
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
|
|
/// A common place to store a reference to the `HostState`.
|
|
///
|
|
/// This structure is passed into each host function handler and retained in the implementation of
|
|
/// `WasmRuntime`. Whenever a call into a runtime method is initiated, the host state is populated
|
|
/// with the state for that runtime method call.
|
|
///
|
|
/// During the execution of the runtime method call, wasm can call imported host functions. When
|
|
/// that happens the host function handler gets a `HostContext` (obtainable through having a
|
|
/// `HostState` reference).
|
|
#[derive(Clone)]
|
|
pub struct StateHolder {
|
|
// This is `Some` only during a call.
|
|
state: Rc<RefCell<Option<HostState>>>,
|
|
}
|
|
|
|
impl StateHolder {
|
|
/// Create a placeholder `StateHolder`.
|
|
pub fn empty() -> StateHolder {
|
|
StateHolder {
|
|
state: Rc::new(RefCell::new(None)),
|
|
}
|
|
}
|
|
|
|
/// Provide `HostState` for the runtime method call and execute the given function `f`.
|
|
///
|
|
/// During the execution of the provided function `with_context` will be callable.
|
|
pub fn with_initialized_state<R, F>(&self, state: HostState, f: F) -> (R, HostState)
|
|
where
|
|
F: FnOnce() -> R,
|
|
{
|
|
*self.state.borrow_mut() = Some(state);
|
|
|
|
let ret = f();
|
|
let state = self
|
|
.state
|
|
.borrow_mut()
|
|
.take()
|
|
.expect("cannot be None since was just assigned; qed");
|
|
|
|
(ret, state)
|
|
}
|
|
|
|
/// Create a `HostContext` from the contained `HostState` and execute the given function `f`.
|
|
///
|
|
/// This function is only callable within closure passed to `init_state`. Otherwise, the passed
|
|
/// context will be `None`.
|
|
pub fn with_context<R, F>(&self, f: F) -> R
|
|
where
|
|
F: FnOnce(Option<HostContext>) -> R,
|
|
{
|
|
let state = self.state.borrow();
|
|
match *state {
|
|
Some(ref state) => f(Some(state.materialize())),
|
|
None => f(None),
|
|
}
|
|
}
|
|
}
|