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