1pub mod wit;
2
3use crate::capability_granter::CapabilityGranter;
4use crate::{ExtensionManifest, ExtensionSettings};
5use anyhow::{Context as _, Result, anyhow, bail};
6use async_trait::async_trait;
7use dap::{DebugRequest, StartDebuggingRequestArgumentsRequest};
8use extension::{
9 CodeLabel, Command, Completion, ContextServerConfiguration, DebugAdapterBinary,
10 DebugTaskDefinition, ExtensionCapability, ExtensionHostProxy, KeyValueStoreDelegate,
11 ProjectDelegate, SlashCommand, SlashCommandArgumentCompletion, SlashCommandOutput, Symbol,
12 WorktreeDelegate,
13};
14use fs::{Fs, normalize_path};
15use futures::future::LocalBoxFuture;
16use futures::{
17 Future, FutureExt, StreamExt as _,
18 channel::{
19 mpsc::{self, UnboundedSender},
20 oneshot,
21 },
22 future::BoxFuture,
23};
24use gpui::{App, AsyncApp, BackgroundExecutor, Task, Timer};
25use http_client::HttpClient;
26use language::LanguageName;
27use lsp::LanguageServerName;
28use moka::sync::Cache;
29use node_runtime::NodeRuntime;
30use release_channel::ReleaseChannel;
31use semantic_version::SemanticVersion;
32use settings::Settings;
33use std::{
34 borrow::Cow,
35 path::{Path, PathBuf},
36 sync::{
37 Arc, LazyLock, OnceLock,
38 atomic::{AtomicBool, Ordering},
39 },
40 time::Duration,
41};
42use task::{DebugScenario, SpawnInTerminal, TaskTemplate, ZedDebugConfig};
43use util::paths::SanitizedPath;
44use wasmtime::{
45 CacheStore, Engine, Store,
46 component::{Component, ResourceTable},
47};
48use wasmtime_wasi::{self as wasi, WasiView};
49use wit::Extension;
50
51pub struct WasmHost {
52 engine: Engine,
53 release_channel: ReleaseChannel,
54 http_client: Arc<dyn HttpClient>,
55 node_runtime: NodeRuntime,
56 pub(crate) proxy: Arc<ExtensionHostProxy>,
57 fs: Arc<dyn Fs>,
58 pub work_dir: PathBuf,
59 /// The capabilities granted to extensions running on the host.
60 pub(crate) granted_capabilities: Vec<ExtensionCapability>,
61 _main_thread_message_task: Task<()>,
62 main_thread_message_tx: mpsc::UnboundedSender<MainThreadCall>,
63}
64
65#[derive(Clone, Debug)]
66pub struct WasmExtension {
67 tx: UnboundedSender<ExtensionCall>,
68 pub manifest: Arc<ExtensionManifest>,
69 pub work_dir: Arc<Path>,
70 #[allow(unused)]
71 pub zed_api_version: SemanticVersion,
72 _task: Arc<Task<Result<(), gpui_tokio::JoinError>>>,
73}
74
75impl Drop for WasmExtension {
76 fn drop(&mut self) {
77 self.tx.close_channel();
78 }
79}
80
81#[async_trait]
82impl extension::Extension for WasmExtension {
83 fn manifest(&self) -> Arc<ExtensionManifest> {
84 self.manifest.clone()
85 }
86
87 fn work_dir(&self) -> Arc<Path> {
88 self.work_dir.clone()
89 }
90
91 async fn language_server_command(
92 &self,
93 language_server_id: LanguageServerName,
94 language_name: LanguageName,
95 worktree: Arc<dyn WorktreeDelegate>,
96 ) -> Result<Command> {
97 self.call(|extension, store| {
98 async move {
99 let resource = store.data_mut().table().push(worktree)?;
100 let command = extension
101 .call_language_server_command(
102 store,
103 &language_server_id,
104 &language_name,
105 resource,
106 )
107 .await?
108 .map_err(|err| store.data().extension_error(err))?;
109
110 Ok(command.into())
111 }
112 .boxed()
113 })
114 .await?
115 }
116
117 async fn language_server_initialization_options(
118 &self,
119 language_server_id: LanguageServerName,
120 language_name: LanguageName,
121 worktree: Arc<dyn WorktreeDelegate>,
122 ) -> Result<Option<String>> {
123 self.call(|extension, store| {
124 async move {
125 let resource = store.data_mut().table().push(worktree)?;
126 let options = extension
127 .call_language_server_initialization_options(
128 store,
129 &language_server_id,
130 &language_name,
131 resource,
132 )
133 .await?
134 .map_err(|err| store.data().extension_error(err))?;
135 anyhow::Ok(options)
136 }
137 .boxed()
138 })
139 .await?
140 }
141
142 async fn language_server_workspace_configuration(
143 &self,
144 language_server_id: LanguageServerName,
145 worktree: Arc<dyn WorktreeDelegate>,
146 ) -> Result<Option<String>> {
147 self.call(|extension, store| {
148 async move {
149 let resource = store.data_mut().table().push(worktree)?;
150 let options = extension
151 .call_language_server_workspace_configuration(
152 store,
153 &language_server_id,
154 resource,
155 )
156 .await?
157 .map_err(|err| store.data().extension_error(err))?;
158 anyhow::Ok(options)
159 }
160 .boxed()
161 })
162 .await?
163 }
164
165 async fn language_server_additional_initialization_options(
166 &self,
167 language_server_id: LanguageServerName,
168 target_language_server_id: LanguageServerName,
169 worktree: Arc<dyn WorktreeDelegate>,
170 ) -> Result<Option<String>> {
171 self.call(|extension, store| {
172 async move {
173 let resource = store.data_mut().table().push(worktree)?;
174 let options = extension
175 .call_language_server_additional_initialization_options(
176 store,
177 &language_server_id,
178 &target_language_server_id,
179 resource,
180 )
181 .await?
182 .map_err(|err| store.data().extension_error(err))?;
183 anyhow::Ok(options)
184 }
185 .boxed()
186 })
187 .await?
188 }
189
190 async fn language_server_additional_workspace_configuration(
191 &self,
192 language_server_id: LanguageServerName,
193 target_language_server_id: LanguageServerName,
194 worktree: Arc<dyn WorktreeDelegate>,
195 ) -> Result<Option<String>> {
196 self.call(|extension, store| {
197 async move {
198 let resource = store.data_mut().table().push(worktree)?;
199 let options = extension
200 .call_language_server_additional_workspace_configuration(
201 store,
202 &language_server_id,
203 &target_language_server_id,
204 resource,
205 )
206 .await?
207 .map_err(|err| store.data().extension_error(err))?;
208 anyhow::Ok(options)
209 }
210 .boxed()
211 })
212 .await?
213 }
214
215 async fn labels_for_completions(
216 &self,
217 language_server_id: LanguageServerName,
218 completions: Vec<Completion>,
219 ) -> Result<Vec<Option<CodeLabel>>> {
220 self.call(|extension, store| {
221 async move {
222 let labels = extension
223 .call_labels_for_completions(
224 store,
225 &language_server_id,
226 completions.into_iter().map(Into::into).collect(),
227 )
228 .await?
229 .map_err(|err| store.data().extension_error(err))?;
230
231 Ok(labels
232 .into_iter()
233 .map(|label| label.map(Into::into))
234 .collect())
235 }
236 .boxed()
237 })
238 .await?
239 }
240
241 async fn labels_for_symbols(
242 &self,
243 language_server_id: LanguageServerName,
244 symbols: Vec<Symbol>,
245 ) -> Result<Vec<Option<CodeLabel>>> {
246 self.call(|extension, store| {
247 async move {
248 let labels = extension
249 .call_labels_for_symbols(
250 store,
251 &language_server_id,
252 symbols.into_iter().map(Into::into).collect(),
253 )
254 .await?
255 .map_err(|err| store.data().extension_error(err))?;
256
257 Ok(labels
258 .into_iter()
259 .map(|label| label.map(Into::into))
260 .collect())
261 }
262 .boxed()
263 })
264 .await?
265 }
266
267 async fn complete_slash_command_argument(
268 &self,
269 command: SlashCommand,
270 arguments: Vec<String>,
271 ) -> Result<Vec<SlashCommandArgumentCompletion>> {
272 self.call(|extension, store| {
273 async move {
274 let completions = extension
275 .call_complete_slash_command_argument(store, &command.into(), &arguments)
276 .await?
277 .map_err(|err| store.data().extension_error(err))?;
278
279 Ok(completions.into_iter().map(Into::into).collect())
280 }
281 .boxed()
282 })
283 .await?
284 }
285
286 async fn run_slash_command(
287 &self,
288 command: SlashCommand,
289 arguments: Vec<String>,
290 delegate: Option<Arc<dyn WorktreeDelegate>>,
291 ) -> Result<SlashCommandOutput> {
292 self.call(|extension, store| {
293 async move {
294 let resource = if let Some(delegate) = delegate {
295 Some(store.data_mut().table().push(delegate)?)
296 } else {
297 None
298 };
299
300 let output = extension
301 .call_run_slash_command(store, &command.into(), &arguments, resource)
302 .await?
303 .map_err(|err| store.data().extension_error(err))?;
304
305 Ok(output.into())
306 }
307 .boxed()
308 })
309 .await?
310 }
311
312 async fn context_server_command(
313 &self,
314 context_server_id: Arc<str>,
315 project: Arc<dyn ProjectDelegate>,
316 ) -> Result<Command> {
317 self.call(|extension, store| {
318 async move {
319 let project_resource = store.data_mut().table().push(project)?;
320 let command = extension
321 .call_context_server_command(store, context_server_id.clone(), project_resource)
322 .await?
323 .map_err(|err| store.data().extension_error(err))?;
324 anyhow::Ok(command.into())
325 }
326 .boxed()
327 })
328 .await?
329 }
330
331 async fn context_server_configuration(
332 &self,
333 context_server_id: Arc<str>,
334 project: Arc<dyn ProjectDelegate>,
335 ) -> Result<Option<ContextServerConfiguration>> {
336 self.call(|extension, store| {
337 async move {
338 let project_resource = store.data_mut().table().push(project)?;
339 let Some(configuration) = extension
340 .call_context_server_configuration(
341 store,
342 context_server_id.clone(),
343 project_resource,
344 )
345 .await?
346 .map_err(|err| store.data().extension_error(err))?
347 else {
348 return Ok(None);
349 };
350
351 Ok(Some(configuration.try_into()?))
352 }
353 .boxed()
354 })
355 .await?
356 }
357
358 async fn suggest_docs_packages(&self, provider: Arc<str>) -> Result<Vec<String>> {
359 self.call(|extension, store| {
360 async move {
361 let packages = extension
362 .call_suggest_docs_packages(store, provider.as_ref())
363 .await?
364 .map_err(|err| store.data().extension_error(err))?;
365
366 Ok(packages)
367 }
368 .boxed()
369 })
370 .await?
371 }
372
373 async fn index_docs(
374 &self,
375 provider: Arc<str>,
376 package_name: Arc<str>,
377 kv_store: Arc<dyn KeyValueStoreDelegate>,
378 ) -> Result<()> {
379 self.call(|extension, store| {
380 async move {
381 let kv_store_resource = store.data_mut().table().push(kv_store)?;
382 extension
383 .call_index_docs(
384 store,
385 provider.as_ref(),
386 package_name.as_ref(),
387 kv_store_resource,
388 )
389 .await?
390 .map_err(|err| store.data().extension_error(err))?;
391
392 anyhow::Ok(())
393 }
394 .boxed()
395 })
396 .await?
397 }
398
399 async fn get_dap_binary(
400 &self,
401 dap_name: Arc<str>,
402 config: DebugTaskDefinition,
403 user_installed_path: Option<PathBuf>,
404 worktree: Arc<dyn WorktreeDelegate>,
405 ) -> Result<DebugAdapterBinary> {
406 self.call(|extension, store| {
407 async move {
408 let resource = store.data_mut().table().push(worktree)?;
409 let dap_binary = extension
410 .call_get_dap_binary(store, dap_name, config, user_installed_path, resource)
411 .await?
412 .map_err(|err| store.data().extension_error(err))?;
413 let dap_binary = dap_binary.try_into()?;
414 Ok(dap_binary)
415 }
416 .boxed()
417 })
418 .await?
419 }
420 async fn dap_request_kind(
421 &self,
422 dap_name: Arc<str>,
423 config: serde_json::Value,
424 ) -> Result<StartDebuggingRequestArgumentsRequest> {
425 self.call(|extension, store| {
426 async move {
427 let kind = extension
428 .call_dap_request_kind(store, dap_name, config)
429 .await?
430 .map_err(|err| store.data().extension_error(err))?;
431 Ok(kind.into())
432 }
433 .boxed()
434 })
435 .await?
436 }
437
438 async fn dap_config_to_scenario(&self, config: ZedDebugConfig) -> Result<DebugScenario> {
439 self.call(|extension, store| {
440 async move {
441 let kind = extension
442 .call_dap_config_to_scenario(store, config)
443 .await?
444 .map_err(|err| store.data().extension_error(err))?;
445 Ok(kind)
446 }
447 .boxed()
448 })
449 .await?
450 }
451
452 async fn dap_locator_create_scenario(
453 &self,
454 locator_name: String,
455 build_config_template: TaskTemplate,
456 resolved_label: String,
457 debug_adapter_name: String,
458 ) -> Result<Option<DebugScenario>> {
459 self.call(|extension, store| {
460 async move {
461 extension
462 .call_dap_locator_create_scenario(
463 store,
464 locator_name,
465 build_config_template,
466 resolved_label,
467 debug_adapter_name,
468 )
469 .await
470 }
471 .boxed()
472 })
473 .await?
474 }
475 async fn run_dap_locator(
476 &self,
477 locator_name: String,
478 config: SpawnInTerminal,
479 ) -> Result<DebugRequest> {
480 self.call(|extension, store| {
481 async move {
482 extension
483 .call_run_dap_locator(store, locator_name, config)
484 .await?
485 .map_err(|err| store.data().extension_error(err))
486 }
487 .boxed()
488 })
489 .await?
490 }
491}
492
493pub struct WasmState {
494 manifest: Arc<ExtensionManifest>,
495 pub table: ResourceTable,
496 ctx: wasi::WasiCtx,
497 pub host: Arc<WasmHost>,
498 pub(crate) capability_granter: CapabilityGranter,
499}
500
501std::thread_local! {
502 /// Used by the crash handler to ignore panics in extension-related threads.
503 pub static IS_WASM_THREAD: AtomicBool = const { AtomicBool::new(false) };
504}
505
506type MainThreadCall = Box<dyn Send + for<'a> FnOnce(&'a mut AsyncApp) -> LocalBoxFuture<'a, ()>>;
507
508type ExtensionCall = Box<
509 dyn Send + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, ()>,
510>;
511
512fn wasm_engine(executor: &BackgroundExecutor) -> wasmtime::Engine {
513 static WASM_ENGINE: OnceLock<wasmtime::Engine> = OnceLock::new();
514 WASM_ENGINE
515 .get_or_init(|| {
516 let mut config = wasmtime::Config::new();
517 config.wasm_component_model(true);
518 config.async_support(true);
519 config
520 .enable_incremental_compilation(cache_store())
521 .unwrap();
522 // Async support introduces the issue that extension execution happens during `Future::poll`,
523 // which could block an async thread.
524 // https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#execution-in-poll
525 //
526 // Epoch interruption is a lightweight mechanism to allow the extensions to yield control
527 // back to the executor at regular intervals.
528 config.epoch_interruption(true);
529
530 let engine = wasmtime::Engine::new(&config).unwrap();
531
532 // It might be safer to do this on a non-async thread to make sure it makes progress
533 // regardless of if extensions are blocking.
534 // However, due to our current setup, this isn't a likely occurrence and we'd rather
535 // not have a dedicated thread just for this. If it becomes an issue, we can consider
536 // creating a separate thread for epoch interruption.
537 let engine_ref = engine.weak();
538 executor
539 .spawn(async move {
540 IS_WASM_THREAD.with(|v| v.store(true, Ordering::Release));
541 // Somewhat arbitrary interval, as it isn't a guaranteed interval.
542 // But this is a rough upper bound for how long the extension execution can block on
543 // `Future::poll`.
544 const EPOCH_INTERVAL: Duration = Duration::from_millis(100);
545 let mut timer = Timer::interval(EPOCH_INTERVAL);
546 while (timer.next().await).is_some() {
547 // Exit the loop and thread once the engine is dropped.
548 let Some(engine) = engine_ref.upgrade() else {
549 break;
550 };
551 engine.increment_epoch();
552 }
553 })
554 .detach();
555
556 engine
557 })
558 .clone()
559}
560
561fn cache_store() -> Arc<IncrementalCompilationCache> {
562 static CACHE_STORE: LazyLock<Arc<IncrementalCompilationCache>> =
563 LazyLock::new(|| Arc::new(IncrementalCompilationCache::new()));
564 CACHE_STORE.clone()
565}
566
567impl WasmHost {
568 pub fn new(
569 fs: Arc<dyn Fs>,
570 http_client: Arc<dyn HttpClient>,
571 node_runtime: NodeRuntime,
572 proxy: Arc<ExtensionHostProxy>,
573 work_dir: PathBuf,
574 cx: &mut App,
575 ) -> Arc<Self> {
576 let (tx, mut rx) = mpsc::unbounded::<MainThreadCall>();
577 let task = cx.spawn(async move |cx| {
578 while let Some(message) = rx.next().await {
579 message(cx).await;
580 }
581 });
582
583 let extension_settings = ExtensionSettings::get_global(cx);
584
585 Arc::new(Self {
586 engine: wasm_engine(cx.background_executor()),
587 fs,
588 work_dir,
589 http_client,
590 node_runtime,
591 proxy,
592 release_channel: ReleaseChannel::global(cx),
593 granted_capabilities: extension_settings.granted_capabilities.clone(),
594 _main_thread_message_task: task,
595 main_thread_message_tx: tx,
596 })
597 }
598
599 pub fn load_extension(
600 self: &Arc<Self>,
601 wasm_bytes: Vec<u8>,
602 manifest: &Arc<ExtensionManifest>,
603 cx: &AsyncApp,
604 ) -> Task<Result<WasmExtension>> {
605 let this = self.clone();
606 let manifest = manifest.clone();
607 let executor = cx.background_executor().clone();
608 let load_extension_task = async move {
609 let zed_api_version = parse_wasm_extension_version(&manifest.id, &wasm_bytes)?;
610
611 let component = Component::from_binary(&this.engine, &wasm_bytes)
612 .context("failed to compile wasm component")?;
613 let mut store = wasmtime::Store::new(
614 &this.engine,
615 WasmState {
616 ctx: this.build_wasi_ctx(&manifest).await?,
617 manifest: manifest.clone(),
618 table: ResourceTable::new(),
619 host: this.clone(),
620 capability_granter: CapabilityGranter::new(
621 this.granted_capabilities.clone(),
622 manifest.clone(),
623 ),
624 },
625 );
626 // Store will yield after 1 tick, and get a new deadline of 1 tick after each yield.
627 store.set_epoch_deadline(1);
628 store.epoch_deadline_async_yield_and_update(1);
629
630 let mut extension = Extension::instantiate_async(
631 &executor,
632 &mut store,
633 this.release_channel,
634 zed_api_version,
635 &component,
636 )
637 .await?;
638
639 extension
640 .call_init_extension(&mut store)
641 .await
642 .context("failed to initialize wasm extension")?;
643
644 let (tx, mut rx) = mpsc::unbounded::<ExtensionCall>();
645 let extension_task = async move {
646 while let Some(call) = rx.next().await {
647 (call)(&mut extension, &mut store).await;
648 }
649 };
650
651 anyhow::Ok((
652 extension_task,
653 manifest.clone(),
654 this.work_dir.join(manifest.id.as_ref()).into(),
655 tx,
656 zed_api_version,
657 ))
658 };
659 cx.spawn(async move |cx| {
660 let (extension_task, manifest, work_dir, tx, zed_api_version) =
661 load_extension_task.await?;
662 // we need to run run the task in an extension context as wasmtime_wasi may
663 // call into tokio, accessing its runtime handle
664 let task = Arc::new(gpui_tokio::Tokio::spawn(cx, extension_task)?);
665
666 Ok(WasmExtension {
667 manifest,
668 work_dir,
669 tx,
670 zed_api_version,
671 _task: task,
672 })
673 })
674 }
675
676 async fn build_wasi_ctx(&self, manifest: &Arc<ExtensionManifest>) -> Result<wasi::WasiCtx> {
677 let extension_work_dir = self.work_dir.join(manifest.id.as_ref());
678 self.fs
679 .create_dir(&extension_work_dir)
680 .await
681 .context("failed to create extension work dir")?;
682
683 let file_perms = wasi::FilePerms::all();
684 let dir_perms = wasi::DirPerms::all();
685 let path = SanitizedPath::new(&extension_work_dir).to_string();
686 #[cfg(target_os = "windows")]
687 let path = path.replace('\\', "/");
688
689 let mut ctx = wasi::WasiCtxBuilder::new();
690 ctx.inherit_stdio()
691 .env("PWD", &path)
692 .env("RUST_BACKTRACE", "full");
693
694 ctx.preopened_dir(&path, ".", dir_perms, file_perms)?;
695 ctx.preopened_dir(&path, &path, dir_perms, file_perms)?;
696
697 Ok(ctx.build())
698 }
699
700 pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Result<PathBuf> {
701 let extension_work_dir = self.work_dir.join(id.as_ref());
702 let path = normalize_path(&extension_work_dir.join(path));
703 anyhow::ensure!(
704 path.starts_with(&extension_work_dir),
705 "cannot write to path {path:?}",
706 );
707 Ok(path)
708 }
709}
710
711pub fn parse_wasm_extension_version(
712 extension_id: &str,
713 wasm_bytes: &[u8],
714) -> Result<SemanticVersion> {
715 let mut version = None;
716
717 for part in wasmparser::Parser::new(0).parse_all(wasm_bytes) {
718 if let wasmparser::Payload::CustomSection(s) =
719 part.context("error parsing wasm extension")?
720 && s.name() == "zed:api-version"
721 {
722 version = parse_wasm_extension_version_custom_section(s.data());
723 if version.is_none() {
724 bail!(
725 "extension {} has invalid zed:api-version section: {:?}",
726 extension_id,
727 s.data()
728 );
729 }
730 }
731 }
732
733 // The reason we wait until we're done parsing all of the Wasm bytes to return the version
734 // is to work around a panic that can happen inside of Wasmtime when the bytes are invalid.
735 //
736 // By parsing the entirety of the Wasm bytes before we return, we're able to detect this problem
737 // earlier as an `Err` rather than as a panic.
738 version.with_context(|| format!("extension {extension_id} has no zed:api-version section"))
739}
740
741fn parse_wasm_extension_version_custom_section(data: &[u8]) -> Option<SemanticVersion> {
742 if data.len() == 6 {
743 Some(SemanticVersion::new(
744 u16::from_be_bytes([data[0], data[1]]) as _,
745 u16::from_be_bytes([data[2], data[3]]) as _,
746 u16::from_be_bytes([data[4], data[5]]) as _,
747 ))
748 } else {
749 None
750 }
751}
752
753impl WasmExtension {
754 pub async fn load(
755 extension_dir: &Path,
756 manifest: &Arc<ExtensionManifest>,
757 wasm_host: Arc<WasmHost>,
758 cx: &AsyncApp,
759 ) -> Result<Self> {
760 let path = extension_dir.join("extension.wasm");
761
762 let mut wasm_file = wasm_host
763 .fs
764 .open_sync(&path)
765 .await
766 .context("failed to open wasm file")?;
767
768 let mut wasm_bytes = Vec::new();
769 wasm_file
770 .read_to_end(&mut wasm_bytes)
771 .context("failed to read wasm")?;
772
773 wasm_host
774 .load_extension(wasm_bytes, manifest, cx)
775 .await
776 .with_context(|| format!("failed to load wasm extension {}", manifest.id))
777 }
778
779 pub async fn call<T, Fn>(&self, f: Fn) -> Result<T>
780 where
781 T: 'static + Send,
782 Fn: 'static
783 + Send
784 + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, T>,
785 {
786 let (return_tx, return_rx) = oneshot::channel();
787 self.tx
788 .unbounded_send(Box::new(move |extension, store| {
789 async {
790 let result = f(extension, store).await;
791 return_tx.send(result).ok();
792 }
793 .boxed()
794 }))
795 .map_err(|_| {
796 anyhow!(
797 "wasm extension channel should not be closed yet, extension {} (id {})",
798 self.manifest.name,
799 self.manifest.id,
800 )
801 })?;
802 return_rx.await.with_context(|| {
803 format!(
804 "wasm extension channel, extension {} (id {})",
805 self.manifest.name, self.manifest.id,
806 )
807 })
808 }
809}
810
811impl WasmState {
812 fn on_main_thread<T, Fn>(&self, f: Fn) -> impl 'static + Future<Output = T>
813 where
814 T: 'static + Send,
815 Fn: 'static + Send + for<'a> FnOnce(&'a mut AsyncApp) -> LocalBoxFuture<'a, T>,
816 {
817 let (return_tx, return_rx) = oneshot::channel();
818 self.host
819 .main_thread_message_tx
820 .clone()
821 .unbounded_send(Box::new(move |cx| {
822 async {
823 let result = f(cx).await;
824 return_tx.send(result).ok();
825 }
826 .boxed_local()
827 }))
828 .unwrap_or_else(|_| {
829 panic!(
830 "main thread message channel should not be closed yet, extension {} (id {})",
831 self.manifest.name, self.manifest.id,
832 )
833 });
834 let name = self.manifest.name.clone();
835 let id = self.manifest.id.clone();
836 async move {
837 return_rx.await.unwrap_or_else(|_| {
838 panic!("main thread message channel, extension {name} (id {id})")
839 })
840 }
841 }
842
843 fn work_dir(&self) -> PathBuf {
844 self.host.work_dir.join(self.manifest.id.as_ref())
845 }
846
847 fn extension_error(&self, message: String) -> anyhow::Error {
848 anyhow!(
849 "from extension \"{}\" version {}: {}",
850 self.manifest.name,
851 self.manifest.version,
852 message
853 )
854 }
855}
856
857impl wasi::WasiView for WasmState {
858 fn table(&mut self) -> &mut ResourceTable {
859 &mut self.table
860 }
861
862 fn ctx(&mut self) -> &mut wasi::WasiCtx {
863 &mut self.ctx
864 }
865}
866
867/// Wrapper around a mini-moka bounded cache for storing incremental compilation artifacts.
868/// Since wasm modules have many similar elements, this can save us a lot of work at the
869/// cost of a small memory footprint. However, we don't want this to be unbounded, so we use
870/// a LFU/LRU cache to evict less used cache entries.
871#[derive(Debug)]
872struct IncrementalCompilationCache {
873 cache: Cache<Vec<u8>, Vec<u8>>,
874}
875
876impl IncrementalCompilationCache {
877 fn new() -> Self {
878 let cache = Cache::builder()
879 // Cap this at 32 MB for now. Our extensions turn into roughly 512kb in the cache,
880 // which means we could store 64 completely novel extensions in the cache, but in
881 // practice we will more than that, which is more than enough for our use case.
882 .max_capacity(32 * 1024 * 1024)
883 .weigher(|k: &Vec<u8>, v: &Vec<u8>| (k.len() + v.len()).try_into().unwrap_or(u32::MAX))
884 .build();
885 Self { cache }
886 }
887}
888
889impl CacheStore for IncrementalCompilationCache {
890 fn get(&self, key: &[u8]) -> Option<Cow<'_, [u8]>> {
891 self.cache.get(key).map(|v| v.into())
892 }
893
894 fn insert(&self, key: &[u8], value: Vec<u8>) -> bool {
895 self.cache.insert(key.to_vec(), value);
896 true
897 }
898}