wasmer-cli 7.2.0-alpha.2

Wasmer CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#![allow(missing_docs, unused)]

mod capabilities;
mod package_source;
mod runtime;
mod target;
mod wasi;

use std::{
    borrow::Cow,
    collections::{BTreeMap, hash_map::DefaultHasher},
    fmt::{Binary, Display},
    fs::File,
    hash::{BuildHasherDefault, Hash, Hasher},
    io::{ErrorKind, LineWriter, Read, Write},
    net::SocketAddr,
    path::{Path, PathBuf},
    str::FromStr,
    sync::{Arc, Mutex},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use anyhow::{Context, Error, anyhow, bail};
use clap::{Parser, ValueEnum};
use colored::Colorize;
use futures::future::BoxFuture;
use indicatif::{MultiProgress, ProgressBar};
use once_cell::sync::Lazy;
use tempfile::NamedTempFile;
use url::Url;
#[cfg(feature = "sys")]
use wasmer::sys::NativeEngineExt;
use wasmer::{
    AsStoreMut, DeserializeError, Engine, Function, Imports, Instance, Module, RuntimeError, Store,
    Type, TypedFunction, Value, wat2wasm,
};

use wasmer_types::{Features, target::Target};

#[cfg(feature = "compiler")]
use wasmer_compiler::ArtifactBuild;
use wasmer_config::package::PackageSource;
use wasmer_package::utils::from_disk;
use wasmer_types::ModuleHash;

#[cfg(feature = "journal")]
use wasmer_wasix::journal::{LogFileJournal, SnapshotTrigger};
use wasmer_wasix::{
    Runtime, SpawnError, WasiError,
    bin_factory::{BinaryPackage, BinaryPackageCommand},
    journal::CompactingLogFileJournal,
    runners::{
        MappedCommand, MappedDirectory, Runner,
        dcgi::{DcgiInstanceFactory, DcgiRunner},
        dproxy::DProxyRunner,
        wasi::{RuntimeOrEngine, WasiRunner},
        wcgi::{self, AbortHandle, NoOpWcgiCallbacks, WcgiRunner},
    },
    runtime::{
        OverriddenRuntime,
        module_cache::{CacheError, HashedModuleData},
        package_loader::PackageLoader,
        resolver::QueryError,
        task_manager::VirtualTaskManagerExt,
    },
};
use webc::Container;
use webc::metadata::Manifest;

use crate::{
    backend::RuntimeOptions,
    commands::run::{target::TargetOnDisk, wasi::Wasi},
    config::WasmerEnv,
    error::PrettyError,
    logging::Output,
};

use self::{
    package_source::CliPackageSource, runtime::MonitoringRuntime, target::ExecutableTarget,
};

const TICK: Duration = Duration::from_millis(250);

/// The unstable `wasmer run` subcommand.
#[derive(Debug, Parser)]
pub struct Run {
    #[clap(flatten)]
    env: WasmerEnv,
    #[clap(flatten)]
    rt: RuntimeOptions,
    #[clap(flatten)]
    wasi: crate::commands::run::Wasi,
    #[clap(flatten)]
    wcgi: WcgiOptions,
    /// Set the default stack size (default is 1048576)
    #[clap(long = "stack-size")]
    stack_size: Option<usize>,
    /// The entrypoint module for webc packages.
    #[clap(short, long, aliases = &["command", "command-name"])]
    entrypoint: Option<String>,
    /// The function to invoke.
    #[clap(short, long)]
    invoke: Option<String>,
    /// Generate a coredump at this path if a WebAssembly trap occurs
    #[clap(name = "COREDUMP_PATH", long)]
    coredump_on_trap: Option<PathBuf>,
    /// Enable experimental N-API imports for modules that require them
    #[clap(long = "experimental-napi")]
    experimental_napi: bool,
    /// The file, URL, or package to run.
    #[clap(value_parser = CliPackageSource::infer)]
    input: CliPackageSource,
    /// Command-line arguments passed to the package
    args: Vec<String>,
}

impl Run {
    #[cfg(feature = "napi-v8")]
    fn module_needs_napi(module: &Module) -> bool {
        let (napi_version, napi_extension_version) = wasmer_napi::module_needs_napi(module);
        napi_version.is_some() || napi_extension_version.is_some()
    }

    #[cfg(feature = "napi-v8")]
    fn maybe_wrap_runtime_with_napi(
        &self,
        module: &Module,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<Arc<dyn Runtime + Send + Sync>, Error> {
        use anyhow::ensure;

        if !Self::module_needs_napi(module) {
            return Ok(runtime);
        }
        ensure!(
            self.experimental_napi,
            "This module imports N-API. Re-run with '--experimental-napi' to enable the experimental N-API runtime."
        );

        let hooks = wasmer_napi::NapiCtx::default().runtime_hooks();
        Ok(Arc::new(
            OverriddenRuntime::new(runtime)
                .with_additional_imports({
                    let hooks = hooks.clone();
                    move |module, store| hooks.additional_imports(module, store)
                })
                .with_instance_setup(move |module, store, instance, imported_memory| {
                    hooks.configure_instance(module, store, instance, imported_memory)
                }),
        ))
    }

    #[cfg(feature = "napi-v8")]
    fn configure_wasi_runner_for_napi(&self, module: &Module, runner: &mut WasiRunner) {
        if Self::module_needs_napi(module) {
            runner
                .capabilities_mut()
                .threading
                .enable_asynchronous_threading = false;
        }
    }

    #[cfg(not(feature = "napi-v8"))]
    fn maybe_wrap_runtime_with_napi(
        &self,
        _module: &Module,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<Arc<dyn Runtime + Send + Sync>, Error> {
        Ok(runtime)
    }

    #[cfg(not(feature = "napi-v8"))]
    fn configure_wasi_runner_for_napi(&self, _module: &Module, _runner: &mut WasiRunner) {}

    pub fn execute(self, output: Output) -> ! {
        let result = self.execute_inner(output);
        exit_with_wasi_exit_code(result);
    }

    #[tracing::instrument(level = "debug", name = "wasmer_run", skip_all)]
    fn execute_inner(mut self, output: Output) -> Result<(), Error> {
        self.print_option_warnings();

        let pb = ProgressBar::new_spinner();
        pb.set_draw_target(output.draw_target());
        pb.enable_steady_tick(TICK);

        pb.set_message("Initializing the WebAssembly VM");

        let runtime = tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()?;
        let handle = runtime.handle().clone();

        // Check for the preferred webc version.
        // Default to v3.
        let webc_version_var = std::env::var("WASMER_WEBC_VERSION");
        let preferred_webc_version = match webc_version_var.as_deref() {
            Ok("2") => webc::Version::V2,
            Ok("3") | Err(_) => webc::Version::V3,
            Ok(other) => {
                bail!("unknown webc version: '{other}'");
            }
        };

        let _guard = handle.enter();

        // Get the input file path
        let mut wasm_bytes: Option<Vec<u8>> = None;

        // Try to detect WebAssembly features before selecting a backend
        tracing::info!("Input source: {:?}", self.input);
        if let CliPackageSource::File(path) = &self.input {
            tracing::info!("Input file path: {}", path.display());

            // Try to read and detect any file that exists, regardless of extension
            let target = TargetOnDisk::from_file(path);
            if let Ok(target) = target {
                match target {
                    TargetOnDisk::WebAssemblyBinary => {
                        if let Ok(data) = std::fs::read(path) {
                            wasm_bytes = Some(data);
                        } else {
                            tracing::info!("Failed to read file: {}", path.display());
                        }
                    }
                    TargetOnDisk::Wat => match std::fs::read(path) {
                        Ok(data) => match wat2wasm(&data) {
                            Ok(wasm) => {
                                wasm_bytes = Some(wasm.to_vec());
                            }
                            Err(e) => {
                                tracing::info!(
                                    "Failed to convert WAT to Wasm for {}: {e}",
                                    path.display()
                                );
                            }
                        },
                        Err(e) => {
                            tracing::info!("Failed to read WAT file {}: {e}", path.display());
                        }
                    },
                    _ => {}
                }
            } else {
                tracing::info!(
                    "Failed to read file for feature detection: {}",
                    path.display()
                );
            }
        } else {
            tracing::info!("Input is not a file, skipping WebAssembly feature detection");
        }

        // Get engine with feature-based backend selection if possible
        let mut engine = match &wasm_bytes {
            Some(wasm_bytes) => {
                tracing::info!("Attempting to detect WebAssembly features from binary");

                self.rt
                    .get_engine_for_module(wasm_bytes, &Target::default())?
            }
            None => {
                // No WebAssembly file available for analysis, check if we have a webc package
                if let CliPackageSource::Package(pkg_source) = &self.input {
                    tracing::info!("Checking package for WebAssembly features: {}", pkg_source);
                    self.rt.get_engine(&Target::default())?
                } else {
                    tracing::info!("No feature detection possible, using default engine");
                    self.rt.get_engine(&Target::default())?
                }
            }
        };

        let engine_kind = engine.deterministic_id();
        tracing::info!("Executing on backend {engine_kind:?}");

        #[cfg(feature = "sys")]
        if engine.is_sys() && self.stack_size.is_some() {
            wasmer_vm::set_stack_size(self.stack_size.unwrap());
        }

        let engine = engine.clone();

        let runtime = self.wasi.prepare_runtime(
            engine,
            &self.env,
            &capabilities::get_capability_cache_path(&self.env, &self.input)?,
            runtime,
            preferred_webc_version,
            self.rt.compiler_debug_dir.is_some(),
        )?;

        // This is a slow operation, so let's temporarily wrap the runtime with
        // something that displays progress
        let monitoring_runtime = Arc::new(MonitoringRuntime::new(
            runtime,
            pb.clone(),
            output.is_quiet_or_no_tty(),
        ));
        let runtime: Arc<dyn Runtime + Send + Sync> = monitoring_runtime.runtime.clone();
        let monitoring_runtime: Arc<dyn Runtime + Send + Sync> = monitoring_runtime;

        let target = self.input.resolve_target(&monitoring_runtime, &pb)?;

        if let ExecutableTarget::Package(ref pkg) = target {
            self.wasi
                .volumes
                .extend(pkg.additional_host_mapped_directories.clone());
        }

        pb.finish_and_clear();

        // push the TTY state so we can restore it after the program finishes
        let tty = runtime.tty().map(|tty| tty.tty_get());

        let result = {
            match target {
                ExecutableTarget::WebAssembly {
                    module,
                    module_hash,
                    path,
                } => self.execute_wasm(&path, module, module_hash, runtime.clone()),
                ExecutableTarget::Package(pkg) => {
                    // Check if we should update the engine based on the WebC package features
                    if let Some(cmd) = pkg.get_entrypoint_command()
                        && let Some(features) = cmd.wasm_features()
                    {
                        // Get the right engine for these features
                        let backends = self.rt.get_available_backends()?;
                        let available_engines = backends
                            .iter()
                            .map(|b| b.to_string())
                            .collect::<Vec<_>>()
                            .join(", ");

                        let filtered_backends = RuntimeOptions::filter_backends_by_features(
                            backends.clone(),
                            &features,
                            &Target::default(),
                        );

                        if let Some(backend) = filtered_backends.first() {
                            let engine_id = backend.to_string();

                            // Get a new engine that's compatible with the required features
                            if let Ok(new_engine) = backend.get_engine(&Target::default(), &self.rt)
                            {
                                tracing::info!(
                                    "The command '{}' requires to run the Wasm module with the features {:?}. The backends available are {}. Choosing {}.",
                                    cmd.name(),
                                    features,
                                    available_engines,
                                    engine_id
                                );
                                // Create a new runtime with the updated engine
                                let capability_cache_path =
                                    capabilities::get_capability_cache_path(
                                        &self.env,
                                        &self.input,
                                    )?;
                                let new_runtime = self.wasi.prepare_runtime(
                                    new_engine,
                                    &self.env,
                                    &capability_cache_path,
                                    tokio::runtime::Builder::new_multi_thread()
                                        .enable_all()
                                        .build()?,
                                    preferred_webc_version,
                                    self.rt.compiler_debug_dir.is_some(),
                                )?;

                                let new_runtime = Arc::new(MonitoringRuntime::new(
                                    new_runtime,
                                    pb.clone(),
                                    output.is_quiet_or_no_tty(),
                                ));
                                return self.execute_webc(&pkg, new_runtime);
                            }
                        }
                    }
                    self.execute_webc(&pkg, monitoring_runtime)
                }
            }
        };

        // restore the TTY state as the execution may have changed it
        if let Some(state) = tty
            && let Some(tty) = runtime.tty()
        {
            tty.tty_set(state);
        }

        if let Err(e) = &result {
            self.maybe_save_coredump(e);
        }

        result
    }

    #[tracing::instrument(skip_all)]
    fn execute_wasm(
        &self,
        path: &Path,
        module: Module,
        module_hash: ModuleHash,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<(), Error> {
        if wasmer_wasix::is_wasi_module(&module) || wasmer_wasix::is_wasix_module(&module) {
            self.execute_wasi_module(path, module, module_hash, runtime)
        } else {
            self.execute_pure_wasm_module(&module)
        }
    }

    #[tracing::instrument(skip_all)]
    fn execute_webc(
        &self,
        pkg: &BinaryPackage,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<(), Error> {
        let id = match self.entrypoint.as_deref() {
            Some(cmd) => cmd,
            None => pkg.infer_entrypoint()?,
        };
        let cmd = pkg
            .get_command(id)
            .with_context(|| format!("Unable to get metadata for the \"{id}\" command"))?;

        let uses = self.load_injected_packages(&runtime)?;

        if DcgiRunner::can_run_command(cmd.metadata())? {
            self.run_dcgi(id, pkg, uses, runtime)
        } else if DProxyRunner::can_run_command(cmd.metadata())? {
            self.run_dproxy(id, pkg, runtime)
        } else if WcgiRunner::can_run_command(cmd.metadata())? {
            self.run_wcgi(id, pkg, uses, runtime)
        } else if WasiRunner::can_run_command(cmd.metadata())? {
            self.run_wasi(id, pkg, uses, runtime)
        } else {
            bail!(
                "Unable to find a runner that supports \"{}\"",
                cmd.metadata().runner
            );
        }
    }

    #[tracing::instrument(level = "debug", skip_all)]
    fn load_injected_packages(
        &self,
        runtime: &Arc<dyn Runtime + Send + Sync>,
    ) -> Result<Vec<BinaryPackage>, Error> {
        let mut dependencies = Vec::new();

        for name in &self.wasi.uses {
            let specifier = name
                .parse::<PackageSource>()
                .with_context(|| format!("Unable to parse \"{name}\" as a package specifier"))?;
            let pkg = {
                let specifier = specifier.clone();
                let inner_runtime = runtime.clone();
                runtime
                    .task_manager()
                    .spawn_and_block_on(async move {
                        BinaryPackage::from_registry(&specifier, inner_runtime.as_ref()).await
                    })
                    .with_context(|| format!("Unable to load \"{name}\""))??
            };
            dependencies.push(pkg);
        }

        Ok(dependencies)
    }

    fn run_wasi(
        &self,
        command_name: &str,
        pkg: &BinaryPackage,
        uses: Vec<BinaryPackage>,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<(), Error> {
        #[cfg(feature = "napi-v8")]
        let (module, runtime) = {
            let cmd = pkg.get_command(command_name).with_context(|| {
                format!("Unable to get metadata for the \"{command_name}\" command")
            })?;
            let module = runtime.resolve_module_sync(
                wasmer_wasix::runtime::ModuleInput::Command(Cow::Borrowed(cmd)),
                None,
                None,
            )?;
            let runtime = self.maybe_wrap_runtime_with_napi(&module, runtime)?;
            (module, runtime)
        };

        // Assume webcs are always WASIX
        let mut runner = self.build_wasi_runner(&runtime, true)?;
        #[cfg(feature = "napi-v8")]
        self.configure_wasi_runner_for_napi(&module, &mut runner);
        Runner::run_command(&mut runner, command_name, pkg, runtime)
    }

    fn run_wcgi(
        &self,
        command_name: &str,
        pkg: &BinaryPackage,
        uses: Vec<BinaryPackage>,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<(), Error> {
        let mut runner = wasmer_wasix::runners::wcgi::WcgiRunner::new(NoOpWcgiCallbacks);
        self.config_wcgi(runner.config(), uses)?;
        runner.run_command(command_name, pkg, runtime)
    }

    fn config_wcgi(
        &self,
        config: &mut wcgi::Config,
        uses: Vec<BinaryPackage>,
    ) -> Result<(), Error> {
        config
            .args(self.args.clone())
            .addr(self.wcgi.addr)
            .envs(self.wasi.env_vars.clone())
            .map_directories(self.wasi.all_volumes())
            .callbacks(Callbacks::new(self.wcgi.addr))
            .inject_packages(uses);
        *config.capabilities() = self.wasi.capabilities();
        if self.wasi.forward_host_env {
            config.forward_host_env();
        }

        #[cfg(feature = "journal")]
        {
            for trigger in self.wasi.snapshot_on.iter().cloned() {
                config.add_snapshot_trigger(trigger);
            }
            if self.wasi.snapshot_on.is_empty() && !self.wasi.writable_journals.is_empty() {
                config.add_default_snapshot_triggers();
            }
            if let Some(period) = self.wasi.snapshot_interval {
                if self.wasi.writable_journals.is_empty() {
                    return Err(anyhow::format_err!(
                        "If you specify a snapshot interval then you must also specify a writable journal file"
                    ));
                }
                config.with_snapshot_interval(Duration::from_millis(period));
            }
            if self.wasi.stop_after_snapshot {
                config.with_stop_running_after_snapshot(true);
            }
            let (r, w) = self.wasi.build_journals()?;
            for journal in r {
                config.add_read_only_journal(journal);
            }
            for journal in w {
                config.add_writable_journal(journal);
            }
        }

        Ok(())
    }

    fn run_dcgi(
        &self,
        command_name: &str,
        pkg: &BinaryPackage,
        uses: Vec<BinaryPackage>,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<(), Error> {
        let factory = DcgiInstanceFactory::new();
        let mut runner = wasmer_wasix::runners::dcgi::DcgiRunner::new(factory);
        self.config_wcgi(runner.config().inner(), uses);
        runner.run_command(command_name, pkg, runtime)
    }

    fn run_dproxy(
        &self,
        command_name: &str,
        pkg: &BinaryPackage,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<(), Error> {
        let mut inner = self.build_wasi_runner(&runtime, true)?;
        let mut runner = wasmer_wasix::runners::dproxy::DProxyRunner::new(inner, pkg);
        runner.run_command(command_name, pkg, runtime)
    }

    #[tracing::instrument(skip_all)]
    fn execute_pure_wasm_module(&self, module: &Module) -> Result<(), Error> {
        /// The rest of the execution happens in the main thread, so we can create the
        /// store here.
        let mut store = self.rt.get_store()?;
        let imports = Imports::default();
        let instance = Instance::new(&mut store, module, &imports)
            .context("Unable to instantiate the WebAssembly module")?;

        let entry_function  = match &self.invoke {
            Some(entry) => {
                instance.exports
                    .get_function(entry)
                    .with_context(|| format!("The module doesn't export a function named \"{entry}\""))?
            },
            None => {
                instance.exports.get_function("_start")
                    .context("The module doesn't export a \"_start\" function. Either implement it or specify an entry function with --invoke")?
            }
        };

        let result = invoke_function(&instance, &mut store, entry_function, &self.args)?;

        match result {
            Ok(return_values) => {
                println!(
                    "{}",
                    return_values
                        .iter()
                        .map(|val| val.to_string())
                        .collect::<Vec<String>>()
                        .join(" ")
                );
                Ok(())
            }
            Err(err) => {
                bail!("{}", err.display(&mut store));
            }
        }
    }

    fn build_wasi_runner(
        &self,
        runtime: &Arc<dyn Runtime + Send + Sync>,
        is_wasix: bool,
    ) -> Result<WasiRunner, anyhow::Error> {
        let packages = self.load_injected_packages(runtime)?;

        let mut runner = WasiRunner::new();

        let (is_home_mapped, mapped_directories) = self.wasi.build_mapped_directories(is_wasix)?;

        runner
            .with_args(&self.args)
            .with_injected_packages(packages)
            .with_envs(self.wasi.env_vars.clone())
            .with_mapped_host_commands(self.wasi.build_mapped_commands()?)
            .with_mapped_directories(mapped_directories)
            .with_home_mapped(is_home_mapped)
            .with_forward_host_env(self.wasi.forward_host_env)
            .with_capabilities(self.wasi.capabilities());

        if let Some(cwd) = self.wasi.cwd.as_ref() {
            if !cwd.starts_with("/") {
                bail!("The argument to --cwd must be an absolute path");
            }
            runner.with_current_dir(cwd.clone());
        }

        if let Some(ref entry_function) = self.invoke {
            runner.with_entry_function(entry_function);
        }

        #[cfg(feature = "journal")]
        {
            for trigger in self.wasi.snapshot_on.iter().cloned() {
                runner.with_snapshot_trigger(trigger);
            }
            if self.wasi.snapshot_on.is_empty() && !self.wasi.writable_journals.is_empty() {
                runner.with_default_snapshot_triggers();
            }
            if let Some(period) = self.wasi.snapshot_interval {
                if self.wasi.writable_journals.is_empty() {
                    return Err(anyhow::format_err!(
                        "If you specify a snapshot interval then you must also specify a writable journal file"
                    ));
                }
                runner.with_snapshot_interval(Duration::from_millis(period));
            }
            if self.wasi.stop_after_snapshot {
                runner.with_stop_running_after_snapshot(true);
            }
            let (r, w) = self.wasi.build_journals()?;
            for journal in r {
                runner.with_read_only_journal(journal);
            }
            for journal in w {
                runner.with_writable_journal(journal);
            }
            runner.with_skip_stdio_during_bootstrap(self.wasi.skip_stdio_during_bootstrap);
        }

        Ok(runner)
    }

    #[tracing::instrument(skip_all)]
    fn execute_wasi_module(
        &self,
        wasm_path: &Path,
        module: Module,
        module_hash: ModuleHash,
        runtime: Arc<dyn Runtime + Send + Sync>,
    ) -> Result<(), Error> {
        let program_name = wasm_path.display().to_string();
        let runtime = self.maybe_wrap_runtime_with_napi(&module, runtime)?;

        let mut runner =
            self.build_wasi_runner(&runtime, wasmer_wasix::is_wasix_module(&module))?;
        self.configure_wasi_runner_for_napi(&module, &mut runner);
        runner.run_wasm(
            RuntimeOrEngine::Runtime(runtime),
            &program_name,
            module,
            module_hash,
        )
    }

    #[allow(unused_variables)]
    fn maybe_save_coredump(&self, e: &Error) {
        #[cfg(feature = "coredump")]
        if let Some(coredump) = &self.coredump_on_trap
            && let Err(e) = generate_coredump(e, self.input.to_string(), coredump)
        {
            tracing::warn!(
                error = &*e as &dyn std::error::Error,
                coredump_path=%coredump.display(),
                "Unable to generate a coredump",
            );
        }
    }

    fn print_option_warnings(&self) {
        if !self.wasi.mapped_dirs.is_empty() {
            eprintln!(
                "{}The `{}` option is deprecated and will be removed in the next major release. Please use `{}` instead.",
                "warning: ".yellow(),
                "--mapdir".yellow(),
                "--volume".green()
            );
        }
        if !self.wasi.pre_opened_directories.is_empty() {
            eprintln!(
                "{}The `{}` option is deprecated and will be removed in the next major release. Please use `{}` instead.",
                "warning: ".yellow(),
                "--dir".yellow(),
                "--volume".green()
            );
        }
    }
}

fn invoke_function(
    instance: &Instance,
    store: &mut Store,
    func: &Function,
    args: &[String],
) -> anyhow::Result<Result<Box<[Value]>, RuntimeError>> {
    let func_ty = func.ty(store);
    let required_arguments = func_ty.params().len();
    let provided_arguments = args.len();

    anyhow::ensure!(
        required_arguments == provided_arguments,
        "Function expected {required_arguments} arguments, but received {provided_arguments}"
    );

    let invoke_args = args
        .iter()
        .zip(func_ty.params().iter())
        .map(|(arg, param_type)| {
            parse_value(arg, *param_type)
                .with_context(|| format!("Unable to convert {arg:?} to {param_type:?}"))
        })
        .collect::<Result<Vec<_>, _>>()?;

    Ok(func.call(store, &invoke_args))
}

fn parse_value(s: &str, ty: wasmer_types::Type) -> Result<Value, Error> {
    let value = match ty {
        Type::I32 => Value::I32(s.parse()?),
        Type::I64 => Value::I64(s.parse()?),
        Type::F32 => Value::F32(s.parse()?),
        Type::F64 => Value::F64(s.parse()?),
        Type::V128 => Value::V128(s.parse()?),
        _ => bail!("There is no known conversion from {s:?} to {ty:?}"),
    };
    Ok(value)
}

#[cfg(feature = "coredump")]
fn generate_coredump(err: &Error, source_name: String, coredump_path: &Path) -> Result<(), Error> {
    let err: &wasmer::RuntimeError = match err.downcast_ref() {
        Some(e) => e,
        None => {
            log::warn!("no runtime error found to generate coredump with");
            return Ok(());
        }
    };

    let mut coredump_builder =
        wasm_coredump_builder::CoredumpBuilder::new().executable_name(&source_name);

    let mut thread_builder = wasm_coredump_builder::ThreadBuilder::new().thread_name("main");

    for frame in err.trace() {
        let coredump_frame = wasm_coredump_builder::FrameBuilder::new()
            .codeoffset(frame.func_offset() as u32)
            .funcidx(frame.func_index())
            .build();
        thread_builder.add_frame(coredump_frame);
    }

    coredump_builder.add_thread(thread_builder.build());

    let coredump = coredump_builder
        .serialize()
        .map_err(Error::msg)
        .context("Coredump serializing failed")?;

    std::fs::write(coredump_path, &coredump).with_context(|| {
        format!(
            "Unable to save the coredump to \"{}\"",
            coredump_path.display()
        )
    })?;

    Ok(())
}

#[derive(Debug, Clone, Parser)]
pub(crate) struct WcgiOptions {
    /// The address to serve on.
    #[clap(long, short, env, default_value_t = ([127, 0, 0, 1], 8000).into())]
    pub(crate) addr: SocketAddr,
}

impl Default for WcgiOptions {
    fn default() -> Self {
        Self {
            addr: ([127, 0, 0, 1], 8000).into(),
        }
    }
}

#[derive(Debug)]
struct Callbacks {
    stderr: Mutex<LineWriter<std::io::Stderr>>,
    addr: SocketAddr,
}

impl Callbacks {
    fn new(addr: SocketAddr) -> Self {
        Callbacks {
            stderr: Mutex::new(LineWriter::new(std::io::stderr())),
            addr,
        }
    }
}

impl wasmer_wasix::runners::wcgi::Callbacks for Callbacks {
    fn started(&self, _abort: AbortHandle) {
        println!("WCGI Server running at http://{}/", self.addr);
    }

    fn on_stderr(&self, raw_message: &[u8]) {
        if let Ok(mut stderr) = self.stderr.lock() {
            // If the WCGI runner printed any log messages we want to make sure
            // they get propagated to the user. Line buffering is important here
            // because it helps prevent the output from becoming a complete
            // mess.
            let _ = stderr.write_all(raw_message);
        }
    }
}

/// Exit the current process, using the WASI exit code if the error contains
/// one.
fn exit_with_wasi_exit_code(result: Result<(), Error>) -> ! {
    let exit_code = match result {
        Ok(_) => 0,
        Err(error) => {
            match error.chain().find_map(get_exit_code) {
                Some(exit_code) => exit_code.raw(),
                None => {
                    eprintln!("{:?}", PrettyError::new(error));
                    // Something else happened
                    1
                }
            }
        }
    };

    std::io::stdout().flush().ok();
    std::io::stderr().flush().ok();

    std::process::exit(exit_code);
}

fn get_exit_code(
    error: &(dyn std::error::Error + 'static),
) -> Option<wasmer_wasix::types::wasi::ExitCode> {
    if let Some(WasiError::Exit(exit_code)) = error.downcast_ref() {
        return Some(*exit_code);
    }
    if let Some(error) = error.downcast_ref::<wasmer_wasix::WasiRuntimeError>() {
        return error.as_exit_code();
    }

    None
}