Skip to content

Commit

Permalink
!temp Start implementing record.lift.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Mar 31, 2020
1 parent 0af1076 commit 1c0f894
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
25 changes: 22 additions & 3 deletions lib/interface-types/src/interpreter/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod argument_get;
mod call_core;
mod numbers;
mod records;
mod strings;

use crate::{
Expand All @@ -10,6 +11,7 @@ use crate::{
pub(crate) use argument_get::argument_get;
pub(crate) use call_core::call_core;
pub(crate) use numbers::*;
pub(crate) use records::*;
use std::convert::TryFrom;
pub(crate) use strings::*;

Expand Down Expand Up @@ -158,9 +160,12 @@ where

#[cfg(test)]
pub(crate) mod tests {
use crate::interpreter::wasm::{
self,
values::{InterfaceType, InterfaceValue},
use crate::{
ast,
interpreter::wasm::{
self,
values::{InterfaceType, InterfaceValue},
},
};
use std::{cell::Cell, collections::HashMap, convert::TryInto, ops::Deref, rc::Rc};

Expand Down Expand Up @@ -257,6 +262,7 @@ pub(crate) mod tests {
pub(crate) exports: HashMap<String, Export>,
pub(crate) locals_or_imports: HashMap<usize, LocalImport>,
pub(crate) memory: Memory,
pub(crate) wit_types: Vec<ast::Type>,
}

impl Instance {
Expand Down Expand Up @@ -313,6 +319,15 @@ pub(crate) mod tests {
hashmap
},
memory: Memory::new(vec![Cell::new(0); 128]),
wit_types: vec![ast::Type::Record(ast::RecordType {
fields: vec![
InterfaceType::I32,
InterfaceType::Record(ast::RecordType {
fields: vec![InterfaceType::String, InterfaceType::F32],
}),
InterfaceType::I64,
],
})],
}
}
}
Expand All @@ -332,5 +347,9 @@ pub(crate) mod tests {
fn memory(&self, _index: usize) -> Option<&Memory> {
Some(&self.memory)
}

fn wit_type(&self, index: u32) -> Option<&ast::Type> {
self.wit_types.get(index as usize)
}
}
}
65 changes: 65 additions & 0 deletions lib/interface-types/src/interpreter/instructions/records.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::{
ast::{InterfaceType, RecordType, Type},
interpreter::{wasm::values::InterfaceValue, Instruction},
};

fn record_size(record_type: &RecordType) -> usize {
record_type.fields.iter().fold(0, |acc, field| {
acc + match field {
InterfaceType::Record(record_type) => record_size(&record_type),
_ => 1,
}
})
}

executable_instruction!(
record_lift(type_index: u32, _instruction: Instruction) -> _ {
move |runtime| -> _ {
let instance = &runtime.wasm_instance;
let record_type = match instance.wit_type(type_index).unwrap() {
Type::Record(record_type) => record_type,
_ => panic!("aïe"),
};
let record_size = record_size(&record_type);

dbg!(&record_size);

let values = runtime.stack.pop(record_size).unwrap();
// check that the type matches
let record = InterfaceValue::Record(values);

dbg!(&record);

runtime.stack.push(record);

Ok(())
}
}
);

#[cfg(test)]
mod tests {
test_executable_instruction!(
test_record_lift =
instructions: [
Instruction::ArgumentGet { index: 0 },
Instruction::ArgumentGet { index: 1 },
Instruction::ArgumentGet { index: 2 },
Instruction::ArgumentGet { index: 3 },
Instruction::RecordLift { type_index: 0 },
],
invocation_inputs: [
InterfaceValue::I32(1),
InterfaceValue::String("Hello".to_string()),
InterfaceValue::F32(2.),
InterfaceValue::I64(3),
],
instance: Instance::new(),
stack: [InterfaceValue::Record(vec![
InterfaceValue::I32(1),
InterfaceValue::String("Hello".to_string()),
InterfaceValue::F32(2.),
InterfaceValue::I64(3),
])],
);
}
4 changes: 3 additions & 1 deletion lib/interface-types/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ where
}
Instruction::StringSize => instructions::string_size(*instruction),

Instruction::RecordLift { type_index: _ } => todo!(),
Instruction::RecordLift { type_index } => {
instructions::record_lift(*type_index, *instruction)
}
})
.collect();

Expand Down
10 changes: 9 additions & 1 deletion lib/interface-types/src/interpreter/wasm/structures.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(missing_docs)]

use super::values::{InterfaceType, InterfaceValue};
use crate::{
ast,
interpreter::wasm::values::{InterfaceType, InterfaceValue},
};
use std::{cell::Cell, ops::Deref};

pub trait TypedIndex: Copy + Clone {
Expand Down Expand Up @@ -74,6 +77,7 @@ where
fn export(&self, export_name: &str) -> Option<&E>;
fn local_or_import<I: TypedIndex + LocalImportIndex>(&mut self, index: I) -> Option<&LI>;
fn memory(&self, index: usize) -> Option<&M>;
fn wit_type(&self, index: u32) -> Option<&ast::Type>;
}

impl Export for () {
Expand Down Expand Up @@ -156,4 +160,8 @@ where
fn local_or_import<I: TypedIndex + LocalImportIndex>(&mut self, _index: I) -> Option<&LI> {
None
}

fn wit_type(&self, _index: u32) -> Option<&ast::Type> {
None
}
}

0 comments on commit 1c0f894

Please sign in to comment.