Skip to content

Commit

Permalink
Try #1285:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Mar 10, 2020
2 parents 674d18e + abf30d1 commit 4975cd8
Show file tree
Hide file tree
Showing 20 changed files with 800 additions and 529 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## **[Unreleased]**

- [#1285](https://github.com/wasmerio/wasmer/pull/1285) Greatly improve errors in `wasmer-interface-types`
- [#1284](https://github.com/wasmerio/wasmer/pull/1284) Implement string and memory instructions in `wasmer-interface-types`
- [#1272](https://github.com/wasmerio/wasmer/pull/1272) Fix off-by-one error bug when accessing memory with a `WasmPtr` that contains the last valid byte of memory. Also changes the behavior of `WasmPtr<T, Array>` with a length of 0 and `WasmPtr<T>` where `std::mem::size_of::<T>()` is 0 to always return `None`

## 0.15.0 - 2020-03-04
Expand Down
8 changes: 4 additions & 4 deletions lib/interface-types/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::interpreter::Instruction;
use std::str;

/// Represents the types supported by WIT.
#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, Clone)]
pub enum InterfaceType {
/// A 8-bits signed integer.
S8,
Expand Down Expand Up @@ -89,12 +89,12 @@ pub struct Export<'input> {

/// Represents an adapter.
#[derive(PartialEq, Debug)]
pub struct Adapter<'input> {
pub struct Adapter {
/// The adapter function type.
pub function_type: u32,

/// The instructions.
pub instructions: Vec<Instruction<'input>>,
pub instructions: Vec<Instruction>,
}

/// Represents an implementation.
Expand Down Expand Up @@ -137,7 +137,7 @@ pub struct Interfaces<'input> {
pub imports: Vec<Import<'input>>,

/// All the adapters.
pub adapters: Vec<Adapter<'input>>,
pub adapters: Vec<Adapter>,

/// All the exported functions.
pub exports: Vec<Export<'input>>,
Expand Down
18 changes: 8 additions & 10 deletions lib/interface-types/src/decoders/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ fn instruction<'input, E: ParseError<&'input [u8]>>(
)
}

0x03 => (input, Instruction::ReadUtf8),
0x03 => (input, Instruction::MemoryToString),

0x04 => {
consume!((input, argument_0) = string(input)?);
consume!((input, argument_0) = uleb(input)?);
(
input,
Instruction::WriteUtf8 {
allocator_name: argument_0,
Instruction::StringToMemory {
allocator_index: argument_0 as u32,
},
)
}
Expand Down Expand Up @@ -630,8 +630,8 @@ mod tests {
0x2b, // list of 43 items
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, 0x01, // CallCore { function_index: 1 }
0x03, // ReadUtf8
0x04, 0x03, 0x61, 0x62, 0x63, // WriteUtf8 { allocator_name: "abc" }
0x03, // MemoryToString
0x04, 0x01, // StringToMemory { allocator_index: 1 }
0x07, // I32ToS8
0x08, // I32ToS8X
0x09, // I32ToU8
Expand Down Expand Up @@ -678,10 +678,8 @@ mod tests {
vec![
Instruction::ArgumentGet { index: 1 },
Instruction::CallCore { function_index: 1 },
Instruction::ReadUtf8,
Instruction::WriteUtf8 {
allocator_name: "abc",
},
Instruction::MemoryToString,
Instruction::StringToMemory { allocator_index: 1 },
Instruction::I32ToS8,
Instruction::I32ToS8X,
Instruction::I32ToU8,
Expand Down
34 changes: 17 additions & 17 deletions lib/interface-types/src/decoders/wat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ mod keyword {
// Instructions.
custom_keyword!(argument_get = "arg.get");
custom_keyword!(call_core = "call-core");
custom_keyword!(read_utf8 = "read-utf8");
custom_keyword!(write_utf8 = "write-utf8");
custom_keyword!(memory_to_string = "memory-to-string");
custom_keyword!(string_to_memory = "string-to-memory");
custom_keyword!(i32_to_s8 = "i32-to-s8");
custom_keyword!(i32_to_s8x = "i32-to-s8x");
custom_keyword!(i32_to_u8 = "i32-to-u8");
Expand Down Expand Up @@ -137,7 +137,7 @@ impl Parse<'_> for InterfaceType {
}
}

impl<'a> Parse<'a> for Instruction<'a> {
impl<'a> Parse<'a> for Instruction {
#[allow(clippy::cognitive_complexity)]
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut lookahead = parser.lookahead1();
Expand All @@ -154,15 +154,15 @@ impl<'a> Parse<'a> for Instruction<'a> {
Ok(Instruction::CallCore {
function_index: parser.parse::<u64>()? as usize,
})
} else if lookahead.peek::<keyword::read_utf8>() {
parser.parse::<keyword::read_utf8>()?;
} else if lookahead.peek::<keyword::memory_to_string>() {
parser.parse::<keyword::memory_to_string>()?;

Ok(Instruction::ReadUtf8)
} else if lookahead.peek::<keyword::write_utf8>() {
parser.parse::<keyword::write_utf8>()?;
Ok(Instruction::MemoryToString)
} else if lookahead.peek::<keyword::string_to_memory>() {
parser.parse::<keyword::string_to_memory>()?;

Ok(Instruction::WriteUtf8 {
allocator_name: parser.parse()?,
Ok(Instruction::StringToMemory {
allocator_index: parser.parse()?,
})
} else if lookahead.peek::<keyword::i32_to_s8>() {
parser.parse::<keyword::i32_to_s8>()?;
Expand Down Expand Up @@ -392,7 +392,7 @@ impl Parse<'_> for FunctionType {
enum Interface<'a> {
Type(Type),
Import(Import<'a>),
Adapter(Adapter<'a>),
Adapter(Adapter),
Export(Export<'a>),
Implementation(Implementation),
}
Expand Down Expand Up @@ -520,7 +520,7 @@ impl<'a> Parse<'a> for Implementation {
}
}

impl<'a> Parse<'a> for Adapter<'a> {
impl<'a> Parse<'a> for Adapter {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<keyword::func>()?;

Expand Down Expand Up @@ -667,8 +667,8 @@ mod tests {
let inputs = vec![
"arg.get 7",
"call-core 7",
"read-utf8",
r#"write-utf8 "foo""#,
"memory-to-string",
"string-to-memory 42",
"i32-to-s8",
"i32-to-s8x",
"i32-to-u8",
Expand Down Expand Up @@ -712,9 +712,9 @@ mod tests {
let outputs = vec![
Instruction::ArgumentGet { index: 7 },
Instruction::CallCore { function_index: 7 },
Instruction::ReadUtf8,
Instruction::WriteUtf8 {
allocator_name: "foo",
Instruction::MemoryToString,
Instruction::StringToMemory {
allocator_index: 42,
},
Instruction::I32ToS8,
Instruction::I32ToS8X,
Expand Down
20 changes: 9 additions & 11 deletions lib/interface-types/src/encoders/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ where
/// Encode an `Adapter` into bytes.
///
/// Decoder is in `decoders::binary::adapters`.
impl<W> ToBytes<W> for Adapter<'_>
impl<W> ToBytes<W> for Adapter
where
W: Write,
{
Expand Down Expand Up @@ -244,7 +244,7 @@ where
/// Encode an `Instruction` into bytes.
///
/// Decoder is `decoders::binary::instruction`.
impl<W> ToBytes<W> for Instruction<'_>
impl<W> ToBytes<W> for Instruction
where
W: Write,
{
Expand All @@ -260,11 +260,11 @@ where
(*function_index as u64).to_bytes(writer)?;
}

Instruction::ReadUtf8 => 0x03_u8.to_bytes(writer)?,
Instruction::MemoryToString => 0x03_u8.to_bytes(writer)?,

Instruction::WriteUtf8 { allocator_name } => {
Instruction::StringToMemory { allocator_index } => {
0x04_u8.to_bytes(writer)?;
allocator_name.to_bytes(writer)?;
(*allocator_index as u64).to_bytes(writer)?;
}

Instruction::I32ToS8 => 0x07_u8.to_bytes(writer)?,
Expand Down Expand Up @@ -550,10 +550,8 @@ mod tests {
vec![
Instruction::ArgumentGet { index: 1 },
Instruction::CallCore { function_index: 1 },
Instruction::ReadUtf8,
Instruction::WriteUtf8 {
allocator_name: "abc",
},
Instruction::MemoryToString,
Instruction::StringToMemory { allocator_index: 1 },
Instruction::I32ToS8,
Instruction::I32ToS8X,
Instruction::I32ToU8,
Expand Down Expand Up @@ -598,8 +596,8 @@ mod tests {
0x2b, // list of 43 items
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, 0x01, // CallCore { function_index: 1 }
0x03, // ReadUtf8
0x04, 0x03, 0x61, 0x62, 0x63, // WriteUtf8 { allocator_name: "abc" }
0x03, // MemoryToString
0x04, 0x01, // StringToMemory { allocator_index: 1 }
0x07, // I32ToS8
0x08, // I32ToS8X
0x09, // I32ToU8
Expand Down
20 changes: 10 additions & 10 deletions lib/interface-types/src/encoders/wat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ impl ToString for &InterfaceType {
}

/// Encode an `Instruction` into a string.
impl<'input> ToString for &Instruction<'input> {
impl ToString for &Instruction {
fn to_string(&self) -> String {
match self {
Instruction::ArgumentGet { index } => format!("arg.get {}", index),
Instruction::CallCore { function_index } => format!("call-core {}", function_index),
Instruction::ReadUtf8 => "read-utf8".into(),
Instruction::WriteUtf8 { allocator_name } => {
format!(r#"write-utf8 "{}""#, allocator_name)
Instruction::MemoryToString => "memory-to-string".into(),
Instruction::StringToMemory { allocator_index } => {
format!(r#"string-to-memory {}"#, allocator_index)
}
Instruction::I32ToS8 => "i32-to-s8".into(),
Instruction::I32ToS8X => "i32-to-s8x".into(),
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'input> ToString for &Import<'input> {
}

/// Encode an `Adapter` into a string.
impl<'input> ToString for &Adapter<'input> {
impl ToString for &Adapter {
fn to_string(&self) -> String {
format!(
r#"(@interface func (type {function_type}){instructions})"#,
Expand Down Expand Up @@ -361,9 +361,9 @@ mod tests {
let inputs: Vec<String> = vec![
(&Instruction::ArgumentGet { index: 7 }).to_string(),
(&Instruction::CallCore { function_index: 7 }).to_string(),
(&Instruction::ReadUtf8).to_string(),
(&Instruction::WriteUtf8 {
allocator_name: "foo",
(&Instruction::MemoryToString).to_string(),
(&Instruction::StringToMemory {
allocator_index: 42,
})
.to_string(),
(&Instruction::I32ToS8).to_string(),
Expand Down Expand Up @@ -409,8 +409,8 @@ mod tests {
let outputs = vec![
"arg.get 7",
"call-core 7",
"read-utf8",
r#"write-utf8 "foo""#,
"memory-to-string",
"string-to-memory 42",
"i32-to-s8",
"i32-to-s8x",
"i32-to-u8",
Expand Down
Loading

0 comments on commit 4975cd8

Please sign in to comment.