Simplified signature checking in wasm jumploader

This commit is contained in:
Erin 2022-05-08 13:32:19 +02:00 committed by ondra05
parent ae70548cc0
commit 86f466cdb0

View file

@ -14,108 +14,24 @@ const SEND_SIGNAL_INDEX: usize = 1;
pub struct HostExternals;
impl HostExternals {
fn check_signature(&self, index: usize, signature: &Signature) -> bool {
match index {
ADD_FUNC_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[ValueType::I32, ValueType::I32], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
true
}
let (params, ret_ty): (&[_], _) = match index {
ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
SEND_SIGNAL_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
GET_TIME_INDEX => (&[], Some(ValueType::I32)),
GET_RANDOM_INDEX => (&[], Some(ValueType::I32)),
GET_INPUT_INDEX => (&[], Some(ValueType::I32)),
PRINT_CHAR => (&[ValueType::I32], None),
_ => return false,
};
SEND_SIGNAL_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[ValueType::I32, ValueType::I32], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
true
}
GET_TIME_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
true
}
GET_RANDOM_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
true
}
GET_INPUT_INDEX => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) =
(&[], Some(ValueType::I32));
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
true
}
PRINT_CHAR => {
let (params, ret_ty): (&[ValueType], Option<ValueType>) = (&[ValueType::I32], None);
if params.len() != signature.params().len() {
return false;
}
if ret_ty != signature.return_type() {
return false;
}
for (ty, param) in params.iter().zip(signature.params()) {
if *ty != *param {
return false;
}
}
true
}
_ => false,
if params.len() != signature.params().len() || ret_ty != signature.return_type() {
false
} else {
params
.iter()
.zip(signature.params())
.find(|(ty, param)| ty != param)
.is_none()
}
}
}