|
| 1 | +//! WebAssembly linker code. |
| 2 | +
|
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use anyhow::{Context, Result}; |
| 6 | +use wasmtime::{ |
| 7 | + Engine, Store, |
| 8 | + component::{Component, HasData, Linker}, |
| 9 | +}; |
| 10 | +use wasmtime_wasi::{ResourceTable, WasiView}; |
| 11 | + |
| 12 | +use crate::{WasmStateImpl, bindings::Datafusion}; |
| 13 | + |
| 14 | +/// Link everything. |
| 15 | +pub(crate) async fn link( |
| 16 | + engine: &Engine, |
| 17 | + component: &Component, |
| 18 | + state: WasmStateImpl, |
| 19 | +) -> Result<(Arc<Datafusion>, Store<WasmStateImpl>)> { |
| 20 | + let mut store = Store::new(engine, state); |
| 21 | + |
| 22 | + let mut linker = Linker::new(engine); |
| 23 | + link_wasi_p2(&mut linker).context("link WASI p2")?; |
| 24 | + wasmtime_wasi_http::add_only_http_to_linker_async(&mut linker) |
| 25 | + .context("link WASI p2 HWasmStateImplWasmStateImplP")?; |
| 26 | + |
| 27 | + let bindings = Arc::new( |
| 28 | + Datafusion::instantiate_async(&mut store, component, &linker) |
| 29 | + .await |
| 30 | + .context("initialize bindings")?, |
| 31 | + ); |
| 32 | + Ok((bindings, store)) |
| 33 | +} |
| 34 | + |
| 35 | +/// Link WASIp2 interfaces. |
| 36 | +fn link_wasi_p2(linker: &mut Linker<WasmStateImpl>) -> Result<()> { |
| 37 | + use wasmtime_wasi::{ |
| 38 | + cli::{WasiCli, WasiCliView}, |
| 39 | + clocks::{WasiClocks, WasiClocksView}, |
| 40 | + filesystem::{WasiFilesystem, WasiFilesystemView}, |
| 41 | + p2::bindings, |
| 42 | + random::WasiRandom, |
| 43 | + sockets::{WasiSockets, WasiSocketsView}, |
| 44 | + }; |
| 45 | + |
| 46 | + let options = bindings::LinkOptions::default(); |
| 47 | + wasmtime_wasi_io::bindings::wasi::io::error::add_to_linker::<WasmStateImpl, HasIo>( |
| 48 | + linker, |
| 49 | + |t| t.ctx().table, |
| 50 | + )?; |
| 51 | + wasmtime_wasi_io::bindings::wasi::io::poll::add_to_linker::<WasmStateImpl, HasIo>( |
| 52 | + linker, |
| 53 | + |t| t.ctx().table, |
| 54 | + )?; |
| 55 | + wasmtime_wasi_io::bindings::wasi::io::streams::add_to_linker::<WasmStateImpl, HasIo>( |
| 56 | + linker, |
| 57 | + |t| t.ctx().table, |
| 58 | + )?; |
| 59 | + bindings::clocks::wall_clock::add_to_linker::<WasmStateImpl, WasiClocks>( |
| 60 | + linker, |
| 61 | + WasmStateImpl::clocks, |
| 62 | + )?; |
| 63 | + bindings::clocks::monotonic_clock::add_to_linker::<WasmStateImpl, WasiClocks>( |
| 64 | + linker, |
| 65 | + WasmStateImpl::clocks, |
| 66 | + )?; |
| 67 | + bindings::cli::exit::add_to_linker::<WasmStateImpl, WasiCli>( |
| 68 | + linker, |
| 69 | + &(&options).into(), |
| 70 | + WasmStateImpl::cli, |
| 71 | + )?; |
| 72 | + bindings::cli::environment::add_to_linker::<WasmStateImpl, WasiCli>( |
| 73 | + linker, |
| 74 | + WasmStateImpl::cli, |
| 75 | + )?; |
| 76 | + bindings::cli::stdin::add_to_linker::<WasmStateImpl, WasiCli>(linker, WasmStateImpl::cli)?; |
| 77 | + bindings::cli::stdout::add_to_linker::<WasmStateImpl, WasiCli>(linker, WasmStateImpl::cli)?; |
| 78 | + bindings::cli::stderr::add_to_linker::<WasmStateImpl, WasiCli>(linker, WasmStateImpl::cli)?; |
| 79 | + bindings::cli::terminal_input::add_to_linker::<WasmStateImpl, WasiCli>( |
| 80 | + linker, |
| 81 | + WasmStateImpl::cli, |
| 82 | + )?; |
| 83 | + bindings::cli::terminal_output::add_to_linker::<WasmStateImpl, WasiCli>( |
| 84 | + linker, |
| 85 | + WasmStateImpl::cli, |
| 86 | + )?; |
| 87 | + bindings::cli::terminal_stdin::add_to_linker::<WasmStateImpl, WasiCli>( |
| 88 | + linker, |
| 89 | + WasmStateImpl::cli, |
| 90 | + )?; |
| 91 | + bindings::cli::terminal_stdout::add_to_linker::<WasmStateImpl, WasiCli>( |
| 92 | + linker, |
| 93 | + WasmStateImpl::cli, |
| 94 | + )?; |
| 95 | + bindings::cli::terminal_stderr::add_to_linker::<WasmStateImpl, WasiCli>( |
| 96 | + linker, |
| 97 | + WasmStateImpl::cli, |
| 98 | + )?; |
| 99 | + bindings::filesystem::types::add_to_linker::<WasmStateImpl, WasiFilesystem>( |
| 100 | + linker, |
| 101 | + WasmStateImpl::filesystem, |
| 102 | + )?; |
| 103 | + bindings::filesystem::preopens::add_to_linker::<WasmStateImpl, WasiFilesystem>( |
| 104 | + linker, |
| 105 | + WasmStateImpl::filesystem, |
| 106 | + )?; |
| 107 | + bindings::random::random::add_to_linker::<WasmStateImpl, WasiRandom>(linker, |t| { |
| 108 | + t.ctx().ctx.random() |
| 109 | + })?; |
| 110 | + bindings::random::insecure::add_to_linker::<WasmStateImpl, WasiRandom>(linker, |t| { |
| 111 | + t.ctx().ctx.random() |
| 112 | + })?; |
| 113 | + bindings::random::insecure_seed::add_to_linker::<WasmStateImpl, WasiRandom>(linker, |t| { |
| 114 | + t.ctx().ctx.random() |
| 115 | + })?; |
| 116 | + bindings::sockets::instance_network::add_to_linker::<WasmStateImpl, WasiSockets>( |
| 117 | + linker, |
| 118 | + WasmStateImpl::sockets, |
| 119 | + )?; |
| 120 | + bindings::sockets::network::add_to_linker::<WasmStateImpl, WasiSockets>( |
| 121 | + linker, |
| 122 | + &(&options).into(), |
| 123 | + WasmStateImpl::sockets, |
| 124 | + )?; |
| 125 | + bindings::sockets::ip_name_lookup::add_to_linker::<WasmStateImpl, WasiSockets>( |
| 126 | + linker, |
| 127 | + WasmStateImpl::sockets, |
| 128 | + )?; |
| 129 | + bindings::sockets::tcp::add_to_linker::<WasmStateImpl, WasiSockets>( |
| 130 | + linker, |
| 131 | + WasmStateImpl::sockets, |
| 132 | + )?; |
| 133 | + bindings::sockets::tcp_create_socket::add_to_linker::<WasmStateImpl, WasiSockets>( |
| 134 | + linker, |
| 135 | + WasmStateImpl::sockets, |
| 136 | + )?; |
| 137 | + bindings::sockets::udp::add_to_linker::<WasmStateImpl, WasiSockets>( |
| 138 | + linker, |
| 139 | + WasmStateImpl::sockets, |
| 140 | + )?; |
| 141 | + bindings::sockets::udp_create_socket::add_to_linker::<WasmStateImpl, WasiSockets>( |
| 142 | + linker, |
| 143 | + WasmStateImpl::sockets, |
| 144 | + )?; |
| 145 | + Ok(()) |
| 146 | +} |
| 147 | + |
| 148 | +/// Marker struct to tell linker that we do in fact provide IO-related resource tables. |
| 149 | +struct HasIo; |
| 150 | + |
| 151 | +impl HasData for HasIo { |
| 152 | + type Data<'a> = &'a mut ResourceTable; |
| 153 | +} |
0 commit comments