diff --git a/build.rs b/build.rs index a06812f..543218c 100644 --- a/build.rs +++ b/build.rs @@ -178,7 +178,7 @@ fn make_opcode(os: &mut File, lines: &[String]) { "// // This file is automatically generated at build time. DO NOT EDIT DIRECTLY. // -use crate::{{leb128::*, CompileErrorKind, WasmMemArg, WasmBlockType, BrTableVec}}; +use crate::{{leb128::*, BrTableVec, WasmBlockType, WasmCompileErrorKind, WasmMemArg}}; use core::fmt; /// WebAssembly Opcode @@ -200,7 +200,7 @@ pub enum WasmOpcode {{ "}} impl WasmOpcode {{ - pub fn fetch(reader: &mut Leb128Reader) -> Result {{ + pub fn fetch(reader: &mut Leb128Reader) -> Result {{ let leading = reader.read_byte()?; match leading {{ " @@ -292,7 +292,7 @@ impl WasmOpcode {{ writeln!( os, - " _ => Err(CompileErrorKind::InvalidBytecode2(leading, trailing)) + " _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)) }} }}", ) @@ -301,7 +301,7 @@ impl WasmOpcode {{ write!( os, - " _ => Err(CompileErrorKind::InvalidBytecode(leading)) + " _ => Err(WasmCompileErrorKind::InvalidBytecode(leading)) }} }} diff --git a/lib/wami_macro/src/lib.rs b/lib/wami_macro/src/lib.rs index 8e09806..f70d37e 100644 --- a/lib/wami_macro/src/lib.rs +++ b/lib/wami_macro/src/lib.rs @@ -22,8 +22,7 @@ macro_rules! unexpected_token { fn is_primitive(value: &str) -> bool { let primitive_types = [ - "bool", "isize", "usize", "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64", "f32", - "f64", + "bool", "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64", "f32", "f64", ]; primitive_types.contains(&value) @@ -141,7 +140,7 @@ fn reduce_path(path: &Path) -> String { fn type_to_signature(ident: &str) -> &str { match ident { "void" => "v", - "bool" | "i8" | "u8" | "i16" | "u16" | "isize" | "usize" | "i32" | "u32" => "i", + "bool" | "i8" | "u8" | "i16" | "u16" | "i32" | "u32" => "i", "u64" | "i64" => "l", "f32" => "f", "f64" => "d", diff --git a/sample/hello/src/main.rs b/sample/hello/src/main.rs index 8234f09..c318622 100644 --- a/sample/hello/src/main.rs +++ b/sample/hello/src/main.rs @@ -3,8 +3,7 @@ use wami::prelude::*; fn main() { - let bin = include_bytes!("../hello.wasm"); - let instance = WebAssembly::instantiate(bin, &Env {}).unwrap(); + let instance = WebAssembly::instantiate(include_bytes!("../hello.wasm"), &Env {}).unwrap(); assert_eq!(instance.exports().add(123, 456).unwrap(), 123 + 456); } diff --git a/src/_generated/opcode.rs b/src/_generated/opcode.rs index 7cc181d..670725b 100644 --- a/src/_generated/opcode.rs +++ b/src/_generated/opcode.rs @@ -1863,7 +1863,7 @@ impl WasmOpcode { // 0xFB 0x1E `i31.get_u` (Gc) 0x1e => Ok(Self::I31GetU), - _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)), + _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)) } } 0xFC => { @@ -1931,7 +1931,7 @@ impl WasmOpcode { // 0xFC 0x11 `table.fill` u32 (ReferenceTypes) 0x11 => Ok(Self::TableFill(reader.read()?)), - _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)), + _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)) } } 0xFD => { @@ -2708,7 +2708,7 @@ impl WasmOpcode { // 0xFD 0x114 `f32x4.relaxed_dot_bf16x8_add_f32x4` (RelaxedSimd) 0x114 => Ok(Self::F32x4RelaxedDotBf16x8AddF32x4), - _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)), + _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)) } } 0xFE => { @@ -2915,10 +2915,10 @@ impl WasmOpcode { // 0xFE 0x4E `i64.atomic.rmw32.cmpxchg_u` (Threads) 0x4e => Ok(Self::I64AtomicRmw32CmpxchgU), - _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)), + _ => Err(WasmCompileErrorKind::InvalidBytecode2(leading, trailing)) } } - _ => Err(WasmCompileErrorKind::InvalidBytecode(leading)), + _ => Err(WasmCompileErrorKind::InvalidBytecode(leading)) } } @@ -3493,14 +3493,16 @@ impl WasmOpcode { Self::V128Xor => WasmMnemonic::V128Xor, Self::Zi32x4RelaxedTruncF32x4S => WasmMnemonic::Zi32x4RelaxedTruncF32x4S, Self::Zi8x16RelaxedSwizzle => WasmMnemonic::Zi8x16RelaxedSwizzle, + } } + } impl fmt::Display for WasmOpcode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - _ => f.write_str(self.as_str()), + _ => f.write_str(self.as_str()) } } } @@ -3508,7 +3510,7 @@ impl fmt::Display for WasmOpcode { impl fmt::Debug for WasmOpcode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - _ => f.write_str(self.as_str()), + _ => f.write_str(self.as_str()) } } } @@ -5215,7 +5217,7 @@ impl WasmMnemonic { Self::Zi8x16RelaxedSwizzle => "Zi8x16.relaxed_swizzle", } } - + pub const fn proposal(&self) -> WasmProposal { match self { Self::AnyConvertExtern => WasmProposal::Gc, diff --git a/src/memory.rs b/src/memory.rs index 3f86327..c2a6c1e 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -297,21 +297,21 @@ impl DerefMut for SharedDataStore { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct WasmPtr { repr: u32, - _phantom: PhantomData, + _phantom: PhantomData<*const T>, } impl WasmPtr { #[inline] - pub const fn from_usize(value: usize) -> Self { + pub const fn from_u32(value: u32) -> Self { Self { - repr: value as u32, + repr: value, _phantom: PhantomData, } } #[inline] - pub const fn as_usize(&self) -> usize { - self.repr as usize + pub const fn as_u32(&self) -> u32 { + self.repr } } @@ -326,21 +326,21 @@ impl From> for WasmValue { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct WasmPtrMut { repr: u32, - _phantom: PhantomData, + _phantom: PhantomData<*mut T>, } impl WasmPtrMut { #[inline] - pub const fn from_usize(value: usize) -> Self { + pub const fn from_u32(value: u32) -> Self { Self { - repr: value as u32, + repr: value, _phantom: PhantomData, } } #[inline] - pub const fn as_usize(&self) -> usize { - self.repr as usize + pub const fn as_u32(&self) -> u32 { + self.repr } } diff --git a/src/wasm.rs b/src/wasm.rs index 259d482..45d536d 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -426,7 +426,9 @@ impl WasmModule { .memories .get_mut(memidx) .ok_or(WasmCompileErrorKind::InvalidData)?; - memory.write_slice(offset, src).unwrap(); + memory + .write_slice(offset, src) + .map_err(|_| WasmCompileErrorKind::InvalidData)? } Ok(()) }