(input: &str, bytes: &[u8])
| 58 | } |
| 59 | |
| 60 | fn load_module_from_bytes(input: &str, bytes: &[u8]) -> Result<LoadedModule> { |
| 61 | if bytes.starts_with(b"TWAS") { |
| 62 | let module = Module::try_from_twasm(bytes).with_context(|| format!("failed to read twasm input `{input}`"))?; |
| 63 | return Ok(LoadedModule { module, format: InputFormat::Twasm }); |
| 64 | } |
| 65 | |
| 66 | #[cfg(feature = "wat")] |
| 67 | if input != "-" && has_extension(input, "wat") { |
| 68 | let wasm = wat::parse_bytes(bytes).with_context(|| format!("failed to parse WAT input `{input}`"))?; |
| 69 | let module = |
| 70 | tinywasm::parse_bytes(&wasm).with_context(|| format!("failed to parse Wasm generated from `{input}`"))?; |
| 71 | return Ok(LoadedModule { module, format: InputFormat::Wat }); |
| 72 | } |
| 73 | |
| 74 | #[cfg(not(feature = "wat"))] |
| 75 | if input != "-" && has_extension(input, "wat") { |
| 76 | bail!("wat support is not enabled in this build") |
| 77 | } |
| 78 | |
| 79 | #[cfg(feature = "wat")] |
| 80 | if input == "-" |
| 81 | && let Ok(wasm) = wat::parse_bytes(bytes) |
| 82 | { |
| 83 | let module = tinywasm::parse_bytes(&wasm).context("failed to parse Wasm generated from stdin WAT input")?; |
| 84 | return Ok(LoadedModule { module, format: InputFormat::Wat }); |
| 85 | } |
| 86 | |
| 87 | let module = tinywasm::parse_bytes(bytes).with_context(|| format!("failed to parse Wasm input `{input}`"))?; |
| 88 | Ok(LoadedModule { module, format: InputFormat::Wasm }) |
| 89 | } |
| 90 | |
| 91 | fn read_input_bytes(input: &str) -> Result<Vec<u8>> { |
| 92 | if input == "-" { |
no test coverage detected