@@ -60,6 +60,7 @@ struct libwasmtime_vm
6060 wasm_engine_t * engine ;
6161 wasmtime_store_t * store ;
6262 wasmtime_context_t * context ;
63+ wasi_config_t * config ;
6364};
6465
6566static struct libwasmtime_vm *
@@ -94,15 +95,17 @@ libwasmtime_setup_vm (void *cookie, char *const argv[], struct libwasmtime_vm *v
9495 = libwasmtime_load_symbol (cookie , "wasi_config_inherit_stdout" );
9596 void (* wasi_config_inherit_stderr ) (wasi_config_t * config )
9697 = libwasmtime_load_symbol (cookie , "wasi_config_inherit_stderr" );
97- wasmtime_error_t * (* wasmtime_context_set_wasi ) (wasmtime_context_t * context , wasi_config_t * wasi )
98- = libwasmtime_load_symbol (cookie , "wasmtime_context_set_wasi" );
9998 bool (* wasi_config_preopen_dir ) (
10099 wasi_config_t * config ,
101100 const char * path ,
102101 const char * guest_path ,
103102 wasi_dir_perms dir_perms ,
104103 wasi_file_perms file_perms )
105104 = libwasmtime_load_symbol (cookie , "wasi_config_preopen_dir" );
105+ # if WASMTIME_VERSION_MAJOR >= 39
106+ wasmtime_error_t * (* wasmtime_context_set_wasi ) (wasmtime_context_t * context , wasi_config_t * wasi )
107+ = libwasmtime_load_symbol (cookie , "wasmtime_context_set_wasi" );
108+ # endif
106109
107110 // Set up WebAssembly engine
108111 vm -> engine = wasm_engine_new ();
@@ -116,32 +119,37 @@ libwasmtime_setup_vm (void *cookie, char *const argv[], struct libwasmtime_vm *v
116119 vm -> context = wasmtime_store_context (vm -> store );
117120
118121 // Init WASI program
119- wasi_config_t * wasi_config = wasi_config_new ("crun_wasi_program" );
120- if (wasi_config == NULL )
122+ vm -> config = wasi_config_new ("crun_wasi_program" );
123+ if (vm -> config == NULL )
121124 error (EXIT_FAILURE , 0 , "could not create WASI configuration" );
122125
123126 // Calculate argc for `wasi_config_set_argv`
124127 for (arg = argv ; * arg != NULL ; ++ arg )
125128 args_size ++ ;
126129
127- wasi_config_set_argv (wasi_config , args_size , (const char * * ) argv );
128- wasi_config_inherit_env (wasi_config );
129- wasi_config_inherit_stdin (wasi_config );
130- wasi_config_inherit_stdout (wasi_config );
131- wasi_config_inherit_stderr (wasi_config );
130+ wasi_config_set_argv (vm -> config , args_size , (const char * * ) argv );
131+ wasi_config_inherit_env (vm -> config );
132+ wasi_config_inherit_stdin (vm -> config );
133+ wasi_config_inherit_stdout (vm -> config );
134+ wasi_config_inherit_stderr (vm -> config );
132135 wasi_config_preopen_dir (
133- wasi_config ,
136+ vm -> config ,
134137 "." ,
135138 "." ,
136139 WASMTIME_WASI_DIR_PERMS_READ | WASMTIME_WASI_DIR_PERMS_WRITE ,
137140 WASMTIME_WASI_FILE_PERMS_READ | WASMTIME_WASI_FILE_PERMS_WRITE );
138- wasmtime_error_t * err = wasmtime_context_set_wasi (vm -> context , wasi_config );
141+
142+ // If we are compiling wasmtime against 39 or higher
143+ // we can make use of the unified wasi API.
144+ # if WASMTIME_VERSION_MAJOR >= 39
145+ wasmtime_error_t * err = wasmtime_context_set_wasi (vm -> context , vm -> config );
139146 if (err != NULL )
140147 {
141148 wasmtime_error_message (err , & error_message );
142149 wasmtime_error_delete (err );
143150 error (EXIT_FAILURE , 0 , "failed to instantiate WASI: %.*s" , (int ) error_message .size , error_message .data );
144151 }
152+ # endif
145153
146154 return vm ;
147155}
@@ -241,6 +249,7 @@ libwasmtime_load_symbol (void *cookie, char *const symbol)
241249static void
242250libwasmtime_run_module (void * cookie , struct libwasmtime_vm * vm , wasm_byte_vec_t * wasm )
243251{
252+ wasmtime_error_t * err ;
244253 wasm_byte_vec_t error_message ;
245254
246255 // Load needed functions
@@ -281,8 +290,20 @@ libwasmtime_run_module (void *cookie, struct libwasmtime_vm *vm, wasm_byte_vec_t
281290 void (* wasmtime_module_delete ) (wasmtime_module_t * m )
282291 = libwasmtime_load_symbol (cookie , "wasmtime_module_delete" );
283292
293+ # if WASMTIME_VERSION_MAJOR < 39
294+ wasmtime_error_t * (* wasmtime_context_set_wasi ) (wasmtime_context_t * context , wasi_config_t * wasi )
295+ = libwasmtime_load_symbol (cookie , "wasmtime_context_set_wasi" );
296+ err = wasmtime_context_set_wasi (vm -> context , vm -> config );
297+ if (err != NULL )
298+ {
299+ wasmtime_error_message (err , & error_message );
300+ wasmtime_error_delete (err );
301+ error (EXIT_FAILURE , 0 , "failed to instantiate WASI: %.*s" , (int ) error_message .size , error_message .data );
302+ }
303+ # endif
304+
284305 wasmtime_linker_t * linker = wasmtime_linker_new (vm -> engine );
285- wasmtime_error_t * err = wasmtime_linker_define_wasi (linker );
306+ err = wasmtime_linker_define_wasi (linker );
286307 if (err != NULL )
287308 {
288309 wasmtime_error_message (err , & error_message );
@@ -389,6 +410,12 @@ libwasmtime_run_component (void *cookie, struct libwasmtime_vm *vm, wasm_byte_ve
389410 void (* wasmtime_component_linker_delete ) (wasmtime_component_linker_t * linker )
390411 = libwasmtime_load_symbol (cookie , "wasmtime_component_linker_delete" );
391412
413+ # if WASMTIME_VERSION_MAJOR < 39
414+ void (* wasmtime_context_set_wasip2 ) (wasmtime_context_t * context , wasmtime_wasip2_config_t * config )
415+ = libwasmtime_load_symbol (cookie , "wasmtime_context_set_wasip2" );
416+ wasmtime_context_set_wasip2 (vm -> context , (wasmtime_wasip2_config_t * ) vm -> config );
417+ # endif
418+
392419 // Compile wasm component
393420 wasmtime_component_t * component = NULL ;
394421 wasmtime_error_t * err = wasmtime_component_new (vm -> engine , (uint8_t * ) wasm -> data , wasm -> size , & component );
0 commit comments