@@ -81,6 +81,12 @@ pub struct Opts {
81
81
/// types for borrowing and owning, if necessary.
82
82
#[ cfg_attr( feature = "clap" , arg( long) ) ]
83
83
pub ownership : Ownership ,
84
+
85
+ /// The optional path to the wit-bindgen runtime module to use.
86
+ ///
87
+ /// This defaults to `wit_bindgen::rt`.
88
+ #[ cfg_attr( feature = "clap" , arg( long) ) ]
89
+ pub runtime_path : Option < String > ,
84
90
}
85
91
86
92
impl Opts {
@@ -149,6 +155,13 @@ impl RustWasm {
149
155
uwriteln ! ( self . src, "}}" ) ;
150
156
}
151
157
}
158
+
159
+ fn runtime_path ( & self ) -> & str {
160
+ self . opts
161
+ . runtime_path
162
+ . as_deref ( )
163
+ . unwrap_or ( "wit_bindgen::rt" )
164
+ }
152
165
}
153
166
154
167
impl WorldGenerator for RustWasm {
@@ -414,12 +427,13 @@ impl InterfaceGenerator<'_> {
414
427
self . src,
415
428
"
416
429
#[allow(unused_imports)]
417
- use wit_bindgen::rt ::{{alloc, vec::Vec, string::String}};
430
+ use {rt} ::{{alloc, vec::Vec, string::String}};
418
431
419
432
#[repr(align({align}))]
420
433
struct _RetArea([u8; {size}]);
421
434
static mut _RET_AREA: _RetArea = _RetArea([0; {size}]);
422
435
" ,
436
+ rt = self . gen . runtime_path( ) ,
423
437
align = self . return_pointer_area_align,
424
438
size = self . return_pointer_area_size,
425
439
) ;
@@ -498,12 +512,13 @@ impl InterfaceGenerator<'_> {
498
512
self . src . push_str ( "#[allow(clippy::all)]\n " ) ;
499
513
let params = self . print_signature ( func, param_mode, & sig) ;
500
514
self . src . push_str ( "{\n " ) ;
501
- self . src . push_str (
515
+ self . src . push_str ( & format ! (
502
516
"
503
517
#[allow(unused_imports)]
504
- use wit_bindgen::rt ::{alloc, vec::Vec, string::String};
518
+ use {rt} ::{{ alloc, vec::Vec, string::String} };
505
519
" ,
506
- ) ;
520
+ rt = self . gen . runtime_path( )
521
+ ) ) ;
507
522
self . src . push_str ( "unsafe {\n " ) ;
508
523
509
524
let mut f = FunctionBindgen :: new ( self , params) ;
@@ -613,7 +628,7 @@ impl InterfaceGenerator<'_> {
613
628
self . src,
614
629
"
615
630
#[allow(unused_imports)]
616
- use wit_bindgen::rt ::{{alloc, vec::Vec, string::String}};
631
+ use {rt} ::{{alloc, vec::Vec, string::String}};
617
632
618
633
// Before executing any other code, use this function to run all static
619
634
// constructors, if they have not yet been run. This is a hack required
@@ -627,9 +642,10 @@ impl InterfaceGenerator<'_> {
627
642
// https://github.com/bytecodealliance/preview2-prototyping/issues/99
628
643
// for more details.
629
644
#[cfg(target_arch=\" wasm32\" )]
630
- wit_bindgen::rt ::run_ctors_once();
645
+ {rt} ::run_ctors_once();
631
646
632
- "
647
+ " ,
648
+ rt = self . gen . runtime_path( )
633
649
) ;
634
650
635
651
// Finish out the macro-generated export implementation.
@@ -774,12 +790,15 @@ impl<'a> RustGenerator<'a> for InterfaceGenerator<'a> {
774
790
self . gen . opts . raw_strings
775
791
}
776
792
777
- fn vec_name ( & self ) -> & ' static str {
778
- "wit_bindgen::rt ::vec::Vec"
793
+ fn push_vec_name ( & mut self ) {
794
+ self . push_str ( & format ! ( "{rt} ::vec::Vec", rt = self . gen . runtime_path ( ) ) ) ;
779
795
}
780
796
781
- fn string_name ( & self ) -> & ' static str {
782
- "wit_bindgen::rt::string::String"
797
+ fn push_string_name ( & mut self ) {
798
+ self . push_str ( & format ! (
799
+ "{rt}::string::String" ,
800
+ rt = self . gen . runtime_path( )
801
+ ) ) ;
783
802
}
784
803
785
804
fn push_str ( & mut self , s : & str ) {
@@ -1090,7 +1109,10 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1090
1109
1091
1110
Instruction :: I64FromU64 | Instruction :: I64FromS64 => {
1092
1111
let s = operands. pop ( ) . unwrap ( ) ;
1093
- results. push ( format ! ( "wit_bindgen::rt::as_i64({})" , s) ) ;
1112
+ results. push ( format ! (
1113
+ "{rt}::as_i64({s})" ,
1114
+ rt = self . gen . gen . runtime_path( )
1115
+ ) ) ;
1094
1116
}
1095
1117
Instruction :: I32FromChar
1096
1118
| Instruction :: I32FromU8
@@ -1100,16 +1122,25 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1100
1122
| Instruction :: I32FromU32
1101
1123
| Instruction :: I32FromS32 => {
1102
1124
let s = operands. pop ( ) . unwrap ( ) ;
1103
- results. push ( format ! ( "wit_bindgen::rt::as_i32({})" , s) ) ;
1125
+ results. push ( format ! (
1126
+ "{rt}::as_i32({s})" ,
1127
+ rt = self . gen . gen . runtime_path( )
1128
+ ) ) ;
1104
1129
}
1105
1130
1106
1131
Instruction :: F32FromFloat32 => {
1107
1132
let s = operands. pop ( ) . unwrap ( ) ;
1108
- results. push ( format ! ( "wit_bindgen::rt::as_f32({})" , s) ) ;
1133
+ results. push ( format ! (
1134
+ "{rt}::as_f32({s})" ,
1135
+ rt = self . gen . gen . runtime_path( )
1136
+ ) ) ;
1109
1137
}
1110
1138
Instruction :: F64FromFloat64 => {
1111
1139
let s = operands. pop ( ) . unwrap ( ) ;
1112
- results. push ( format ! ( "wit_bindgen::rt::as_f64({})" , s) ) ;
1140
+ results. push ( format ! (
1141
+ "{rt}::as_f64({s})" ,
1142
+ rt = self . gen . gen . runtime_path( )
1143
+ ) ) ;
1113
1144
}
1114
1145
Instruction :: Float32FromF32
1115
1146
| Instruction :: Float64FromF64
@@ -1604,7 +1635,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1604
1635
self . push_str ( "}\n " ) ;
1605
1636
results. push ( result) ;
1606
1637
self . push_str ( & format ! (
1607
- "wit_bindgen::rt::dealloc({base}, ({len} as usize) * {size}, {align});\n " ,
1638
+ "{rt}::dealloc({base}, ({len} as usize) * {size}, {align});\n " ,
1639
+ rt = self . gen . gen . runtime_path( ) ,
1608
1640
) ) ;
1609
1641
}
1610
1642
@@ -1742,15 +1774,18 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1742
1774
1743
1775
Instruction :: GuestDeallocate { size, align } => {
1744
1776
self . push_str ( & format ! (
1745
- "wit_bindgen::rt::dealloc({}, {}, {});\n " ,
1746
- operands[ 0 ] , size, align
1777
+ "{rt}::dealloc({op}, {size}, {align});\n " ,
1778
+ rt = self . gen . gen . runtime_path( ) ,
1779
+ op = operands[ 0 ]
1747
1780
) ) ;
1748
1781
}
1749
1782
1750
1783
Instruction :: GuestDeallocateString => {
1751
1784
self . push_str ( & format ! (
1752
- "wit_bindgen::rt::dealloc({}, ({}) as usize, 1);\n " ,
1753
- operands[ 0 ] , operands[ 1 ] ,
1785
+ "{rt}::dealloc({op0}, ({op1}) as usize, 1);\n " ,
1786
+ rt = self . gen . gen . runtime_path( ) ,
1787
+ op0 = operands[ 0 ] ,
1788
+ op1 = operands[ 1 ] ,
1754
1789
) ) ;
1755
1790
}
1756
1791
@@ -1802,7 +1837,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1802
1837
self . push_str ( "\n }\n " ) ;
1803
1838
}
1804
1839
self . push_str ( & format ! (
1805
- "wit_bindgen::rt::dealloc({base}, ({len} as usize) * {size}, {align});\n " ,
1840
+ "{rt}::dealloc({base}, ({len} as usize) * {size}, {align});\n " ,
1841
+ rt = self . gen . gen . runtime_path( ) ,
1806
1842
) ) ;
1807
1843
}
1808
1844
0 commit comments