wasm_host.rs

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