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