test_gen: support RocResult for Wasm tests

This commit is contained in:
Brian Carroll 2022-07-06 08:59:50 +01:00
parent 617e18af98
commit a9aee13086
No known key found for this signature in database
GPG key ID: 9CF4E3BF9C4722C7
4 changed files with 49 additions and 6 deletions

View file

@ -1,5 +1,5 @@
use roc_gen_wasm::wasm32_sized::Wasm32Sized;
use roc_std::{RocDec, RocList, RocOrder, RocStr};
use roc_std::{RocDec, RocList, RocOrder, RocResult, RocStr};
use std::convert::TryInto;
pub trait FromWasmerMemory: Wasm32Sized {
@ -95,6 +95,26 @@ impl<T: FromWasmerMemory + Clone> FromWasmerMemory for RocList<T> {
}
}
impl<T: FromWasmerMemory + Wasm32Sized, E: FromWasmerMemory + Wasm32Sized> FromWasmerMemory
for RocResult<T, E>
{
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
let tag_offset = if T::ACTUAL_WIDTH > E::ACTUAL_WIDTH {
T::ACTUAL_WIDTH
} else {
E::ACTUAL_WIDTH
};
let tag = <u8 as FromWasmerMemory>::decode(memory, offset + tag_offset as u32);
if tag == 1 {
let value = <T as FromWasmerMemory>::decode(memory, offset);
RocResult::ok(value)
} else {
let payload = <E as FromWasmerMemory>::decode(memory, offset);
RocResult::err(payload)
}
}
}
impl<T: FromWasmerMemory> FromWasmerMemory for &'_ T {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
let elements = <u32 as FromWasmerMemory>::decode(memory, offset);