Path: blob/main/tests/all/component_model/instance.rs
1691 views
use anyhow::Result;1use wasmtime::component::*;2use wasmtime::{Module, Store};34#[test]5#[cfg_attr(miri, ignore)]6fn instance_exports() -> Result<()> {7let engine = super::engine();8let component = r#"9(component10(import "a" (instance $i))11(import "b" (instance $i2 (export "m" (core module))))1213(alias export $i2 "m" (core module $m))1415(component $c16(component $c17(export "m" (core module $m))18)19(instance $c (instantiate $c))20(export "i" (instance $c))21)22(instance $c (instantiate $c))23(export "i" (instance $c))24(export "r" (instance $i))25(export "r2" (instance $i2))26)27"#;28let component = Component::new(&engine, component)?;29let mut store = Store::new(&engine, ());30let mut linker = Linker::new(&engine);31linker.instance("a")?;32linker33.instance("b")?34.module("m", &Module::new(&engine, "(module)")?)?;35let instance = linker.instantiate(&mut store, &component)?;3637assert!(38instance39.get_export(&mut store, None, "not an instance")40.is_none()41);42let i = instance.get_export_index(&mut store, None, "r").unwrap();43assert!(instance.get_export(&mut store, Some(&i), "x").is_none());44instance.get_export(&mut store, None, "i").unwrap();45let i2 = instance.get_export_index(&mut store, None, "r2").unwrap();46let m = instance47.get_export_index(&mut store, Some(&i2), "m")48.unwrap();49assert!(instance.get_func(&mut store, &m).is_none());50assert!(instance.get_module(&mut store, &m).is_some());5152let i = instance.get_export_index(&mut store, None, "i").unwrap();53let i = instance54.get_export_index(&mut store, Some(&i), "i")55.unwrap();56let m = instance57.get_export_index(&mut store, Some(&i), "m")58.unwrap();59instance.get_module(&mut store, &m).unwrap();6061Ok(())62}6364#[test]65fn export_old_get_new() -> Result<()> {66let engine = super::engine();67let component = r#"68(component69(core module $m)70(export "a:b/[email protected]" (core module $m))7172(instance $i (export "m" (core module $m)))73(export "a:b/[email protected]" (instance $i))74)75"#;7677let component = Component::new(&engine, component)?;78component.get_export(None, "a:b/[email protected]").unwrap();79let i = component.get_export_index(None, "a:b/[email protected]").unwrap();80component.get_export(Some(&i), "m").unwrap();8182let mut store = Store::new(&engine, ());83let linker = Linker::new(&engine);84let instance = linker.instantiate(&mut store, &component)?;8586instance.get_module(&mut store, "a:b/[email protected]").unwrap();87instance88.get_export(&mut store, None, "a:b/[email protected]")89.unwrap();9091let i = instance92.get_export_index(&mut store, None, "a:b/[email protected]")93.unwrap();94instance.get_export(&mut store, Some(&i), "m").unwrap();9596Ok(())97}9899#[test]100fn export_new_get_old() -> Result<()> {101let engine = super::engine();102let component = r#"103(component104(core module $m)105(export "a:b/[email protected]" (core module $m))106107(instance $i (export "m" (core module $m)))108(export "a:b/[email protected]" (instance $i))109)110"#;111112let component = Component::new(&engine, component)?;113component.get_export(None, "a:b/[email protected]").unwrap();114let i = component.get_export_index(None, "a:b/[email protected]").unwrap();115component.get_export(Some(&i), "m").unwrap();116117let mut store = Store::new(&engine, ());118let linker = Linker::new(&engine);119let instance = linker.instantiate(&mut store, &component)?;120121instance.get_module(&mut store, "a:b/[email protected]").unwrap();122instance123.get_export(&mut store, None, "a:b/[email protected]")124.unwrap();125126let i = instance127.get_export_index(&mut store, None, "a:b/[email protected]")128.unwrap();129instance.get_export(&mut store, Some(&i), "m").unwrap();130131Ok(())132}133134#[test]135#[cfg_attr(miri, ignore)]136fn export_missing_get_max() -> Result<()> {137let engine = super::engine();138let component = r#"139(component140(core module $m1)141(core module $m2 (import "" "" (func)))142(export "a:b/[email protected]" (core module $m1))143(export "a:b/[email protected]" (core module $m2))144)145"#;146147fn assert_m2(module: &Module) {148assert_eq!(module.imports().len(), 1);149}150fn assert_m1(module: &Module) {151assert_eq!(module.imports().len(), 0);152}153154let component = Component::new(&engine, component)?;155let mut store = Store::new(&engine, ());156let instance = Linker::new(&engine).instantiate(&mut store, &component)?;157158let tests = [159("a:b/[email protected]", assert_m2 as fn(&_)), // no exact, should pick max available160("a:b/[email protected]", assert_m1), // exact hit161("a:b/[email protected]", assert_m2), // no exact, should pick max available162("a:b/[email protected]", assert_m2), // exact hit163("a:b/[email protected]", assert_m2), // no exact, should pick max available164];165166for (name, test_fn) in tests {167println!("test {name}");168let m = component.get_export_index(None, name).unwrap();169let m = instance.get_module(&mut store, &m).unwrap();170test_fn(&m);171172let m = instance.get_module(&mut store, name).unwrap();173test_fn(&m);174175let m = instance.get_export_index(&mut store, None, name).unwrap();176let m = instance.get_module(&mut store, &m).unwrap();177test_fn(&m);178}179180Ok(())181}182183184