Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from Wasmer to Wasmtime #3349

Merged
merged 10 commits into from
Jun 28, 2024
Next Next commit
Remove ForeignFunctionEnv wrapper around PluginEnv
This will enable PluginEnv to be the Store context when migrating to
Wasmtime.
  • Loading branch information
bjorn3 committed Jun 14, 2024
commit 7aa6462a1ea87abe09025f14c69de5693dd87b1b
80 changes: 21 additions & 59 deletions zellij-server/src/plugins/plugin_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::get_store;
use crate::plugins::plugin_map::{PluginEnv, PluginMap, RunningPlugin, Subscriptions};
use crate::plugins::plugin_map::{PluginEnv, PluginMap, RunningPlugin};
use crate::plugins::plugin_worker::{plugin_worker, RunningWorker};
use crate::plugins::zellij_exports::{wasi_write_object, zellij_exports};
use crate::plugins::PluginId;
Expand Down Expand Up @@ -115,14 +115,8 @@ impl<'a> PluginLoader<'a> {
plugin_loader
.load_module_from_memory()
.and_then(|module| plugin_loader.create_plugin_environment(module))
.and_then(|(store, instance, plugin_env, subscriptions)| {
plugin_loader.load_plugin_instance(
store,
&instance,
&plugin_env,
&plugin_map,
&subscriptions,
)
.and_then(|(store, instance, plugin_env)| {
plugin_loader.load_plugin_instance(store, &instance, &plugin_env, &plugin_map)
})
.and_then(|_| {
plugin_loader.clone_instance_for_other_clients(&connected_clients, &plugin_map)
Expand Down Expand Up @@ -178,14 +172,8 @@ impl<'a> PluginLoader<'a> {
plugin_loader
.compile_module()
.and_then(|module| plugin_loader.create_plugin_environment(module))
.and_then(|(store, instance, plugin_env, subscriptions)| {
plugin_loader.load_plugin_instance(
store,
&instance,
&plugin_env,
&plugin_map,
&subscriptions,
)
.and_then(|(store, instance, plugin_env)| {
plugin_loader.load_plugin_instance(store, &instance, &plugin_env, &plugin_map)
})
.and_then(|_| {
plugin_loader.clone_instance_for_other_clients(
Expand All @@ -200,14 +188,8 @@ impl<'a> PluginLoader<'a> {
.or_else(|_e| plugin_loader.load_module_from_hd_cache())
.or_else(|_e| plugin_loader.compile_module())
.and_then(|module| plugin_loader.create_plugin_environment(module))
.and_then(|(store, instance, plugin_env, subscriptions)| {
plugin_loader.load_plugin_instance(
store,
&instance,
&plugin_env,
&plugin_map,
&subscriptions,
)
.and_then(|(store, instance, plugin_env)| {
plugin_loader.load_plugin_instance(store, &instance, &plugin_env, &plugin_map)
})
.and_then(|_| {
plugin_loader.clone_instance_for_other_clients(
Expand Down Expand Up @@ -263,14 +245,8 @@ impl<'a> PluginLoader<'a> {
plugin_loader
.load_module_from_memory()
.and_then(|module| plugin_loader.create_plugin_environment(module))
.and_then(|(store, instance, plugin_env, subscriptions)| {
plugin_loader.load_plugin_instance(
store,
&instance,
&plugin_env,
&plugin_map,
&subscriptions,
)
.and_then(|(store, instance, plugin_env)| {
plugin_loader.load_plugin_instance(store, &instance, &plugin_env, &plugin_map)
})?
}
connected_clients.lock().unwrap().push(client_id);
Expand Down Expand Up @@ -323,14 +299,8 @@ impl<'a> PluginLoader<'a> {
plugin_loader
.compile_module()
.and_then(|module| plugin_loader.create_plugin_environment(module))
.and_then(|(store, instance, plugin_env, subscriptions)| {
plugin_loader.load_plugin_instance(
store,
&instance,
&plugin_env,
&plugin_map,
&subscriptions,
)
.and_then(|(store, instance, plugin_env)| {
plugin_loader.load_plugin_instance(store, &instance, &plugin_env, &plugin_map)
})
.and_then(|_| {
plugin_loader.clone_instance_for_other_clients(&connected_clients, &plugin_map)
Expand Down Expand Up @@ -584,16 +554,15 @@ impl<'a> PluginLoader<'a> {
pub fn create_plugin_environment(
&mut self,
module: Module,
) -> Result<(Store, Instance, PluginEnv, Arc<Mutex<Subscriptions>>)> {
let (store, instance, plugin_env, subscriptions) =
self.create_plugin_instance_env_and_subscriptions(&module)?;
) -> Result<(Store, Instance, PluginEnv)> {
let (store, instance, plugin_env) = self.create_plugin_instance_env(&module)?;
// Only do an insert when everything went well!
let cloned_plugin = self.plugin.clone();
self.plugin_cache
.lock()
.unwrap()
.insert(cloned_plugin.path, module);
Ok((store, instance, plugin_env, subscriptions))
Ok((store, instance, plugin_env))
}
pub fn create_plugin_instance_and_wasi_env_for_worker(
&mut self,
Expand All @@ -611,8 +580,7 @@ impl<'a> PluginLoader<'a> {
.get(&self.plugin.path)
.with_context(err_context)?
.clone();
let (store, instance, plugin_env, _subscriptions) =
self.create_plugin_instance_env_and_subscriptions(&module)?;
let (store, instance, plugin_env) = self.create_plugin_instance_env(&module)?;
Ok((store, instance, plugin_env))
}
pub fn load_plugin_instance(
Expand All @@ -621,7 +589,6 @@ impl<'a> PluginLoader<'a> {
instance: &Instance,
plugin_env: &PluginEnv,
plugin_map: &Arc<Mutex<PluginMap>>,
subscriptions: &Arc<Mutex<Subscriptions>>,
) -> Result<()> {
let err_context = || format!("failed to load plugin from instance {instance:#?}");
let main_user_instance = instance.clone();
Expand Down Expand Up @@ -673,7 +640,7 @@ impl<'a> PluginLoader<'a> {
self.plugin_id,
self.client_id,
plugin.clone(),
subscriptions.clone(),
plugin_env.subscriptions.clone(),
workers,
);

Expand Down Expand Up @@ -757,13 +724,12 @@ impl<'a> PluginLoader<'a> {
plugin_loader_for_client
.load_module_from_memory()
.and_then(|module| plugin_loader_for_client.create_plugin_environment(module))
.and_then(|(store, instance, plugin_env, subscriptions)| {
.and_then(|(store, instance, plugin_env)| {
plugin_loader_for_client.load_plugin_instance(
store,
&instance,
&plugin_env,
plugin_map,
&subscriptions,
)
})?
}
Expand Down Expand Up @@ -799,10 +765,7 @@ impl<'a> PluginLoader<'a> {
},
}
}
fn create_plugin_instance_env_and_subscriptions(
&self,
module: &Module,
) -> Result<(Store, Instance, PluginEnv, Arc<Mutex<Subscriptions>>)> {
fn create_plugin_instance_env(&self, module: &Module) -> Result<(Store, Instance, PluginEnv)> {
let err_context = || {
format!(
"Failed to create instance, plugin env and subscriptions for plugin {}",
Expand Down Expand Up @@ -862,18 +825,17 @@ impl<'a> PluginLoader<'a> {
input_pipes_to_unblock: Arc::new(Mutex::new(HashSet::new())),
input_pipes_to_block: Arc::new(Mutex::new(HashSet::new())),
layout_dir: self.layout_dir.clone(),
subscriptions: Arc::new(Mutex::new(HashSet::new())),
};

let subscriptions = Arc::new(Mutex::new(HashSet::new()));

let mut zellij = zellij_exports(store_mut, &plugin_env, &subscriptions);
let mut zellij = zellij_exports(store_mut, &plugin_env);
zellij.extend(&wasi);

let instance = Instance::new(store_mut, &module, &zellij).with_context(err_context)?;

wasi_env.initialize(store_mut, &instance)?;

Ok((store, instance, plugin_env, subscriptions))
Ok((store, instance, plugin_env))
}
}

Expand Down
1 change: 1 addition & 0 deletions zellij-server/src/plugins/plugin_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ pub struct PluginEnv {
pub plugin_cwd: PathBuf,
pub input_pipes_to_unblock: Arc<Mutex<HashSet<String>>>,
pub input_pipes_to_block: Arc<Mutex<HashSet<String>>>,
pub subscriptions: Arc<Mutex<Subscriptions>>,
}

impl PluginEnv {
Expand Down
Loading