Skip to content

Commit

Permalink
Fix most storage unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Oct 31, 2023
1 parent 23fb576 commit e578385
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
10 changes: 5 additions & 5 deletions storage/src/demo_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::rc::Rc;

use crate::rc_key::RcKey;
use crate::storage_backend::{box_to_storage_error, StorageError};
use crate::storage_backend::StorageError;
use crate::storage_entity::StorageEntity;
use crate::storage_entry::{StorageEntry, StorageEntryReader};
use crate::storage_ptr::StorageEntryPtr;
Expand Down Expand Up @@ -64,15 +64,15 @@ impl DemoVal {
_tx: &mut Tx,
reader: &mut StorageEntryReader,
) -> Result<DemoVal, StorageError<SB>> {
let tag = reader.read_u8().map_err(box_to_storage_error)?;
let tag = reader.read_u8().map_err(StorageError::from)?;

Ok(match tag {
NUMBER_TAG => {
let n = reader.read_u64().map_err(box_to_storage_error)?;
let n = reader.read_u64().map_err(StorageError::from)?;
DemoVal::Number(n)
}
ARRAY_TAG => {
let len = reader.read_u64().map_err(box_to_storage_error)?;
let len = reader.read_u64().map_err(StorageError::from)?;
let mut items = Vec::new();

for _ in 0..len {
Expand All @@ -82,7 +82,7 @@ impl DemoVal {
DemoVal::Array(Rc::new(items))
}
PTR_TAG => {
let ptr = reader.read_ref().map_err(box_to_storage_error)?;
let ptr = reader.read_ref().map_err(StorageError::from)?;
DemoVal::Ptr(ptr)
}
_ => panic!("Invalid tag"),
Expand Down
12 changes: 8 additions & 4 deletions storage/src/storage_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ pub enum StorageError<SB: StorageBackend> {
Error(Box<dyn Error>),
}

pub fn to_storage_error<SB: StorageBackend, E: Error + 'static>(e: Box<E>) -> StorageError<SB> {
StorageError::Error(e)
impl<SB: StorageBackend> From<Box<dyn Error>> for StorageError<SB> {
fn from(e: Box<dyn Error>) -> Self {
StorageError::Error(e)
}
}

pub fn box_to_storage_error<SB: StorageBackend, E: Error + 'static>(e: E) -> StorageError<SB> {
StorageError::Error(Box::new(e))
impl<SB: StorageBackend> StorageError<SB> {
pub fn from<E: Error + 'static>(e: E) -> Self {
StorageError::Error(Box::new(e))
}
}
8 changes: 4 additions & 4 deletions storage/src/storage_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use rand::thread_rng;
use serde::{Deserialize, Serialize};

use crate::{
storage_backend::{to_storage_error, StorageError},
RcKey, StorageBackend, StorageEntity, StorageEntryPtr, StorageHeadPtr, StoragePtr,
storage_backend::StorageError, RcKey, StorageBackend, StorageEntity, StorageEntryPtr,
StorageHeadPtr, StoragePtr,
};

pub trait StorageTx<'a, SB: StorageBackend>: Sized {
Expand All @@ -29,7 +29,7 @@ pub trait StorageTx<'a, SB: StorageBackend>: Sized {

bincode::deserialize(&data)
.map(Some)
.map_err(to_storage_error)
.map_err(StorageError::from)
}

fn read_or_err<T: for<'de> Deserialize<'de>>(
Expand All @@ -48,7 +48,7 @@ pub trait StorageTx<'a, SB: StorageBackend>: Sized {
data: Option<&T>,
) -> Result<(), StorageError<SB>> {
let bytes = match data {
Some(data) => Some(bincode::serialize(&data).map_err(to_storage_error)?),
Some(data) => Some(bincode::serialize(&data).map_err(StorageError::from)?),
None => None,
};

Expand Down
76 changes: 40 additions & 36 deletions valuescript_vm/src/val_storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, io::Read, rc::Rc};
use std::{collections::BTreeMap, error::Error, io::Read, rc::Rc};

use num_bigint::BigInt;
use num_derive::{FromPrimitive, ToPrimitive};
Expand Down Expand Up @@ -43,8 +43,8 @@ impl Tag {
ToPrimitive::to_u8(self).unwrap()
}

fn from_byte(byte: u8) -> Tag {
FromPrimitive::from_u8(byte).unwrap()
fn from_byte(byte: u8) -> Result<Tag, Box<dyn Error>> {
FromPrimitive::from_u8(byte).ok_or("Invalid tag byte".into())
}
}

Expand Down Expand Up @@ -269,37 +269,39 @@ fn read_from_entry<'a, SB: StorageBackend, Tx: StorageTx<'a, SB>>(
tx: &mut Tx,
reader: &mut StorageEntryReader,
) -> Result<Val, StorageError<SB>> {
let tag = Tag::from_byte(reader.read_u8().unwrap());
let tag = Tag::from_byte(reader.read_u8().map_err(StorageError::from)?)?;

Ok(match tag {
Tag::Void => Val::Void,
Tag::Undefined => Val::Undefined,
Tag::Null => Val::Null,
Tag::Bool => Val::Bool(match reader.read_u8().unwrap() {
Tag::Bool => Val::Bool(match reader.read_u8().map_err(StorageError::from)? {
0 => false,
1 => true,
_ => panic!("Invalid bool byte"),
}),
Tag::Number => {
let mut bytes = [0; 8];
reader.read_exact(&mut bytes).unwrap();
reader.read_exact(&mut bytes).map_err(StorageError::from)?;
Val::Number(f64::from_le_bytes(bytes))
}
Tag::BigInt => {
let len = reader.read_vlq().unwrap();
let len = reader.read_vlq().map_err(StorageError::from)?;
let mut bytes = vec![0; len as usize];
reader.read_exact(&mut bytes).unwrap();
reader.read_exact(&mut bytes).map_err(StorageError::from)?;
Val::BigInt(BigInt::from_signed_bytes_le(&bytes))
}
Tag::Symbol => Val::Symbol(FromPrimitive::from_u64(reader.read_vlq().unwrap()).unwrap()),
Tag::Symbol => {
Val::Symbol(FromPrimitive::from_u64(reader.read_vlq().map_err(StorageError::from)?).unwrap())
}
Tag::String => {
let len = reader.read_vlq().unwrap();
let len = reader.read_vlq().map_err(StorageError::from)?;
let mut bytes = vec![0; len as usize];
reader.read_exact(&mut bytes).unwrap();
Val::String(String::from_utf8(bytes).unwrap().into())
reader.read_exact(&mut bytes).map_err(StorageError::from)?;
Val::String(String::from_utf8(bytes).map_err(StorageError::from)?.into())
}
Tag::Array => {
let len = reader.read_vlq().unwrap();
let len = reader.read_vlq().map_err(StorageError::from)?;
let mut items = Vec::new();

for _ in 0..len {
Expand All @@ -309,21 +311,21 @@ fn read_from_entry<'a, SB: StorageBackend, Tx: StorageTx<'a, SB>>(
VsArray::from(items).to_val()
}
Tag::Object => {
let len = reader.read_vlq().unwrap();
let len = reader.read_vlq().map_err(StorageError::from)?;
let mut string_map = BTreeMap::<String, Val>::new();

for _ in 0..len {
let key = read_string_from_entry(reader);
let key = read_string_from_entry(reader)?;
let value = read_from_entry(tx, reader)?;

string_map.insert(key, value);
}

let len = reader.read_vlq().unwrap();
let len = reader.read_vlq().map_err(StorageError::from)?;
let mut symbol_map = BTreeMap::<VsSymbol, Val>::new();

for _ in 0..len {
let key = read_symbol_from_entry(reader);
let key = read_symbol_from_entry(reader)?;
let value = read_from_entry(tx, reader)?;

symbol_map.insert(key, value);
Expand All @@ -344,23 +346,23 @@ fn read_from_entry<'a, SB: StorageBackend, Tx: StorageTx<'a, SB>>(
Tag::Function => {
let bytecode = read_ref_bytecode_from_entry(tx, reader)?;

let meta_pos = match reader.read_u8().unwrap() {
let meta_pos = match reader.read_u8().map_err(StorageError::from)? {
0 => None,
1 => Some(reader.read_vlq().unwrap() as usize),
1 => Some(reader.read_vlq().map_err(StorageError::from)? as usize),
_ => panic!("Invalid meta_pos byte"),
};

let is_generator = match reader.read_u8().unwrap() {
let is_generator = match reader.read_u8().map_err(StorageError::from)? {
0 => false,
1 => true,
_ => panic!("Invalid is_generator byte"),
};

let register_count = reader.read_vlq().unwrap() as usize;
let parameter_count = reader.read_vlq().unwrap() as usize;
let start = reader.read_vlq().unwrap() as usize;
let register_count = reader.read_vlq().map_err(StorageError::from)? as usize;
let parameter_count = reader.read_vlq().map_err(StorageError::from)? as usize;
let start = reader.read_vlq().map_err(StorageError::from)? as usize;

let len = reader.read_vlq().unwrap();
let len = reader.read_vlq().map_err(StorageError::from)?;
let mut binds = Vec::new();

for _ in 0..len {
Expand All @@ -379,13 +381,13 @@ fn read_from_entry<'a, SB: StorageBackend, Tx: StorageTx<'a, SB>>(
.to_val()
}
Tag::Class => {
let name = read_string_from_entry(reader);
let name = read_string_from_entry(reader)?;

let content_hash = match reader.read_u8().unwrap() {
let content_hash = match reader.read_u8().map_err(StorageError::from)? {
0 => None,
1 => {
let mut res = [0u8; 32];
reader.read_exact(&mut res).unwrap();
reader.read_exact(&mut res).map_err(StorageError::from)?;

Some(res)
}
Expand All @@ -408,27 +410,29 @@ fn read_from_entry<'a, SB: StorageBackend, Tx: StorageTx<'a, SB>>(
Tag::Static => todo!(),
Tag::Dynamic => todo!(),
Tag::CopyCounter => todo!(),
Tag::StoragePtr => VsStoragePtr::from_ptr(reader.read_ref().unwrap()).to_val(),
Tag::StoragePtr => {
VsStoragePtr::from_ptr(reader.read_ref().map_err(StorageError::from)?).to_val()
}
})
}

fn read_string_from_entry(reader: &mut StorageEntryReader) -> String {
let len = reader.read_vlq().unwrap();
fn read_string_from_entry(reader: &mut StorageEntryReader) -> Result<String, Box<dyn Error>> {
let len = reader.read_vlq()?;
let mut bytes = vec![0; len as usize];
reader.read_exact(&mut bytes).unwrap();
String::from_utf8(bytes).unwrap()
reader.read_exact(&mut bytes)?;
Ok(String::from_utf8(bytes)?)
}

fn read_symbol_from_entry(reader: &mut StorageEntryReader) -> VsSymbol {
FromPrimitive::from_u64(reader.read_vlq().unwrap()).unwrap()
fn read_symbol_from_entry(reader: &mut StorageEntryReader) -> Result<VsSymbol, Box<dyn Error>> {
Ok(FromPrimitive::from_u64(reader.read_vlq()?).unwrap())
}

fn read_ref_bytecode_from_entry<'a, SB: StorageBackend, Tx: StorageTx<'a, SB>>(
tx: &mut Tx,
reader: &mut StorageEntryReader,
) -> Result<Rc<Bytecode>, StorageError<SB>> {
let ptr = reader.read_ref().unwrap();
let entry = tx.read(ptr)?.unwrap();
let ptr = reader.read_ref().map_err(StorageError::from)?;
let entry = tx.read_or_err(ptr)?;

// TODO: Cached reads
Ok(Rc::new(Bytecode::from_storage_entry(tx, entry)?))
Expand Down

0 comments on commit e578385

Please sign in to comment.