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

Commit 7e668f9

Browse files
feat: use postcard for new twasm format
Closes #32, #31 Co-authored-by: Mateusz <[email protected]> Signed-off-by: Henry Gressmann <[email protected]>
1 parent e978227 commit 7e668f9

File tree

15 files changed

+109
-290
lines changed

15 files changed

+109
-290
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16-
- **Breaking:**: New backwards-incompatible version of the twasm format (upgraded `rkyv` to 0.8.0)
16+
- **Breaking:**: New backwards-incompatible version of the twasm format based on `postcard` (thanks [@dragonnn](https://github.com/dragonnn))
1717
- **Breaking:**: `RefNull` has been removed and replaced with new `FuncRef` and `ExternRef` structs
1818
- Increased MSRV to 1.83.0
1919

Cargo.lock

Lines changed: 36 additions & 149 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/parser/src/visit.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::Result;
33
use crate::conversion::{convert_heaptype, convert_valtype};
44
use alloc::string::ToString;
55
use alloc::{boxed::Box, vec::Vec};
6-
use tinywasm_types::{Instruction, MemoryArg, SimdInstruction, WasmFunctionData};
6+
use tinywasm_types::{Instruction, MemoryArg, WasmFunctionData};
77
use wasmparser::{
88
FuncValidator, FuncValidatorAllocations, FunctionBody, VisitOperator, VisitSimdOperator, WasmModuleResources,
99
};
@@ -84,12 +84,6 @@ macro_rules! define_operands {
8484
)*};
8585
}
8686

87-
macro_rules! define_operands_simd {
88-
($($name:ident($instr:ident $(,$ty:ty)*)),*) => {$(
89-
define_operand!($name(SimdInstruction::$instr $(,$ty)*));
90-
)*};
91-
}
92-
9387
macro_rules! define_mem_operands {
9488
($($name:ident($instr:ident)),*) => {$(
9589
fn $name(&mut self, memarg: wasmparser::MemArg) -> Self::Output {
@@ -101,15 +95,15 @@ macro_rules! define_mem_operands {
10195
macro_rules! define_mem_operands_simd {
10296
($($name:ident($instr:ident)),*) => {$(
10397
fn $name(&mut self, memarg: wasmparser::MemArg) -> Self::Output {
104-
self.instructions.push(SimdInstruction::$instr(MemoryArg::new(memarg.offset, memarg.memory)).into());
98+
self.instructions.push(Instruction::$instr(MemoryArg::new(memarg.offset, memarg.memory)).into());
10599
}
106100
)*};
107101
}
108102

109103
macro_rules! define_mem_operands_simd_lane {
110104
($($name:ident($instr:ident)),*) => {$(
111105
fn $name(&mut self, memarg: wasmparser::MemArg, lane: u8) -> Self::Output {
112-
self.instructions.push(SimdInstruction::$instr(MemoryArg::new(memarg.offset, memarg.memory), lane).into());
106+
self.instructions.push(Instruction::$instr(MemoryArg::new(memarg.offset, memarg.memory), lane).into());
113107
}
114108
)*};
115109
}
@@ -496,7 +490,7 @@ impl<R: WasmModuleResources> wasmparser::VisitSimdOperator<'_> for FunctionBuild
496490
visit_v128_load8_lane(V128Load8Lane), visit_v128_load16_lane(V128Load16Lane), visit_v128_load32_lane(V128Load32Lane), visit_v128_load64_lane(V128Load64Lane),
497491
visit_v128_store8_lane(V128Store8Lane), visit_v128_store16_lane(V128Store16Lane), visit_v128_store32_lane(V128Store32Lane), visit_v128_store64_lane(V128Store64Lane)
498492
}
499-
define_operands_simd! {
493+
define_operands! {
500494
visit_v128_not(V128Not), visit_v128_and(V128And), visit_v128_andnot(V128AndNot), visit_v128_or(V128Or), visit_v128_xor(V128Xor), visit_v128_bitselect(V128Bitselect), visit_v128_any_true(V128AnyTrue),
501495
visit_i8x16_splat(I8x16Splat), visit_i8x16_swizzle(I8x16Swizzle), visit_i8x16_eq(I8x16Eq), visit_i8x16_ne(I8x16Ne), visit_i8x16_lt_s(I8x16LtS), visit_i8x16_lt_u(I8x16LtU), visit_i8x16_gt_s(I8x16GtS), visit_i8x16_gt_u(I8x16GtU), visit_i8x16_le_s(I8x16LeS), visit_i8x16_le_u(I8x16LeU), visit_i8x16_ge_s(I8x16GeS), visit_i8x16_ge_u(I8x16GeU),
502496
visit_i16x8_splat(I16x8Splat), visit_i16x8_eq(I16x8Eq), visit_i16x8_ne(I16x8Ne), visit_i16x8_lt_s(I16x8LtS), visit_i16x8_lt_u(I16x8LtU), visit_i16x8_gt_s(I16x8GtS), visit_i16x8_gt_u(I16x8GtU), visit_i16x8_le_s(I16x8LeS), visit_i16x8_le_u(I16x8LeU), visit_i16x8_ge_s(I16x8GeS), visit_i16x8_ge_u(I16x8GeU),
@@ -537,11 +531,11 @@ impl<R: WasmModuleResources> wasmparser::VisitSimdOperator<'_> for FunctionBuild
537531

538532
fn visit_i8x16_shuffle(&mut self, lanes: [u8; 16]) -> Self::Output {
539533
self.v128_constants.push(u128::from_le_bytes(lanes));
540-
self.instructions.push(SimdInstruction::I8x16Shuffle(self.v128_constants.len() as u32 - 1).into());
534+
self.instructions.push(Instruction::I8x16Shuffle(self.v128_constants.len() as u32 - 1).into());
541535
}
542536

543537
fn visit_v128_const(&mut self, value: wasmparser::V128) -> Self::Output {
544538
self.v128_constants.push(value.i128() as u128);
545-
self.instructions.push(SimdInstruction::V128Const(self.v128_constants.len() as u32 - 1).into());
539+
self.instructions.push(Instruction::V128Const(self.v128_constants.len() as u32 - 1).into());
546540
}
547541
}

crates/tinywasm/benches/argon2id.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use criterion::{criterion_group, criterion_main, Criterion};
22
use eyre::Result;
33
use tinywasm::{types, ModuleInstance, Store};
4-
use types::{archive::AlignedVec, TinyWasmModule};
4+
use types::TinyWasmModule;
55

66
const WASM: &[u8] = include_bytes!("../../../examples/rust/out/argon2id.opt.wasm");
77

@@ -11,13 +11,13 @@ fn argon2id_parse() -> Result<TinyWasmModule> {
1111
Ok(data)
1212
}
1313

14-
fn argon2id_to_twasm(module: TinyWasmModule) -> Result<AlignedVec> {
15-
let twasm = module.serialize_twasm();
14+
fn argon2id_to_twasm(module: &TinyWasmModule) -> Result<Vec<u8>> {
15+
let twasm = module.serialize_twasm()?;
1616
Ok(twasm)
1717
}
1818

19-
fn argon2id_from_twasm(twasm: AlignedVec) -> Result<TinyWasmModule> {
20-
let module = TinyWasmModule::from_twasm(&twasm)?;
19+
fn argon2id_from_twasm(twasm: &[u8]) -> Result<TinyWasmModule> {
20+
let module = TinyWasmModule::from_twasm(twasm)?;
2121
Ok(module)
2222
}
2323

@@ -31,11 +31,11 @@ fn argon2id_run(module: TinyWasmModule) -> Result<()> {
3131

3232
fn criterion_benchmark(c: &mut Criterion) {
3333
let module = argon2id_parse().expect("argon2id_parse");
34-
let twasm = argon2id_to_twasm(module.clone()).expect("argon2id_to_twasm");
34+
let twasm = argon2id_to_twasm(&module).expect("argon2id_to_twasm");
3535

3636
c.bench_function("argon2id_parse", |b| b.iter(argon2id_parse));
37-
c.bench_function("argon2id_to_twasm", |b| b.iter(|| argon2id_to_twasm(module.clone())));
38-
c.bench_function("argon2id_from_twasm", |b| b.iter(|| argon2id_from_twasm(twasm.clone())));
37+
c.bench_function("argon2id_to_twasm", |b| b.iter(|| argon2id_to_twasm(&module)));
38+
c.bench_function("argon2id_from_twasm", |b| b.iter(|| argon2id_from_twasm(&twasm)));
3939
c.bench_function("argon2id", |b| b.iter(|| argon2id_run(module.clone())));
4040
}
4141

0 commit comments

Comments
 (0)