mod instructions;
pub mod stack;
pub mod wasm;
use crate::{
errors::{InstructionResult, InterpreterResult},
values::InterfaceValue,
};
pub use instructions::Instruction;
use stack::Stack;
use std::{convert::TryFrom, marker::PhantomData};
pub(crate) struct Runtime<'invocation, 'instance, Instance, Export, LocalImport, Memory, MemoryView>
where
Export: wasm::structures::Export + 'instance,
LocalImport: wasm::structures::LocalImport + 'instance,
Memory: wasm::structures::Memory<MemoryView> + 'instance,
MemoryView: wasm::structures::MemoryView,
Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView> + 'instance,
{
invocation_inputs: &'invocation [InterfaceValue],
stack: Stack<InterfaceValue>,
wasm_instance: &'instance mut Instance,
_phantom: PhantomData<(Export, LocalImport, Memory, MemoryView)>,
}
pub(crate) type ExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView> = Box<
dyn Fn(&mut Runtime<Instance, Export, LocalImport, Memory, MemoryView>) -> InstructionResult<()>
+ Send,
>;
pub struct Interpreter<Instance, Export, LocalImport, Memory, MemoryView>
where
Export: wasm::structures::Export,
LocalImport: wasm::structures::LocalImport,
Memory: wasm::structures::Memory<MemoryView>,
MemoryView: wasm::structures::MemoryView,
Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView>,
{
executable_instructions:
Vec<ExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView>>,
}
impl<Instance, Export, LocalImport, Memory, MemoryView>
Interpreter<Instance, Export, LocalImport, Memory, MemoryView>
where
Export: wasm::structures::Export,
LocalImport: wasm::structures::LocalImport,
Memory: wasm::structures::Memory<MemoryView>,
MemoryView: wasm::structures::MemoryView,
Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView>,
{
fn iter(
&self,
) -> impl Iterator<
Item = &ExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView>,
> + '_ {
self.executable_instructions.iter()
}
pub fn run(
&self,
invocation_inputs: &[InterfaceValue],
wasm_instance: &mut Instance,
) -> InterpreterResult<Stack<InterfaceValue>> {
let mut runtime = Runtime {
invocation_inputs,
stack: Stack::new(),
wasm_instance,
_phantom: PhantomData,
};
for executable_instruction in self.iter() {
executable_instruction(&mut runtime)?;
}
Ok(runtime.stack)
}
}
impl<Instance, Export, LocalImport, Memory, MemoryView> TryFrom<&Vec<Instruction>>
for Interpreter<Instance, Export, LocalImport, Memory, MemoryView>
where
Export: wasm::structures::Export,
LocalImport: wasm::structures::LocalImport,
Memory: wasm::structures::Memory<MemoryView>,
MemoryView: wasm::structures::MemoryView,
Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView>,
{
type Error = ();
fn try_from(instructions: &Vec<Instruction>) -> Result<Self, Self::Error> {
let executable_instructions = instructions
.iter()
.map(|instruction| match instruction {
Instruction::ArgumentGet { index } => {
instructions::argument_get(*index, *instruction)
}
Instruction::CallCore { function_index } => {
instructions::call_core(*function_index, *instruction)
}
Instruction::S8FromI32 => instructions::s8_from_i32(*instruction),
Instruction::S8FromI64 => instructions::s8_from_i64(*instruction),
Instruction::S16FromI32 => instructions::s16_from_i32(*instruction),
Instruction::S16FromI64 => instructions::s16_from_i64(*instruction),
Instruction::S32FromI32 => instructions::s32_from_i32(*instruction),
Instruction::S32FromI64 => instructions::s32_from_i64(*instruction),
Instruction::S64FromI32 => instructions::s64_from_i32(*instruction),
Instruction::S64FromI64 => instructions::s64_from_i64(*instruction),
Instruction::I32FromS8 => instructions::i32_from_s8(*instruction),
Instruction::I32FromS16 => instructions::i32_from_s16(*instruction),
Instruction::I32FromS32 => instructions::i32_from_s32(*instruction),
Instruction::I32FromS64 => instructions::i32_from_s64(*instruction),
Instruction::I64FromS8 => instructions::i64_from_s8(*instruction),
Instruction::I64FromS16 => instructions::i64_from_s16(*instruction),
Instruction::I64FromS32 => instructions::i64_from_s32(*instruction),
Instruction::I64FromS64 => instructions::i64_from_s64(*instruction),
Instruction::U8FromI32 => instructions::u8_from_i32(*instruction),
Instruction::U8FromI64 => instructions::u8_from_i64(*instruction),
Instruction::U16FromI32 => instructions::u16_from_i32(*instruction),
Instruction::U16FromI64 => instructions::u16_from_i64(*instruction),
Instruction::U32FromI32 => instructions::u32_from_i32(*instruction),
Instruction::U32FromI64 => instructions::u32_from_i64(*instruction),
Instruction::U64FromI32 => instructions::u64_from_i32(*instruction),
Instruction::U64FromI64 => instructions::u64_from_i64(*instruction),
Instruction::I32FromU8 => instructions::i32_from_u8(*instruction),
Instruction::I32FromU16 => instructions::i32_from_u16(*instruction),
Instruction::I32FromU32 => instructions::i32_from_u32(*instruction),
Instruction::I32FromU64 => instructions::i32_from_u64(*instruction),
Instruction::I64FromU8 => instructions::i64_from_u8(*instruction),
Instruction::I64FromU16 => instructions::i64_from_u16(*instruction),
Instruction::I64FromU32 => instructions::i64_from_u32(*instruction),
Instruction::I64FromU64 => instructions::i64_from_u64(*instruction),
Instruction::StringLiftMemory => instructions::string_lift_memory(*instruction),
Instruction::StringLowerMemory => instructions::string_lower_memory(*instruction),
Instruction::StringSize => instructions::string_size(*instruction),
Instruction::ByteArrayLiftMemory => {
instructions::byte_array_lift_memory(*instruction)
}
Instruction::ByteArrayLowerMemory => {
instructions::byte_array_lower_memory(*instruction)
}
Instruction::ByteArraySize => instructions::byte_array_size(*instruction),
Instruction::RecordLift { type_index } => {
instructions::record_lift(*type_index, *instruction)
}
Instruction::RecordLower { type_index } => {
instructions::record_lower(*type_index, *instruction)
}
Instruction::RecordLiftMemory { type_index } => {
instructions::record_lift_memory(*type_index, *instruction)
}
Instruction::RecordLowerMemory { type_index } => {
instructions::record_lower_memory(*type_index, *instruction)
}
Instruction::Dup => instructions::dup(*instruction),
Instruction::Swap2 => instructions::swap2(*instruction),
})
.collect();
Ok(Interpreter {
executable_instructions,
})
}
}