feat(interface-types) Encodes/decodes the record.lower instruction.

This commit is contained in:
Ivan Enderlin 2020-04-02 12:05:22 +02:00
parent 3aeff59b11
commit c7bb9904f7
5 changed files with 38 additions and 6 deletions

View File

@ -227,7 +227,6 @@ fn instruction<'input, E: ParseError<&'input [u8]>>(
0x21 => (input, Instruction::I64FromU64),
0x22 => (input, Instruction::StringLiftMemory),
0x23 => {
consume!((input, argument_0) = uleb(input)?);
@ -238,7 +237,6 @@ fn instruction<'input, E: ParseError<&'input [u8]>>(
},
)
}
0x24 => (input, Instruction::StringSize),
0x25 => {
@ -251,6 +249,16 @@ fn instruction<'input, E: ParseError<&'input [u8]>>(
},
)
}
0x26 => {
consume!((input, argument_0) = uleb(input)?);
(
input,
Instruction::RecordLower {
type_index: argument_0 as u32,
},
)
}
_ => return Err(Err::Error(make_error(input, ErrorKind::ParseTo))),
})
@ -715,7 +723,7 @@ mod tests {
#[test]
fn test_instructions() {
let input = &[
0x26, // list of 38 items
0x27, // list of 39 items
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, 0x01, // CallCore { function_index: 1 }
0x02, // S8FromI32
@ -754,6 +762,7 @@ mod tests {
0x23, 0x01, // StringLowerMemory { allocator_index: 1 }
0x24, // StringSize
0x25, 0x01, // RecordLift { type_index: 1 },
0x26, 0x01, // RecordLower { type_index: 1 },
0x0a,
];
let output = Ok((
@ -797,6 +806,7 @@ mod tests {
Instruction::StringLowerMemory { allocator_index: 1 },
Instruction::StringSize,
Instruction::RecordLift { type_index: 1 },
Instruction::RecordLower { type_index: 1 },
],
));

View File

@ -66,6 +66,7 @@ mod keyword {
custom_keyword!(string_lower_memory = "string.lower_memory");
custom_keyword!(string_size = "string.size");
custom_keyword!(record_lift = "record.lift");
custom_keyword!(record_lower = "record.lower");
}
impl Parse<'_> for InterfaceType {
@ -319,6 +320,12 @@ impl<'a> Parse<'a> for Instruction {
Ok(Instruction::RecordLift {
type_index: parser.parse()?,
})
} else if lookahead.peek::<keyword::record_lower>() {
parser.parse::<keyword::record_lower>()?;
Ok(Instruction::RecordLower {
type_index: parser.parse()?,
})
} else {
Err(lookahead.error())
}
@ -764,6 +771,7 @@ mod tests {
"string.lower_memory 42",
"string.size",
"record.lift 42",
"record.lower 42",
];
let outputs = vec![
Instruction::ArgumentGet { index: 7 },
@ -806,6 +814,7 @@ mod tests {
},
Instruction::StringSize,
Instruction::RecordLift { type_index: 42 },
Instruction::RecordLower { type_index: 42 },
];
assert_eq!(inputs.len(), outputs.len());

View File

@ -331,18 +331,20 @@ where
Instruction::I64FromU64 => 0x21_u8.to_bytes(writer)?,
Instruction::StringLiftMemory => 0x22_u8.to_bytes(writer)?,
Instruction::StringLowerMemory { allocator_index } => {
0x23_u8.to_bytes(writer)?;
(*allocator_index as u64).to_bytes(writer)?;
}
Instruction::StringSize => 0x24_u8.to_bytes(writer)?,
Instruction::RecordLift { type_index } => {
0x25_u8.to_bytes(writer)?;
(*type_index as u64).to_bytes(writer)?
}
Instruction::RecordLower { type_index } => {
0x26_u8.to_bytes(writer)?;
(*type_index as u64).to_bytes(writer)?
}
}
Ok(())
@ -689,9 +691,10 @@ mod tests {
Instruction::StringLowerMemory { allocator_index: 1 },
Instruction::StringSize,
Instruction::RecordLift { type_index: 1 },
Instruction::RecordLower { type_index: 1 },
],
&[
0x26, // list of 38 items
0x27, // list of 39 items
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, 0x01, // CallCore { function_index: 1 }
0x02, // S8FromI32
@ -730,6 +733,7 @@ mod tests {
0x23, 0x01, // StringLowerMemory { allocator_index: 1 }
0x24, // StringSize
0x025, 0x01, // RecordLift { type_index: 1 }
0x026, 0x01, // RecordLower { type_index: 1 }
]
);
}

View File

@ -140,6 +140,7 @@ impl ToString for &Instruction {
}
Instruction::StringSize => "string.size".into(),
Instruction::RecordLift { type_index } => format!("record.lift {}", type_index),
Instruction::RecordLower { type_index } => format!("record.lower {}", type_index),
}
}
}
@ -467,6 +468,7 @@ mod tests {
.to_string(),
(&Instruction::StringSize).to_string(),
(&Instruction::RecordLift { type_index: 42 }).to_string(),
(&Instruction::RecordLower { type_index: 42 }).to_string(),
];
let outputs = vec![
"arg.get 7",
@ -507,6 +509,7 @@ mod tests {
"string.lower_memory 42",
"string.size",
"record.lift 42",
"record.lower 42",
];
assert_eq!(inputs, outputs);

View File

@ -143,6 +143,12 @@ pub enum Instruction {
/// The type index of the record.
type_index: u32,
},
/// The `record.lower` instruction.
RecordLower {
/// The type index of the record.
type_index: u32,
},
}
/// Just a short helper to map the error of a cast from an