@@ -7,7 +7,7 @@ use wasmtime_wasi::I32Exit;
77
88use crate :: function_run_result:: FunctionRunResult ;
99use crate :: io:: { IOHandler , OutputAndLogs } ;
10- use crate :: validated_module:: ValidatedModule ;
10+ pub use crate :: validated_module:: ValidatedModule ;
1111use crate :: { BytesContainer , BytesContainerType } ;
1212
1313#[ derive( Clone ) ]
@@ -31,8 +31,9 @@ pub struct FunctionRunParams<'a> {
3131 pub export : & ' a str ,
3232 pub profile_opts : Option < & ' a ProfileOpts > ,
3333 pub scale_factor : f64 ,
34- pub module : Module ,
35- pub engine : Engine ,
34+ /// The validated Function module. Reuse it across runs to compile the
35+ /// Function and its provider once.
36+ pub module : & ' a ValidatedModule ,
3637}
3738
3839const STARTING_FUEL : u64 = u64:: MAX ;
@@ -95,15 +96,15 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
9596 export,
9697 profile_opts,
9798 scale_factor,
98- engine,
9999 module,
100100 } = params;
101101
102- let mut io_handler = IOHandler :: new ( ValidatedModule :: new ( module) ?, input. clone ( ) ) ;
102+ let engine = module. inner ( ) . engine ( ) ;
103+ let mut io_handler = IOHandler :: new ( module, input. clone ( ) ) ;
103104
104105 let mut error_logs: String = String :: new ( ) ;
105106
106- let mut linker = Linker :: new ( & engine) ;
107+ let mut linker = Linker :: new ( engine) ;
107108 let wasi = io_handler. wasi ( ) ;
108109 if wasi. is_some ( ) {
109110 wasmtime_wasi:: preview1:: add_to_linker_sync ( & mut linker, |ctx : & mut FunctionContext | {
@@ -113,10 +114,10 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
113114 }
114115
115116 let function_context = FunctionContext :: new ( wasi) ;
116- let mut store = Store :: new ( & engine, function_context) ;
117+ let mut store = Store :: new ( engine, function_context) ;
117118 store. limiter ( |s| & mut s. limiter ) ;
118119
119- io_handler. initialize ( & engine , & mut linker, & mut store) ?;
120+ io_handler. initialize ( & mut linker, & mut store) ?;
120121
121122 store. set_fuel ( STARTING_FUEL ) ?;
122123 store. set_epoch_deadline ( 1 ) ;
@@ -235,8 +236,7 @@ mod tests {
235236 function_path : Path :: new ( "tests/fixtures/build/js_function.wasm" ) . to_path_buf ( ) ,
236237 input,
237238 export : DEFAULT_EXPORT ,
238- module,
239- engine,
239+ module : & ValidatedModule :: new ( module) ?,
240240 scale_factor : 1.0 ,
241241 profile_opts : None ,
242242 } ) ?;
@@ -260,8 +260,7 @@ mod tests {
260260 function_path : Path :: new ( "tests/fixtures/build/js_function_v2.wasm" ) . to_path_buf ( ) ,
261261 input,
262262 export : DEFAULT_EXPORT ,
263- module,
264- engine,
263+ module : & ValidatedModule :: new ( module) ?,
265264 scale_factor : 1.0 ,
266265 profile_opts : None ,
267266 } ) ?;
@@ -285,8 +284,7 @@ mod tests {
285284 function_path : Path :: new ( "tests/fixtures/build/js_function_v3.wasm" ) . to_path_buf ( ) ,
286285 input,
287286 export : DEFAULT_EXPORT ,
288- module,
289- engine,
287+ module : & ValidatedModule :: new ( module) ?,
290288 scale_factor : 1.0 ,
291289 profile_opts : None ,
292290 } ) ?;
@@ -295,6 +293,37 @@ mod tests {
295293 Ok ( ( ) )
296294 }
297295
296+ #[ test]
297+ fn test_validated_module_can_be_reused_across_runs ( ) -> Result < ( ) > {
298+ let path = Path :: new ( "tests/fixtures/build/js_function_javy_plugin_v3.wasm" ) ;
299+ let engine = new_engine ( ) ?;
300+ let module = ValidatedModule :: new ( Module :: from_file ( & engine, path) ?) ?;
301+
302+ for _ in 0 ..3 {
303+ let input = BytesContainer :: new (
304+ BytesContainerType :: Input ,
305+ Codec :: Messagepack ,
306+ serde_json:: to_vec ( & json ! ( { "hello" : "world" } ) ) ?,
307+ ) ?;
308+ let function_run_result = run ( FunctionRunParams {
309+ function_path : path. to_path_buf ( ) ,
310+ input,
311+ export : DEFAULT_EXPORT ,
312+ module : & module,
313+ scale_factor : 1.0 ,
314+ profile_opts : None ,
315+ } ) ?;
316+
317+ assert ! ( function_run_result. success) ;
318+ assert_eq ! (
319+ function_run_result. output. json_value,
320+ Some ( json!( { "hello" : "world output" } ) )
321+ ) ;
322+ }
323+
324+ Ok ( ( ) )
325+ }
326+
298327 #[ test]
299328 fn test_js_functions_javy_v1 ( ) -> Result < ( ) > {
300329 let engine = new_engine ( ) ?;
@@ -311,8 +340,7 @@ mod tests {
311340 . to_path_buf ( ) ,
312341 input,
313342 export : DEFAULT_EXPORT ,
314- module,
315- engine,
343+ module : & ValidatedModule :: new ( module) ?,
316344 scale_factor : 1.0 ,
317345 profile_opts : None ,
318346 } ) ?;
@@ -329,8 +357,7 @@ mod tests {
329357 function_path : Path :: new ( "tests/fixtures/build/exit_code.wasm" ) . to_path_buf ( ) ,
330358 input : json_input ( & serde_json:: to_vec ( & json ! ( { "code" : 0 } ) ) ?) ?,
331359 export : DEFAULT_EXPORT ,
332- module,
333- engine,
360+ module : & ValidatedModule :: new ( module) ?,
334361 scale_factor : 1.0 ,
335362 profile_opts : None ,
336363 } ) ?;
@@ -347,8 +374,7 @@ mod tests {
347374 function_path : Path :: new ( "tests/fixtures/build/exit_code.wasm" ) . to_path_buf ( ) ,
348375 input : json_input ( & serde_json:: to_vec ( & json ! ( { "code" : 1 } ) ) ?) ?,
349376 export : DEFAULT_EXPORT ,
350- module,
351- engine,
377+ module : & ValidatedModule :: new ( module) ?,
352378 scale_factor : 1.0 ,
353379 profile_opts : None ,
354380 } ) ?;
@@ -368,8 +394,7 @@ mod tests {
368394 function_path : Path :: new ( "tests/fixtures/build/linear_memory.wasm" ) . to_path_buf ( ) ,
369395 input : json_input ( & serde_json:: to_vec ( & json ! ( { } ) ) ?) ?,
370396 export : DEFAULT_EXPORT ,
371- module,
372- engine,
397+ module : & ValidatedModule :: new ( module) ?,
373398 scale_factor : 1.0 ,
374399 profile_opts : None ,
375400 } ) ?;
@@ -390,8 +415,7 @@ mod tests {
390415 function_path : Path :: new ( "tests/fixtures/build/log_truncation_function.wasm" )
391416 . to_path_buf ( ) ,
392417 export : DEFAULT_EXPORT ,
393- module,
394- engine,
418+ module : & ValidatedModule :: new ( module) ?,
395419 scale_factor : 1.0 ,
396420 profile_opts : None ,
397421 } ) ?;
@@ -416,8 +440,7 @@ mod tests {
416440 function_path : file_path. to_path_buf ( ) ,
417441 input : json_input ( & serde_json:: to_vec ( & json ! ( { "code" : 0 } ) ) ?) ?,
418442 export : DEFAULT_EXPORT ,
419- module,
420- engine,
443+ module : & ValidatedModule :: new ( module) ?,
421444 scale_factor : 1.0 ,
422445 profile_opts : None ,
423446 } ) ?;
@@ -445,8 +468,7 @@ mod tests {
445468 function_path : trampolined_path. to_path_buf ( ) ,
446469 input : input_bytes. unwrap ( ) ,
447470 export : DEFAULT_EXPORT ,
448- module,
449- engine,
471+ module : & ValidatedModule :: new ( module) ?,
450472 scale_factor : 1.0 ,
451473 profile_opts : None ,
452474 } ) ;
0 commit comments