1pub(crate) mod wit;
2
3use crate::ExtensionManifest;
4use anyhow::{anyhow, bail, Context as _, Result};
5use fs::{normalize_path, Fs};
6use futures::{
7 channel::{
8 mpsc::{self, UnboundedSender},
9 oneshot,
10 },
11 future::BoxFuture,
12 Future, FutureExt, StreamExt as _,
13};
14use gpui::BackgroundExecutor;
15use language::LanguageRegistry;
16use node_runtime::NodeRuntime;
17use std::{
18 path::{Path, PathBuf},
19 sync::{Arc, OnceLock},
20};
21use util::{http::HttpClient, SemanticVersion};
22use wasmtime::{
23 component::{Component, ResourceTable},
24 Engine, Store,
25};
26use wasmtime_wasi as wasi;
27use wit::Extension;
28
29pub(crate) struct WasmHost {
30 engine: Engine,
31 http_client: Arc<dyn HttpClient>,
32 node_runtime: Arc<dyn NodeRuntime>,
33 pub(crate) language_registry: Arc<LanguageRegistry>,
34 fs: Arc<dyn Fs>,
35 pub(crate) work_dir: PathBuf,
36}
37
38#[derive(Clone)]
39pub struct WasmExtension {
40 tx: UnboundedSender<ExtensionCall>,
41 pub(crate) manifest: Arc<ExtensionManifest>,
42 #[allow(unused)]
43 pub zed_api_version: SemanticVersion,
44}
45
46pub(crate) struct WasmState {
47 manifest: Arc<ExtensionManifest>,
48 pub(crate) table: ResourceTable,
49 ctx: wasi::WasiCtx,
50 pub(crate) host: Arc<WasmHost>,
51}
52
53type ExtensionCall = Box<
54 dyn Send + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, ()>,
55>;
56
57fn wasm_engine() -> wasmtime::Engine {
58 static WASM_ENGINE: OnceLock<wasmtime::Engine> = OnceLock::new();
59
60 WASM_ENGINE
61 .get_or_init(|| {
62 let mut config = wasmtime::Config::new();
63 config.wasm_component_model(true);
64 config.async_support(true);
65 wasmtime::Engine::new(&config).unwrap()
66 })
67 .clone()
68}
69
70impl WasmHost {
71 pub fn new(
72 fs: Arc<dyn Fs>,
73 http_client: Arc<dyn HttpClient>,
74 node_runtime: Arc<dyn NodeRuntime>,
75 language_registry: Arc<LanguageRegistry>,
76 work_dir: PathBuf,
77 ) -> Arc<Self> {
78 Arc::new(Self {
79 engine: wasm_engine(),
80 fs,
81 work_dir,
82 http_client,
83 node_runtime,
84 language_registry,
85 })
86 }
87
88 pub fn load_extension(
89 self: &Arc<Self>,
90 wasm_bytes: Vec<u8>,
91 manifest: Arc<ExtensionManifest>,
92 executor: BackgroundExecutor,
93 ) -> impl 'static + Future<Output = Result<WasmExtension>> {
94 let this = self.clone();
95 async move {
96 let zed_api_version = parse_wasm_extension_version(&manifest.id, &wasm_bytes)?;
97
98 let component = Component::from_binary(&this.engine, &wasm_bytes)
99 .context("failed to compile wasm component")?;
100
101 let mut store = wasmtime::Store::new(
102 &this.engine,
103 WasmState {
104 ctx: this.build_wasi_ctx(&manifest).await?,
105 manifest: manifest.clone(),
106 table: ResourceTable::new(),
107 host: this.clone(),
108 },
109 );
110
111 let (mut extension, instance) =
112 Extension::instantiate_async(&mut store, zed_api_version, &component).await?;
113
114 extension
115 .call_init_extension(&mut store)
116 .await
117 .context("failed to initialize wasm extension")?;
118
119 let (tx, mut rx) = mpsc::unbounded::<ExtensionCall>();
120 executor
121 .spawn(async move {
122 let _instance = instance;
123 while let Some(call) = rx.next().await {
124 (call)(&mut extension, &mut store).await;
125 }
126 })
127 .detach();
128
129 Ok(WasmExtension {
130 manifest,
131 tx,
132 zed_api_version,
133 })
134 }
135 }
136
137 async fn build_wasi_ctx(&self, manifest: &Arc<ExtensionManifest>) -> Result<wasi::WasiCtx> {
138 use cap_std::{ambient_authority, fs::Dir};
139
140 let extension_work_dir = self.work_dir.join(manifest.id.as_ref());
141 self.fs
142 .create_dir(&extension_work_dir)
143 .await
144 .context("failed to create extension work dir")?;
145
146 let work_dir_preopen = Dir::open_ambient_dir(&extension_work_dir, ambient_authority())
147 .context("failed to preopen extension work directory")?;
148 let current_dir_preopen = work_dir_preopen
149 .try_clone()
150 .context("failed to preopen extension current directory")?;
151 let extension_work_dir = extension_work_dir.to_string_lossy();
152
153 let perms = wasi::FilePerms::all();
154 let dir_perms = wasi::DirPerms::all();
155
156 Ok(wasi::WasiCtxBuilder::new()
157 .inherit_stdio()
158 .preopened_dir(current_dir_preopen, dir_perms, perms, ".")
159 .preopened_dir(work_dir_preopen, dir_perms, perms, &extension_work_dir)
160 .env("PWD", &extension_work_dir)
161 .env("RUST_BACKTRACE", "full")
162 .build())
163 }
164
165 pub fn path_from_extension(&self, id: &Arc<str>, path: &Path) -> PathBuf {
166 let extension_work_dir = self.work_dir.join(id.as_ref());
167 normalize_path(&extension_work_dir.join(path))
168 }
169
170 pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Result<PathBuf> {
171 let extension_work_dir = self.work_dir.join(id.as_ref());
172 let path = normalize_path(&extension_work_dir.join(path));
173 if path.starts_with(&extension_work_dir) {
174 Ok(path)
175 } else {
176 Err(anyhow!("cannot write to path {}", path.display()))
177 }
178 }
179}
180
181pub fn parse_wasm_extension_version(
182 extension_id: &str,
183 wasm_bytes: &[u8],
184) -> Result<SemanticVersion> {
185 for part in wasmparser::Parser::new(0).parse_all(wasm_bytes) {
186 if let wasmparser::Payload::CustomSection(s) = part? {
187 if s.name() == "zed:api-version" {
188 let version = parse_wasm_extension_version_custom_section(s.data());
189 if let Some(version) = version {
190 return Ok(version);
191 } else {
192 bail!(
193 "extension {} has invalid zed:api-version section: {:?}",
194 extension_id,
195 s.data()
196 );
197 }
198 }
199 }
200 }
201 bail!("extension {} has no zed:api-version section", extension_id)
202}
203
204fn parse_wasm_extension_version_custom_section(data: &[u8]) -> Option<SemanticVersion> {
205 if data.len() == 6 {
206 Some(SemanticVersion {
207 major: u16::from_be_bytes([data[0], data[1]]) as _,
208 minor: u16::from_be_bytes([data[2], data[3]]) as _,
209 patch: u16::from_be_bytes([data[4], data[5]]) as _,
210 })
211 } else {
212 None
213 }
214}
215
216impl WasmExtension {
217 pub async fn call<T, Fn>(&self, f: Fn) -> T
218 where
219 T: 'static + Send,
220 Fn: 'static
221 + Send
222 + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, T>,
223 {
224 let (return_tx, return_rx) = oneshot::channel();
225 self.tx
226 .clone()
227 .unbounded_send(Box::new(move |extension, store| {
228 async {
229 let result = f(extension, store).await;
230 return_tx.send(result).ok();
231 }
232 .boxed()
233 }))
234 .expect("wasm extension channel should not be closed yet");
235 return_rx.await.expect("wasm extension channel")
236 }
237}
238
239impl WasmState {
240 fn work_dir(&self) -> PathBuf {
241 self.host.work_dir.join(self.manifest.id.as_ref())
242 }
243}
244
245impl wasi::WasiView for WasmState {
246 fn table(&mut self) -> &mut ResourceTable {
247 &mut self.table
248 }
249
250 fn ctx(&mut self) -> &mut wasi::WasiCtx {
251 &mut self.ctx
252 }
253}