@@ -10,9 +10,11 @@ use std::mem::{self, MaybeUninit};
1010use std:: panic:: { self , AssertUnwindSafe } ;
1111use std:: ptr;
1212use std:: str;
13+ #[ cfg( feature = "gc" ) ]
14+ use wasmtime:: RootScope ;
1315use wasmtime:: {
14- AsContext , AsContextMut , Error , Extern , Func , Result , RootScope , StoreContext , StoreContextMut ,
15- Trap , Val , ValRaw ,
16+ AsContext , AsContextMut , Error , Extern , Func , Result , StoreContext , StoreContextMut , Trap , Val ,
17+ ValRaw ,
1618} ;
1719
1820#[ derive( Clone ) ]
@@ -351,38 +353,74 @@ pub unsafe extern "C" fn wasmtime_func_call(
351353 nresults : usize ,
352354 trap_ret : & mut * mut wasm_trap_t ,
353355) -> Option < Box < wasmtime_error_t > > {
354- let mut scope = RootScope :: new ( & mut store) ;
355- let mut params = mem:: take ( & mut scope. as_context_mut ( ) . data_mut ( ) . wasm_val_storage ) ;
356- let ( wt_params, wt_results) = translate_args (
357- & mut params,
358- crate :: slice_from_raw_parts ( args, nargs)
359- . iter ( )
360- . map ( |i| i. to_val ( & mut scope) ) ,
361- nresults,
362- ) ;
356+ #[ cfg( feature = "gc" ) ]
357+ {
358+ let mut scope = RootScope :: new ( & mut store) ;
359+ let mut params = mem:: take ( & mut scope. as_context_mut ( ) . data_mut ( ) . wasm_val_storage ) ;
360+ let ( wt_params, wt_results) = translate_args (
361+ & mut params,
362+ crate :: slice_from_raw_parts ( args, nargs)
363+ . iter ( )
364+ . map ( |i| i. to_val ( & mut scope) ) ,
365+ nresults,
366+ ) ;
363367
364- // We're calling arbitrary code here most of the time, and we in general
365- // want to try to insulate callers against bugs in wasmtime/wasi/etc if we
366- // can. As a result we catch panics here and transform them to traps to
367- // allow the caller to have any insulation possible against Rust panics.
368- let result = panic:: catch_unwind ( AssertUnwindSafe ( || {
369- func. call ( & mut scope, wt_params, wt_results)
370- } ) ) ;
371- match result {
372- Ok ( Ok ( ( ) ) ) => {
373- let results = crate :: slice_from_raw_parts_mut ( results, nresults) ;
374- for ( slot, val) in results. iter_mut ( ) . zip ( wt_results. iter ( ) ) {
375- crate :: initialize ( slot, wasmtime_val_t:: from_val ( & mut scope, * val) ) ;
368+ // We're calling arbitrary code here most of the time, and we in general
369+ // want to try to insulate callers against bugs in wasmtime/wasi/etc if we
370+ // can. As a result we catch panics here and transform them to traps to
371+ // allow the caller to have any insulation possible against Rust panics.
372+ let result = panic:: catch_unwind ( AssertUnwindSafe ( || {
373+ func. call ( & mut scope, wt_params, wt_results)
374+ } ) ) ;
375+ match result {
376+ Ok ( Ok ( ( ) ) ) => {
377+ let results = crate :: slice_from_raw_parts_mut ( results, nresults) ;
378+ for ( slot, val) in results. iter_mut ( ) . zip ( wt_results. iter ( ) ) {
379+ crate :: initialize ( slot, wasmtime_val_t:: from_val ( & mut scope, * val) ) ;
380+ }
381+ params. truncate ( 0 ) ;
382+ scope. as_context_mut ( ) . data_mut ( ) . wasm_val_storage = params;
383+ None
384+ }
385+ Ok ( Err ( trap) ) => store_err ( trap, trap_ret) ,
386+ Err ( panic) => {
387+ let err = error_from_panic ( panic) ;
388+ * trap_ret = Box :: into_raw ( Box :: new ( wasm_trap_t:: new ( err) ) ) ;
389+ None
376390 }
377- params. truncate ( 0 ) ;
378- scope. as_context_mut ( ) . data_mut ( ) . wasm_val_storage = params;
379- None
380391 }
381- Ok ( Err ( trap) ) => store_err ( trap, trap_ret) ,
382- Err ( panic) => {
383- let err = error_from_panic ( panic) ;
384- * trap_ret = Box :: into_raw ( Box :: new ( wasm_trap_t:: new ( err) ) ) ;
385- None
392+ }
393+
394+ #[ cfg( not( feature = "gc" ) ) ]
395+ {
396+ let mut params = mem:: take ( & mut store. data_mut ( ) . wasm_val_storage ) ;
397+ let ( wt_params, wt_results) = translate_args (
398+ & mut params,
399+ crate :: slice_from_raw_parts ( args, nargs)
400+ . iter ( )
401+ . map ( |i| i. to_val_unscoped ( & mut store) ) ,
402+ nresults,
403+ ) ;
404+
405+ let result = panic:: catch_unwind ( AssertUnwindSafe ( || {
406+ func. call ( & mut store, wt_params, wt_results)
407+ } ) ) ;
408+ match result {
409+ Ok ( Ok ( ( ) ) ) => {
410+ let results = crate :: slice_from_raw_parts_mut ( results, nresults) ;
411+ for ( slot, val) in results. iter_mut ( ) . zip ( wt_results. iter ( ) ) {
412+ crate :: initialize ( slot, wasmtime_val_t:: from_val_unscoped ( & mut store, * val) ) ;
413+ }
414+ params. truncate ( 0 ) ;
415+ store. data_mut ( ) . wasm_val_storage = params;
416+ None
417+ }
418+ Ok ( Err ( trap) ) => store_err ( trap, trap_ret) ,
419+ Err ( panic) => {
420+ let err = error_from_panic ( panic) ;
421+ * trap_ret = Box :: into_raw ( Box :: new ( wasm_trap_t:: new ( err) ) ) ;
422+ None
423+ }
386424 }
387425 }
388426}
0 commit comments