Function wasmedge_sdk::wat2wasm

pub fn wat2wasm(bytes: &[u8]) -> Result<Cow<'_, [u8]>, Error>
Expand description

Parses in-memory bytes as either the WebAssembly Text format, or a binary WebAssembly module Parses in-memory bytes as either the WebAssembly Text format, or a binary WebAssembly module.

This function will attempt to interpret the given bytes as one of two options:

  • A utf-8 string which is a *.wat file to be parsed.
  • A binary WebAssembly file starting with b"\0asm"

If the input is a string then it will be parsed as *.wat, and then after parsing it will be encoded back into a WebAssembly binary module. If the input is a binary that starts with b"\0asm" it will be returned verbatim. Everything that doesn’t start with b"\0asm" will be parsed as a utf-8 *.wat file, returning errors as appropriate.

For more information about parsing wat files, see [parse_str].

Errors

In addition to all of the errors that can be returned from [parse_str], this function will also return an error if the input does not start with b"\0asm" and is invalid utf-8. (failed to even try to call [parse_str]).

Examples

// Parsing bytes that are actually `*.wat` files
assert_eq!(&*wat::parse_bytes(b"(module)")?, b"\0asm\x01\0\0\0");
assert!(wat::parse_bytes(b"module").is_err());
assert!(wat::parse_bytes(b"binary\0file\0\that\0is\0not\0wat").is_err());

// Pass through binaries that look like real wasm files
assert_eq!(&*wat::parse_bytes(b"\0asm\x01\0\0\0")?, b"\0asm\x01\0\0\0");