Use enum to differentiate between normal and precompiled plugins

This commit is contained in:
Isaac Clayton 2022-07-13 14:26:52 +02:00
parent daf1674ca6
commit a6edf85078
3 changed files with 18 additions and 16 deletions

View file

@ -46,10 +46,9 @@ mod tests {
.map(|output| output.stdout) .map(|output| output.stdout)
}) })
.unwrap() .unwrap()
.init( .init(PluginBinary::Wasm(
false, include_bytes!("../../../plugins/bin/test_plugin.wasm").as_ref(),
include_bytes!("../../../plugins/bin/test_plugin.wasm"), ))
)
.await .await
.unwrap(); .unwrap();

View file

@ -345,8 +345,8 @@ impl PluginBuilder {
/// Initializes a [`Plugin`] from a given compiled Wasm module. /// Initializes a [`Plugin`] from a given compiled Wasm module.
/// Both binary (`.wasm`) and text (`.wat`) module formats are supported. /// Both binary (`.wasm`) and text (`.wat`) module formats are supported.
pub async fn init<T: AsRef<[u8]>>(self, precompiled: bool, module: T) -> Result<Plugin, Error> { pub async fn init<'a>(self, binary: PluginBinary<'a>) -> Result<Plugin, Error> {
Plugin::init(precompiled, module.as_ref(), self).await Plugin::init(binary, self).await
} }
} }
@ -379,6 +379,11 @@ impl WasiCtxAlloc {
} }
} }
pub enum PluginBinary<'a> {
Wasm(&'a [u8]),
Precompiled(&'a [u8]),
}
/// Represents a WebAssembly plugin, with access to the WebAssembly System Inferface. /// Represents a WebAssembly plugin, with access to the WebAssembly System Inferface.
/// Build a new plugin using [`PluginBuilder`]. /// Build a new plugin using [`PluginBuilder`].
pub struct Plugin { pub struct Plugin {
@ -406,7 +411,7 @@ impl Plugin {
println!(); println!();
} }
async fn init(precompiled: bool, module: &[u8], plugin: PluginBuilder) -> Result<Self, Error> { async fn init<'a>(binary: PluginBinary<'a>, plugin: PluginBuilder) -> Result<Self, Error> {
// initialize the WebAssembly System Interface context // initialize the WebAssembly System Interface context
let engine = plugin.engine; let engine = plugin.engine;
let mut linker = plugin.linker; let mut linker = plugin.linker;
@ -422,10 +427,9 @@ impl Plugin {
}, },
); );
let module = if precompiled { let module = match binary {
unsafe { Module::deserialize(&engine, module)? } PluginBinary::Precompiled(bytes) => unsafe { Module::deserialize(&engine, bytes)? },
} else { PluginBinary::Wasm(bytes) => Module::new(&engine, bytes)?,
Module::new(&engine, module)?
}; };
// set up automatic yielding based on configuration // set up automatic yielding based on configuration

View file

@ -4,7 +4,7 @@ use client::http::HttpClient;
use futures::lock::Mutex; use futures::lock::Mutex;
use gpui::executor::Background; use gpui::executor::Background;
use language::{LanguageServerName, LspAdapter}; use language::{LanguageServerName, LspAdapter};
use plugin_runtime::{Plugin, PluginBuilder, PluginYield, WasiFn}; use plugin_runtime::{Plugin, PluginBinary, PluginBuilder, PluginYield, WasiFn};
use std::{any::Any, path::PathBuf, sync::Arc}; use std::{any::Any, path::PathBuf, sync::Arc};
use util::ResultExt; use util::ResultExt;
@ -24,10 +24,9 @@ pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
.log_err() .log_err()
.map(|output| output.stdout) .map(|output| output.stdout)
})? })?
.init( .init(PluginBinary::Precompiled(include_bytes!(
true, "../../../../plugins/bin/json_language.wasm.pre"
include_bytes!("../../../../plugins/bin/json_language.wasm.pre"), )))
)
.await?; .await?;
PluginLspAdapter::new(plugin, executor).await PluginLspAdapter::new(plugin, executor).await