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 0a48cee..dd2d072 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/call_core.rs b/src/interpreter/instructions/call_core.rs index 9da19ef..fb72b33 100644 --- a/src/interpreter/instructions/call_core.rs +++ b/src/interpreter/instructions/call_core.rs @@ -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!( diff --git a/src/interpreter/instructions/mod.rs b/src/interpreter/instructions/mod.rs index 49ab7ca..cddc165 100644 --- a/src/interpreter/instructions/mod.rs +++ b/src/interpreter/instructions/mod.rs @@ -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; @@ -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 { @@ -265,7 +259,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 +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, @@ -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) } } diff --git a/src/interpreter/instructions/numbers.rs b/src/interpreter/instructions/numbers.rs index ad2d38c..bedae26 100644 --- a/src/interpreter/instructions/numbers.rs +++ b/src/interpreter/instructions/numbers.rs @@ -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; diff --git a/src/interpreter/instructions/records.rs b/src/interpreter/instructions/records.rs index ad1c58e..e63dbf8 100644 --- a/src/interpreter/instructions/records.rs +++ b/src/interpreter/instructions/records.rs @@ -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; @@ -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!( @@ -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 { @@ -139,7 +143,7 @@ executable_instruction!( #[cfg(test)] mod tests { - use crate::ast::{RecordType, Type}; + use super::*; test_executable_instruction!( test_record_lift = @@ -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.), ]), @@ -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}; @@ -252,7 +259,7 @@ mod tests { instance }, - stack: [InterfaceValue::Record(vec![ + stack: [InterfaceValue::Record(vec1![ InterfaceValue::I32(1), InterfaceValue::I32(2), ])], @@ -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.), ]), @@ -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.), ]), @@ -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.), ]), @@ -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), diff --git a/src/interpreter/instructions/strings.rs b/src/interpreter/instructions/strings.rs index d84e631..854728f 100644 --- a/src/interpreter/instructions/strings.rs +++ b/src/interpreter/instructions/strings.rs @@ -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}; diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 0d6b331..e7d93ce 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -4,11 +4,13 @@ mod instructions; pub mod stack; pub mod wasm; -use crate::errors::{InstructionResult, InterpreterResult}; +use crate::{ + errors::{InstructionResult, InterpreterResult}, + values::InterfaceValue, +}; pub use instructions::Instruction; use stack::Stack; use std::{convert::TryFrom, marker::PhantomData}; -use wasm::values::InterfaceValue; /// Represents the `Runtime`, which is used by an adapter to execute /// its instructions. @@ -59,13 +61,16 @@ pub(crate) type ExecutableInstruction { compile_error!("Cannot create an empty `Vec1`, it violates its invariant.") @@ -96,11 +98,14 @@ macro_rules! test_executable_instruction { #[test] #[allow(non_snake_case, unused)] fn $test_name() { - use crate::interpreter::{ - instructions::tests::{Export, Instance, LocalImport, Memory, MemoryView}, - stack::Stackable, - wasm::values::{InterfaceType, InterfaceValue}, - Instruction, Interpreter, + use crate::{ + interpreter::{ + instructions::tests::{Export, Instance, LocalImport, Memory, MemoryView}, + stack::Stackable, + Instruction, Interpreter, + }, + types::InterfaceType, + values::InterfaceValue, }; use std::{cell::Cell, collections::HashMap, convert::TryInto}; @@ -135,11 +140,14 @@ macro_rules! test_executable_instruction { #[test] #[allow(non_snake_case, unused)] fn $test_name() { - use crate::interpreter::{ - instructions::tests::{Export, Instance, LocalImport, Memory, MemoryView}, - stack::Stackable, - wasm::values::{InterfaceType, InterfaceValue}, - Instruction, Interpreter, + use crate::{ + interpreter::{ + instructions::tests::{Export, Instance, LocalImport, Memory, MemoryView}, + stack::Stackable, + Instruction, Interpreter, + }, + types::InterfaceType, + values::InterfaceValue, }; use std::{cell::Cell, collections::HashMap, convert::TryInto}; diff --git a/src/interpreter/wasm/serde/de.rs b/src/serde/de.rs similarity index 95% rename from src/interpreter/wasm/serde/de.rs rename to src/serde/de.rs index 49e166e..af8bf44 100644 --- a/src/interpreter/wasm/serde/de.rs +++ b/src/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, + values::{FlattenInterfaceValueIterator, InterfaceValue}, }; use serde::{de, Deserialize}; use std::{ @@ -20,9 +20,9 @@ use std::{ /// # Example /// /// ```rust -/// use wasmer_interface_types::interpreter::wasm::values::{ -/// InterfaceValue, -/// from_interface_values, +/// use wasmer_interface_types::{ +/// values::{InterfaceValue, from_interface_values}, +/// vec1::Vec1, /// }; /// use serde::Deserialize; /// @@ -36,11 +36,11 @@ use std::{ /// y: f32, /// }; /// -/// let values = vec![InterfaceValue::Record(vec![ +/// let values = vec![InterfaceValue::Record(Vec1::new(vec![ /// InterfaceValue::String("abc".to_string()), -/// InterfaceValue::Record(vec![InterfaceValue::I32(1), InterfaceValue::I64(2)]), +/// InterfaceValue::Record(Vec1::new(vec![InterfaceValue::I32(1), InterfaceValue::I64(2)]).unwrap()), /// InterfaceValue::F32(3.), -/// ])]; +/// ]).unwrap())]; /// let t = from_interface_values::(&values).unwrap(); /// /// assert_eq!( @@ -513,7 +513,7 @@ mod tests { #[derive(Deserialize, Debug, PartialEq)] struct S(i8); - let input = vec![InterfaceValue::Record(vec![InterfaceValue::S8(42)])]; + let input = vec![InterfaceValue::Record(vec1![InterfaceValue::S8(42)])]; let output = S(42); assert_eq!(from_interface_values::(&input).unwrap(), output); @@ -525,7 +525,7 @@ mod tests { #[derive(Deserialize, Debug, PartialEq)] struct S(i8, f32); - let input = vec![InterfaceValue::Record(vec![ + let input = vec![InterfaceValue::Record(vec1![ InterfaceValue::S8(7), InterfaceValue::F32(42.), ])]; @@ -543,7 +543,7 @@ mod tests { y: f32, } - let input = vec![InterfaceValue::Record(vec![ + let input = vec![InterfaceValue::Record(vec1![ InterfaceValue::S8(7), InterfaceValue::F32(42.), ])]; @@ -568,13 +568,13 @@ mod tests { p2: Point, } - let input = vec![InterfaceValue::Record(vec![ - InterfaceValue::Record(vec![ + let input = vec![InterfaceValue::Record(vec1![ + InterfaceValue::Record(vec1![ InterfaceValue::I32(1), InterfaceValue::I32(2), InterfaceValue::I32(3), ]), - InterfaceValue::Record(vec![ + InterfaceValue::Record(vec1![ InterfaceValue::I32(4), InterfaceValue::I32(5), InterfaceValue::I32(6), diff --git a/src/interpreter/wasm/serde/mod.rs b/src/serde/mod.rs similarity index 100% rename from src/interpreter/wasm/serde/mod.rs rename to src/serde/mod.rs diff --git a/src/interpreter/wasm/serde/ser.rs b/src/serde/ser.rs similarity index 92% rename from src/interpreter/wasm/serde/ser.rs rename to src/serde/ser.rs index dd3cfb9..093a5d5 100644 --- a/src/interpreter/wasm/serde/ser.rs +++ b/src/serde/ser.rs @@ -1,6 +1,6 @@ //! Provides a serializer from Rust value to WIT values. -use crate::interpreter::wasm::values::InterfaceValue; +use crate::{values::InterfaceValue, vec1::Vec1}; use serde::{ser, Serialize}; use std::fmt::{self, Display}; @@ -13,9 +13,9 @@ use std::fmt::{self, Display}; /// # Example /// /// ```rust -/// use wasmer_interface_types::interpreter::wasm::values::{ -/// InterfaceValue, -/// to_interface_value, +/// use wasmer_interface_types::{ +/// values::{InterfaceValue, to_interface_value}, +/// vec1::Vec1, /// }; /// use serde::Serialize; /// @@ -37,11 +37,11 @@ use std::fmt::{self, Display}; /// /// assert_eq!( /// to_interface_value(&input).unwrap(), -/// InterfaceValue::Record(vec![ +/// InterfaceValue::Record(Vec1::new(vec![ /// InterfaceValue::String("abc".to_string()), -/// InterfaceValue::Record(vec![InterfaceValue::I32(1), InterfaceValue::I64(2)]), +/// InterfaceValue::Record(Vec1::new(vec![InterfaceValue::I32(1), InterfaceValue::I64(2)]).unwrap()), /// InterfaceValue::F32(3.), -/// ]), +/// ]).unwrap()), /// ); /// ``` pub fn to_interface_value(value: &T) -> Result @@ -107,6 +107,9 @@ pub enum SerializeError { /// serialization. InternalValuesCorrupted, + /// A record must contain at least one field. + RecordNeedsAtLeastOneField, + /// Arbitrary message. Message(String), } @@ -128,6 +131,10 @@ impl Display for SerializeError { formatter, "the internal values have been corrutped during the serialization" ), + Self::RecordNeedsAtLeastOneField => write!( + formatter, + "a record must contain at least one field, zero given" + ), Self::Message(ref msg) => write!(formatter, "{}", msg), } } @@ -374,7 +381,9 @@ impl<'a> ser::SerializeTupleStruct for &'a mut Serializer { } fn end(self) -> Result { - let record = InterfaceValue::Record(self.pop()?); + let record = InterfaceValue::Record( + Vec1::new(self.pop()?).map_err(|_| Self::Error::RecordNeedsAtLeastOneField)?, + ); self.last().push(record); Ok(()) @@ -432,7 +441,9 @@ impl<'a> ser::SerializeStruct for &'a mut Serializer { } fn end(self) -> Result { - let record = InterfaceValue::Record(self.pop()?); + let record = InterfaceValue::Record( + Vec1::new(self.pop()?).map_err(|_| Self::Error::RecordNeedsAtLeastOneField)?, + ); self.last().push(record); Ok(()) @@ -512,7 +523,7 @@ mod tests { struct S(i8, f32); let input = S(7, 42.); - let output = InterfaceValue::Record(vec![InterfaceValue::S8(7), InterfaceValue::F32(42.)]); + let output = InterfaceValue::Record(vec1![InterfaceValue::S8(7), InterfaceValue::F32(42.)]); assert_eq!(to_interface_value(&input).unwrap(), output); } @@ -527,7 +538,7 @@ mod tests { } let input = S { x: 7, y: 42. }; - let output = InterfaceValue::Record(vec![InterfaceValue::S8(7), InterfaceValue::F32(42.)]); + let output = InterfaceValue::Record(vec1![InterfaceValue::S8(7), InterfaceValue::F32(42.)]); assert_eq!(to_interface_value(&input).unwrap(), output); } @@ -552,13 +563,13 @@ mod tests { p1: Point { x: 1, y: 2, z: 3 }, p2: Point { x: 4, y: 5, z: 6 }, }; - let output = InterfaceValue::Record(vec![ - InterfaceValue::Record(vec![ + let output = InterfaceValue::Record(vec1![ + InterfaceValue::Record(vec1![ InterfaceValue::I32(1), InterfaceValue::I32(2), InterfaceValue::I32(3), ]), - InterfaceValue::Record(vec![ + InterfaceValue::Record(vec1![ InterfaceValue::I32(4), InterfaceValue::I32(5), InterfaceValue::I32(6), diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..48ac786 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,61 @@ +//! 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. + /// A record must have at least one field, hence the + /// [`Vec1`][crate::vec1::Vec1]. + pub fields: Vec1, +} diff --git a/src/interpreter/wasm/values.rs b/src/values.rs similarity index 93% rename from src/interpreter/wasm/values.rs rename to src/values.rs index 4e93a3b..2bb871e 100644 --- a/src/interpreter/wasm/values.rs +++ b/src/values.rs @@ -1,11 +1,14 @@ //! Defines WIT values and associated operations. -pub use crate::ast::{InterfaceType, RecordType}; -use crate::{errors::WasmValueNativeCastError, vec1::Vec1}; +use crate::{ + errors::WasmValueNativeCastError, + types::{InterfaceType, RecordType}, + vec1::Vec1, +}; use std::{convert::TryFrom, slice::Iter}; #[cfg(feature = "serde")] -pub use crate::interpreter::wasm::serde::{de::*, ser::*}; +pub use crate::serde::{de::from_interface_values, ser::to_interface_value}; /// A WIT value. #[derive(Debug, Clone, PartialEq)] @@ -51,7 +54,7 @@ pub enum InterfaceValue { I64(i64), /// A record. - Record(Vec), + Record(Vec1), } impl From<&InterfaceValue> for InterfaceType { @@ -71,7 +74,7 @@ impl From<&InterfaceValue> for InterfaceType { //InterfaceValue::Anyref(_) => Self::Anyref, InterfaceValue::I32(_) => Self::I32, InterfaceValue::I64(_) => Self::I64, - InterfaceValue::Record(values) => Self::Record(values.into()), + InterfaceValue::Record(values) => Self::Record((&**values).into()), } } } @@ -215,7 +218,7 @@ mod tests { #[allow(non_snake_case)] fn interface_type_from_interface_value__record() { assert_eq!( - InterfaceType::from(&InterfaceValue::Record(vec![ + InterfaceType::from(&InterfaceValue::Record(vec1![ InterfaceValue::I32(1), InterfaceValue::S8(2) ])), @@ -225,9 +228,9 @@ mod tests { ); assert_eq!( - InterfaceType::from(&InterfaceValue::Record(vec![ + InterfaceType::from(&InterfaceValue::Record(vec1![ InterfaceValue::I32(1), - InterfaceValue::Record(vec![ + InterfaceValue::Record(vec1![ InterfaceValue::String("a".to_string()), InterfaceValue::F64(42.) ]), diff --git a/tests/binary.rs b/tests/binary.rs index c9d4709..81f8730 100644 --- a/tests/binary.rs +++ b/tests/binary.rs @@ -1,6 +1,6 @@ use wasmer_interface_types::{ - ast::*, decoders::binary::parse, encoders::binary::ToBytes, interpreter::Instruction, - vec1::Vec1, + ast::*, decoders::binary::parse, encoders::binary::ToBytes, interpreter::Instruction, types::*, + vec1, }; /// Tests an AST to binary, then binary to AST roundtrip. @@ -17,7 +17,7 @@ fn test_binary_encoding_decoding_roundtrip() { outputs: vec![InterfaceType::S32], }, Type::Record(RecordType { - fields: Vec1::new(vec![InterfaceType::String, InterfaceType::I32]).unwrap(), + fields: vec1![InterfaceType::String, InterfaceType::I32], }), ], imports: vec![Import {