|
| 1 | +pub mod preview1; |
| 2 | + |
| 3 | +use spin_factors::{Factor, FactorBuilder, InitContext, PrepareContext, Result, SpinFactors}; |
| 4 | +use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView}; |
| 5 | + |
| 6 | +pub struct WasiFactor; |
| 7 | + |
| 8 | +impl Factor for WasiFactor { |
| 9 | + type Builder = Builder; |
| 10 | + type Data = Data; |
| 11 | + |
| 12 | + fn init<Factors: SpinFactors>(&mut self, mut ctx: InitContext<Factors, Self>) -> Result<()> { |
| 13 | + use wasmtime_wasi::bindings; |
| 14 | + ctx.link_bindings(bindings::clocks::wall_clock::add_to_linker_get_host)?; |
| 15 | + ctx.link_bindings(bindings::clocks::monotonic_clock::add_to_linker_get_host)?; |
| 16 | + ctx.link_bindings(bindings::filesystem::types::add_to_linker_get_host)?; |
| 17 | + ctx.link_bindings(bindings::filesystem::preopens::add_to_linker_get_host)?; |
| 18 | + ctx.link_bindings(bindings::io::error::add_to_linker_get_host)?; |
| 19 | + ctx.link_bindings(bindings::io::poll::add_to_linker_get_host)?; |
| 20 | + ctx.link_bindings(bindings::io::streams::add_to_linker_get_host)?; |
| 21 | + ctx.link_bindings(bindings::random::random::add_to_linker_get_host)?; |
| 22 | + ctx.link_bindings(bindings::random::insecure::add_to_linker_get_host)?; |
| 23 | + ctx.link_bindings(bindings::random::insecure_seed::add_to_linker_get_host)?; |
| 24 | + ctx.link_bindings(bindings::cli::exit::add_to_linker_get_host)?; |
| 25 | + ctx.link_bindings(bindings::cli::environment::add_to_linker_get_host)?; |
| 26 | + ctx.link_bindings(bindings::cli::stdin::add_to_linker_get_host)?; |
| 27 | + ctx.link_bindings(bindings::cli::stdout::add_to_linker_get_host)?; |
| 28 | + ctx.link_bindings(bindings::cli::stderr::add_to_linker_get_host)?; |
| 29 | + ctx.link_bindings(bindings::cli::terminal_input::add_to_linker_get_host)?; |
| 30 | + ctx.link_bindings(bindings::cli::terminal_output::add_to_linker_get_host)?; |
| 31 | + ctx.link_bindings(bindings::cli::terminal_stdin::add_to_linker_get_host)?; |
| 32 | + ctx.link_bindings(bindings::cli::terminal_stdout::add_to_linker_get_host)?; |
| 33 | + ctx.link_bindings(bindings::cli::terminal_stderr::add_to_linker_get_host)?; |
| 34 | + ctx.link_bindings(bindings::sockets::tcp::add_to_linker_get_host)?; |
| 35 | + ctx.link_bindings(bindings::sockets::tcp_create_socket::add_to_linker_get_host)?; |
| 36 | + ctx.link_bindings(bindings::sockets::udp::add_to_linker_get_host)?; |
| 37 | + ctx.link_bindings(bindings::sockets::udp_create_socket::add_to_linker_get_host)?; |
| 38 | + ctx.link_bindings(bindings::sockets::instance_network::add_to_linker_get_host)?; |
| 39 | + ctx.link_bindings(bindings::sockets::network::add_to_linker_get_host)?; |
| 40 | + ctx.link_bindings(bindings::sockets::ip_name_lookup::add_to_linker_get_host)?; |
| 41 | + Ok(()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +pub struct Builder { |
| 46 | + wasi_ctx: WasiCtxBuilder, |
| 47 | +} |
| 48 | + |
| 49 | +impl FactorBuilder<WasiFactor> for Builder { |
| 50 | + fn prepare<Factors: SpinFactors>( |
| 51 | + _factor: &WasiFactor, |
| 52 | + _ctx: PrepareContext<Factors>, |
| 53 | + ) -> Result<Self> { |
| 54 | + Ok(Self { |
| 55 | + wasi_ctx: WasiCtxBuilder::new(), |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + fn build(mut self) -> Result<Data> { |
| 60 | + Ok(Data { |
| 61 | + ctx: self.wasi_ctx.build(), |
| 62 | + table: Default::default(), |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +pub struct Data { |
| 68 | + ctx: WasiCtx, |
| 69 | + table: ResourceTable, |
| 70 | +} |
| 71 | + |
| 72 | +impl WasiView for Data { |
| 73 | + fn ctx(&mut self) -> &mut WasiCtx { |
| 74 | + &mut self.ctx |
| 75 | + } |
| 76 | + |
| 77 | + fn table(&mut self) -> &mut ResourceTable { |
| 78 | + &mut self.table |
| 79 | + } |
| 80 | +} |
0 commit comments