Use thiserror for MemoryAccessError

This commit is contained in:
Amanieu d'Antras
2021-11-03 16:51:50 +00:00
committed by Manos Pitsidianakis
parent d94745c9fc
commit dd7808b534
3 changed files with 18 additions and 34 deletions

View File

@@ -82,6 +82,9 @@ pub struct Memory {
view: js_sys::Uint8Array, view: js_sys::Uint8Array,
} }
unsafe impl Send for Memory {}
unsafe impl Sync for Memory {}
impl Memory { impl Memory {
/// Creates a new host `Memory` from the provided [`MemoryType`]. /// Creates a new host `Memory` from the provided [`MemoryType`].
/// ///
@@ -234,8 +237,9 @@ impl Memory {
Ok(Pages(new_pages)) Ok(Pages(new_pages))
} }
#[cfg(test)] /// Used by tests
pub(crate) fn uint8view(&self) -> js_sys::Uint8Array { #[doc(hidden)]
pub fn uint8view(&self) -> js_sys::Uint8Array {
self.view.clone() self.view.clone()
} }

View File

@@ -1,10 +1,7 @@
use crate::RuntimeError; use crate::RuntimeError;
use wasmer_types::ValueType;
use crate::{Memory, Memory32, Memory64, WasmPtr}; use crate::{Memory, Memory32, Memory64, WasmPtr};
use std::{ use std::{
convert::TryInto, convert::TryInto,
error::Error,
fmt, fmt,
marker::PhantomData, marker::PhantomData,
mem::{self, MaybeUninit}, mem::{self, MaybeUninit},
@@ -12,31 +9,24 @@ use std::{
slice, slice,
string::FromUtf8Error, string::FromUtf8Error,
}; };
use thiserror::Error;
use wasmer_types::ValueType;
/// Error for invalid [`Memory`] access. /// Error for invalid [`Memory`] access.
#[derive(Debug)] #[derive(Clone, Copy, Debug, Error)]
#[non_exhaustive] #[non_exhaustive]
pub enum MemoryAccessError { pub enum MemoryAccessError {
/// Memory access is outside heap bounds. /// Memory access is outside heap bounds.
#[error("memory access out of bounds")]
HeapOutOfBounds, HeapOutOfBounds,
/// Address calculation overflow. /// Address calculation overflow.
#[error("address calculation overflow")]
Overflow, Overflow,
/// String is not valid UTF-8. /// String is not valid UTF-8.
#[error("string is not valid utf-8")]
NonUtf8String, NonUtf8String,
} }
impl fmt::Display for MemoryAccessError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MemoryAccessError::HeapOutOfBounds => write!(f, "memory access out of bounds"),
MemoryAccessError::Overflow => write!(f, "address calculation overflow"),
MemoryAccessError::NonUtf8String => write!(f, "string is not valid utf-8"),
}
}
}
impl Error for MemoryAccessError {}
impl From<MemoryAccessError> for RuntimeError { impl From<MemoryAccessError> for RuntimeError {
fn from(err: MemoryAccessError) -> Self { fn from(err: MemoryAccessError) -> Self {
RuntimeError::new(err.to_string()) RuntimeError::new(err.to_string())

View File

@@ -1,10 +1,7 @@
use crate::RuntimeError; use crate::RuntimeError;
use wasmer_types::ValueType;
use crate::{Memory, Memory32, Memory64, WasmPtr}; use crate::{Memory, Memory32, Memory64, WasmPtr};
use std::{ use std::{
convert::TryInto, convert::TryInto,
error::Error,
fmt, fmt,
marker::PhantomData, marker::PhantomData,
mem::{self, MaybeUninit}, mem::{self, MaybeUninit},
@@ -12,31 +9,24 @@ use std::{
slice, slice,
string::FromUtf8Error, string::FromUtf8Error,
}; };
use thiserror::Error;
use wasmer_types::ValueType;
/// Error for invalid [`Memory`] access. /// Error for invalid [`Memory`] access.
#[derive(Debug)] #[derive(Clone, Copy, Debug, Error)]
#[non_exhaustive] #[non_exhaustive]
pub enum MemoryAccessError { pub enum MemoryAccessError {
/// Memory access is outside heap bounds. /// Memory access is outside heap bounds.
#[error("memory access out of bounds")]
HeapOutOfBounds, HeapOutOfBounds,
/// Address calculation overflow. /// Address calculation overflow.
#[error("address calculation overflow")]
Overflow, Overflow,
/// String is not valid UTF-8. /// String is not valid UTF-8.
#[error("string is not valid utf-8")]
NonUtf8String, NonUtf8String,
} }
impl fmt::Display for MemoryAccessError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MemoryAccessError::HeapOutOfBounds => write!(f, "memory access out of bounds"),
MemoryAccessError::Overflow => write!(f, "address calculation overflow"),
MemoryAccessError::NonUtf8String => write!(f, "string is not valid utf-8"),
}
}
}
impl Error for MemoryAccessError {}
impl From<MemoryAccessError> for RuntimeError { impl From<MemoryAccessError> for RuntimeError {
fn from(err: MemoryAccessError) -> Self { fn from(err: MemoryAccessError) -> Self {
RuntimeError::new(err.to_string()) RuntimeError::new(err.to_string())