Skip to content

Commit

Permalink
feat: full no_std support
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Jan 26, 2024
1 parent b9a4e87 commit 461126c
Show file tree
Hide file tree
Showing 24 changed files with 363 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ jobs:
run: rustup update nightly

- name: Build (nightly, no default features)
run: cargo +nightly build --workspace --exclude wasm-testsuite --no-default-features
run: cargo +nightly build --workspace --exclude wasm-testsuite --exclude rust-wasm-examples --no-default-features

- name: Run tests (nightly, no default features)
run: cargo +nightly test --workspace --exclude wasm-testsuite --no-default-features
run: cargo +nightly test --workspace --exclude wasm-testsuite --exclude rust-wasm-examples --no-default-features

- name: Run MVP testsuite (nightly)
run: cargo +nightly test-mvp
101 changes: 101 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[workspace]
members=["crates/*"]
default-members=["crates/cli"]
resolver="2"

[workspace.package]
Expand All @@ -9,3 +8,12 @@ edition="2021"
license="MIT OR Apache-2.0"
authors=["Henry Gressmann <mail@henrygressmann.de>"]
repository="https://github.com/explodingcamera/tinywasm"

[package]
name="tinywasm-root"
publish=false
edition="2021"

[dev-dependencies]
color-eyre="0.6"
tinywasm={path="crates/tinywasm"}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ $ cargo add tinywasm
- **`parser`**\
Enables the `tinywasm-parser` crate. This is enabled by default.

With all these features disabled, TinyWasm does not depend on any external crates and can be used in `no_std` environments.
With all these features disabled, TinyWasm only depends on `core`, `alloc` and `libm` and can be used in `no_std` environments.

<!-- # 🎯 Goals
Expand Down
6 changes: 3 additions & 3 deletions crates/parser/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::log;
use alloc::{boxed::Box, format, string::ToString, vec::Vec};
use log::debug;
use tinywasm_types::*;
use wasmparser::{FuncValidator, OperatorsReader, ValidatorResources};

Expand Down Expand Up @@ -239,7 +239,7 @@ pub fn process_operators<'a>(
let mut labels_ptrs = Vec::new(); // indexes into the instructions array

for op in ops {
debug!("op: {:?}", op);
log::debug!("op: {:?}", op);

let op = op?;
validator.op(offset, &op)?;
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn process_operators<'a>(
}
End => {
if let Some(label_pointer) = labels_ptrs.pop() {
debug!("ending block: {:?}", instructions[label_pointer]);
log::debug!("ending block: {:?}", instructions[label_pointer]);

let current_instr_ptr = instructions.len();

Expand Down
2 changes: 2 additions & 0 deletions crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use log;
#[cfg(not(feature = "logging"))]
mod log {
macro_rules! debug ( ($($tt:tt)*) => {{}} );
macro_rules! error ( ($($tt:tt)*) => {{}} );
pub(crate) use debug;
pub(crate) use error;
}

mod conversion;
Expand Down
4 changes: 2 additions & 2 deletions crates/parser/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ impl ModuleReader {
validator.end(offset)?;
self.end_reached = true;
}
CustomSection(reader) => {
CustomSection(_reader) => {
debug!("Found custom section");
debug!("Skipping custom section: {:?}", reader.name());
debug!("Skipping custom section: {:?}", _reader.name());
}
// TagSection(tag) => {
// debug!("Found tag section");
Expand Down
1 change: 1 addition & 0 deletions crates/tinywasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path="src/lib.rs"
log={version="0.4", optional=true}
tinywasm-parser={version="0.3.0-alpha.0", path="../parser", default-features=false, optional=true}
tinywasm-types={version="0.3.0-alpha.0", path="../types", default-features=false}
libm={version="0.2", default-features=false}

[dev-dependencies]
wasm-testsuite={path="../wasm-testsuite"}
Expand Down
6 changes: 3 additions & 3 deletions crates/tinywasm/src/func.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::log;
use alloc::{boxed::Box, format, string::String, string::ToString, vec, vec::Vec};
use log::{debug, info};
use tinywasm_types::{FuncAddr, FuncType, ValType, WasmValue};

use crate::{
Expand Down Expand Up @@ -34,7 +34,7 @@ impl FuncHandle {

// 4. If the length of the provided argument values is different from the number of expected arguments, then fail
if func_ty.params.len() != params.len() {
info!("func_ty.params: {:?}", func_ty.params);
log::info!("func_ty.params: {:?}", func_ty.params);
return Err(Error::Other(format!(
"param count mismatch: expected {}, got {}",
func_ty.params.len(),
Expand Down Expand Up @@ -62,7 +62,7 @@ impl FuncHandle {
};

// 6. Let f be the dummy frame
debug!("locals: {:?}", locals);
log::debug!("locals: {:?}", locals);
let call_frame = CallFrame::new(func_inst, params, locals);

// 7. Push the frame f to the call stack
Expand Down
2 changes: 1 addition & 1 deletion crates/tinywasm/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::fmt::Debug;

use crate::{
func::{FromWasmValueTuple, IntoWasmValueTuple, ValTypesFromTuple},
LinkingError, Result,
log, LinkingError, Result,
};
use alloc::{
collections::BTreeMap,
Expand Down
5 changes: 3 additions & 2 deletions crates/tinywasm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tinywasm_types::{

use crate::{
func::{FromWasmValueTuple, IntoWasmValueTuple},
Error, FuncHandle, FuncHandleTyped, Imports, Module, Result, Store,
log, Error, FuncHandle, FuncHandleTyped, Imports, Module, Result, Store,
};

/// An instanciated WebAssembly module
Expand Down Expand Up @@ -123,7 +123,8 @@ impl ModuleInstance {
&self.0.func_addrs
}

pub(crate) fn func_tys(&self) -> &[FuncType] {
/// Get the module's function types
pub fn func_tys(&self) -> &[FuncType] {
&self.0.types
}

Expand Down
6 changes: 6 additions & 0 deletions crates/tinywasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ use log;
#[cfg(not(feature = "logging"))]
pub(crate) mod log {
macro_rules! debug ( ($($tt:tt)*) => {{}} );
macro_rules! info ( ($($tt:tt)*) => {{}} );
macro_rules! trace ( ($($tt:tt)*) => {{}} );
macro_rules! error ( ($($tt:tt)*) => {{}} );
pub(crate) use debug;
pub(crate) use error;
pub(crate) use info;
pub(crate) use trace;
}

mod error;
Expand Down
12 changes: 10 additions & 2 deletions crates/tinywasm/src/runtime/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use core::ops::{BitAnd, BitOr, BitXor, Neg};

use super::{InterpreterRuntime, Stack};
use crate::log;
use crate::{
log::debug,
runtime::{BlockType, CallFrame, LabelArgs, LabelFrame},
Error, FuncContext, ModuleInstance, Result, Store, Trap,
};
use alloc::{string::ToString, vec::Vec};
use core::ops::{BitAnd, BitOr, BitXor, Neg};
use tinywasm_types::{ElementKind, Instruction, ValType};

#[cfg(not(feature = "std"))]
mod no_std_floats;

#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use no_std_floats::FExt;

mod macros;
mod traits;

use macros::*;
use traits::*;

Expand Down
Loading

0 comments on commit 461126c

Please sign in to comment.