wasm_host.rs

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