diff --git a/common/src/lib.rs b/common/src/lib.rs index 1b4a90c93..86ab00916 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + #[cfg(test)] mod tests { #[test] diff --git a/small_string/lib.rs b/small_string/lib.rs index 9cdf0bcab..72067686d 100644 --- a/small_string/lib.rs +++ b/small_string/lib.rs @@ -1,10 +1,12 @@ +#![no_std] + #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct SmallString { bytes: [u8; 7], } -impl std::fmt::Debug for SmallString { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Debug for SmallString { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "\"{}\"", self.as_str()) } } @@ -27,7 +29,7 @@ impl SmallString { #[inline] pub fn as_str(&self) -> &str { // SAFETY: Guaranteed to be UTF-8. - unsafe { std::str::from_utf8_unchecked(self.as_bytes()) } + unsafe { core::str::from_utf8_unchecked(self.as_bytes()) } } #[inline] diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 5b17da703..71c5d0c04 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -3,6 +3,10 @@ name = "wasm" version = "0.1.0" edition = "2021" +[features] +default = ["no_std"] +no_std = ["dep:core2"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +core2 = {version = "0.4", optional = true} \ No newline at end of file diff --git a/wasm/src/decoder/code_section.rs b/wasm/src/decoder/code_section.rs index 80f660239..93df0d7e3 100644 --- a/wasm/src/decoder/code_section.rs +++ b/wasm/src/decoder/code_section.rs @@ -2,9 +2,15 @@ use super::common::CodeBlock; use super::util; use crate::error::Result; use crate::varint::decode_u32; +#[cfg(feature = "no_std")] +use alloc::vec::Vec; +#[cfg(feature = "no_std")] +use core2::io; +#[cfg(not(feature = "no_std"))] +use std::io; #[allow(clippy::read_zero_byte_vec)] -pub fn decode_code_section(reader: &mut R) -> Result { +pub fn decode_code_section(reader: &mut R) -> Result { let body_size = decode_u32(reader)?.value; let v = util::decode_vec(reader, |x| Ok((decode_u32(x)?, util::decode_kind(x)?)))?; diff --git a/wasm/src/decoder/common.rs b/wasm/src/decoder/common.rs index b7d2aef40..adc96c4e1 100644 --- a/wasm/src/decoder/common.rs +++ b/wasm/src/decoder/common.rs @@ -1,5 +1,8 @@ use crate::error; +#[cfg(feature = "no_std")] +use alloc::{boxed::Box, vec::Vec}; + #[repr(u8)] #[derive(Copy, Clone)] pub enum ExternalKind { diff --git a/wasm/src/decoder/mod.rs b/wasm/src/decoder/mod.rs index 77655b692..52cc8d360 100644 --- a/wasm/src/decoder/mod.rs +++ b/wasm/src/decoder/mod.rs @@ -2,6 +2,12 @@ mod code_section; mod common; mod type_section; mod util; +#[cfg(feature = "no_std")] +use alloc::vec::Vec; +#[cfg(feature = "no_std")] +use core2::io; +#[cfg(not(feature = "no_std"))] +use std::io; use crate::error::Error; use crate::error::Result; @@ -29,7 +35,7 @@ pub struct Module { } impl Module { - pub fn new(reader: &mut R) -> Result { + pub fn new(reader: &mut R) -> Result { let mut module = Self::default(); let mut last_section_id: i8 = -1; for i in 0..12 { @@ -58,7 +64,7 @@ impl Module { } } -pub fn decode_any_section(reader: &mut R) -> Result
{ +pub fn decode_any_section(reader: &mut R) -> Result
{ let mut section_id_byte: [u8; 1] = [0; 1]; reader.read_exact(&mut section_id_byte)?; // if section_id_byte[0] == 0x08 { diff --git a/wasm/src/decoder/type_section.rs b/wasm/src/decoder/type_section.rs index 77db5c18e..2ca371c73 100644 --- a/wasm/src/decoder/type_section.rs +++ b/wasm/src/decoder/type_section.rs @@ -3,9 +3,14 @@ use super::util; use crate::error::Error; use crate::error::Result; +#[cfg(feature = "no_std")] +use core2::io; +#[cfg(not(feature = "no_std"))] +use std::io; + const FN_SIGNATURE: u8 = common::ValueKind::Func as u8; -pub fn decode_type_section(reader: &mut R) -> Result { +pub fn decode_type_section(reader: &mut R) -> Result { let mut byte: [u8; 1] = [0; 1]; reader.read_exact(&mut byte)?; diff --git a/wasm/src/decoder/util.rs b/wasm/src/decoder/util.rs index 412460253..8c49c9933 100644 --- a/wasm/src/decoder/util.rs +++ b/wasm/src/decoder/util.rs @@ -2,9 +2,18 @@ use super::common; use crate::error::Error; use crate::error::Result; use crate::varint::decode_u32; + +#[cfg(feature = "no_std")] +use alloc::vec::Vec; + +#[cfg(feature = "no_std")] +use core2::io; +#[cfg(not(feature = "no_std"))] +use std::io; + pub fn decode_vec(reader: &mut R, func: F) -> Result> where - R: std::io::Read, + R: io::Read, F: Fn(&mut R) -> Result, { // This is fine. It's already range checked by `decode_u32` @@ -19,7 +28,7 @@ where Ok(v) } -pub fn decode_kind(reader: &mut R) -> Result { +pub fn decode_kind(reader: &mut R) -> Result { let mut byte: [u8; 1] = [0; 1]; reader.read_exact(&mut byte)?; diff --git a/wasm/src/error.rs b/wasm/src/error.rs index 567620ac1..6a82796a9 100644 --- a/wasm/src/error.rs +++ b/wasm/src/error.rs @@ -1,9 +1,14 @@ -pub type Result = std::result::Result; +#[cfg(feature = "no_std")] +use core2::io; +#[cfg(not(feature = "no_std"))] +use std::io; + +pub type Result = core::result::Result; #[repr(u8)] #[derive(Debug)] pub enum Error { - IoError(std::io::Error), + IoError(io::Error), ReadZeroBytes, TooManyBytes { expected: u8, found: u8 }, InvalidSectionOrder, @@ -14,8 +19,8 @@ pub enum Error { ArrayTooLarge, } -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Display for Error { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Self::IoError(e) => e.fmt(f), Self::ReadZeroBytes => f.write_str("Tried to read from reader but got 0 bytes"), @@ -35,10 +40,10 @@ impl std::fmt::Display for Error { } } -impl std::error::Error for Error {} +impl core::error::Error for Error {} -impl From for Error { - fn from(v: std::io::Error) -> Self { +impl From for Error { + fn from(v: io::Error) -> Self { Self::IoError(v) } } diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index a1b362d66..a45a5596d 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -1,8 +1,17 @@ +#![cfg_attr(feature = "no_std", no_std, feature(error_in_core))] + +#[cfg(feature = "no_std")] +use core2::io; +#[cfg(feature = "no_std")] +extern crate alloc; +#[cfg(not(feature = "no_std"))] +use std::io; + mod decoder; pub mod error; mod varint; -pub fn compile_module(bytes: &mut R) -> Result<(), error::Error> { +pub fn compile_module(bytes: &mut R) -> Result<(), error::Error> { let _module = decoder::Module::new(bytes)?; todo!("Still need to add compiler and export generator"); } diff --git a/wasm/src/varint.rs b/wasm/src/varint.rs index 6b99b1762..076762202 100644 --- a/wasm/src/varint.rs +++ b/wasm/src/varint.rs @@ -1,3 +1,8 @@ +#[cfg(feature = "no_std")] +use core2::io; +#[cfg(not(feature = "no_std"))] +use std::io; + use crate::error::Error; const SEGMENT_BITS: u8 = 0x7F; @@ -9,7 +14,7 @@ pub struct DecodedResult { pub bytes_read: u8, } -pub fn decode_u32(reader: &mut R) -> Result, Error> { +pub fn decode_u32(reader: &mut R) -> Result, Error> { let mut length = 0; let mut value = 0; let mut bytes_read: u8 = 0; @@ -42,7 +47,7 @@ pub fn decode_u32(reader: &mut R) -> Result } #[allow(dead_code)] -pub fn decode_u64(reader: &mut R) -> Result, Error> { +pub fn decode_u64(reader: &mut R) -> Result, Error> { let mut length = 0; let mut value = 0; let mut bytes_read: u8 = 0;