From 0f1ddce3c94bc36c3b5a90d3cf1aaa27e288d95e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 9 Apr 2020 10:48:50 +0200 Subject: [PATCH] feat(interface-types) Extract `InterfaceType` and `RecordType` into its own `types` module. --- src/ast.rs | 61 ++----------------------- src/decoders/binary.rs | 5 +- src/decoders/wat.rs | 5 +- src/encoders/binary.rs | 2 +- src/encoders/wat.rs | 7 +-- src/errors.rs | 5 +- src/interpreter/instructions/mod.rs | 16 +++---- src/interpreter/instructions/numbers.rs | 2 +- src/interpreter/instructions/records.rs | 5 +- src/interpreter/instructions/strings.rs | 2 +- src/interpreter/wasm/serde/de.rs | 2 +- src/interpreter/wasm/values.rs | 2 +- src/lib.rs | 1 + src/types.rs | 59 ++++++++++++++++++++++++ tests/binary.rs | 2 +- 15 files changed, 91 insertions(+), 85 deletions(-) create mode 100644 src/types.rs diff --git a/src/ast.rs b/src/ast.rs index 542ef09..42fc850 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -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, -} - /// Represents the kind of type. #[derive(PartialEq, Debug)] pub enum TypeKind { diff --git a/src/decoders/binary.rs b/src/decoders/binary.rs index 7e0684a..ce0f110 100644 --- a/src/decoders/binary.rs +++ b/src/decoders/binary.rs @@ -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 = &[ diff --git a/src/decoders/wat.rs b/src/decoders/wat.rs index c0f66ad..eb2f244 100644 --- a/src/decoders/wat.rs +++ b/src/decoders/wat.rs @@ -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( diff --git a/src/encoders/binary.rs b/src/encoders/binary.rs index 6788bdd..1ad2efe 100644 --- a/src/encoders/binary.rs +++ b/src/encoders/binary.rs @@ -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. diff --git a/src/encoders/wat.rs b/src/encoders/wat.rs index a99adf4..6d58e04 100644 --- a/src/encoders/wat.rs +++ b/src/encoders/wat.rs @@ -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() { diff --git a/src/errors.rs b/src/errors.rs index fb443ab..0762a24 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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}, diff --git a/src/interpreter/instructions/mod.rs b/src/interpreter/instructions/mod.rs index 49ab7ca..5b07396 100644 --- a/src/interpreter/instructions/mod.rs +++ b/src/interpreter/instructions/mod.rs @@ -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, pub(crate) locals_or_imports: HashMap, pub(crate) memory: Memory, - pub(crate) wit_types: Vec, + pub(crate) wit_types: Vec, } 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) } } diff --git a/src/interpreter/instructions/numbers.rs b/src/interpreter/instructions/numbers.rs index ad2d38c..ac7012e 100644 --- a/src/interpreter/instructions/numbers.rs +++ b/src/interpreter/instructions/numbers.rs @@ -1,7 +1,7 @@ use crate::{ - ast::InterfaceType, errors::{InstructionError, InstructionErrorKind}, interpreter::{wasm::values::InterfaceValue, Instruction}, + types::InterfaceType, }; use std::convert::TryInto; diff --git a/src/interpreter/instructions/records.rs b/src/interpreter/instructions/records.rs index ad1c58e..a9bbda1 100644 --- a/src/interpreter/instructions/records.rs +++ b/src/interpreter/instructions/records.rs @@ -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 = diff --git a/src/interpreter/instructions/strings.rs b/src/interpreter/instructions/strings.rs index d84e631..f1223ff 100644 --- a/src/interpreter/instructions/strings.rs +++ b/src/interpreter/instructions/strings.rs @@ -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}; diff --git a/src/interpreter/wasm/serde/de.rs b/src/interpreter/wasm/serde/de.rs index 49e166e..efab677 100644 --- a/src/interpreter/wasm/serde/de.rs +++ b/src/interpreter/wasm/serde/de.rs @@ -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::{ diff --git a/src/interpreter/wasm/values.rs b/src/interpreter/wasm/values.rs index 4e93a3b..bd9f6dd 100644 --- a/src/interpreter/wasm/values.rs +++ b/src/interpreter/wasm/values.rs @@ -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}; diff --git a/src/lib.rs b/src/lib.rs index 1e1c5c2..c376ab0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..70a4ca5 --- /dev/null +++ b/src/types.rs @@ -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, +} diff --git a/tests/binary.rs b/tests/binary.rs index c9d4709..4e31214 100644 --- a/tests/binary.rs +++ b/tests/binary.rs @@ -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, };