From 2d6b98779174ab595b2b8f00745ff97a77aa3a38 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 9 Mar 2020 15:06:35 +0100 Subject: [PATCH] feat(interface-types) `read-utf8` is renamed `memory-to-string`. --- src/decoders/binary.rs | 6 ++--- src/decoders/wat.rs | 12 ++++----- src/encoders/binary.rs | 6 ++--- src/encoders/wat.rs | 6 ++--- src/interpreter/instruction.rs | 4 +-- .../{read_utf8.rs => memory_to_string.rs} | 26 +++++++++---------- src/interpreter/instructions/mod.rs | 4 +-- src/interpreter/instructions/write_utf8.rs | 4 +-- src/interpreter/mod.rs | 4 +-- 9 files changed, 36 insertions(+), 36 deletions(-) rename src/interpreter/instructions/{read_utf8.rs => memory_to_string.rs} (82%) diff --git a/src/decoders/binary.rs b/src/decoders/binary.rs index 571548c..6412cba 100644 --- a/src/decoders/binary.rs +++ b/src/decoders/binary.rs @@ -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", }, diff --git a/src/decoders/wat.rs b/src/decoders/wat.rs index 2342017..8a25064 100644 --- a/src/decoders/wat.rs +++ b/src/decoders/wat.rs @@ -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::() { - parser.parse::()?; + } else if lookahead.peek::() { + parser.parse::()?; - Ok(Instruction::ReadUtf8) + Ok(Instruction::MemoryToString) } else if lookahead.peek::() { parser.parse::()?; @@ -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", }, diff --git a/src/encoders/binary.rs b/src/encoders/binary.rs index 7486d5e..1d67625 100644 --- a/src/encoders/binary.rs +++ b/src/encoders/binary.rs @@ -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 diff --git a/src/encoders/wat.rs b/src/encoders/wat.rs index 1e883b7..c0cff22 100644 --- a/src/encoders/wat.rs +++ b/src/encoders/wat.rs @@ -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", diff --git a/src/interpreter/instruction.rs b/src/interpreter/instruction.rs index feca1cc..9cccacd 100644 --- a/src/interpreter/instruction.rs +++ b/src/interpreter/instruction.rs @@ -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 { diff --git a/src/interpreter/instructions/read_utf8.rs b/src/interpreter/instructions/memory_to_string.rs similarity index 82% rename from src/interpreter/instructions/read_utf8.rs rename to src/interpreter/instructions/memory_to_string.rs index a06bc56..678d08d 100644 --- a/src/interpreter/instructions/read_utf8.rs +++ b/src/interpreter/instructions/memory_to_string.rs @@ -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::>>()), ..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)."#, ); } diff --git a/src/interpreter/instructions/mod.rs b/src/interpreter/instructions/mod.rs index 28aa520..3bf7ed4 100644 --- a/src/interpreter/instructions/mod.rs +++ b/src/interpreter/instructions/mod.rs @@ -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)] diff --git a/src/interpreter/instructions/write_utf8.rs b/src/interpreter/instructions/write_utf8.rs index a1e2150..a3f53d5 100644 --- a/src/interpreter/instructions/write_utf8.rs +++ b/src/interpreter/instructions/write_utf8.rs @@ -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(), diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 1cfe665..1dfd5af 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -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> =