Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Support re-opening a `LinkerInstance` in `component::Linker` (#13908)

Alex Crichton557fb5c2eb2d
diff --git a/crates/wasmtime/src/runtime/component/linker.rs b/crates/wasmtime/src/runtime/component/linker.rs
index b468321b29..028ec394ef 100644
--- a/crates/wasmtime/src/runtime/component/linker.rs
+++ b/crates/wasmtime/src/runtime/component/linker.rs
@@ -13,7 +13,7 @@ use alloc::sync::Arc;
use core::marker;
#[cfg(feature = "component-model-async")]
use core::pin::Pin;
-use wasmtime_environ::component::NameMap;
+use wasmtime_environ::component::{NameMap, NameMapIntern};
use wasmtime_environ::{Atom, PrimaryMap, StringPool};
/// A type used to instantiate [`Component`]s.
@@ -908,13 +908,28 @@ impl<T: 'static> LinkerInstance<'_, T> {
/// Same as [`LinkerInstance::instance`] except with different lifetime
/// parameters.
pub fn into_instance(mut self, name: &str) -> Result<Self> {
- let name = self.insert(name, Definition::Instance(NameMap::default()))?;
- self.map = match self.map.raw_get_mut(&name) {
+ let atom = self.strings.intern(name)?;
+
+ // If this item is already an instance then don't stomp over it with a
+ // new empty instance (or fail due to shadowing being disallowed).
+ // Instead continue through to below to explicitly allow re-opening an
+ // instance multiple times over separate API calls.
+ //
+ // If this item isn't defined, or is defined as anything other than an
+ // instance, however, the insert a fresh new instance and see what
+ // happens as a result.
+ match self.map.raw_get_mut(&atom) {
+ Some(Definition::Instance(_)) => {}
+ _ => {
+ self.insert(name, Definition::Instance(NameMap::default()))?;
+ }
+ }
+ self.map = match self.map.raw_get_mut(&atom) {
Some(Definition::Instance(map)) => map,
_ => unreachable!(),
};
self.path.truncate(self.path_len);
- self.path.push(name);
+ self.path.push(atom);
self.path_len += 1;
Ok(self)
}
diff --git a/tests/all/component_model/linker.rs b/tests/all/component_model/linker.rs
index 42e8c7b44e..826baa8b9d 100644
--- a/tests/all/component_model/linker.rs
+++ b/tests/all/component_model/linker.rs
@@ -1,7 +1,7 @@
use wasmtime::Result;
use wasmtime::component::types::ComponentItem;
use wasmtime::component::{Component, Linker, ResourceType};
-use wasmtime::{Config, Engine, Store};
+use wasmtime::{Config, Engine, Module, Store};
#[test]
fn old_import_importing_new_item() -> Result<()> {
@@ -211,3 +211,20 @@ fn linker_fails_to_define_unknown_core_module_imports_as_traps() -> Result<()> {
Ok(())
}
+
+#[test]
+fn open_instance_twice() -> Result<()> {
+ let engine = Engine::default();
+ let mut linker = Linker::<()>::new(&engine);
+
+ let module = Module::new(&engine, "(module)")?;
+ linker.instance("foo")?.module("a", &module)?;
+ linker.instance("foo")?.module("b", &module)?;
+ assert!(linker.instance("foo")?.module("a", &module).is_err());
+ assert!(linker.instance("foo")?.module("b", &module).is_err());
+ linker.instance("foo")?.module("c", &module)?;
+ assert!(linker.root().module("foo", &module).is_err());
+ linker.instance("foo")?.module("d", &module)?;
+
+ Ok(())
+}