@@ -117,7 +117,8 @@ impl Instance {
117117 // Note that the unsafety here should be satisfied by the call to
118118 // `typecheck_externs` above which satisfies the condition that all
119119 // the imports are valid for this module.
120- unsafe { Instance :: new_started ( & mut store, module, imports. as_ref ( ) ) }
120+ assert ! ( !store. 0 . async_support( ) ) ;
121+ vm:: assert_ready ( unsafe { Instance :: new_started ( & mut store, module, imports. as_ref ( ) ) } )
121122 }
122123
123124 /// Same as [`Instance::new`], except for usage in [asynchronous stores].
@@ -197,7 +198,7 @@ impl Instance {
197198 let mut store = store. as_context_mut ( ) ;
198199 let imports = Instance :: typecheck_externs ( store. 0 , module, imports) ?;
199200 // See `new` for notes on this unsafety
200- unsafe { Instance :: new_started_async ( & mut store, module, imports. as_ref ( ) ) . await }
201+ unsafe { Instance :: new_started ( & mut store, module, imports. as_ref ( ) ) . await }
201202 }
202203
203204 fn typecheck_externs (
@@ -239,62 +240,31 @@ impl Instance {
239240 /// Internal function to create an instance and run the start function.
240241 ///
241242 /// This function's unsafety is the same as `Instance::new_raw`.
242- pub ( crate ) unsafe fn new_started < T > (
243- store : & mut StoreContextMut < ' _ , T > ,
244- module : & Module ,
245- imports : Imports < ' _ > ,
246- ) -> Result < Instance > {
247- assert ! (
248- !store. 0 . async_support( ) ,
249- "must use async instantiation when async support is enabled" ,
250- ) ;
251-
252- // SAFETY: the safety contract of `new_started_impl` is the same as this
253- // function.
254- unsafe { Self :: new_started_impl ( store, module, imports) }
255- }
256-
257- /// Internal function to create an instance and run the start function.
258- ///
259- /// ONLY CALL THIS IF YOU HAVE ALREADY CHECKED FOR ASYNCNESS AND HANDLED
260- /// THE FIBER NONSENSE
261- pub ( crate ) unsafe fn new_started_impl < T > (
243+ pub ( crate ) async unsafe fn new_started < T > (
262244 store : & mut StoreContextMut < ' _ , T > ,
263245 module : & Module ,
264246 imports : Imports < ' _ > ,
265247 ) -> Result < Instance > {
266248 // SAFETY: the safety contract of `new_raw` is the same as this
267249 // function.
268- let ( instance, start) = unsafe { Instance :: new_raw ( store. 0 , module, imports) ? } ;
250+ let ( instance, start) = unsafe { Instance :: new_raw ( store. 0 , module, imports) . await ? } ;
269251 if let Some ( start) = start {
270- instance. start_raw ( store, start) ?;
252+ if store. 0 . async_support ( ) {
253+ #[ cfg( feature = "async" ) ]
254+ {
255+ store
256+ . on_fiber ( |store| instance. start_raw ( store, start) )
257+ . await ??;
258+ }
259+ #[ cfg( not( feature = "async" ) ) ]
260+ unreachable ! ( ) ;
261+ } else {
262+ instance. start_raw ( store, start) ?;
263+ }
271264 }
272265 Ok ( instance)
273266 }
274267
275- /// Internal function to create an instance and run the start function.
276- ///
277- /// This function's unsafety is the same as `Instance::new_raw`.
278- #[ cfg( feature = "async" ) ]
279- async unsafe fn new_started_async < T > (
280- store : & mut StoreContextMut < ' _ , T > ,
281- module : & Module ,
282- imports : Imports < ' _ > ,
283- ) -> Result < Instance > {
284- assert ! (
285- store. 0 . async_support( ) ,
286- "must use sync instantiation when async support is disabled" ,
287- ) ;
288-
289- store
290- . on_fiber ( |store| {
291- // SAFETY: the unsafe contract of `new_started_impl` is the same
292- // as this function.
293- unsafe { Self :: new_started_impl ( store, module, imports) }
294- } )
295- . await ?
296- }
297-
298268 /// Internal function to create an instance which doesn't have its `start`
299269 /// function run yet.
300270 ///
@@ -310,7 +280,7 @@ impl Instance {
310280 /// This method is unsafe because it does not type-check the `imports`
311281 /// provided. The `imports` provided must be suitable for the module
312282 /// provided as well.
313- unsafe fn new_raw (
283+ async unsafe fn new_raw (
314284 store : & mut StoreOpaque ,
315285 module : & Module ,
316286 imports : Imports < ' _ > ,
@@ -338,11 +308,13 @@ impl Instance {
338308 // SAFETY: this module, by construction, was already validated within
339309 // the store.
340310 let id = unsafe {
341- store. allocate_instance (
342- AllocateInstanceKind :: Module ( module_id) ,
343- & ModuleRuntimeInfo :: Module ( module. clone ( ) ) ,
344- imports,
345- ) ?
311+ store
312+ . allocate_instance (
313+ AllocateInstanceKind :: Module ( module_id) ,
314+ & ModuleRuntimeInfo :: Module ( module. clone ( ) ) ,
315+ imports,
316+ )
317+ . await ?
346318 } ;
347319
348320 // Additionally, before we start doing fallible instantiation, we
@@ -885,7 +857,10 @@ impl<T: 'static> InstancePre<T> {
885857 // This unsafety should be handled by the type-checking performed by the
886858 // constructor of `InstancePre` to assert that all the imports we're passing
887859 // in match the module we're instantiating.
888- unsafe { Instance :: new_started ( & mut store, & self . module , imports. as_ref ( ) ) }
860+ assert ! ( !store. 0 . async_support( ) ) ;
861+ vm:: assert_ready ( unsafe {
862+ Instance :: new_started ( & mut store, & self . module , imports. as_ref ( ) )
863+ } )
889864 }
890865
891866 /// Creates a new instance, running the start function asynchronously
@@ -919,7 +894,7 @@ impl<T: 'static> InstancePre<T> {
919894 // This unsafety should be handled by the type-checking performed by the
920895 // constructor of `InstancePre` to assert that all the imports we're passing
921896 // in match the module we're instantiating.
922- unsafe { Instance :: new_started_async ( & mut store, & self . module , imports. as_ref ( ) ) . await }
897+ unsafe { Instance :: new_started ( & mut store, & self . module , imports. as_ref ( ) ) . await }
923898 }
924899}
925900
0 commit comments