Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1370: feat(interface-types) Crate reorganization r=Hywan a=Hywan

This PR re-organizes the crate:

* `interpreter/wasm/values.rs` moves to `values.rs`,
* `interpreter/wasm/serde/*` moves to `serde/*`,
* `types.rs` is new, and includes `InterfaceType` and `RecordType`,
* Update the documentation.

Bonus: `InterfaceValue::Record`  now takes a `Vec1` rather than a `Vec` to match `InterfaceType::Record`.

Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
bors[bot] and Hywan authored Apr 10, 2020
2 parents da1e963 + 3e9c3a1 commit 6661ca7
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 186 deletions.
61 changes: 4 additions & 57 deletions lib/interface-types/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,12 @@
//! Represents the WIT language as a tree. This is the central
//! representation of the language.
use crate::{interpreter::Instruction, vec1::Vec1};
use crate::{
interpreter::Instruction,
types::{InterfaceType, RecordType},
};
use std::str;

/// Represents the types supported by WIT.
#[derive(PartialEq, Debug, Clone)]
pub enum InterfaceType {
/// A 8-bits signed integer.
S8,

/// A 16-bits signed integer.
S16,

/// A 32-bits signed integer.
S32,

/// A 64-bits signed integer.
S64,

/// A 8-bits unsigned integer.
U8,

/// A 16-bits unsigned integer.
U16,

/// A 32-bits unsigned integer.
U32,

/// A 64-bits unsigned integer.
U64,

/// A 32-bits float.
F32,

/// A 64-bits float.
F64,

/// A string.
String,

/// An `any` reference.
Anyref,

/// A 32-bits integer (as defined in WebAssembly core).
I32,

/// A 64-bits integer (as defiend in WebAssembly core).
I64,

/// A record.
Record(RecordType),
}

/// Represents a record type.
#[derive(PartialEq, Debug, Clone)]
pub struct RecordType {
/// Types representing the fields.
pub fields: Vec1<InterfaceType>,
}

/// Represents the kind of type.
#[derive(PartialEq, Debug)]
pub enum TypeKind {
Expand Down
5 changes: 3 additions & 2 deletions lib/interface-types/src/decoders/binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parse the WIT binary representation into an [AST](crate::ast).
use crate::{ast::*, interpreter::Instruction, vec1::Vec1};
use crate::{ast::*, interpreter::Instruction, types::*, vec1::Vec1};
use nom::{
error::{make_error, ErrorKind, ParseError},
Err, IResult,
Expand Down Expand Up @@ -445,9 +445,10 @@ fn interfaces<'input, E: ParseError<&'input [u8]>>(
///
/// ```rust
/// use wasmer_interface_types::{
/// ast::*,
/// ast::{Adapter, Export, Implementation, Import, Interfaces, Type},
/// decoders::binary::parse,
/// interpreter::Instruction,
/// types::InterfaceType,
/// };
///
/// let input = &[
Expand Down
5 changes: 3 additions & 2 deletions lib/interface-types/src/decoders/wat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parse the WIT textual representation into an [AST](crate::ast).
use crate::{ast::*, interpreter::Instruction, vec1::Vec1};
use crate::{ast::*, interpreter::Instruction, types::*, vec1::Vec1};
pub use wast::parser::ParseBuffer as Buffer;
use wast::parser::{self, Cursor, Parse, Parser, Peek, Result};

Expand Down Expand Up @@ -590,9 +590,10 @@ impl<'a> Parse<'a> for Interfaces<'a> {
///
/// ```rust
/// use wasmer_interface_types::{
/// ast::*,
/// ast::{Adapter, Export, Implementation, Import, Interfaces, Type},
/// decoders::wat::{parse, Buffer},
/// interpreter::Instruction,
/// types::InterfaceType,
/// };
///
/// let input = Buffer::new(
Expand Down
2 changes: 1 addition & 1 deletion lib/interface-types/src/encoders/binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Writes the AST into bytes representing WIT with its binary format.
use crate::{ast::*, interpreter::Instruction};
use crate::{ast::*, interpreter::Instruction, types::*};
use std::io::{self, Write};

/// A trait for converting a value to bytes.
Expand Down
7 changes: 4 additions & 3 deletions lib/interface-types/src/encoders/wat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
//!
//! ```rust
//! use wasmer_interface_types::{
//! ast::*,
//! ast::{Adapter, Export, Implementation, Import, Interfaces, Type},
//! encoders::wat::*,
//! interpreter::Instruction,
//! types::InterfaceType,
//! };
//!
//! let input: String = (&Interfaces {
Expand Down Expand Up @@ -54,7 +55,7 @@
//! assert_eq!(input, output);
//! ```
use crate::{ast::*, interpreter::Instruction};
use crate::{ast::*, interpreter::Instruction, types::*};
use std::string::ToString;

/// Encode an `InterfaceType` into a string.
Expand Down Expand Up @@ -346,7 +347,7 @@ impl<'input> ToString for &Interfaces<'input> {

#[cfg(test)]
mod tests {
use crate::{ast::*, interpreter::Instruction};
use super::*;

#[test]
fn test_interface_types() {
Expand Down
5 changes: 1 addition & 4 deletions lib/interface-types/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! The error module contains all the data structures that represent
//! an error.
use crate::{
ast::{InterfaceType, TypeKind},
interpreter::Instruction,
};
use crate::{ast::TypeKind, interpreter::Instruction, types::InterfaceType};
use std::{
error::Error,
fmt::{self, Display, Formatter},
Expand Down
6 changes: 2 additions & 4 deletions lib/interface-types/src/interpreter/instructions/call_core.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::{
errors::{InstructionError, InstructionErrorKind},
interpreter::wasm::{
structures::{FunctionIndex, TypedIndex},
values::InterfaceType,
},
interpreter::wasm::structures::{FunctionIndex, TypedIndex},
interpreter::Instruction,
types::InterfaceType,
};

executable_instruction!(
Expand Down
18 changes: 6 additions & 12 deletions lib/interface-types/src/interpreter/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod strings;

use crate::{
errors::{InstructionError, InstructionErrorKind, InstructionResult, WasmValueNativeCastError},
interpreter::wasm::values::{InterfaceValue, NativeType},
values::{InterfaceValue, NativeType},
};
pub(crate) use argument_get::argument_get;
pub(crate) use call_core::call_core;
Expand Down Expand Up @@ -163,13 +163,7 @@ where

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

pub(crate) struct Export {
Expand Down Expand Up @@ -265,7 +259,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>,
pub(crate) wit_types: Vec<Type>,
}

impl Instance {
Expand Down Expand Up @@ -322,10 +316,10 @@ pub(crate) mod tests {
hashmap
},
memory: Memory::new(vec![Cell::new(0); 128]),
wit_types: vec![ast::Type::Record(ast::RecordType {
wit_types: vec![Type::Record(RecordType {
fields: vec1![
InterfaceType::I32,
InterfaceType::Record(ast::RecordType {
InterfaceType::Record(RecordType {
fields: vec1![InterfaceType::String, InterfaceType::F32],
}),
InterfaceType::I64,
Expand All @@ -351,7 +345,7 @@ pub(crate) mod tests {
Some(&self.memory)
}

fn wit_type(&self, index: u32) -> Option<&ast::Type> {
fn wit_type(&self, index: u32) -> Option<&Type> {
self.wit_types.get(index as usize)
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/interface-types/src/interpreter/instructions/numbers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
ast::InterfaceType,
errors::{InstructionError, InstructionErrorKind},
interpreter::{wasm::values::InterfaceValue, Instruction},
interpreter::Instruction,
types::InterfaceType,
values::InterfaceValue,
};
use std::convert::TryInto;

Expand Down
51 changes: 29 additions & 22 deletions lib/interface-types/src/interpreter/instructions/records.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
ast::{InterfaceType, RecordType, Type, TypeKind},
ast::{Type, TypeKind},
errors::{InstructionError, InstructionErrorKind},
interpreter::wasm::values::FlattenInterfaceValueIterator,
interpreter::{
stack::{Stack, Stackable},
wasm::values::InterfaceValue,
Instruction,
},
types::{InterfaceType, RecordType},
values::{FlattenInterfaceValueIterator, InterfaceValue},
vec1::Vec1,
};
use std::collections::VecDeque;

Expand Down Expand Up @@ -56,7 +57,10 @@ fn record_lift_(
}
}

Ok(InterfaceValue::Record(values.into_iter().collect()))
Ok(InterfaceValue::Record(
Vec1::new(values.into_iter().collect())
.expect("Record must have at least one field, zero given"), // normally unreachable because of the type-checking
))
}

executable_instruction!(
Expand Down Expand Up @@ -110,7 +114,7 @@ executable_instruction!(
};

match runtime.stack.pop1() {
Some(InterfaceValue::Record(record_values)) if record_type == &(&record_values).into() => {
Some(InterfaceValue::Record(record_values)) if record_type == &(&*record_values).into() => {
let values = FlattenInterfaceValueIterator::new(&record_values);

for value in values {
Expand Down Expand Up @@ -139,7 +143,7 @@ executable_instruction!(

#[cfg(test)]
mod tests {
use crate::ast::{RecordType, Type};
use super::*;

test_executable_instruction!(
test_record_lift =
Expand All @@ -157,9 +161,9 @@ mod tests {
InterfaceValue::I64(3),
],
instance: Instance::new(),
stack: [InterfaceValue::Record(vec![
stack: [InterfaceValue::Record(vec1![
InterfaceValue::I32(1),
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::String("Hello".to_string()),
InterfaceValue::F32(2.),
]),
Expand All @@ -171,11 +175,14 @@ mod tests {
#[test]
#[allow(non_snake_case, unused)]
fn test_record_lift__to_rust_struct() {
use crate::interpreter::{
instructions::tests::{Export, Instance, LocalImport, Memory, MemoryView},
stack::Stackable,
wasm::values::{from_interface_values, InterfaceType, InterfaceValue},
Instruction, Interpreter,
use crate::{
interpreter::{
instructions::tests::{Export, Instance, LocalImport, Memory, MemoryView},
stack::Stackable,
Instruction, Interpreter,
},
types::InterfaceType,
values::{from_interface_values, InterfaceValue},
};
use serde::Deserialize;
use std::{cell::Cell, collections::HashMap, convert::TryInto};
Expand Down Expand Up @@ -252,7 +259,7 @@ mod tests {

instance
},
stack: [InterfaceValue::Record(vec![
stack: [InterfaceValue::Record(vec1![
InterfaceValue::I32(1),
InterfaceValue::I32(2),
])],
Expand Down Expand Up @@ -295,9 +302,9 @@ mod tests {
Instruction::RecordLower { type_index: 0 },
],
invocation_inputs: [
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::I32(1),
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::String("Hello".to_string()),
InterfaceValue::F32(2.),
]),
Expand All @@ -321,9 +328,9 @@ mod tests {
Instruction::RecordLift { type_index: 0 },
],
invocation_inputs: [
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::I32(1),
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::String("Hello".to_string()),
InterfaceValue::F32(2.),
]),
Expand All @@ -332,9 +339,9 @@ mod tests {
],
instance: Instance::new(),
stack: [
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::I32(1),
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::String("Hello".to_string()),
InterfaceValue::F32(2.),
]),
Expand Down Expand Up @@ -363,9 +370,9 @@ mod tests {
Instruction::RecordLower { type_index: 0 },
],
invocation_inputs: [
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::I32(1),
InterfaceValue::Record(vec![
InterfaceValue::Record(vec1![
InterfaceValue::String("Hello".to_string()),
]),
InterfaceValue::I64(3),
Expand Down
5 changes: 3 additions & 2 deletions lib/interface-types/src/interpreter/instructions/strings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::to_native;
use crate::{
ast::InterfaceType,
errors::{InstructionError, InstructionErrorKind},
interpreter::{wasm::values::InterfaceValue, Instruction},
interpreter::Instruction,
types::InterfaceType,
values::InterfaceValue,
};
use std::{cell::Cell, convert::TryInto};

Expand Down
Loading

0 comments on commit 6661ca7

Please sign in to comment.