rename FromWasmerMemory -> FromWasm32Memory

This commit is contained in:
Folkert 2022-07-09 19:35:07 +02:00
parent ffaf56bec2
commit d7e347dcac
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
4 changed files with 48 additions and 49 deletions

View file

@ -2,13 +2,13 @@ use roc_gen_wasm::wasm32_sized::Wasm32Sized;
use roc_std::{RocDec, RocList, RocOrder, RocResult, RocStr};
use std::convert::TryInto;
pub trait FromWasmerMemory: Wasm32Sized {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self;
pub trait FromWasm32Memory: Wasm32Sized {
fn decode(memory_bytes: &[u8], offset: u32) -> Self;
}
macro_rules! from_wasm_memory_primitive_decode {
($type_name:ident) => {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
fn decode(memory_bytes: &[u8], offset: u32) -> Self {
use core::mem::MaybeUninit;
let mut output: MaybeUninit<Self> = MaybeUninit::uninit();
@ -18,7 +18,6 @@ macro_rules! from_wasm_memory_primitive_decode {
let raw_ptr = ptr as *mut u8;
let slice = unsafe { std::slice::from_raw_parts_mut(raw_ptr, width) };
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
let index = offset as usize;
let wasm_slice = &memory_bytes[index..][..width];
@ -32,7 +31,7 @@ macro_rules! from_wasm_memory_primitive_decode {
macro_rules! from_wasm_memory_primitive {
($($type_name:ident ,)+) => {
$(
impl FromWasmerMemory for $type_name {
impl FromWasm32Memory for $type_name {
from_wasm_memory_primitive_decode!($type_name);
}
)*
@ -43,13 +42,12 @@ from_wasm_memory_primitive!(
u8, i8, u16, i16, u32, i32, char, u64, i64, u128, i128, f32, f64, bool, RocDec, RocOrder,
);
impl FromWasmerMemory for () {
fn decode(_: &wasmer::Memory, _: u32) -> Self {}
impl FromWasm32Memory for () {
fn decode(_: &[u8], _: u32) -> Self {}
}
impl FromWasmerMemory for RocStr {
fn decode(memory: &wasmer::Memory, addr: u32) -> Self {
let memory_bytes = unsafe { memory.data_unchecked() };
impl FromWasm32Memory for RocStr {
fn decode(memory_bytes: &[u8], addr: u32) -> Self {
let index = addr as usize;
let mut str_bytes = [0; 12];
@ -74,9 +72,9 @@ impl FromWasmerMemory for RocStr {
}
}
impl<T: FromWasmerMemory + Clone> FromWasmerMemory for RocList<T> {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
let bytes = <u64 as FromWasmerMemory>::decode(memory, offset);
impl<T: FromWasm32Memory + Clone> FromWasm32Memory for RocList<T> {
fn decode(memory: &[u8], offset: u32) -> Self {
let bytes = <u64 as FromWasm32Memory>::decode(memory, offset);
let length = (bytes >> 32) as u32;
let elements = bytes as u32;
@ -84,7 +82,7 @@ impl<T: FromWasmerMemory + Clone> FromWasmerMemory for RocList<T> {
let mut items = Vec::with_capacity(length as usize);
for i in 0..length {
let item = <T as FromWasmerMemory>::decode(
let item = <T as FromWasm32Memory>::decode(
memory,
elements + i * <T as Wasm32Sized>::SIZE_OF_WASM as u32,
);
@ -95,29 +93,29 @@ impl<T: FromWasmerMemory + Clone> FromWasmerMemory for RocList<T> {
}
}
impl<T, E> FromWasmerMemory for RocResult<T, E>
impl<T, E> FromWasm32Memory for RocResult<T, E>
where
T: FromWasmerMemory + Wasm32Sized,
E: FromWasmerMemory + Wasm32Sized,
T: FromWasm32Memory + Wasm32Sized,
E: FromWasm32Memory + Wasm32Sized,
{
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
fn decode(memory: &[u8], offset: u32) -> Self {
let tag_offset = Ord::max(T::ACTUAL_WIDTH, E::ACTUAL_WIDTH);
let tag = <u8 as FromWasmerMemory>::decode(memory, offset + tag_offset as u32);
let tag = <u8 as FromWasm32Memory>::decode(memory, offset + tag_offset as u32);
if tag == 1 {
let value = <T as FromWasmerMemory>::decode(memory, offset);
let value = <T as FromWasm32Memory>::decode(memory, offset);
RocResult::ok(value)
} else {
let payload = <E as FromWasmerMemory>::decode(memory, offset);
let payload = <E as FromWasm32Memory>::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);
impl<T: FromWasm32Memory> FromWasm32Memory for &'_ T {
fn decode(memory: &[u8], offset: u32) -> Self {
let elements = <u32 as FromWasm32Memory>::decode(memory, offset);
let actual = <T as FromWasmerMemory>::decode(memory, elements);
let actual = <T as FromWasm32Memory>::decode(memory, elements);
let b = Box::new(actual);
@ -125,9 +123,8 @@ impl<T: FromWasmerMemory> FromWasmerMemory for &'_ T {
}
}
impl<T: FromWasmerMemory + Clone, const N: usize> FromWasmerMemory for [T; N] {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
impl<T: FromWasm32Memory + Clone, const N: usize> FromWasm32Memory for [T; N] {
fn decode(memory_bytes: &[u8], offset: u32) -> Self {
let index = offset as usize;
debug_assert!(memory_bytes.len() >= index + (N * <T as Wasm32Sized>::SIZE_OF_WASM));
@ -140,29 +137,29 @@ impl<T: FromWasmerMemory + Clone, const N: usize> FromWasmerMemory for [T; N] {
}
}
impl FromWasmerMemory for usize {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
<u32 as FromWasmerMemory>::decode(memory, offset) as usize
impl FromWasm32Memory for usize {
fn decode(memory: &[u8], offset: u32) -> Self {
<u32 as FromWasm32Memory>::decode(memory, offset) as usize
}
}
impl<T: FromWasmerMemory, U: FromWasmerMemory> FromWasmerMemory for (T, U) {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
impl<T: FromWasm32Memory, U: FromWasm32Memory> FromWasm32Memory for (T, U) {
fn decode(memory: &[u8], offset: u32) -> Self {
debug_assert!(
T::ALIGN_OF_WASM >= U::ALIGN_OF_WASM,
"this function does not handle alignment"
);
let t = <T as FromWasmerMemory>::decode(memory, offset);
let t = <T as FromWasm32Memory>::decode(memory, offset);
let u = <U as FromWasmerMemory>::decode(memory, offset + T::ACTUAL_WIDTH as u32);
let u = <U as FromWasm32Memory>::decode(memory, offset + T::ACTUAL_WIDTH as u32);
(t, u)
}
}
impl<T: FromWasmerMemory, U: FromWasmerMemory, V: FromWasmerMemory> FromWasmerMemory for (T, U, V) {
fn decode(memory: &wasmer::Memory, offset: u32) -> Self {
impl<T: FromWasm32Memory, U: FromWasm32Memory, V: FromWasm32Memory> FromWasm32Memory for (T, U, V) {
fn decode(memory: &[u8], offset: u32) -> Self {
debug_assert!(
T::ALIGN_OF_WASM >= U::ALIGN_OF_WASM,
"this function does not handle alignment"
@ -173,11 +170,11 @@ impl<T: FromWasmerMemory, U: FromWasmerMemory, V: FromWasmerMemory> FromWasmerMe
"this function does not handle alignment"
);
let t = <T as FromWasmerMemory>::decode(memory, offset);
let t = <T as FromWasm32Memory>::decode(memory, offset);
let u = <U as FromWasmerMemory>::decode(memory, offset + T::ACTUAL_WIDTH as u32);
let u = <U as FromWasm32Memory>::decode(memory, offset + T::ACTUAL_WIDTH as u32);
let v = <V as FromWasmerMemory>::decode(
let v = <V as FromWasm32Memory>::decode(
memory,
offset + T::ACTUAL_WIDTH as u32 + U::ACTUAL_WIDTH as u32,
);

View file

@ -1,4 +1,4 @@
use crate::helpers::from_wasmer_memory::FromWasmerMemory;
use crate::helpers::from_wasm32_memory::FromWasm32Memory;
use inkwell::module::Module;
use libloading::Library;
use roc_build::link::llvm_module_to_dylib;
@ -475,7 +475,7 @@ fn fake_wasm_main_function(_: u32, _: u32) -> u32 {
#[allow(dead_code)]
pub fn assert_wasm_evals_to_help<T>(src: &str, ignore_problems: bool) -> Result<T, String>
where
T: FromWasmerMemory,
T: FromWasm32Memory,
{
let arena = bumpalo::Bump::new();
let context = inkwell::context::Context::create();
@ -501,8 +501,9 @@ where
Ok(result) => {
let address = result[0].unwrap_i32();
let output = <T as crate::helpers::llvm::FromWasmerMemory>::decode(
memory,
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
let output = <T as crate::helpers::llvm::FromWasm32Memory>::decode(
memory_bytes,
// skip the RocCallResult tag id
address as u32 + 8,
);

View file

@ -4,7 +4,7 @@ pub mod platform_functions;
#[cfg(feature = "gen-dev")]
pub mod dev;
pub mod from_wasmer_memory;
pub mod from_wasm32_memory;
#[cfg(feature = "gen-llvm")]
pub mod llvm;
#[cfg(feature = "gen-wasm")]

View file

@ -1,5 +1,5 @@
use super::RefCount;
use crate::helpers::from_wasmer_memory::FromWasmerMemory;
use crate::helpers::from_wasm32_memory::FromWasm32Memory;
use roc_collections::all::MutSet;
use roc_gen_wasm::wasm32_result::Wasm32Result;
use roc_gen_wasm::wasm_module::{Export, ExportType};
@ -195,7 +195,7 @@ fn load_bytes_into_runtime(bytes: &[u8]) -> wasmer::Instance {
#[allow(dead_code)]
pub fn assert_evals_to_help<T>(src: &str, phantom: PhantomData<T>) -> Result<T, String>
where
T: FromWasmerMemory + Wasm32Result,
T: FromWasm32Memory + Wasm32Result,
{
let arena = bumpalo::Bump::new();
@ -226,7 +226,8 @@ where
// Manually provide address and size based on printf in wasm_test_platform.c
crate::helpers::wasm::debug_memory_hex(memory, 0x11440, 24);
}
let output = <T as FromWasmerMemory>::decode(memory, address as u32);
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
let output = <T as FromWasm32Memory>::decode(memory_bytes, address as u32);
Ok(output)
}
@ -265,7 +266,7 @@ pub fn assert_wasm_refcounts_help<T>(
num_refcounts: usize,
) -> Result<Vec<RefCount>, String>
where
T: FromWasmerMemory + Wasm32Result,
T: FromWasm32Memory + Wasm32Result,
{
let arena = bumpalo::Bump::new();