|
| 1 | +//! Tests related to compile-time builtins. |
| 2 | +
|
| 3 | +use super::*; |
| 4 | +use std::path::Path; |
| 5 | +use wasmtime::{CodeBuilder, Engine, Instance, Store}; |
| 6 | + |
| 7 | +#[test] |
| 8 | +#[cfg_attr(miri, ignore)] |
| 9 | +fn basic() -> Result<()> { |
| 10 | + let _ = env_logger::try_init(); |
| 11 | + |
| 12 | + let engine = Engine::default(); |
| 13 | + |
| 14 | + let mut builder = CodeBuilder::new(&engine); |
| 15 | + builder.wasm_binary_or_text( |
| 16 | + r#" |
| 17 | + (module |
| 18 | + (import "math" "add" (func $add (param i32 i32) (result i32))) |
| 19 | + (func (export "f") (result i32) |
| 20 | + (call $add (i32.const 1) (i32.const 2)) |
| 21 | + ) |
| 22 | + ) |
| 23 | + "# |
| 24 | + .as_bytes(), |
| 25 | + Some(Path::new("basic.wat")), |
| 26 | + )?; |
| 27 | + |
| 28 | + let mut builtin = builder.define_compile_time_builtin("math", "add"); |
| 29 | + let x = builtin.i32_arg(0); |
| 30 | + let y = builtin.i32_arg(1); |
| 31 | + let z = builtin.i32_add(x, y); |
| 32 | + builtin.finish([z])?; |
| 33 | + |
| 34 | + let module = builder.compile_module()?; |
| 35 | + |
| 36 | + assert_eq!( |
| 37 | + module.imports().count(), |
| 38 | + 0, |
| 39 | + "import was satisfied at compile time" |
| 40 | + ); |
| 41 | + |
| 42 | + let mut store = Store::new(&engine, ()); |
| 43 | + let instance = Instance::new(&mut store, &module, &[])?; |
| 44 | + |
| 45 | + let f = instance.get_typed_func::<(), i32>(&mut store, "f")?; |
| 46 | + assert_eq!(f.call(&mut store, ())?, 3); |
| 47 | + |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +// TODO FITZGEN: test re-export of imported compile-time builtin |
| 52 | + |
| 53 | +// TODO FITZGEN: test `ref.func` of imported compile-time builtin |
| 54 | + |
| 55 | +// TODO FITZGEN: test type error in builtin def |
| 56 | + |
| 57 | +// TODO FITZGEN: test using invalid builtin value from different builtin |
0 commit comments