2019-09-19 23:05:17 +02:00
|
|
|
use crate::instructions::{
|
|
|
|
stack::{Stack, Stackable},
|
2019-09-20 00:06:15 +02:00
|
|
|
wasm, Instruction,
|
2019-09-19 23:05:17 +02:00
|
|
|
};
|
2019-09-19 00:18:36 +02:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2019-09-20 00:06:15 +02:00
|
|
|
struct Runtime<'invocation, 'instance, Instance>
|
|
|
|
where
|
|
|
|
Instance: wasm::Instance,
|
|
|
|
{
|
|
|
|
invocation_inputs: &'invocation Vec<u64>,
|
|
|
|
stack: Stack<u64>,
|
|
|
|
wasm_instance: &'instance Instance,
|
|
|
|
}
|
2019-09-19 00:18:36 +02:00
|
|
|
|
2019-09-20 00:06:15 +02:00
|
|
|
pub(crate) struct Interpreter<Instance>
|
|
|
|
where
|
|
|
|
Instance: wasm::Instance,
|
|
|
|
{
|
|
|
|
executable_instructions: Vec<Box<dyn Fn(&mut Runtime<Instance>) -> Result<(), String>>>,
|
2019-09-19 00:18:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 00:06:15 +02:00
|
|
|
impl<Instance> Interpreter<Instance>
|
|
|
|
where
|
|
|
|
Instance: wasm::Instance,
|
|
|
|
{
|
|
|
|
fn iter(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = &Box<dyn Fn(&mut Runtime<Instance>) -> Result<(), String>>> + '_ {
|
2019-09-19 23:05:17 +02:00
|
|
|
self.executable_instructions.iter()
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:06:15 +02:00
|
|
|
pub(crate) fn run(
|
|
|
|
&self,
|
|
|
|
invocation_inputs: &Vec<u64>,
|
|
|
|
wasm_instance: &Instance,
|
|
|
|
) -> Result<Stack<u64>, String> {
|
|
|
|
let mut runtime = Runtime {
|
|
|
|
invocation_inputs,
|
|
|
|
stack: Stack::new(),
|
|
|
|
wasm_instance,
|
|
|
|
};
|
2019-09-19 23:05:17 +02:00
|
|
|
|
|
|
|
for executable_instruction in self.iter() {
|
2019-09-20 00:06:15 +02:00
|
|
|
match executable_instruction(&mut runtime) {
|
2019-09-19 23:05:17 +02:00
|
|
|
Ok(_) => continue,
|
|
|
|
Err(message) => return Err(message),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:06:15 +02:00
|
|
|
Ok(runtime.stack)
|
2019-09-19 00:18:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:06:15 +02:00
|
|
|
impl<'binary_input, Instance> TryFrom<&Vec<Instruction<'binary_input>>> for Interpreter<Instance>
|
|
|
|
where
|
|
|
|
Instance: wasm::Instance,
|
|
|
|
{
|
|
|
|
type Error = String;
|
2019-09-19 00:18:36 +02:00
|
|
|
|
|
|
|
fn try_from(instructions: &Vec<Instruction>) -> Result<Self, Self::Error> {
|
|
|
|
let executable_instructions = instructions
|
|
|
|
.iter()
|
2019-09-20 00:06:15 +02:00
|
|
|
.map(
|
|
|
|
|instruction| -> Box<dyn Fn(&mut Runtime<Instance>) -> Result<(), String>> {
|
|
|
|
match instruction {
|
|
|
|
Instruction::ArgumentGet(index) => {
|
|
|
|
let index = index.to_owned();
|
|
|
|
let instruction_name: String = instruction.into();
|
|
|
|
|
|
|
|
Box::new(move |runtime: &mut Runtime<Instance>| -> Result<(), _> {
|
|
|
|
let invocation_inputs = runtime.invocation_inputs;
|
|
|
|
|
|
|
|
if index >= (invocation_inputs.len() as u64) {
|
|
|
|
return Err(format!(
|
|
|
|
"`{}` cannot access argument #{} because it does't exist.",
|
|
|
|
instruction_name, index
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.stack.push(invocation_inputs[index as usize]);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Instruction::CallExport(export_name) => {
|
|
|
|
let export_name = (*export_name).to_owned();
|
|
|
|
|
|
|
|
Box::new(move |_runtime: &mut Runtime<Instance>| -> Result<(), _> {
|
|
|
|
println!("call export {}", export_name);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Instruction::ReadUtf8 => {
|
|
|
|
Box::new(|_runtime: &mut Runtime<Instance>| -> Result<(), _> {
|
|
|
|
println!("read utf8");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Instruction::Call(index) => {
|
|
|
|
let index = index.to_owned();
|
|
|
|
|
|
|
|
Box::new(move |_runtime: &mut Runtime<Instance>| -> Result<(), _> {
|
|
|
|
println!("call {}", index);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
2019-09-19 00:18:36 +02:00
|
|
|
}
|
2019-09-20 00:06:15 +02:00
|
|
|
},
|
|
|
|
)
|
2019-09-19 00:25:28 +02:00
|
|
|
.collect();
|
2019-09-19 00:18:36 +02:00
|
|
|
|
|
|
|
Ok(Interpreter {
|
2019-09-19 23:05:17 +02:00
|
|
|
executable_instructions,
|
2019-09-19 00:18:36 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Interpreter;
|
2019-09-20 00:06:15 +02:00
|
|
|
use crate::instructions::{stack::Stackable, wasm, Instruction};
|
|
|
|
use std::{collections::HashMap, convert::TryInto};
|
|
|
|
|
|
|
|
struct Instance {
|
|
|
|
exports: HashMap<String, ()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Instance {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
exports: {
|
|
|
|
let mut hashmap = HashMap::new();
|
|
|
|
hashmap.insert("foo".into(), ());
|
|
|
|
|
|
|
|
hashmap
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl wasm::Instance for Instance {
|
|
|
|
fn export_exists(&self, export_name: &str) -> bool {
|
|
|
|
self.exports.contains_key(export_name)
|
|
|
|
}
|
|
|
|
}
|
2019-09-19 00:18:36 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_interpreter_from_instructions() {
|
|
|
|
let instructions = vec![
|
|
|
|
Instruction::ArgumentGet(0),
|
|
|
|
Instruction::ArgumentGet(0),
|
2019-09-20 00:06:15 +02:00
|
|
|
Instruction::CallExport("foo"),
|
2019-09-19 00:18:36 +02:00
|
|
|
Instruction::ReadUtf8,
|
|
|
|
Instruction::Call(7),
|
|
|
|
];
|
2019-09-20 00:06:15 +02:00
|
|
|
let interpreter: Interpreter<()> = (&instructions).try_into().unwrap();
|
2019-09-19 23:05:17 +02:00
|
|
|
|
|
|
|
assert_eq!(interpreter.executable_instructions.len(), 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_interpreter_argument_get() {
|
2019-09-20 00:06:15 +02:00
|
|
|
let interpreter: Interpreter<Instance> =
|
|
|
|
(&vec![Instruction::ArgumentGet(0)]).try_into().unwrap();
|
|
|
|
let invocation_inputs = vec![42];
|
|
|
|
let instance = Instance::new();
|
|
|
|
let run = interpreter.run(&invocation_inputs, &instance);
|
2019-09-19 23:05:17 +02:00
|
|
|
|
|
|
|
assert!(run.is_ok());
|
|
|
|
|
|
|
|
let stack = run.unwrap();
|
2019-09-19 00:18:36 +02:00
|
|
|
|
2019-09-19 23:05:17 +02:00
|
|
|
assert_eq!(stack.as_slice(), &[42]);
|
2019-09-19 00:18:36 +02:00
|
|
|
}
|
2019-09-20 00:06:15 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_interpreter_argument_get_invalid_index() {
|
|
|
|
let interpreter: Interpreter<Instance> =
|
|
|
|
(&vec![Instruction::ArgumentGet(1)]).try_into().unwrap();
|
|
|
|
let invocation_inputs = vec![42];
|
|
|
|
let instance = Instance::new();
|
|
|
|
let run = interpreter.run(&invocation_inputs, &instance);
|
|
|
|
|
|
|
|
assert!(run.is_err());
|
|
|
|
|
|
|
|
let error = run.unwrap_err();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
error,
|
|
|
|
String::from("`arg.get 1` cannot access argument #1 because it does't exist.")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_interpreter_argument_get_argument_get() {
|
|
|
|
let interpreter: Interpreter<Instance> =
|
|
|
|
(&vec![Instruction::ArgumentGet(0), Instruction::ArgumentGet(1)])
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let invocation_inputs = vec![7, 42];
|
|
|
|
let instance = Instance::new();
|
|
|
|
let run = interpreter.run(&invocation_inputs, &instance);
|
|
|
|
|
|
|
|
assert!(run.is_ok());
|
|
|
|
|
|
|
|
let stack = run.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(stack.as_slice(), &[7, 42]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
#[test]
|
|
|
|
fn test_interpreter_call_export() {
|
|
|
|
let interpreter: Interpreter<Instance> =
|
|
|
|
(&vec![Instruction::ArgumentGet(7), Instruction::ArgumentGet(42)])
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let run = interpreter.run(&Instance::new());
|
|
|
|
|
|
|
|
assert!(run.is_ok());
|
|
|
|
|
|
|
|
let stack = run.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(stack.as_slice(), &[]);
|
|
|
|
}
|
|
|
|
*/
|
2019-09-19 00:18:36 +02:00
|
|
|
}
|