tc_executor_common/wasm_runtime.rs
1// This file is part of Tetcore.
2
3// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Definitions for a wasm runtime.
20
21use crate::error::Error;
22use tetcore_wasm_interface::Value;
23
24/// A method to be used to find the entrypoint when calling into the runtime
25///
26/// Contains variants on how to resolve wasm function that will be invoked.
27pub enum InvokeMethod<'a> {
28 /// Call function exported with this name.
29 ///
30 /// Located function should have (u32, u32) -> u64 signature.
31 Export(&'a str),
32 /// Call a function found in the exported table found under the given index.
33 ///
34 /// Located function should have (u32, u32) -> u64 signature.
35 Table(u32),
36 /// Call function by reference from table through a wrapper.
37 ///
38 /// Invoked function (`dispatcher_ref`) function
39 /// should have (u32, u32, u32) -> u64 signature.
40 ///
41 /// `func` will be passed to the invoked function as a first argument.
42 TableWithWrapper {
43 /// Wrapper for the call.
44 ///
45 /// Function pointer, index into runtime exported table.
46 dispatcher_ref: u32,
47 /// Extra argument for dispatch.
48 ///
49 /// Common usage would be to use it as an actual wasm function pointer
50 /// that should be invoked, but can be used as any extra argument on the
51 /// callee side.
52 ///
53 /// This is typically generated and invoked by the runtime itself.
54 func: u32,
55 },
56}
57
58impl<'a> From<&'a str> for InvokeMethod<'a> {
59 fn from(val: &'a str) -> InvokeMethod<'a> {
60 InvokeMethod::Export(val)
61 }
62}
63
64/// A trait that defines an abstract WASM runtime module.
65///
66/// This can be implemented by an execution engine.
67pub trait WasmModule: Sync + Send {
68 /// Create a new instance.
69 fn new_instance(&self) -> Result<Box<dyn WasmInstance>, Error>;
70}
71
72/// A trait that defines an abstract wasm module instance.
73///
74/// This can be implemented by an execution engine.
75pub trait WasmInstance: Send {
76 /// Call a method on this WASM instance.
77 ///
78 /// Before execution, instance is reset.
79 ///
80 /// Returns the encoded result on success.
81 fn call(&self, method: InvokeMethod, data: &[u8]) -> Result<Vec<u8>, Error>;
82
83 /// Call an exported method on this WASM instance.
84 ///
85 /// Before execution, instance is reset.
86 ///
87 /// Returns the encoded result on success.
88 fn call_export(&self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
89 self.call(method.into(), data)
90 }
91
92 /// Get the value from a global with the given `name`.
93 ///
94 /// This method is only suitable for getting immutable globals.
95 fn get_global_const(&self, name: &str) -> Result<Option<Value>, Error>;
96}