This directory contains tests for the core WebAssembly semantics, as described in [Semantics.md](https://github.com/WebAssembly/design/blob/master/Semantics.md) and specified by the [spec interpreter](https://github.com/WebAssembly/spec/blob/master/interpreter/spec).

These files should be a direct copy of the original [WebAssembly spec tests](/test/core).

Tests are written in the [S-Expression script format](https://github.com/WebAssembly/spec/blob/master/interpreter/README.md#s-expression-syntax) defined by the interpreter.

## Autogenerated Rust test cases

These files will serve as a base for autogenerating Rust testcases
when `WASM_GENERATE_SPECTESTS=1 cargo build` is executed
([src/build_spectests.rs](/src/build_spectests.rs)).

The resulting autogenerated spec tests live in the [src/spectests](/src/spectests).

## Testcases

Currently supported command assertions:

- [x] `module` _mostly implemented_ (it should support named modules `(module $Xx)`).
- [x] `assert_return` _mostly implemented_ (it should support calls to named modules `(invoke $Xx "call")`).
- [x] `assert_return_canonical_nan` _fully implemented_
- [x] `assert_return_arithmetic_nan` _fully implemented_
- [x] `assert_trap` _fully implemented_
- [x] `assert_invalid` _fully implemented_ (it should not require validation to be performed separate from compilation)
- [x] `assert_malformed` _fully implemented_
- [ ] `assert_uninstantiable` _not implemented yet_
- [ ] `assert_exhaustion` _not implemented yet_
- [ ] `register` _not implemented yet_
- [x] `perform_action` _partially implemented, only function invocations for now_

### Covered spec tests

The following spec tests are currently covered:

- [x] address.wast
- [x] align.wast
- [x] binary.wast
- [x] block.wast
- [x] br.wast
- [x] br_if.wast
- [x] br_table.wast
- [x] break-drop.wast
- [x] call.wast
- [x] call_indirect.wast
- [x] comments.wast
- [x] const.wast
- [x] conversions.wast
- [x] custom.wast
- [x] data.wast
- [ ] elem.wast
- [x] endianness.wast
- [x] exports.wast
- [x] f32.wast
- [x] f32_bitwise.wast
- [x] f32_cmp.wast
- [x] f64.wast
- [x] f64_bitwise.wast
- [x] f64_cmp.wast
- [x] fac.wast
- [x] float_exprs.wast
- [x] float_literals.wast
- [x] float_memory.wast
- [x] float_misc.wast
- [x] forward.wast
- [x] func.wast
- [x] func_ptrs.wast
- [x] get_local.wast
- [x] globals.wast
- [x] i32.wast
- [x] i64.wast
- [x] if.wast
- [ ] imports.wast
- [ ] inline-module.wast
- [x] int_exprs.wast
- [x] int_literals.wast
- [x] labels.wast
- [x] left-to-right.wast
- [ ] linking.wast
- [x] loop.wast
- [x] memory.wast
- [x] memory_grow.wast
- [x] memory_redundancy.wast
- [x] memory_trap.wast
- [x] names.wast
- [x] nop.wast
- [x] return.wast
- [x] select.wast
- [x] set_local.wast
- [ ] skip-stack-guard-page.wast
- [x] stack.wast
- [x] start.wast
- [x] store_retval.wast
- [x] switch.wast
- [x] tee_local.wast
- [x] token.wast
- [x] traps.wast
- [x] type.wast
- [x] typecheck.wast
- [ ] unreachable.wast
- [ ] unreached-invalid.wast
- [x] unwind.wast
- [ ] utf8-custom-section-id.wast
- [ ] utf8-import-field.wast
- [ ] utf8-import-module.wast
- [ ] utf8-invalid-encoding.wast

### Specific non-supported cases

There are some cases that we decided to skip for now to accelerate the release schedule:

- `SKIP_MUTABLE_GLOBALS`: Right now the Wasm parser can't validate a module with imported/exported `mut` globals. We decided to skip the tests until Cranelift and wasmparser can handle this (see [original spec proposal](https://github.com/WebAssembly/mutable-global)). Spec tests affected:
  - `globals.wast`
- `SKIP_CALL_INDIRECT_TYPE_MISMATCH`: we implemented traps in a fast way. We haven't yet covered the type mismatch on `call_indirect`. Specs affected:

  - `call_indirect.wast`

- `SKIP_CALL_UNDEFINED_ELEMENT`
  Tables are imported into every spec module, even for modules that don't expect it. We need to figure out a way to prevent importing of objects that are not explicitly imported into the module.

Currently `cranelift_wasm::ModuleEnvironment` does not provide `declare_table_import`, etc. so there is no meaningful way of fixing this yet.

- `call_indirect.wast`

- `SKIP_SHARED_TABLE` [elem.wast]
  Currently sharing tables between instances/modules does not work. Below are some of the reasons it is hard to achieve:

  - Rust naturally prevents such because of the possibility of race conditions
  - `ImportObject` is just a wrapper, what we really care about is references to its content.
  - `Instance::new` contains a mutation points, the part where after getting the object (memory or table) we push values to it
    `table[table_element_index] = func_addr`
  - Instance has its own created memories and tables and references to them must outlive `Instance::new()`
  - Possible strategy:

    ```rust
    // ImportObject should be passed by ref
    Instance::new<'a>(..., &ImportObject);

    // Add OwnedData to Instance struct
    struct OwnedData;

    // For parts where mutatation is really needed
    fn get_mut(&import) -> &mut ImportObject {
        unsafe { transmute::<&ImportObject, &mut ImportObject>(import) }
    }
    ```

  - `elem.wast`

- `SKIP_UNARY_OPERATION` [memory_grow.wast]
  In some versions of MacOS this is failing (perhaps because of the chip).
  More info here: 
  ```
Executing function c82_l299_action_invoke
thread 'test_memory_grow::test_module_5' panicked at 'assertion failed: `(left == right)`
  left: `Ok([I32(0)])`,
 right: `Ok([I32(31)])`', /Users/distiller/project/target/release/build/wasmer-spectests-98805f54de053dd1/out/spectests.rs:32304:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.


failures:
    test_memory_grow::test_module_5
```
  https://circleci.com/gh/wasmerio/wasmer/9556