@@ -6,7 +6,7 @@ use std::mem;
6
6
use std:: process:: { Command , Stdio } ;
7
7
use wit_bindgen_core:: wit_parser:: abi:: AbiVariant ;
8
8
use wit_bindgen_core:: {
9
- uwrite, wit_parser:: * , Direction , Files , Generator , Source , TypeInfo , Types ,
9
+ uwrite, uwriteln , wit_parser:: * , Direction , Files , Generator , Source , TypeInfo , Types ,
10
10
} ;
11
11
use wit_bindgen_gen_rust_lib:: { to_rust_ident, FnSig , RustGenerator , TypeMode } ;
12
12
@@ -352,7 +352,7 @@ impl Generator for Wasmtime {
352
352
let prev = mem:: take ( & mut self . src ) ;
353
353
uwrite ! (
354
354
self . src,
355
- "pub fn {}(&self, mut store: impl wasmtime::AsContextMut<Data = T> , " ,
355
+ "pub fn {}(&self, mut store: impl wasmtime::AsContextMut, " ,
356
356
func. name. to_snake_case( ) ,
357
357
) ;
358
358
for ( i, param) in func. params . iter ( ) . enumerate ( ) {
@@ -379,25 +379,34 @@ impl Generator for Wasmtime {
379
379
) ) ;
380
380
}
381
381
382
- self . src . push_str ( "let (" ) ;
383
- for ( i, _) in func. results . iter_types ( ) . enumerate ( ) {
384
- uwrite ! ( self . src, "ret{}," , i) ;
382
+ self . src . push_str ( "let callee = unsafe {\n " ) ;
383
+ self . src . push_str ( "wasmtime::component::TypedFunc::<(" ) ;
384
+ for ( _, ty) in func. params . iter ( ) {
385
+ self . print_ty ( iface, ty, TypeMode :: AllBorrowed ( "'_" ) ) ;
386
+ self . push_str ( ", " ) ;
385
387
}
386
- uwrite ! (
388
+ self . src . push_str ( "), (" ) ;
389
+ for ty in func. results . iter_types ( ) {
390
+ self . print_ty ( iface, ty, TypeMode :: Owned ) ;
391
+ self . push_str ( ", " ) ;
392
+ }
393
+ uwriteln ! (
387
394
self . src,
388
- ") = self.{}.call(store.as_context_mut(), ( " ,
395
+ ")>::new_unchecked( self.{}) " ,
389
396
func. name. to_snake_case( )
390
397
) ;
398
+ self . src . push_str ( "};\n " ) ;
399
+ self . src . push_str ( "let (" ) ;
400
+ for ( i, _) in func. results . iter_types ( ) . enumerate ( ) {
401
+ uwrite ! ( self . src, "ret{}," , i) ;
402
+ }
403
+ uwrite ! ( self . src, ") = callee.call(store.as_context_mut(), (" ) ;
391
404
for ( i, _) in func. params . iter ( ) . enumerate ( ) {
392
405
uwrite ! ( self . src, "arg{}, " , i) ;
393
406
}
394
- uwrite ! ( self . src, "))?;\n " ) ;
407
+ uwriteln ! ( self . src, "))?;" ) ;
395
408
396
- uwrite ! (
397
- self . src,
398
- "self.{}.post_return(store.as_context_mut())?;\n " ,
399
- func. name. to_snake_case( )
400
- ) ;
409
+ uwriteln ! ( self . src, "callee.post_return(store.as_context_mut())?;" ) ;
401
410
402
411
self . src . push_str ( "Ok(" ) ;
403
412
if func. results . iter_types ( ) . len ( ) == 1 {
@@ -417,23 +426,7 @@ impl Generator for Wasmtime {
417
426
let pub_func = mem:: replace ( & mut self . src , prev) . into ( ) ;
418
427
let prev = mem:: take ( & mut self . src ) ;
419
428
420
- self . src . push_str ( "wasmtime::component::TypedFunc<(" ) ;
421
- // ComponentNamedList means using tuple for all:
422
- for ( _, ty) in func. params . iter ( ) {
423
- self . print_ty ( iface, ty, TypeMode :: AllBorrowed ( "'a" ) ) ;
424
- self . push_str ( ", " ) ;
425
- }
426
- self . src . push_str ( "), (" ) ;
427
- for ty in func. results . iter_types ( ) {
428
- self . print_ty ( iface, ty, TypeMode :: Owned ) ;
429
- self . push_str ( ", " ) ;
430
- }
431
- self . src . push_str ( ")>" ) ;
432
-
433
- let type_sig: String = mem:: replace ( & mut self . src , prev) . into ( ) ;
434
- let prev = mem:: take ( & mut self . src ) ;
435
-
436
- self . src . push_str ( "instance.get_typed_func::<(" ) ;
429
+ self . src . push_str ( "*instance.get_typed_func::<(" ) ;
437
430
for ( _, ty) in func. params . iter ( ) {
438
431
self . print_ty ( iface, ty, TypeMode :: AllBorrowed ( "'_" ) ) ;
439
432
self . push_str ( ", " ) ;
@@ -447,17 +440,18 @@ impl Generator for Wasmtime {
447
440
448
441
self . src . push_str ( "), _>(&mut store, \" " ) ;
449
442
self . src . push_str ( & func. name ) ;
450
- self . src . push_str ( "\" )?" ) ;
443
+ self . src . push_str ( "\" )?.func() " ) ;
451
444
let getter: String = mem:: replace ( & mut self . src , prev) . into ( ) ;
452
445
453
446
let exports = self
454
447
. guest_exports
455
448
. entry ( iface. name . to_string ( ) )
456
449
. or_insert_with ( Exports :: default) ;
457
450
exports. funcs . push ( pub_func) ;
458
- exports
459
- . fields
460
- . insert ( to_rust_ident ( & func. name ) , ( type_sig, getter) ) ;
451
+ exports. fields . insert (
452
+ to_rust_ident ( & func. name ) ,
453
+ ( "wasmtime::component::Func" . to_string ( ) , getter) ,
454
+ ) ;
461
455
}
462
456
463
457
fn finish_one ( & mut self , _iface : & Interface , files : & mut Files ) {
@@ -498,16 +492,15 @@ impl Generator for Wasmtime {
498
492
for ( module, exports) in sorted_iter ( & mem:: take ( & mut self . guest_exports ) ) {
499
493
let name = module. to_upper_camel_case ( ) ;
500
494
501
- uwrite ! ( self . src, "pub struct {}<'a, T> {{\n " , name) ;
502
- self . push_str ( "_phantom: std::marker::PhantomData<&'a T>," ) ;
495
+ uwrite ! ( self . src, "pub struct {} {{\n " , name) ;
503
496
for ( name, ( ty, _) ) in exports. fields . iter ( ) {
504
497
self . push_str ( name) ;
505
498
self . push_str ( ": " ) ;
506
499
self . push_str ( ty) ;
507
500
self . push_str ( ",\n " ) ;
508
501
}
509
502
self . push_str ( "}\n " ) ;
510
- uwrite ! ( self . src, "impl<'a, T> {}<'a, T> {{\n " , name) ;
503
+ uwrite ! ( self . src, "impl {} {{\n " , name) ;
511
504
512
505
self . push_str ( & format ! (
513
506
"
@@ -521,7 +514,7 @@ impl Generator for Wasmtime {
521
514
/// instantiate the `module` otherwise using `linker`, and
522
515
/// both an instance of this structure and the underlying
523
516
/// `wasmtime::Instance` will be returned.
524
- pub fn instantiate(
517
+ pub fn instantiate<T> (
525
518
mut store: impl wasmtime::AsContextMut<Data = T>,
526
519
component: &wasmtime::component::Component,
527
520
linker: &mut wasmtime::component::Linker<T>,
@@ -543,7 +536,7 @@ impl Generator for Wasmtime {
543
536
/// returned structure which can be used to interact with
544
537
/// the wasm module.
545
538
pub fn new(
546
- mut store: impl wasmtime::AsContextMut<Data = T> ,
539
+ mut store: impl wasmtime::AsContextMut,
547
540
instance: &wasmtime::component::Instance,
548
541
) -> anyhow::Result<Self> {{
549
542
" ,
@@ -559,7 +552,6 @@ impl Generator for Wasmtime {
559
552
self . push_str ( "Ok(" ) ;
560
553
self . push_str ( & name) ;
561
554
self . push_str ( "{\n " ) ;
562
- self . push_str ( "_phantom: std::marker::PhantomData," ) ;
563
555
for ( name, _) in exports. fields . iter ( ) {
564
556
self . push_str ( name) ;
565
557
self . push_str ( ",\n " ) ;
0 commit comments