@@ -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].
@@ -200,7 +201,7 @@ impl Instance {
200201 let mut store = store. as_context_mut ( ) ;
201202 let imports = Instance :: typecheck_externs ( store. 0 , module, imports) ?;
202203 // See `new` for notes on this unsafety
203- unsafe { Instance :: new_started_async ( & mut store, module, imports. as_ref ( ) ) . await }
204+ unsafe { Instance :: new_started ( & mut store, module, imports. as_ref ( ) ) . await }
204205 }
205206
206207 fn typecheck_externs (
@@ -242,62 +243,31 @@ impl Instance {
242243 /// Internal function to create an instance and run the start function.
243244 ///
244245 /// This function's unsafety is the same as `Instance::new_raw`.
245- pub ( crate ) unsafe fn new_started < T > (
246- store : & mut StoreContextMut < ' _ , T > ,
247- module : & Module ,
248- imports : Imports < ' _ > ,
249- ) -> Result < Instance > {
250- assert ! (
251- !store. 0 . async_support( ) ,
252- "must use async instantiation when async support is enabled" ,
253- ) ;
254-
255- // SAFETY: the safety contract of `new_started_impl` is the same as this
256- // function.
257- unsafe { Self :: new_started_impl ( store, module, imports) }
258- }
259-
260- /// Internal function to create an instance and run the start function.
261- ///
262- /// ONLY CALL THIS IF YOU HAVE ALREADY CHECKED FOR ASYNCNESS AND HANDLED
263- /// THE FIBER NONSENSE
264- pub ( crate ) unsafe fn new_started_impl < T > (
246+ pub ( crate ) async unsafe fn new_started < T > (
265247 store : & mut StoreContextMut < ' _ , T > ,
266248 module : & Module ,
267249 imports : Imports < ' _ > ,
268250 ) -> Result < Instance > {
269251 // SAFETY: the safety contract of `new_raw` is the same as this
270252 // function.
271- let ( instance, start) = unsafe { Instance :: new_raw ( store. 0 , module, imports) ? } ;
253+ let ( instance, start) = unsafe { Instance :: new_raw ( store. 0 , module, imports) . await ? } ;
272254 if let Some ( start) = start {
273- instance. start_raw ( store, start) ?;
255+ if store. 0 . async_support ( ) {
256+ #[ cfg( feature = "async" ) ]
257+ {
258+ store
259+ . on_fiber ( |store| instance. start_raw ( store, start) )
260+ . await ??;
261+ }
262+ #[ cfg( not( feature = "async" ) ) ]
263+ unreachable ! ( ) ;
264+ } else {
265+ instance. start_raw ( store, start) ?;
266+ }
274267 }
275268 Ok ( instance)
276269 }
277270
278- /// Internal function to create an instance and run the start function.
279- ///
280- /// This function's unsafety is the same as `Instance::new_raw`.
281- #[ cfg( feature = "async" ) ]
282- async unsafe fn new_started_async < T > (
283- store : & mut StoreContextMut < ' _ , T > ,
284- module : & Module ,
285- imports : Imports < ' _ > ,
286- ) -> Result < Instance > {
287- assert ! (
288- store. 0 . async_support( ) ,
289- "must use sync instantiation when async support is disabled" ,
290- ) ;
291-
292- store
293- . on_fiber ( |store| {
294- // SAFETY: the unsafe contract of `new_started_impl` is the same
295- // as this function.
296- unsafe { Self :: new_started_impl ( store, module, imports) }
297- } )
298- . await ?
299- }
300-
301271 /// Internal function to create an instance which doesn't have its `start`
302272 /// function run yet.
303273 ///
@@ -313,7 +283,7 @@ impl Instance {
313283 /// This method is unsafe because it does not type-check the `imports`
314284 /// provided. The `imports` provided must be suitable for the module
315285 /// provided as well.
316- unsafe fn new_raw (
286+ async unsafe fn new_raw (
317287 store : & mut StoreOpaque ,
318288 module : & Module ,
319289 imports : Imports < ' _ > ,
@@ -341,11 +311,13 @@ impl Instance {
341311 // SAFETY: this module, by construction, was already validated within
342312 // the store.
343313 let id = unsafe {
344- store. allocate_instance (
345- AllocateInstanceKind :: Module ( module_id) ,
346- & ModuleRuntimeInfo :: Module ( module. clone ( ) ) ,
347- imports,
348- ) ?
314+ store
315+ . allocate_instance (
316+ AllocateInstanceKind :: Module ( module_id) ,
317+ & ModuleRuntimeInfo :: Module ( module. clone ( ) ) ,
318+ imports,
319+ )
320+ . await ?
349321 } ;
350322
351323 // Additionally, before we start doing fallible instantiation, we
@@ -905,7 +877,10 @@ impl<T: 'static> InstancePre<T> {
905877 // This unsafety should be handled by the type-checking performed by the
906878 // constructor of `InstancePre` to assert that all the imports we're passing
907879 // in match the module we're instantiating.
908- unsafe { Instance :: new_started ( & mut store, & self . module , imports. as_ref ( ) ) }
880+ assert ! ( !store. 0 . async_support( ) ) ;
881+ vm:: assert_ready ( unsafe {
882+ Instance :: new_started ( & mut store, & self . module , imports. as_ref ( ) )
883+ } )
909884 }
910885
911886 /// Creates a new instance, running the start function asynchronously
@@ -935,7 +910,7 @@ impl<T: 'static> InstancePre<T> {
935910 // This unsafety should be handled by the type-checking performed by the
936911 // constructor of `InstancePre` to assert that all the imports we're passing
937912 // in match the module we're instantiating.
938- unsafe { Instance :: new_started_async ( & mut store, & self . module , imports. as_ref ( ) ) . await }
913+ unsafe { Instance :: new_started ( & mut store, & self . module , imports. as_ref ( ) ) . await }
939914 }
940915}
941916
0 commit comments