feat(interface-types) Extract InterfaceType and RecordType into its own types module.

This commit is contained in:
Ivan Enderlin 2020-04-09 10:48:50 +02:00
parent 5cb07c9d69
commit 0f1ddce3c9
15 changed files with 91 additions and 85 deletions

View File

@ -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 {

View File

@ -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,
@ -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 = &[

View File

@ -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};
@ -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(

View File

@ -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.

View File

@ -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 {
@ -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.
@ -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() {

View File

@ -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},

View File

@ -164,11 +164,9 @@ where
#[cfg(test)]
pub(crate) mod tests {
use crate::{
ast,
interpreter::wasm::{
self,
values::{InterfaceType, InterfaceValue},
},
ast::*,
interpreter::wasm::{self, values::InterfaceValue},
types::*,
};
use std::{cell::Cell, collections::HashMap, convert::TryInto, ops::Deref, rc::Rc};
@ -265,7 +263,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 {
@ -322,10 +320,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,
@ -351,7 +349,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)
}
}

View File

@ -1,7 +1,7 @@
use crate::{
ast::InterfaceType,
errors::{InstructionError, InstructionErrorKind},
interpreter::{wasm::values::InterfaceValue, Instruction},
types::InterfaceType,
};
use std::convert::TryInto;

View File

@ -1,5 +1,5 @@
use crate::{
ast::{InterfaceType, RecordType, Type, TypeKind},
ast::{Type, TypeKind},
errors::{InstructionError, InstructionErrorKind},
interpreter::wasm::values::FlattenInterfaceValueIterator,
interpreter::{
@ -7,6 +7,7 @@ use crate::{
wasm::values::InterfaceValue,
Instruction,
},
types::{InterfaceType, RecordType},
};
use std::collections::VecDeque;
@ -139,7 +140,7 @@ executable_instruction!(
#[cfg(test)]
mod tests {
use crate::ast::{RecordType, Type};
use super::*;
test_executable_instruction!(
test_record_lift =

View File

@ -1,8 +1,8 @@
use super::to_native;
use crate::{
ast::InterfaceType,
errors::{InstructionError, InstructionErrorKind},
interpreter::{wasm::values::InterfaceValue, Instruction},
types::InterfaceType,
};
use std::{cell::Cell, convert::TryInto};

View File

@ -1,8 +1,8 @@
//! Provides a deserializer from WIT values to Rust value.
use crate::{
ast::InterfaceType,
interpreter::wasm::values::{FlattenInterfaceValueIterator, InterfaceValue},
types::InterfaceType,
};
use serde::{de, Deserialize};
use std::{

View File

@ -1,6 +1,6 @@
//! Defines WIT values and associated operations.
pub use crate::ast::{InterfaceType, RecordType};
pub use crate::types::{InterfaceType, RecordType};
use crate::{errors::WasmValueNativeCastError, vec1::Vec1};
use std::{convert::TryFrom, slice::Iter};

View File

@ -51,6 +51,7 @@
#![doc(html_logo_url = "https://github.com/wasmerio.png")]
pub mod ast;
pub mod types;
#[macro_use]
mod macros;
pub mod decoders;

59
src/types.rs Normal file
View File

@ -0,0 +1,59 @@
//! This module defines the WIT types.
use crate::vec1::Vec1;
/// 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>,
}

View File

@ -1,5 +1,5 @@
use wasmer_interface_types::{
ast::*, decoders::binary::parse, encoders::binary::ToBytes, interpreter::Instruction,
ast::*, decoders::binary::parse, encoders::binary::ToBytes, interpreter::Instruction, types::*,
vec1::Vec1,
};