Files
wasmtime/tests/all/component_model/import.rs
Alex Crichton 3ed6fae7b3 Add trampoline compilation support for lowered imports (#4206)
* Add trampoline compilation support for lowered imports

This commit adds support to the component model implementation for
compiling trampolines suitable for calling host imports. Currently this
is purely just the compilation side of things, modifying the
wasmtime-cranelift crate and additionally filling out a new
`VMComponentOffsets` type (similar to `VMOffsets`). The actual creation
of a `VMComponentContext` is still not performed and will be a
subsequent PR.

Internally though some tests are actually possible with this where we at
least assert that compilation of a component and creation of everything
in-memory doesn't panic or trip any assertions, so some tests are added
here for that as well.

* Fix some test errors
2022-06-03 10:01:42 -05:00

81 lines
2.4 KiB
Rust

use anyhow::Result;
use wasmtime::component::Component;
#[test]
fn can_compile() -> Result<()> {
let engine = super::engine();
let libc = r#"
(module $libc
(memory (export "memory") 1)
(func (export "canonical_abi_realloc") (param i32 i32 i32 i32) (result i32)
unreachable)
(func (export "canonical_abi_free") (param i32 i32 i32)
unreachable)
)
(instance $libc (instantiate (module $libc)))
"#;
Component::new(
&engine,
r#"(component
(import "" (func $f))
(func (canon.lower (func $f)))
)"#,
)?;
Component::new(
&engine,
format!(
r#"(component
(import "" (func $f (param string)))
{libc}
(func (canon.lower (into $libc) (func $f)))
)"#
),
)?;
Component::new(
&engine,
format!(
r#"(component
(import "f1" (func $f1 (param string) (result string)))
{libc}
(func (canon.lower (into $libc) (func $f1)))
(import "f2" (func $f2 (param u32) (result (list u8))))
(instance $libc2 (instantiate (module $libc)))
(func (canon.lower (into $libc2) (func $f2)))
(func (canon.lower (into $libc2) (func $f1)))
(func (canon.lower (into $libc) (func $f2)))
)"#
),
)?;
Component::new(
&engine,
format!(
r#"(component
(import "log" (func $log (param string)))
{libc}
(func $log_lower (canon.lower (into $libc) (func $log)))
(module $logger
(import "host" "log" (func $log (param i32 i32)))
(import "libc" "memory" (memory 1))
(func (export "call")
i32.const 0
i32.const 0
call $log)
)
(instance $logger (instantiate (module $logger)
(with "host" (instance (export "log" (func $log_lower))))
(with "libc" (instance $libc))
))
(func (export "call")
(canon.lift (func) (func $logger "call"))
)
)"#
),
)?;
Ok(())
}