@@ -98,7 +98,7 @@ pub fn trampoline_existing_module(
98
98
99
99
pub struct TrampolineCodegen {
100
100
module : Module ,
101
- guest_memory_id : MemoryId ,
101
+ guest_memory_id : Option < MemoryId > ,
102
102
provider_memory_id : OnceCell < MemoryId > ,
103
103
memcpy_to_guest : OnceCell < FunctionId > ,
104
104
memcpy_to_provider : OnceCell < FunctionId > ,
@@ -136,18 +136,18 @@ impl TrampolineCodegen {
136
136
} )
137
137
}
138
138
139
- fn guest_memory_id ( module : & Module ) -> walrus:: Result < MemoryId > {
139
+ fn guest_memory_id ( module : & Module ) -> walrus:: Result < Option < MemoryId > > {
140
140
let non_imported_memories = module
141
141
. memories
142
142
. iter ( )
143
143
. filter ( |& memory| memory. import . is_none ( ) )
144
144
. map ( |memory| memory. id ( ) )
145
145
. collect :: < Vec < _ > > ( ) ;
146
146
147
- if let Some ( ( memory_id , [ ] ) ) = non_imported_memories. split_first ( ) {
148
- Ok ( * memory_id)
149
- } else {
150
- anyhow :: bail! ( "expected exactly one non-imported guest memory" )
147
+ match non_imported_memories. split_first ( ) {
148
+ Some ( ( memory_id , [ ] ) ) => Ok ( Some ( * memory_id) ) ,
149
+ Some ( _ ) => anyhow :: bail! ( "multiple non-imported memories are not supported" ) ,
150
+ None => Ok ( None ) ,
151
151
}
152
152
}
153
153
@@ -170,7 +170,10 @@ impl TrampolineCodegen {
170
170
. local_get ( dst)
171
171
. local_get ( src)
172
172
. local_get ( size)
173
- . memory_copy ( provider_memory_id, self . guest_memory_id ) ;
173
+ . memory_copy (
174
+ provider_memory_id,
175
+ self . guest_memory_id . expect ( "no guest memory" ) ,
176
+ ) ;
174
177
175
178
memcpy_to_guest. finish ( vec ! [ dst, src, size] , & mut self . module . funcs )
176
179
} )
@@ -195,7 +198,10 @@ impl TrampolineCodegen {
195
198
. local_get ( dst)
196
199
. local_get ( src)
197
200
. local_get ( size)
198
- . memory_copy ( self . guest_memory_id , provider_memory_id) ;
201
+ . memory_copy (
202
+ self . guest_memory_id . expect ( "no guest memory" ) ,
203
+ provider_memory_id,
204
+ ) ;
199
205
200
206
memcpy_to_provider. finish ( vec ! [ dst, src, size] , & mut self . module . funcs )
201
207
} )
@@ -458,6 +464,11 @@ impl TrampolineCodegen {
458
464
}
459
465
460
466
pub fn apply ( mut self ) -> walrus:: Result < Module > {
467
+ // If the module does not have a memory, we should no-op
468
+ if self . guest_memory_id . is_none ( ) {
469
+ return Ok ( self . module ) ;
470
+ }
471
+
461
472
for ( original, new) in IMPORTS {
462
473
match * original {
463
474
"shopify_function_input_read_utf8_str" => {
@@ -542,7 +553,7 @@ mod test {
542
553
let err = result. unwrap_err ( ) ;
543
554
assert_eq ! (
544
555
err. to_string( ) ,
545
- "expected exactly one non-imported guest memory "
556
+ "multiple non-imported memories are not supported "
546
557
) ;
547
558
}
548
559
}
0 commit comments