[go: up one dir, main page]

Module script

Source
Expand description

Module for parsing WebAssembly script format (a.k.a. wast).

These scripts might be useful to integrate the official spec testsuite into implementations of the wasm execution engines (such as wasmi) and for developing fine-grained tests of runtimes or/and if it isn’t desired to use full fledged compilers.

§Example

use wabt::script::{ScriptParser, Command, CommandKind, Action, Value};

let wast = r#"
;; Define anonymous module with function export named `sub`.
(module
  (func (export "sub") (param $x i32) (param $y i32) (result i32)
    ;; return x - y;
    (i32.sub
      (get_local $x) (get_local $y)
    )
  )
)

;; Assert that invoking export `sub` with parameters (8, 3)
;; should return 5.
(assert_return
  (invoke "sub"
    (i32.const 8) (i32.const 3)
  )
  (i32.const 5)
)
"#;

let mut parser = ScriptParser::<f32, f64>::from_str(wast)?;
while let Some(Command { kind, .. }) = parser.next()? {
    match kind {
        CommandKind::Module { module, name } => {
            // The module is declared as annonymous.
            assert_eq!(name, None);

            // Convert the module into the binary representation and check the magic number.
            let module_binary = module.into_vec();
            assert_eq!(&module_binary[0..4], &[0, 97, 115, 109]);
        }
        CommandKind::AssertReturn { action, expected } => {
            assert_eq!(action, Action::Invoke {
                module: None,
                field: "sub".to_string(),
                args: vec![
                    Value::I32(8),
                    Value::I32(3)
                ],
            });
            assert_eq!(expected, vec![Value::I32(5)]);
        },
        _ => panic!("there are no other commands apart from that defined above"),
    }
}

Structs§

Command
Command in the script.
ModuleBinary
This is a handle to get the binary representation of the module.
ScriptParser
Parser which allows to parse WebAssembly script text format.

Enums§

Action
Description of action that should be performed on a wasm module.
CommandKind
Script’s command.
Error
Error that can happen when parsing spec.
Value
Wasm value

Traits§

FromBits
Bitwise conversion from T