Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] No std partial port #221

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

#[cfg(test)]
mod tests {
#[test]
Expand Down
8 changes: 5 additions & 3 deletions small_string/lib.rs
Original file line number Diff line number Diff line change
@@ -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())
}
}
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var hi = 3+4;
aapoalas marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
load1n9 marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 8 additions & 1 deletion wasm/src/decoder/code_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ 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<R: std::io::Read>(reader: &mut R) -> Result<CodeBlock> {
pub fn decode_code_section<R: io::Read>(reader: &mut R) -> Result<CodeBlock> {
let body_size = decode_u32(reader)?.value;

let v = util::decode_vec(reader, |x| Ok((decode_u32(x)?, util::decode_kind(x)?)))?;
Expand Down
3 changes: 3 additions & 0 deletions wasm/src/decoder/common.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions wasm/src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -29,7 +35,7 @@ pub struct Module {
}

impl Module {
pub fn new<R: std::io::Read>(reader: &mut R) -> Result<Self> {
pub fn new<R: io::Read>(reader: &mut R) -> Result<Self> {
let mut module = Self::default();
let mut last_section_id: i8 = -1;
for i in 0..12 {
Expand Down Expand Up @@ -58,7 +64,7 @@ impl Module {
}
}

pub fn decode_any_section<R: std::io::Read>(reader: &mut R) -> Result<Section> {
pub fn decode_any_section<R: io::Read>(reader: &mut R) -> Result<Section> {
let mut section_id_byte: [u8; 1] = [0; 1];
reader.read_exact(&mut section_id_byte)?;
// if section_id_byte[0] == 0x08 {
Expand Down
7 changes: 6 additions & 1 deletion wasm/src/decoder/type_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: std::io::Read>(reader: &mut R) -> Result<common::FnType> {
pub fn decode_type_section<R: io::Read>(reader: &mut R) -> Result<common::FnType> {
let mut byte: [u8; 1] = [0; 1];
reader.read_exact(&mut byte)?;

Expand Down
13 changes: 11 additions & 2 deletions wasm/src/decoder/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, R, F>(reader: &mut R, func: F) -> Result<Vec<T>>
where
R: std::io::Read,
R: io::Read,
F: Fn(&mut R) -> Result<T>,
{
// This is fine. It's already range checked by `decode_u32`
Expand All @@ -19,7 +28,7 @@ where
Ok(v)
}

pub fn decode_kind<R: std::io::Read>(reader: &mut R) -> Result<common::ValueKind> {
pub fn decode_kind<R: io::Read>(reader: &mut R) -> Result<common::ValueKind> {
let mut byte: [u8; 1] = [0; 1];
reader.read_exact(&mut byte)?;

Expand Down
19 changes: 12 additions & 7 deletions wasm/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(feature = "no_std")]
use core2::io;
#[cfg(not(feature = "no_std"))]
use std::io;

pub type Result<T> = core::result::Result<T, Error>;

#[repr(u8)]
#[derive(Debug)]
pub enum Error {
IoError(std::io::Error),
IoError(io::Error),
ReadZeroBytes,
TooManyBytes { expected: u8, found: u8 },
InvalidSectionOrder,
Expand All @@ -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"),
Expand All @@ -35,10 +40,10 @@ impl std::fmt::Display for Error {
}
}

impl std::error::Error for Error {}
impl core::error::Error for Error {}

impl From<std::io::Error> for Error {
fn from(v: std::io::Error) -> Self {
impl From<io::Error> for Error {
fn from(v: io::Error) -> Self {
Self::IoError(v)
}
}
12 changes: 11 additions & 1 deletion wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#![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<R: std::io::Read>(bytes: &mut R) -> Result<(), error::Error> {

pub fn compile_module<R: io::Read>(bytes: &mut R) -> Result<(), error::Error> {
let _module = decoder::Module::new(bytes)?;
todo!("Still need to add compiler and export generator");
}
9 changes: 7 additions & 2 deletions wasm/src/varint.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +14,7 @@ pub struct DecodedResult<T> {
pub bytes_read: u8,
}

pub fn decode_u32<R: std::io::Read>(reader: &mut R) -> Result<DecodedResult<u32>, Error> {
pub fn decode_u32<R: io::Read>(reader: &mut R) -> Result<DecodedResult<u32>, Error> {
let mut length = 0;
let mut value = 0;
let mut bytes_read: u8 = 0;
Expand Down Expand Up @@ -42,7 +47,7 @@ pub fn decode_u32<R: std::io::Read>(reader: &mut R) -> Result<DecodedResult<u32>
}

#[allow(dead_code)]
pub fn decode_u64<R: std::io::Read>(reader: &mut R) -> Result<DecodedResult<u64>, Error> {
pub fn decode_u64<R: io::Read>(reader: &mut R) -> Result<DecodedResult<u64>, Error> {
let mut length = 0;
let mut value = 0;
let mut bytes_read: u8 = 0;
Expand Down
Loading