Skip to content

Commit 17a0780

Browse files
committed
Write some tests for compile-time builtins
1 parent 6d5ca4b commit 17a0780

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

tests/all/compile_time_builtins.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod async_functions;
77
mod call_hook;
88
mod cli_tests;
99
mod code_too_large;
10+
mod compile_time_builtins;
1011
mod component_model;
1112
mod coredump;
1213
mod custom_code_memory;

0 commit comments

Comments
 (0)