feat(interface-types) read-utf8 is renamed memory-to-string.

This commit is contained in:
Ivan Enderlin 2020-03-09 15:06:35 +01:00
parent f38c45e373
commit 2d6b987791
9 changed files with 36 additions and 36 deletions

View File

@ -184,7 +184,7 @@ fn instruction<'input, E: ParseError<&'input [u8]>>(
)
}
0x03 => (input, Instruction::ReadUtf8),
0x03 => (input, Instruction::MemoryToString),
0x04 => {
consume!((input, argument_0) = string(input)?);
@ -641,7 +641,7 @@ mod tests {
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, 0x01, // Call { function_index: 1 }
0x02, 0x03, 0x61, 0x62, 0x63, // CallExport { export_name: "abc" }
0x03, // ReadUtf8
0x03, // MemoryToString
0x04, 0x03, 0x61, 0x62, 0x63, // WriteUtf8 { allocator_name: "abc" }
0x07, // I32ToS8
0x08, // I32ToS8X
@ -690,7 +690,7 @@ mod tests {
Instruction::ArgumentGet { index: 1 },
Instruction::Call { function_index: 1 },
Instruction::CallExport { export_name: "abc" },
Instruction::ReadUtf8,
Instruction::MemoryToString,
Instruction::WriteUtf8 {
allocator_name: "abc",
},

View File

@ -29,7 +29,7 @@ mod keyword {
custom_keyword!(argument_get = "arg.get");
custom_keyword!(call);
custom_keyword!(call_export = "call-export");
custom_keyword!(read_utf8 = "read-utf8");
custom_keyword!(memory_to_string = "memory-to-string");
custom_keyword!(write_utf8 = "write-utf8");
custom_keyword!(i32_to_s8 = "i32-to-s8");
custom_keyword!(i32_to_s8x = "i32-to-s8x");
@ -161,10 +161,10 @@ impl<'a> Parse<'a> for Instruction<'a> {
Ok(Instruction::CallExport {
export_name: parser.parse()?,
})
} 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)
Ok(Instruction::MemoryToString)
} else if lookahead.peek::<keyword::write_utf8>() {
parser.parse::<keyword::write_utf8>()?;
@ -675,7 +675,7 @@ mod tests {
"arg.get 7",
"call 7",
r#"call-export "foo""#,
"read-utf8",
"memory-to-string",
r#"write-utf8 "foo""#,
"i32-to-s8",
"i32-to-s8x",
@ -721,7 +721,7 @@ mod tests {
Instruction::ArgumentGet { index: 7 },
Instruction::Call { function_index: 7 },
Instruction::CallExport { export_name: "foo" },
Instruction::ReadUtf8,
Instruction::MemoryToString,
Instruction::WriteUtf8 {
allocator_name: "foo",
},

View File

@ -265,7 +265,7 @@ where
export_name.to_bytes(writer)?;
}
Instruction::ReadUtf8 => 0x03_u8.to_bytes(writer)?,
Instruction::MemoryToString => 0x03_u8.to_bytes(writer)?,
Instruction::WriteUtf8 { allocator_name } => {
0x04_u8.to_bytes(writer)?;
@ -556,7 +556,7 @@ mod tests {
Instruction::ArgumentGet { index: 1 },
Instruction::Call { function_index: 1 },
Instruction::CallExport { export_name: "abc" },
Instruction::ReadUtf8,
Instruction::MemoryToString,
Instruction::WriteUtf8 {
allocator_name: "abc",
},
@ -605,7 +605,7 @@ mod tests {
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, 0x01, // Call { function_index: 1 }
0x02, 0x03, 0x61, 0x62, 0x63, // CallExport { export_name: "abc" }
0x03, // ReadUtf8
0x03, // MemoryToString
0x04, 0x03, 0x61, 0x62, 0x63, // WriteUtf8 { allocator_name: "abc" }
0x07, // I32ToS8
0x08, // I32ToS8X

View File

@ -86,7 +86,7 @@ impl<'input> ToString for &Instruction<'input> {
Instruction::ArgumentGet { index } => format!("arg.get {}", index),
Instruction::Call { function_index } => format!("call {}", function_index),
Instruction::CallExport { export_name } => format!(r#"call-export "{}""#, export_name),
Instruction::ReadUtf8 => "read-utf8".into(),
Instruction::MemoryToString => "memory-to-string".into(),
Instruction::WriteUtf8 { allocator_name } => {
format!(r#"write-utf8 "{}""#, allocator_name)
}
@ -363,7 +363,7 @@ mod tests {
(&Instruction::ArgumentGet { index: 7 }).to_string(),
(&Instruction::Call { function_index: 7 }).to_string(),
(&Instruction::CallExport { export_name: "foo" }).to_string(),
(&Instruction::ReadUtf8).to_string(),
(&Instruction::MemoryToString).to_string(),
(&Instruction::WriteUtf8 {
allocator_name: "foo",
})
@ -412,7 +412,7 @@ mod tests {
"arg.get 7",
"call 7",
r#"call-export "foo""#,
"read-utf8",
"memory-to-string",
r#"write-utf8 "foo""#,
"i32-to-s8",
"i32-to-s8x",

View File

@ -21,8 +21,8 @@ pub enum Instruction<'input> {
export_name: &'input str,
},
/// The `read-utf8` instruction.
ReadUtf8,
/// The `memory-to-string` instruction.
MemoryToString,
/// The `write-utf8` instruction.
WriteUtf8 {

View File

@ -2,7 +2,7 @@ use crate::interpreter::wasm::values::InterfaceValue;
use std::{cell::Cell, convert::TryFrom};
executable_instruction!(
read_utf8(instruction_name: String) -> _ {
memory_to_string(instruction_name: String) -> _ {
move |runtime| -> _ {
match runtime.stack.pop(2) {
Some(inputs) => match runtime.wasm_instance.memory(0) {
@ -55,11 +55,11 @@ executable_instruction!(
#[cfg(test)]
mod tests {
test_executable_instruction!(
test_read_utf8 =
test_memory_to_string =
instructions: [
Instruction::ArgumentGet { index: 1 },
Instruction::ArgumentGet { index: 0 },
Instruction::ReadUtf8,
Instruction::MemoryToString,
],
invocation_inputs: [
InterfaceValue::I32(13),
@ -75,11 +75,11 @@ mod tests {
);
test_executable_instruction!(
test_read_utf8__read_out_of_memory =
test_memory_to_string__read_out_of_memory =
instructions: [
Instruction::ArgumentGet { index: 1 },
Instruction::ArgumentGet { index: 0 },
Instruction::ReadUtf8,
Instruction::MemoryToString,
],
invocation_inputs: [
InterfaceValue::I32(13),
@ -91,15 +91,15 @@ mod tests {
memory: Memory::new("Hello!".as_bytes().iter().map(|u| Cell::new(*u)).collect()),
..Default::default()
},
error: r#"`read-utf8` failed because it has to read out of the memory bounds (index 13 > memory length 6)."#,
error: r#"`memory-to-string` failed because it has to read out of the memory bounds (index 13 > memory length 6)."#,
);
test_executable_instruction!(
test_read_utf8__invalid_encoding =
test_memory_to_string__invalid_encoding =
instructions: [
Instruction::ArgumentGet { index: 1 },
Instruction::ArgumentGet { index: 0 },
Instruction::ReadUtf8,
Instruction::MemoryToString,
],
invocation_inputs: [
InterfaceValue::I32(4),
@ -111,21 +111,21 @@ mod tests {
memory: Memory::new(vec![0, 159, 146, 150].iter().map(|b| Cell::new(*b)).collect::<Vec<Cell<u8>>>()),
..Default::default()
},
error: r#"`read-utf8` failed because the read string isn't UTF-8 valid (invalid utf-8 sequence of 1 bytes from index 1)."#,
error: r#"`memory-to-string` failed because the read string isn't UTF-8 valid (invalid utf-8 sequence of 1 bytes from index 1)."#,
);
test_executable_instruction!(
test_read_utf8__stack_is_too_small =
test_memory_to_string__stack_is_too_small =
instructions: [
Instruction::ArgumentGet { index: 0 },
Instruction::ReadUtf8,
// ^^^^^^^^ `read-utf8` expects 2 values on the stack, only one is present.
Instruction::MemoryToString,
// ^^^^^^^^^^^^^^ `memory-to-string` expects 2 values on the stack, only one is present.
],
invocation_inputs: [
InterfaceValue::I32(13),
InterfaceValue::I32(0),
],
instance: Instance::new(),
error: r#"`read-utf8` failed because there is not enough data on the stack (needs 2)."#,
error: r#"`memory-to-string` failed because there is not enough data on the stack (needs 2)."#,
);
}

View File

@ -2,14 +2,14 @@ mod argument_get;
mod call;
mod call_export;
mod lowering_lifting;
mod read_utf8;
mod memory_to_string;
mod write_utf8;
pub(crate) use argument_get::argument_get;
pub(crate) use call::call;
pub(crate) use call_export::call_export;
pub(crate) use lowering_lifting::*;
pub(crate) use read_utf8::read_utf8;
pub(crate) use memory_to_string::memory_to_string;
pub(crate) use write_utf8::write_utf8;
#[cfg(test)]

View File

@ -90,11 +90,11 @@ mod tests {
);
test_executable_instruction!(
test_write_utf8__roundtrip_with_read_utf8 =
test_write_utf8__roundtrip_with_memory_to_string =
instructions: [
Instruction::ArgumentGet { index: 0 },
Instruction::WriteUtf8 { allocator_name: "alloc" },
Instruction::ReadUtf8,
Instruction::MemoryToString,
],
invocation_inputs: [InterfaceValue::String("Hello, World!".into())],
instance: Instance::new(),

View File

@ -202,7 +202,7 @@ where
Instruction::CallExport { export_name } => {
instructions::call_export((*export_name).to_owned(), instruction_name)
}
Instruction::ReadUtf8 => instructions::read_utf8(instruction_name),
Instruction::MemoryToString => instructions::memory_to_string(instruction_name),
Instruction::WriteUtf8 { allocator_name } => {
instructions::write_utf8((*allocator_name).to_owned(), instruction_name)
}
@ -265,7 +265,7 @@ mod tests {
Instruction::ArgumentGet { index: 0 },
Instruction::ArgumentGet { index: 0 },
Instruction::CallExport { export_name: "foo" },
Instruction::ReadUtf8,
Instruction::MemoryToString,
Instruction::Call { function_index: 7 },
];
let interpreter: Interpreter<(), (), (), (), EmptyMemoryView> =