2019-01-18 20:13:01 +00:00
|
|
|
use crate::{
|
2019-01-18 22:30:15 +00:00
|
|
|
backing::ImportBacking,
|
2019-01-18 20:13:01 +00:00
|
|
|
error::CompileResult,
|
|
|
|
error::RuntimeResult,
|
|
|
|
module::ModuleInner,
|
|
|
|
types::{FuncIndex, LocalFuncIndex, Value},
|
|
|
|
vm,
|
|
|
|
};
|
2019-02-19 23:36:22 +00:00
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
use crate::{
|
2019-02-22 01:06:49 +00:00
|
|
|
cache::{Artifact, Error as CacheError},
|
2019-02-07 00:26:45 +00:00
|
|
|
module::ModuleInfo,
|
|
|
|
sys::Memory,
|
|
|
|
};
|
2019-03-04 20:57:26 +00:00
|
|
|
use std::{any::Any, ptr::NonNull};
|
2019-01-08 17:09:47 +00:00
|
|
|
|
2019-01-21 19:51:41 +00:00
|
|
|
pub mod sys {
|
|
|
|
pub use crate::sys::*;
|
|
|
|
}
|
2019-01-11 03:59:57 +00:00
|
|
|
pub use crate::sig_registry::SigRegistry;
|
2019-01-09 23:31:11 +00:00
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
|
2019-02-07 00:26:45 +00:00
|
|
|
pub enum Backend {
|
|
|
|
Cranelift,
|
2019-02-08 15:56:14 +00:00
|
|
|
Dynasm,
|
2019-02-08 22:19:58 +00:00
|
|
|
LLVM,
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 20:13:01 +00:00
|
|
|
/// This type cannot be constructed from
|
|
|
|
/// outside the runtime crate.
|
|
|
|
pub struct Token {
|
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Token {
|
|
|
|
pub(crate) fn generate() -> Self {
|
|
|
|
Self { _private: () }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:09:47 +00:00
|
|
|
pub trait Compiler {
|
2019-01-18 20:13:01 +00:00
|
|
|
/// Compiles a `Module` from WebAssembly binary format.
|
|
|
|
/// The `CompileToken` parameter ensures that this can only
|
|
|
|
/// be called from inside the runtime.
|
|
|
|
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner>;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
unsafe fn from_cache(&self, cache: Artifact, _: Token) -> Result<ModuleInner, CacheError>;
|
2019-01-18 20:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The functionality exposed by this trait is expected to be used
|
|
|
|
/// for calling functions exported by a webassembly module from
|
|
|
|
/// host code only.
|
2019-01-28 19:55:44 +00:00
|
|
|
pub trait ProtectedCaller: Send + Sync {
|
2019-01-18 20:13:01 +00:00
|
|
|
/// This calls the exported function designated by `local_func_index`.
|
|
|
|
/// Important to note, this supports calling imported functions that are
|
|
|
|
/// then exported.
|
|
|
|
///
|
|
|
|
/// It's invalid to attempt to call a local function that isn't exported and
|
|
|
|
/// the implementation is expected to check for that. The implementation
|
|
|
|
/// is also expected to check for correct parameter types and correct
|
|
|
|
/// parameter number.
|
|
|
|
///
|
|
|
|
/// The `returns` parameter is filled with dummy values when passed in and upon function
|
|
|
|
/// return, will be filled with the return values of the wasm function, as long as the
|
|
|
|
/// call completed successfully.
|
|
|
|
///
|
|
|
|
/// The existance of the Token parameter ensures that this can only be called from
|
|
|
|
/// within the runtime crate.
|
|
|
|
fn call(
|
|
|
|
&self,
|
|
|
|
module: &ModuleInner,
|
|
|
|
func_index: FuncIndex,
|
|
|
|
params: &[Value],
|
2019-01-18 21:29:43 +00:00
|
|
|
import_backing: &ImportBacking,
|
2019-01-18 20:13:01 +00:00
|
|
|
vmctx: *mut vm::Ctx,
|
|
|
|
_: Token,
|
2019-01-22 23:00:27 +00:00
|
|
|
) -> RuntimeResult<Vec<Value>>;
|
2019-02-08 21:08:03 +00:00
|
|
|
|
|
|
|
fn get_early_trapper(&self) -> Box<dyn UserTrapper>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait UserTrapper {
|
2019-03-04 20:57:26 +00:00
|
|
|
unsafe fn do_early_trap(&self, data: Box<dyn Any>) -> !;
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 19:55:44 +00:00
|
|
|
pub trait FuncResolver: Send + Sync {
|
2019-01-18 20:13:01 +00:00
|
|
|
/// This returns a pointer to the function designated by the `local_func_index`
|
|
|
|
/// parameter.
|
2019-01-16 18:26:10 +00:00
|
|
|
fn get(
|
|
|
|
&self,
|
|
|
|
module: &ModuleInner,
|
|
|
|
local_func_index: LocalFuncIndex,
|
|
|
|
) -> Option<NonNull<vm::Func>>;
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
2019-02-18 19:56:20 +00:00
|
|
|
|
|
|
|
pub trait CacheGen: Send + Sync {
|
2019-02-21 00:41:41 +00:00
|
|
|
fn generate_cache(
|
|
|
|
&self,
|
|
|
|
module: &ModuleInner,
|
|
|
|
) -> Result<(Box<ModuleInfo>, Box<[u8]>, Memory), CacheError>;
|
|
|
|
}
|