Internal function to create an instance which doesn't have its `start` function run yet. This is not intended to be exposed from Wasmtime, it's intended to refactor out common code from `new_started` and `new_started_async`. Note that this step needs to be run on a fiber in async mode even though it doesn't do any blocking work because an async resource limiter may need to yield. # Unsafety Th
(
store: &mut StoreOpaque,
mut limiter: Option<&mut StoreResourceLimiter<'_>>,
module: &Module,
imports: Imports<'_>,
)
| 298 | /// provided. The `imports` provided must be suitable for the module |
| 299 | /// provided as well. |
| 300 | async unsafe fn new_raw( |
| 301 | store: &mut StoreOpaque, |
| 302 | mut limiter: Option<&mut StoreResourceLimiter<'_>>, |
| 303 | module: &Module, |
| 304 | imports: Imports<'_>, |
| 305 | ) -> Result<Instance> { |
| 306 | if !Engine::same(store.engine(), module.engine()) { |
| 307 | bail!("cross-`Engine` instantiation is not currently supported"); |
| 308 | } |
| 309 | store.bump_resource_counts(module)?; |
| 310 | |
| 311 | // Allocate the GC heap, if necessary. |
| 312 | if module.env_module().needs_gc_heap { |
| 313 | store.ensure_gc_store(limiter.as_deref_mut()).await?; |
| 314 | |
| 315 | // Eagerly register trace info for all types in this module. |
| 316 | if let Some(gc_store) = store.optional_gc_store_mut() { |
| 317 | for (_, ty) in module.signatures().as_module_map().iter() { |
| 318 | gc_store.ensure_trace_info(*ty); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Register the module just before instantiation to ensure we keep the module |
| 324 | // properly referenced while in use by the store. |
| 325 | let (modules, engine, breakpoints) = store.modules_and_engine_and_breakpoints_mut(); |
| 326 | let module_id = modules.register_module(module, engine, breakpoints)?; |
| 327 | |
| 328 | // The first thing we do is issue an instance allocation request |
| 329 | // to the instance allocator. This, on success, will give us an |
| 330 | // instance handle. |
| 331 | // |
| 332 | // SAFETY: this module, by construction, was already validated within |
| 333 | // the store. |
| 334 | let id = unsafe { |
| 335 | store |
| 336 | .allocate_instance( |
| 337 | limiter.as_deref_mut(), |
| 338 | AllocateInstanceKind::Module(module_id), |
| 339 | &ModuleRuntimeInfo::Module(module.clone()), |
| 340 | imports, |
| 341 | ) |
| 342 | .await? |
| 343 | }; |
| 344 | |
| 345 | // At this point the instance is created and stored within the store, |
| 346 | // but it's also not quite usable just yet. Initialization hasn't |
| 347 | // completed (e.g. active data/element segments) and the `start` |
| 348 | // function additionally has not yet been invoked. That's the |
| 349 | // responsibility of the caller to handle, however. |
| 350 | Ok(Instance::from_wasmtime(id, store)) |
| 351 | } |
| 352 | |
| 353 | pub(crate) fn from_wasmtime(id: InstanceId, store: &mut StoreOpaque) -> Instance { |
| 354 | Instance { |
nothing calls this directly
no test coverage detected