Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit d6493c2

Browse files
fix: fix build, improve error handling
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 7e668f9 commit d6493c2

File tree

14 files changed

+85
-31
lines changed

14 files changed

+85
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Support for the custom memory page sizes proposal ([#22](https://github.com/explodingcamera/tinywasm/pull/22) by [@danielstuart14](https://github.com/danielstuart14))
1313

14-
### Changed
14+
### Breaking Changes
1515

16-
- **Breaking:**: New backwards-incompatible version of the twasm format based on `postcard` (thanks [@dragonnn](https://github.com/dragonnn))
17-
- **Breaking:**: `RefNull` has been removed and replaced with new `FuncRef` and `ExternRef` structs
16+
- New backwards-incompatible version of the twasm format based on `postcard` (thanks [@dragonnn](https://github.com/dragonnn))
17+
- `RefNull` has been removed and replaced with new `FuncRef` and `ExternRef` structs
1818
- Increased MSRV to 1.83.0
19+
- `tinywasm::Error` is now `non_exhaustive`, `Error::ParseError` has been rename to `Error::Parser` and `Error::Twasm` has been added.
1920

2021
### Fixed
2122

crates/tinywasm/src/error.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use alloc::string::{String, ToString};
22
use alloc::vec::Vec;
33
use core::{fmt::Display, ops::ControlFlow};
4+
use tinywasm_types::archive::TwasmError;
45
use tinywasm_types::FuncType;
56

67
#[cfg(feature = "parser")]
78
pub use tinywasm_parser::ParseError;
89

910
/// Errors that can occur for `TinyWasm` operations
1011
#[derive(Debug)]
12+
#[non_exhaustive]
1113
pub enum Error {
1214
/// A WebAssembly trap occurred
1315
Trap(Trap),
@@ -41,7 +43,10 @@ pub enum Error {
4143

4244
#[cfg(feature = "parser")]
4345
/// A parsing error occurred
44-
ParseError(ParseError),
46+
Parser(ParseError),
47+
48+
/// A serialization error occurred
49+
Twasm(TwasmError),
4550
}
4651

4752
#[derive(Debug)]
@@ -169,6 +174,11 @@ impl From<LinkingError> for Error {
169174
}
170175
}
171176

177+
impl From<TwasmError> for Error {
178+
fn from(value: TwasmError) -> Self {
179+
Self::Twasm(value)
180+
}
181+
}
172182
impl From<Trap> for Error {
173183
fn from(value: Trap) -> Self {
174184
Self::Trap(value)
@@ -179,11 +189,12 @@ impl Display for Error {
179189
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
180190
match self {
181191
#[cfg(feature = "parser")]
182-
Self::ParseError(err) => write!(f, "error parsing module: {err:?}"),
192+
Self::Parser(err) => write!(f, "error parsing module: {err:?}"),
183193

184194
#[cfg(feature = "std")]
185195
Self::Io(err) => write!(f, "I/O error: {err}"),
186196

197+
Self::Twasm(err) => write!(f, "serialization error: {err}"),
187198
Self::Trap(trap) => write!(f, "trap: {trap}"),
188199
Self::Linker(err) => write!(f, "linking error: {err}"),
189200
Self::InvalidLabelType => write!(f, "invalid label type"),
@@ -238,7 +249,7 @@ impl core::error::Error for Error {}
238249
#[cfg(feature = "parser")]
239250
impl From<tinywasm_parser::ParseError> for Error {
240251
fn from(value: tinywasm_parser::ParseError) -> Self {
241-
Self::ParseError(value)
252+
Self::Parser(value)
242253
}
243254
}
244255

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
pub(super) trait NoStdFloatExt {
22
fn round(self) -> Self;
3-
fn abs(self) -> Self;
4-
fn signum(self) -> Self;
53
fn ceil(self) -> Self;
64
fn floor(self) -> Self;
75
fn trunc(self) -> Self;
86
fn sqrt(self) -> Self;
9-
fn copysign(self, other: Self) -> Self;
107
}
118

129
#[rustfmt::skip]
1310
impl NoStdFloatExt for f64 {
1411
#[inline] fn round(self) -> Self { libm::round(self) }
15-
#[inline] fn abs(self) -> Self { libm::fabs(self) }
16-
#[inline] fn signum(self) -> Self { libm::copysign(1.0, self) }
1712
#[inline] fn ceil(self) -> Self { libm::ceil(self) }
1813
#[inline] fn floor(self) -> Self { libm::floor(self) }
1914
#[inline] fn trunc(self) -> Self { libm::trunc(self) }
2015
#[inline] fn sqrt(self) -> Self { libm::sqrt(self) }
21-
#[inline] fn copysign(self, other: Self) -> Self { libm::copysign(self, other) }
2216
}
2317

2418
#[rustfmt::skip]
2519
impl NoStdFloatExt for f32 {
2620
#[inline] fn round(self) -> Self { libm::roundf(self) }
27-
#[inline] fn abs(self) -> Self { libm::fabsf(self) }
28-
#[inline] fn signum(self) -> Self { libm::copysignf(1.0, self) }
2921
#[inline] fn ceil(self) -> Self { libm::ceilf(self) }
3022
#[inline] fn floor(self) -> Self { libm::floorf(self) }
3123
#[inline] fn trunc(self) -> Self { libm::truncf(self) }
3224
#[inline] fn sqrt(self) -> Self { libm::sqrtf(self) }
33-
#[inline] fn copysign(self, other: Self) -> Self { libm::copysignf(self, other) }
3425
}

crates/types/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ pub use value::*;
4545
#[cfg(feature = "archive")]
4646
pub mod archive;
4747

48+
#[cfg(not(feature = "archive"))]
49+
pub mod archive {
50+
#[derive(Debug)]
51+
pub enum TwasmError {}
52+
impl core::fmt::Display for TwasmError {
53+
fn fmt(&self, _: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54+
Err(core::fmt::Error)
55+
}
56+
}
57+
impl core::error::Error for TwasmError {}
58+
}
59+
4860
/// A `TinyWasm` WebAssembly Module
4961
///
5062
/// This is the internal representation of a WebAssembly module in `TinyWasm`.

examples/rust/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ std=["tinywasm/std"]
2222
name="hello"
2323
path="src/hello.rs"
2424

25+
[[bin]]
26+
name="host_fn"
27+
path="src/host_fn.rs"
28+
2529
[[bin]]
2630
name="print"
2731
path="src/print.rs"

examples/rust/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
2-
cd "$(dirname "$0")"
2+
cd "$(dirname "$0")" || exit
33

4-
bins=("hello" "fibonacci" "print" "tinywasm" "argon2id")
4+
bins=("host_fn" "hello" "fibonacci" "print" "tinywasm" "argon2id")
55
exclude_wat=("tinywasm")
66
out_dir="./target/wasm32-unknown-unknown/wasm"
77
dest_dir="out"

examples/rust/src/argon2id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![no_main]
22

3-
#[no_mangle]
3+
#[unsafe(no_mangle)]
44
pub extern "C" fn argon2id(m_cost: i32, t_cost: i32, p_cost: i32) -> i32 {
55
let password = b"password";
66
let salt = b"some random salt";

examples/rust/src/fibonacci.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![no_main]
22

3-
#[no_mangle]
3+
#[unsafe(no_mangle)]
44
pub extern "C" fn fibonacci(n: i32) -> i32 {
55
let mut sum = 0;
66
let mut last = 0;
@@ -13,7 +13,7 @@ pub extern "C" fn fibonacci(n: i32) -> i32 {
1313
sum
1414
}
1515

16-
#[no_mangle]
16+
#[unsafe(no_mangle)]
1717
pub extern "C" fn fibonacci_recursive(n: i32) -> i32 {
1818
if n <= 1 {
1919
return n;

examples/rust/src/hello.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
#![no_main]
22

33
#[link(wasm_import_module = "env")]
4-
extern "C" {
4+
unsafe extern "C" {
55
fn print_utf8(location: i64, len: i32);
66
}
77

88
const ARG: &[u8] = &[0u8; 100];
99

10-
#[no_mangle]
10+
#[unsafe(no_mangle)]
1111
pub unsafe extern "C" fn arg_ptr() -> i32 {
1212
ARG.as_ptr() as i32
1313
}
1414

15-
#[no_mangle]
15+
#[unsafe(no_mangle)]
1616
pub unsafe extern "C" fn arg_size() -> i32 {
1717
ARG.len() as i32
1818
}
1919

20-
#[no_mangle]
20+
#[unsafe(no_mangle)]
2121
pub unsafe extern "C" fn hello(len: i32) {
2222
let arg = core::str::from_utf8(&ARG[0..len as usize]).unwrap();
2323
let res = format!("Hello, {}!", arg).as_bytes().to_vec();

examples/rust/src/host_fn.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![no_main]
2+
3+
#[link(wasm_import_module = "env")]
4+
unsafe extern "C" {
5+
fn bar(left: i64, right: i32) -> i32;
6+
}
7+
8+
#[unsafe(no_mangle)]
9+
pub fn foo() -> i32 {
10+
unsafe { bar(1, 2) }
11+
}

0 commit comments

Comments
 (0)