1pub mod wit;
2
3use crate::{ExtensionManifest, ExtensionRegistrationHooks};
4use anyhow::{anyhow, bail, Context as _, Result};
5use fs::{normalize_path, Fs};
6use futures::future::LocalBoxFuture;
7use futures::{
8 channel::{
9 mpsc::{self, UnboundedSender},
10 oneshot,
11 },
12 future::BoxFuture,
13 Future, FutureExt, StreamExt as _,
14};
15use gpui::{AppContext, AsyncAppContext, BackgroundExecutor, Task};
16use http_client::HttpClient;
17use node_runtime::NodeRuntime;
18use release_channel::ReleaseChannel;
19use semantic_version::SemanticVersion;
20use std::{
21 path::{Path, PathBuf},
22 sync::{Arc, OnceLock},
23};
24use wasmtime::{
25 component::{Component, ResourceTable},
26 Engine, Store,
27};
28use wasmtime_wasi as wasi;
29use wit::Extension;
30pub use wit::SlashCommand;
31
32pub struct WasmHost {
33 engine: Engine,
34 release_channel: ReleaseChannel,
35 http_client: Arc<dyn HttpClient>,
36 node_runtime: NodeRuntime,
37 pub registration_hooks: Arc<dyn ExtensionRegistrationHooks>,
38 fs: Arc<dyn Fs>,
39 pub work_dir: PathBuf,
40 _main_thread_message_task: Task<()>,
41 main_thread_message_tx: mpsc::UnboundedSender<MainThreadCall>,
42}
43
44#[derive(Clone)]
45pub struct WasmExtension {
46 tx: UnboundedSender<ExtensionCall>,
47 pub manifest: Arc<ExtensionManifest>,
48 #[allow(unused)]
49 pub zed_api_version: SemanticVersion,
50}
51
52pub struct WasmState {
53 manifest: Arc<ExtensionManifest>,
54 pub table: ResourceTable,
55 ctx: wasi::WasiCtx,
56 pub host: Arc<WasmHost>,
57}
58
59type MainThreadCall =
60 Box<dyn Send + for<'a> FnOnce(&'a mut AsyncAppContext) -> LocalBoxFuture<'a, ()>>;
61
62type ExtensionCall = Box<
63 dyn Send + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, ()>,
64>;
65
66fn wasm_engine() -> wasmtime::Engine {
67 static WASM_ENGINE: OnceLock<wasmtime::Engine> = OnceLock::new();
68
69 WASM_ENGINE
70 .get_or_init(|| {
71 let mut config = wasmtime::Config::new();
72 config.wasm_component_model(true);
73 config.async_support(true);
74 wasmtime::Engine::new(&config).unwrap()
75 })
76 .clone()
77}
78
79impl WasmHost {
80 pub fn new(
81 fs: Arc<dyn Fs>,
82 http_client: Arc<dyn HttpClient>,
83 node_runtime: NodeRuntime,
84 registration_hooks: Arc<dyn ExtensionRegistrationHooks>,
85 work_dir: PathBuf,
86 cx: &mut AppContext,
87 ) -> Arc<Self> {
88 let (tx, mut rx) = mpsc::unbounded::<MainThreadCall>();
89 let task = cx.spawn(|mut cx| async move {
90 while let Some(message) = rx.next().await {
91 message(&mut cx).await;
92 }
93 });
94 Arc::new(Self {
95 engine: wasm_engine(),
96 fs,
97 work_dir,
98 http_client,
99 node_runtime,
100 registration_hooks,
101 release_channel: ReleaseChannel::global(cx),
102 _main_thread_message_task: task,
103 main_thread_message_tx: tx,
104 })
105 }
106
107 pub fn load_extension(
108 self: &Arc<Self>,
109 wasm_bytes: Vec<u8>,
110 manifest: &Arc<ExtensionManifest>,
111 executor: BackgroundExecutor,
112 ) -> Task<Result<WasmExtension>> {
113 let this = self.clone();
114 let manifest = manifest.clone();
115 executor.clone().spawn(async move {
116 let zed_api_version = parse_wasm_extension_version(&manifest.id, &wasm_bytes)?;
117
118 let component = Component::from_binary(&this.engine, &wasm_bytes)
119 .context("failed to compile wasm component")?;
120
121 let mut store = wasmtime::Store::new(
122 &this.engine,
123 WasmState {
124 ctx: this.build_wasi_ctx(&manifest).await?,
125 manifest: manifest.clone(),
126 table: ResourceTable::new(),
127 host: this.clone(),
128 },
129 );
130
131 let mut extension = Extension::instantiate_async(
132 &mut store,
133 this.release_channel,
134 zed_api_version,
135 &component,
136 )
137 .await?;
138
139 extension
140 .call_init_extension(&mut store)
141 .await
142 .context("failed to initialize wasm extension")?;
143
144 let (tx, mut rx) = mpsc::unbounded::<ExtensionCall>();
145 executor
146 .spawn(async move {
147 while let Some(call) = rx.next().await {
148 (call)(&mut extension, &mut store).await;
149 }
150 })
151 .detach();
152
153 Ok(WasmExtension {
154 manifest: manifest.clone(),
155 tx,
156 zed_api_version,
157 })
158 })
159 }
160
161 async fn build_wasi_ctx(&self, manifest: &Arc<ExtensionManifest>) -> Result<wasi::WasiCtx> {
162 let extension_work_dir = self.work_dir.join(manifest.id.as_ref());
163 self.fs
164 .create_dir(&extension_work_dir)
165 .await
166 .context("failed to create extension work dir")?;
167
168 let file_perms = wasi::FilePerms::all();
169 let dir_perms = wasi::DirPerms::all();
170
171 Ok(wasi::WasiCtxBuilder::new()
172 .inherit_stdio()
173 .preopened_dir(&extension_work_dir, ".", dir_perms, file_perms)?
174 .preopened_dir(
175 &extension_work_dir,
176 extension_work_dir.to_string_lossy(),
177 dir_perms,
178 file_perms,
179 )?
180 .env("PWD", extension_work_dir.to_string_lossy())
181 .env("RUST_BACKTRACE", "full")
182 .build())
183 }
184
185 pub fn path_from_extension(&self, id: &Arc<str>, path: &Path) -> PathBuf {
186 let extension_work_dir = self.work_dir.join(id.as_ref());
187 normalize_path(&extension_work_dir.join(path))
188 }
189
190 pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Result<PathBuf> {
191 let extension_work_dir = self.work_dir.join(id.as_ref());
192 let path = normalize_path(&extension_work_dir.join(path));
193 if path.starts_with(&extension_work_dir) {
194 Ok(path)
195 } else {
196 Err(anyhow!("cannot write to path {}", path.display()))
197 }
198 }
199}
200
201pub fn parse_wasm_extension_version(
202 extension_id: &str,
203 wasm_bytes: &[u8],
204) -> Result<SemanticVersion> {
205 let mut version = None;
206
207 for part in wasmparser::Parser::new(0).parse_all(wasm_bytes) {
208 if let wasmparser::Payload::CustomSection(s) =
209 part.context("error parsing wasm extension")?
210 {
211 if s.name() == "zed:api-version" {
212 version = parse_wasm_extension_version_custom_section(s.data());
213 if version.is_none() {
214 bail!(
215 "extension {} has invalid zed:api-version section: {:?}",
216 extension_id,
217 s.data()
218 );
219 }
220 }
221 }
222 }
223
224 // The reason we wait until we're done parsing all of the Wasm bytes to return the version
225 // is to work around a panic that can happen inside of Wasmtime when the bytes are invalid.
226 //
227 // By parsing the entirety of the Wasm bytes before we return, we're able to detect this problem
228 // earlier as an `Err` rather than as a panic.
229 version.ok_or_else(|| anyhow!("extension {} has no zed:api-version section", extension_id))
230}
231
232fn parse_wasm_extension_version_custom_section(data: &[u8]) -> Option<SemanticVersion> {
233 if data.len() == 6 {
234 Some(SemanticVersion::new(
235 u16::from_be_bytes([data[0], data[1]]) as _,
236 u16::from_be_bytes([data[2], data[3]]) as _,
237 u16::from_be_bytes([data[4], data[5]]) as _,
238 ))
239 } else {
240 None
241 }
242}
243
244impl WasmExtension {
245 pub async fn load(
246 extension_dir: PathBuf,
247 manifest: &Arc<ExtensionManifest>,
248 wasm_host: Arc<WasmHost>,
249 cx: &AsyncAppContext,
250 ) -> Result<Self> {
251 let path = extension_dir.join("extension.wasm");
252
253 let mut wasm_file = wasm_host
254 .fs
255 .open_sync(&path)
256 .await
257 .context("failed to open wasm file")?;
258
259 let mut wasm_bytes = Vec::new();
260 wasm_file
261 .read_to_end(&mut wasm_bytes)
262 .context("failed to read wasm")?;
263
264 wasm_host
265 .load_extension(wasm_bytes, manifest, cx.background_executor().clone())
266 .await
267 .with_context(|| format!("failed to load wasm extension {}", manifest.id))
268 }
269
270 pub async fn call<T, Fn>(&self, f: Fn) -> T
271 where
272 T: 'static + Send,
273 Fn: 'static
274 + Send
275 + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, T>,
276 {
277 let (return_tx, return_rx) = oneshot::channel();
278 self.tx
279 .clone()
280 .unbounded_send(Box::new(move |extension, store| {
281 async {
282 let result = f(extension, store).await;
283 return_tx.send(result).ok();
284 }
285 .boxed()
286 }))
287 .expect("wasm extension channel should not be closed yet");
288 return_rx.await.expect("wasm extension channel")
289 }
290}
291
292impl WasmState {
293 fn on_main_thread<T, Fn>(&self, f: Fn) -> impl 'static + Future<Output = T>
294 where
295 T: 'static + Send,
296 Fn: 'static + Send + for<'a> FnOnce(&'a mut AsyncAppContext) -> LocalBoxFuture<'a, T>,
297 {
298 let (return_tx, return_rx) = oneshot::channel();
299 self.host
300 .main_thread_message_tx
301 .clone()
302 .unbounded_send(Box::new(move |cx| {
303 async {
304 let result = f(cx).await;
305 return_tx.send(result).ok();
306 }
307 .boxed_local()
308 }))
309 .expect("main thread message channel should not be closed yet");
310 async move { return_rx.await.expect("main thread message channel") }
311 }
312
313 fn work_dir(&self) -> PathBuf {
314 self.host.work_dir.join(self.manifest.id.as_ref())
315 }
316}
317
318impl wasi::WasiView for WasmState {
319 fn table(&mut self) -> &mut ResourceTable {
320 &mut self.table
321 }
322
323 fn ctx(&mut self) -> &mut wasi::WasiCtx {
324 &mut self.ctx
325 }
326}