mirror of
https://github.com/zellij-org/zellij.git
synced 2024-11-22 04:33:22 +03:00
dependencies: switch from Wasmer to Wasmtime (#3349)
* Remove ForeignFunctionEnv wrapper around PluginEnv This will enable PluginEnv to be the Store context when migrating to Wasmtime. * Pass PluginEnv by value to load_plugin_instance This will allow removing the Clone impl from PluginEnv when migrating to Wasmtime as required by the missing Clone impl on Wasmtime's WasiCtx. * Avoid passing a Store around when an Engine is enough * Pass PluginEnv to the wasi read/write functions Wasmtime requires storing the read/write end of the pipe outside of the WasiCtx. Passing PluginEnv to these functions allows storing them in the PluginEnv. * Migrate to Wasmtime * Switch from wasi-common to wasmtime-wasi * Reduce verbosity of wasmtime_wasi logs * Increase startup delay To wait for all plugins to be compiled. * Disable some wasmtime features * Update to Wasmtime 21.0.1
This commit is contained in:
parent
fa110515aa
commit
7d7848cddc
@ -92,7 +92,7 @@ Note that the output is truncated at 100KB. This can be adjusted for the purpose
|
||||
When running Zellij with the `--debug` flag, Zellij will dump a copy of all bytes received over the pty for each pane in: `/$temp_dir/zellij-<UID>/zellij-log/zellij-<pane_id>.log`. These might be useful when troubleshooting terminal issues.
|
||||
|
||||
## Testing plugins
|
||||
Zellij allows the use of the [Singlepass](https://crates.io/crates/wasmer-compiler-singlepass) compiler for wasmer. This can enable great gains in compilation time of plugins in detriment of stability, notably on Arm64 architectures.
|
||||
Zellij allows the use of the singlepass [Winch](https://crates.io/crates/wasmtime-winch) compiler for wasmtime. This can enable great gains in compilation time of plugins at the cost of slower execution and less supported architectures.
|
||||
|
||||
To enable the singlepass compiler, use the `singlepass` flag. E.g.:
|
||||
```sh
|
||||
|
1843
Cargo.lock
generated
1843
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -84,7 +84,7 @@ fn start_zellij(channel: &mut ssh2::Channel) {
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn start_zellij_mirrored_session(channel: &mut ssh2::Channel) {
|
||||
@ -99,7 +99,7 @@ fn start_zellij_mirrored_session(channel: &mut ssh2::Channel) {
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn start_zellij_mirrored_session_with_layout(channel: &mut ssh2::Channel, layout_file_name: &str) {
|
||||
@ -118,7 +118,7 @@ fn start_zellij_mirrored_session_with_layout(channel: &mut ssh2::Channel, layout
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn start_zellij_mirrored_session_with_layout_and_viewport_serialization(
|
||||
@ -140,7 +140,7 @@ fn start_zellij_mirrored_session_with_layout_and_viewport_serialization(
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn start_zellij_in_session(channel: &mut ssh2::Channel, session_name: &str, mirrored: bool) {
|
||||
@ -159,7 +159,7 @@ fn start_zellij_in_session(channel: &mut ssh2::Channel, session_name: &str, mirr
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn attach_to_existing_session(channel: &mut ssh2::Channel, session_name: &str) {
|
||||
@ -173,7 +173,7 @@ fn attach_to_existing_session(channel: &mut ssh2::Channel, session_name: &str) {
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn start_zellij_without_frames(channel: &mut ssh2::Channel) {
|
||||
@ -188,7 +188,7 @@ fn start_zellij_without_frames(channel: &mut ssh2::Channel) {
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn start_zellij_with_config(channel: &mut ssh2::Channel, config_path: &str) {
|
||||
@ -207,7 +207,7 @@ fn start_zellij_with_config(channel: &mut ssh2::Channel, config_path: &str) {
|
||||
)
|
||||
.unwrap();
|
||||
channel.flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
std::thread::sleep(std::time::Duration::from_secs(3)); // wait until Zellij stops parsing startup ANSI codes from the terminal STDIN
|
||||
}
|
||||
|
||||
fn read_from_channel(
|
||||
|
@ -14,12 +14,12 @@ ansi_term = "0.12.1"
|
||||
async-trait = "0.1.50"
|
||||
base64 = "0.13.0"
|
||||
byteorder = "1.4.3"
|
||||
bytes = "1.6.0"
|
||||
daemonize = "0.5"
|
||||
serde_json = "1.0"
|
||||
unicode-width = "0.1.8"
|
||||
url = "2.2.2"
|
||||
wasmer = "3.1.1"
|
||||
wasmer-wasi = "3.1.1"
|
||||
wasmtime-wasi = "21.0.1" # Keep in sync with wasmtime
|
||||
cassowary = "0.3.0"
|
||||
zellij-utils = { path = "../zellij-utils/", version = "0.41.0" }
|
||||
log = "0.4.17"
|
||||
@ -33,10 +33,26 @@ arrayvec = "0.7.2"
|
||||
uuid = { version = "1.4.1", features = ["serde", "v4"] }
|
||||
semver = "0.11.0"
|
||||
|
||||
[dependencies.wasmtime]
|
||||
version = "21.0.1" # Keep in sync with wasmtime-wasi
|
||||
default-features = false
|
||||
features = [
|
||||
'async',
|
||||
'cache',
|
||||
'parallel-compilation',
|
||||
'cranelift',
|
||||
'demangle',
|
||||
'addr2line',
|
||||
'debug-builtins',
|
||||
'runtime',
|
||||
'component-model',
|
||||
'std',
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
insta = "1.6.0"
|
||||
tempfile = "3.2.0"
|
||||
wasmer = { version = "3.1.1", features = ["singlepass"] }
|
||||
wasmtime = { version = "21.0.1", features = ["winch"] } # Keep in sync with the other wasmtime dep
|
||||
|
||||
[features]
|
||||
singlepass = ["wasmer/singlepass"]
|
||||
singlepass = ["wasmtime/winch"]
|
||||
|
@ -28,7 +28,7 @@ use zellij_utils::envs;
|
||||
use zellij_utils::nix::sys::stat::{umask, Mode};
|
||||
use zellij_utils::pane_size::Size;
|
||||
|
||||
use wasmer::Store;
|
||||
use wasmtime::{Config, Engine, Strategy};
|
||||
|
||||
use crate::{
|
||||
os_input_output::ServerOsApi,
|
||||
@ -1069,7 +1069,7 @@ fn init_session(
|
||||
Some(&to_background_jobs),
|
||||
None,
|
||||
);
|
||||
let store = get_store();
|
||||
let engine = get_engine();
|
||||
|
||||
let layout = layout.clone();
|
||||
let client_attributes = client_attributes.clone();
|
||||
@ -1079,7 +1079,7 @@ fn init_session(
|
||||
move || {
|
||||
plugin_thread_main(
|
||||
plugin_bus,
|
||||
store,
|
||||
engine,
|
||||
data_dir,
|
||||
layout,
|
||||
layout_dir,
|
||||
@ -1162,22 +1162,13 @@ fn init_session(
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "singlepass"))]
|
||||
fn get_store() -> Store {
|
||||
use wasmer::{BaseTunables, Cranelift, Engine, Pages, Target};
|
||||
fn get_engine() -> Engine {
|
||||
log::info!("Compiling plugins using Cranelift");
|
||||
|
||||
// workaround for https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-ff4p-7xrq-q5r8
|
||||
let mut tunables = BaseTunables::for_target(&Target::default());
|
||||
tunables.static_memory_bound = Pages(0);
|
||||
let compiler = Cranelift::default();
|
||||
let mut engine: Engine = compiler.into();
|
||||
engine.set_tunables(tunables);
|
||||
|
||||
Store::new(engine)
|
||||
Engine::new(Config::new().strategy(Strategy::Cranelift)).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(feature = "singlepass")]
|
||||
fn get_store() -> Store {
|
||||
fn get_engine() -> Engine {
|
||||
log::info!("Compiling plugins using Singlepass");
|
||||
Store::new(wasmer::Singlepass::default())
|
||||
Engine::new(Config::new().strategy(Strategy::Winch)).unwrap()
|
||||
}
|
||||
|
@ -1,11 +1,7 @@
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
io::{Read, Seek, Write},
|
||||
};
|
||||
use std::{collections::VecDeque, io::Write};
|
||||
|
||||
use crate::plugins::PluginId;
|
||||
use log::{debug, error};
|
||||
use wasmer_wasi::{WasiFile, WasiFsError};
|
||||
use zellij_utils::{errors::prelude::*, serde};
|
||||
|
||||
use chrono::prelude::*;
|
||||
@ -41,15 +37,6 @@ impl LoggingPipe {
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for LoggingPipe {
|
||||
fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Can not reed from a LoggingPipe",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for LoggingPipe {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
if self.buffer.len() + buf.len() > ZELLIJ_MAX_PIPE_BUFFER_SIZE {
|
||||
@ -106,40 +93,6 @@ impl Write for LoggingPipe {
|
||||
}
|
||||
}
|
||||
|
||||
impl Seek for LoggingPipe {
|
||||
fn seek(&mut self, _pos: std::io::SeekFrom) -> std::io::Result<u64> {
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"can not seek in a pipe",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl WasiFile for LoggingPipe {
|
||||
fn last_accessed(&self) -> u64 {
|
||||
0
|
||||
}
|
||||
fn last_modified(&self) -> u64 {
|
||||
0
|
||||
}
|
||||
fn created_time(&self) -> u64 {
|
||||
0
|
||||
}
|
||||
fn size(&self) -> u64 {
|
||||
self.buffer.len() as u64
|
||||
}
|
||||
fn set_len(&mut self, len: u64) -> Result<(), WasiFsError> {
|
||||
self.buffer.resize(len as usize, 0);
|
||||
Ok(())
|
||||
}
|
||||
fn unlink(&mut self) -> Result<(), WasiFsError> {
|
||||
Ok(())
|
||||
}
|
||||
fn bytes_available(&self) -> Result<usize, WasiFsError> {
|
||||
Ok(self.buffer.len())
|
||||
}
|
||||
}
|
||||
|
||||
// Unit tests
|
||||
#[cfg(test)]
|
||||
mod logging_pipe_test {
|
||||
|
@ -10,10 +10,9 @@ use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
fs,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
time::Duration,
|
||||
};
|
||||
use wasmer::Store;
|
||||
use wasmtime::Engine;
|
||||
|
||||
use crate::panes::PaneId;
|
||||
use crate::screen::ScreenInstruction;
|
||||
@ -189,7 +188,7 @@ impl From<&PluginInstruction> for PluginContext {
|
||||
|
||||
pub(crate) fn plugin_thread_main(
|
||||
bus: Bus<PluginInstruction>,
|
||||
store: Store,
|
||||
engine: Engine,
|
||||
data_dir: PathBuf,
|
||||
mut layout: Box<Layout>,
|
||||
layout_dir: Option<PathBuf>,
|
||||
@ -204,7 +203,6 @@ pub(crate) fn plugin_thread_main(
|
||||
let plugin_dir = data_dir.join("plugins/");
|
||||
let plugin_global_data_dir = plugin_dir.join("data");
|
||||
layout.populate_plugin_aliases_in_layout(&plugin_aliases);
|
||||
let store = Arc::new(Mutex::new(store));
|
||||
|
||||
// use this channel to ensure that tasks spawned from this thread terminate before exiting
|
||||
// https://tokio.rs/tokio/topics/shutdown#waiting-for-things-to-finish-shutting-down
|
||||
@ -212,7 +210,7 @@ pub(crate) fn plugin_thread_main(
|
||||
|
||||
let mut wasm_bridge = WasmBridge::new(
|
||||
bus.senders.clone(),
|
||||
store,
|
||||
engine,
|
||||
plugin_dir,
|
||||
path_to_default_shell,
|
||||
zellij_cwd,
|
||||
|
@ -3,7 +3,6 @@ use crate::plugins::plugin_map::RunningPlugin;
|
||||
use crate::plugins::wasm_bridge::PluginRenderAsset;
|
||||
use crate::plugins::zellij_exports::{wasi_read_string, wasi_write_object};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use wasmer::Value;
|
||||
use zellij_utils::data::{PipeMessage, PipeSource};
|
||||
use zellij_utils::plugin_api::pipe_message::ProtobufPipeMessage;
|
||||
|
||||
@ -147,7 +146,6 @@ pub fn apply_pipe_message_to_plugin(
|
||||
senders: &ThreadSenders,
|
||||
) -> Result<()> {
|
||||
let instance = &running_plugin.instance;
|
||||
let plugin_env = &running_plugin.plugin_env;
|
||||
let rows = running_plugin.rows;
|
||||
let columns = running_plugin.columns;
|
||||
|
||||
@ -156,31 +154,24 @@ pub fn apply_pipe_message_to_plugin(
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|e| anyhow!("Failed to convert to protobuf: {:?}", e))?;
|
||||
match instance.exports.get_function("pipe") {
|
||||
match instance.get_typed_func::<(), i32>(&mut running_plugin.store, "pipe") {
|
||||
Ok(pipe) => {
|
||||
wasi_write_object(&plugin_env.wasi_env, &protobuf_pipe_message.encode_to_vec())
|
||||
wasi_write_object(
|
||||
running_plugin.store.data(),
|
||||
&protobuf_pipe_message.encode_to_vec(),
|
||||
)
|
||||
.with_context(err_context)?;
|
||||
let should_render = pipe
|
||||
.call(&mut running_plugin.store, ())
|
||||
.with_context(err_context)?;
|
||||
let pipe_return = pipe
|
||||
.call(&mut running_plugin.store, &[])
|
||||
.with_context(err_context)?;
|
||||
let should_render = match pipe_return.get(0) {
|
||||
Some(Value::I32(n)) => *n == 1,
|
||||
_ => false,
|
||||
};
|
||||
let should_render = should_render == 1;
|
||||
if rows > 0 && columns > 0 && should_render {
|
||||
let rendered_bytes = instance
|
||||
.exports
|
||||
.get_function("render")
|
||||
.map_err(anyError::new)
|
||||
.get_typed_func::<(i32, i32), ()>(&mut running_plugin.store, "render")
|
||||
.and_then(|render| {
|
||||
render
|
||||
.call(
|
||||
&mut running_plugin.store,
|
||||
&[Value::I32(rows as i32), Value::I32(columns as i32)],
|
||||
)
|
||||
.map_err(anyError::new)
|
||||
render.call(&mut running_plugin.store, (rows as i32, columns as i32))
|
||||
})
|
||||
.and_then(|_| wasi_read_string(&plugin_env.wasi_env))
|
||||
.and_then(|_| wasi_read_string(running_plugin.store.data()))
|
||||
.with_context(err_context)?;
|
||||
let pipes_to_block_or_unblock =
|
||||
pipes_to_block_or_unblock(running_plugin, Some(&pipe_message.source));
|
||||
@ -230,14 +221,16 @@ pub fn pipes_to_block_or_unblock(
|
||||
) -> HashMap<String, PipeStateChange> {
|
||||
let mut pipe_state_changes = HashMap::new();
|
||||
let mut input_pipes_to_unblock: HashSet<String> = running_plugin
|
||||
.plugin_env
|
||||
.store
|
||||
.data()
|
||||
.input_pipes_to_unblock
|
||||
.lock()
|
||||
.unwrap()
|
||||
.drain()
|
||||
.collect();
|
||||
let mut input_pipes_to_block: HashSet<String> = running_plugin
|
||||
.plugin_env
|
||||
.store
|
||||
.data()
|
||||
.input_pipes_to_block
|
||||
.lock()
|
||||
.unwrap()
|
||||
|
@ -1,19 +1,20 @@
|
||||
use crate::get_store;
|
||||
use crate::plugins::plugin_map::{PluginEnv, PluginMap, RunningPlugin, Subscriptions};
|
||||
use crate::plugins::plugin_map::{
|
||||
PluginEnv, PluginMap, RunningPlugin, VecDequeInputStream, WriteOutputStream,
|
||||
};
|
||||
use crate::plugins::plugin_worker::{plugin_worker, RunningWorker};
|
||||
use crate::plugins::zellij_exports::{wasi_write_object, zellij_exports};
|
||||
use crate::plugins::PluginId;
|
||||
use highway::{HighwayHash, PortableHash};
|
||||
use log::info;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
fs,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use url::Url;
|
||||
use wasmer::{AsStoreRef, Instance, Module, Store};
|
||||
use wasmer_wasi::{Pipe, WasiState};
|
||||
use wasmtime::{Engine, Instance, Linker, Module, Store};
|
||||
use wasmtime_wasi::{DirPerms, FilePerms, WasiCtxBuilder};
|
||||
use zellij_utils::consts::ZELLIJ_PLUGIN_ARTIFACT_DIR;
|
||||
use zellij_utils::prost::Message;
|
||||
|
||||
@ -53,7 +54,7 @@ pub struct PluginLoader<'a> {
|
||||
senders: ThreadSenders,
|
||||
plugin_id: PluginId,
|
||||
client_id: ClientId,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin: PluginConfig,
|
||||
plugin_dir: &'a PathBuf,
|
||||
tab_index: Option<usize>,
|
||||
@ -75,7 +76,7 @@ impl<'a> PluginLoader<'a> {
|
||||
plugin_dir: PathBuf,
|
||||
plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>,
|
||||
senders: ThreadSenders,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_map: Arc<Mutex<PluginMap>>,
|
||||
connected_clients: Arc<Mutex<Vec<ClientId>>>,
|
||||
loading_indication: &mut LoadingIndication,
|
||||
@ -102,7 +103,7 @@ impl<'a> PluginLoader<'a> {
|
||||
&senders,
|
||||
plugin_id,
|
||||
first_client_id,
|
||||
store,
|
||||
engine,
|
||||
&plugin_dir,
|
||||
path_to_default_shell,
|
||||
zellij_cwd,
|
||||
@ -115,14 +116,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_loader.load_plugin_instance(store, &instance, &plugin_map)
|
||||
})
|
||||
.and_then(|_| {
|
||||
plugin_loader.clone_instance_for_other_clients(&connected_clients, &plugin_map)
|
||||
@ -140,7 +135,7 @@ impl<'a> PluginLoader<'a> {
|
||||
plugin_dir: PathBuf,
|
||||
plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>,
|
||||
senders: ThreadSenders,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_map: Arc<Mutex<PluginMap>>,
|
||||
size: Size,
|
||||
connected_clients: Arc<Mutex<Vec<ClientId>>>,
|
||||
@ -161,7 +156,7 @@ impl<'a> PluginLoader<'a> {
|
||||
&senders,
|
||||
plugin_id,
|
||||
client_id,
|
||||
store.clone(),
|
||||
engine,
|
||||
plugin.clone(),
|
||||
&plugin_dir,
|
||||
tab_index,
|
||||
@ -178,14 +173,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_loader.load_plugin_instance(store, &instance, &plugin_map)
|
||||
})
|
||||
.and_then(|_| {
|
||||
plugin_loader.clone_instance_for_other_clients(
|
||||
@ -200,14 +189,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_loader.load_plugin_instance(store, &instance, &plugin_map)
|
||||
})
|
||||
.and_then(|_| {
|
||||
plugin_loader.clone_instance_for_other_clients(
|
||||
@ -226,7 +209,7 @@ impl<'a> PluginLoader<'a> {
|
||||
plugin_dir: PathBuf,
|
||||
plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>,
|
||||
senders: ThreadSenders,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_map: Arc<Mutex<PluginMap>>,
|
||||
connected_clients: Arc<Mutex<Vec<ClientId>>>,
|
||||
loading_indication: &mut LoadingIndication,
|
||||
@ -250,7 +233,7 @@ impl<'a> PluginLoader<'a> {
|
||||
&senders,
|
||||
plugin_id,
|
||||
existing_client_id,
|
||||
store.clone(),
|
||||
engine.clone(),
|
||||
&plugin_dir,
|
||||
path_to_default_shell.clone(),
|
||||
zellij_cwd.clone(),
|
||||
@ -263,14 +246,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_loader.load_plugin_instance(store, &instance, &plugin_map)
|
||||
})?
|
||||
}
|
||||
connected_clients.lock().unwrap().push(client_id);
|
||||
@ -282,7 +259,7 @@ impl<'a> PluginLoader<'a> {
|
||||
plugin_dir: PathBuf,
|
||||
plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>,
|
||||
senders: ThreadSenders,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_map: Arc<Mutex<PluginMap>>,
|
||||
connected_clients: Arc<Mutex<Vec<ClientId>>>,
|
||||
loading_indication: &mut LoadingIndication,
|
||||
@ -310,7 +287,7 @@ impl<'a> PluginLoader<'a> {
|
||||
&senders,
|
||||
plugin_id,
|
||||
first_client_id,
|
||||
store.clone(),
|
||||
engine,
|
||||
&plugin_dir,
|
||||
path_to_default_shell,
|
||||
zellij_cwd,
|
||||
@ -323,14 +300,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_loader.load_plugin_instance(store, &instance, &plugin_map)
|
||||
})
|
||||
.and_then(|_| {
|
||||
plugin_loader.clone_instance_for_other_clients(&connected_clients, &plugin_map)
|
||||
@ -345,7 +316,7 @@ impl<'a> PluginLoader<'a> {
|
||||
senders: &ThreadSenders,
|
||||
plugin_id: PluginId,
|
||||
client_id: ClientId,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin: PluginConfig,
|
||||
plugin_dir: &'a PathBuf,
|
||||
tab_index: Option<usize>,
|
||||
@ -370,7 +341,7 @@ impl<'a> PluginLoader<'a> {
|
||||
senders: senders.clone(),
|
||||
plugin_id,
|
||||
client_id,
|
||||
store: store.clone(),
|
||||
engine,
|
||||
plugin,
|
||||
plugin_dir,
|
||||
tab_index,
|
||||
@ -393,7 +364,7 @@ impl<'a> PluginLoader<'a> {
|
||||
senders: &ThreadSenders,
|
||||
plugin_id: PluginId,
|
||||
client_id: ClientId,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_dir: &'a PathBuf,
|
||||
path_to_default_shell: PathBuf,
|
||||
zellij_cwd: PathBuf,
|
||||
@ -411,20 +382,20 @@ impl<'a> PluginLoader<'a> {
|
||||
.with_context(err_context)?
|
||||
};
|
||||
let running_plugin = running_plugin.lock().unwrap();
|
||||
let tab_index = running_plugin.plugin_env.tab_index;
|
||||
let tab_index = running_plugin.store.data().tab_index;
|
||||
let size = Size {
|
||||
rows: running_plugin.rows,
|
||||
cols: running_plugin.columns,
|
||||
};
|
||||
let plugin_config = running_plugin.plugin_env.plugin.clone();
|
||||
loading_indication.set_name(running_plugin.plugin_env.name());
|
||||
let plugin_config = running_plugin.store.data().plugin.clone();
|
||||
loading_indication.set_name(running_plugin.store.data().name());
|
||||
PluginLoader::new(
|
||||
plugin_cache,
|
||||
loading_indication,
|
||||
senders,
|
||||
plugin_id,
|
||||
client_id,
|
||||
store,
|
||||
engine,
|
||||
plugin_config,
|
||||
plugin_dir,
|
||||
tab_index,
|
||||
@ -445,7 +416,7 @@ impl<'a> PluginLoader<'a> {
|
||||
senders: &ThreadSenders,
|
||||
plugin_id: PluginId,
|
||||
client_id: ClientId,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_dir: &'a PathBuf,
|
||||
path_to_default_shell: PathBuf,
|
||||
zellij_cwd: PathBuf,
|
||||
@ -464,20 +435,20 @@ impl<'a> PluginLoader<'a> {
|
||||
.clone()
|
||||
};
|
||||
let running_plugin = running_plugin.lock().unwrap();
|
||||
let tab_index = running_plugin.plugin_env.tab_index;
|
||||
let tab_index = running_plugin.store.data().tab_index;
|
||||
let size = Size {
|
||||
rows: running_plugin.rows,
|
||||
cols: running_plugin.columns,
|
||||
};
|
||||
let plugin_config = running_plugin.plugin_env.plugin.clone();
|
||||
loading_indication.set_name(running_plugin.plugin_env.name());
|
||||
let plugin_config = running_plugin.store.data().plugin.clone();
|
||||
loading_indication.set_name(running_plugin.store.data().name());
|
||||
PluginLoader::new(
|
||||
plugin_cache,
|
||||
loading_indication,
|
||||
senders,
|
||||
plugin_id,
|
||||
client_id,
|
||||
store.clone(),
|
||||
engine,
|
||||
plugin_config,
|
||||
plugin_dir,
|
||||
tab_index,
|
||||
@ -527,9 +498,7 @@ impl<'a> PluginLoader<'a> {
|
||||
);
|
||||
let (_wasm_bytes, cached_path) = self.plugin_bytes_and_cache_path()?;
|
||||
let timer = std::time::Instant::now();
|
||||
let module = unsafe {
|
||||
Module::deserialize_from_file(&self.store.lock().unwrap().as_store_ref(), &cached_path)?
|
||||
};
|
||||
let module = unsafe { Module::deserialize_file(&self.engine, &cached_path)? };
|
||||
log::info!(
|
||||
"Loaded plugin '{}' from cache folder at '{}' in {:?}",
|
||||
self.plugin_path.display(),
|
||||
@ -565,12 +534,11 @@ impl<'a> PluginLoader<'a> {
|
||||
.map_err(anyError::new)
|
||||
.and_then(|_| {
|
||||
// compile module
|
||||
Module::new(&self.store.lock().unwrap().as_store_ref(), &wasm_bytes)
|
||||
.map_err(anyError::new)
|
||||
Module::new(&self.engine, &wasm_bytes)
|
||||
})
|
||||
.and_then(|m| {
|
||||
// serialize module to HD cache for faster loading in the future
|
||||
m.serialize_to_file(&cached_path).map_err(anyError::new)?;
|
||||
fs::write(&cached_path, m.serialize()?).map_err(anyError::new)?;
|
||||
log::info!(
|
||||
"Compiled plugin '{}' in {:?}",
|
||||
self.plugin_path.display(),
|
||||
@ -584,20 +552,19 @@ 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<PluginEnv>, Instance)> {
|
||||
let (store, instance) = 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))
|
||||
}
|
||||
pub fn create_plugin_instance_and_wasi_env_for_worker(
|
||||
&mut self,
|
||||
) -> Result<(Store, Instance, PluginEnv)> {
|
||||
) -> Result<(Store<PluginEnv>, Instance)> {
|
||||
let err_context = || {
|
||||
format!(
|
||||
"Failed to create instance and plugin env for worker {}",
|
||||
@ -611,21 +578,17 @@ 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)?;
|
||||
Ok((store, instance, plugin_env))
|
||||
let (store, instance) = self.create_plugin_instance_env(&module)?;
|
||||
Ok((store, instance))
|
||||
}
|
||||
pub fn load_plugin_instance(
|
||||
&mut self,
|
||||
store: Store,
|
||||
mut store: Store<PluginEnv>,
|
||||
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();
|
||||
let main_user_env = plugin_env.clone();
|
||||
display_loading_stage!(
|
||||
indicate_starting_plugin,
|
||||
self.loading_indication,
|
||||
@ -633,39 +596,38 @@ impl<'a> PluginLoader<'a> {
|
||||
self.plugin_id
|
||||
);
|
||||
let start_function = instance
|
||||
.exports
|
||||
.get_function("_start")
|
||||
.get_typed_func::<(), ()>(&mut store, "_start")
|
||||
.with_context(err_context)?;
|
||||
let load_function = instance
|
||||
.exports
|
||||
.get_function("load")
|
||||
.get_typed_func::<(), ()>(&mut store, "load")
|
||||
.with_context(err_context)?;
|
||||
let mut workers = HashMap::new();
|
||||
for (function_name, _exported_function) in instance.exports.iter().functions() {
|
||||
for function_name in instance
|
||||
.exports(&mut store)
|
||||
.filter_map(|export| export.clone().into_func().map(|_| export.name()))
|
||||
{
|
||||
if function_name.ends_with("_worker") {
|
||||
let plugin_config = self.plugin.clone();
|
||||
let (mut store, instance, plugin_env) =
|
||||
let (mut store, instance) =
|
||||
self.create_plugin_instance_and_wasi_env_for_worker()?;
|
||||
|
||||
let start_function_for_worker = instance
|
||||
.exports
|
||||
.get_function("_start")
|
||||
.get_typed_func::<(), ()>(&mut store, "_start")
|
||||
.with_context(err_context)?;
|
||||
start_function_for_worker
|
||||
.call(&mut store, &[])
|
||||
.call(&mut store, ())
|
||||
.with_context(err_context)?;
|
||||
|
||||
let worker =
|
||||
RunningWorker::new(store, instance, &function_name, plugin_config, plugin_env);
|
||||
let worker = RunningWorker::new(store, instance, &function_name, plugin_config);
|
||||
let worker_sender = plugin_worker(worker);
|
||||
workers.insert(function_name.into(), worker_sender);
|
||||
}
|
||||
}
|
||||
|
||||
let subscriptions = store.data().subscriptions.clone();
|
||||
let plugin = Arc::new(Mutex::new(RunningPlugin::new(
|
||||
store,
|
||||
main_user_instance,
|
||||
main_user_env,
|
||||
self.size.rows,
|
||||
self.size.cols,
|
||||
)));
|
||||
@ -673,12 +635,12 @@ impl<'a> PluginLoader<'a> {
|
||||
self.plugin_id,
|
||||
self.client_id,
|
||||
plugin.clone(),
|
||||
subscriptions.clone(),
|
||||
subscriptions,
|
||||
workers,
|
||||
);
|
||||
|
||||
start_function
|
||||
.call(&mut plugin.lock().unwrap().store, &[])
|
||||
.call(&mut plugin.lock().unwrap().store, ())
|
||||
.with_context(err_context)?;
|
||||
|
||||
let protobuf_plugin_configuration: ProtobufPluginConfiguration = self
|
||||
@ -689,13 +651,13 @@ impl<'a> PluginLoader<'a> {
|
||||
.map_err(|e| anyhow!("Failed to serialize user configuration: {:?}", e))?;
|
||||
let protobuf_bytes = protobuf_plugin_configuration.encode_to_vec();
|
||||
wasi_write_object(
|
||||
&plugin_env.wasi_env,
|
||||
plugin.lock().unwrap().store.data(),
|
||||
&protobuf_bytes,
|
||||
// &self.plugin.userspace_configuration.inner(),
|
||||
)
|
||||
.with_context(err_context)?;
|
||||
load_function
|
||||
.call(&mut plugin.lock().unwrap().store, &[])
|
||||
.call(&mut plugin.lock().unwrap().store, ())
|
||||
.with_context(err_context)?;
|
||||
|
||||
display_loading_stage!(
|
||||
@ -744,7 +706,7 @@ impl<'a> PluginLoader<'a> {
|
||||
&self.senders.clone(),
|
||||
self.plugin_id,
|
||||
*client_id,
|
||||
self.store.clone(),
|
||||
self.engine.clone(),
|
||||
&self.plugin_dir,
|
||||
self.path_to_default_shell.clone(),
|
||||
self.zellij_cwd.clone(),
|
||||
@ -757,14 +719,8 @@ 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)| {
|
||||
plugin_loader_for_client.load_plugin_instance(
|
||||
store,
|
||||
&instance,
|
||||
&plugin_env,
|
||||
plugin_map,
|
||||
&subscriptions,
|
||||
)
|
||||
.and_then(|(store, instance)| {
|
||||
plugin_loader_for_client.load_plugin_instance(store, &instance, plugin_map)
|
||||
})?
|
||||
}
|
||||
display_loading_stage!(
|
||||
@ -799,18 +755,13 @@ 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<PluginEnv>, Instance)> {
|
||||
let err_context = || {
|
||||
format!(
|
||||
"Failed to create instance, plugin env and subscriptions for plugin {}",
|
||||
self.plugin_id
|
||||
)
|
||||
};
|
||||
let mut store = get_store();
|
||||
let store_mut = &mut store;
|
||||
let dirs = vec![
|
||||
("/host".to_owned(), self.zellij_cwd.clone()),
|
||||
("/data".to_owned(), self.plugin_own_data_dir.clone()),
|
||||
@ -824,22 +775,23 @@ impl<'a> PluginLoader<'a> {
|
||||
// there's no built-in solution
|
||||
dir.try_exists().ok().unwrap_or(false)
|
||||
});
|
||||
let mut wasi_env = WasiState::new("Zellij")
|
||||
.env("CLICOLOR_FORCE", "1")
|
||||
.map_dirs(dirs)
|
||||
.and_then(|wasi| {
|
||||
wasi.stdin(Box::new(Pipe::new()))
|
||||
.stdout(Box::new(Pipe::new()))
|
||||
.stderr(Box::new(LoggingPipe::new(
|
||||
&self.plugin.location.to_string(),
|
||||
self.plugin_id,
|
||||
)))
|
||||
.finalize(store_mut)
|
||||
})
|
||||
.with_context(err_context)?;
|
||||
let wasi = wasi_env
|
||||
.import_object(store_mut, &module)
|
||||
.with_context(err_context)?;
|
||||
let mut wasi_ctx_builder = WasiCtxBuilder::new();
|
||||
wasi_ctx_builder.env("CLICOLOR_FORCE", "1");
|
||||
for (guest_path, host_path) in dirs {
|
||||
wasi_ctx_builder
|
||||
.preopened_dir(host_path, guest_path, DirPerms::all(), FilePerms::all())
|
||||
.with_context(err_context)?;
|
||||
}
|
||||
let stdin_pipe = Arc::new(Mutex::new(VecDeque::new()));
|
||||
let stdout_pipe = Arc::new(Mutex::new(VecDeque::new()));
|
||||
wasi_ctx_builder
|
||||
.stdin(VecDequeInputStream(stdin_pipe.clone()))
|
||||
.stdout(WriteOutputStream(stdout_pipe.clone()))
|
||||
.stderr(WriteOutputStream(Arc::new(Mutex::new(LoggingPipe::new(
|
||||
&self.plugin.location.to_string(),
|
||||
self.plugin_id,
|
||||
)))));
|
||||
let wasi_ctx = wasi_ctx_builder.build_p1();
|
||||
let mut mut_plugin = self.plugin.clone();
|
||||
if let Some(tab_index) = self.tab_index {
|
||||
mut_plugin.set_tab_index(tab_index);
|
||||
@ -850,7 +802,7 @@ impl<'a> PluginLoader<'a> {
|
||||
plugin: mut_plugin,
|
||||
permissions: Arc::new(Mutex::new(None)),
|
||||
senders: self.senders.clone(),
|
||||
wasi_env: wasi_env.data_mut(store_mut).clone(),
|
||||
wasi_ctx,
|
||||
plugin_own_data_dir: self.plugin_own_data_dir.clone(),
|
||||
tab_index: self.tab_index,
|
||||
path_to_default_shell: self.path_to_default_shell.clone(),
|
||||
@ -862,18 +814,28 @@ 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())),
|
||||
stdin_pipe,
|
||||
stdout_pipe,
|
||||
};
|
||||
let mut store = Store::new(&self.engine, plugin_env);
|
||||
|
||||
let subscriptions = Arc::new(Mutex::new(HashSet::new()));
|
||||
let mut linker = Linker::new(&self.engine);
|
||||
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |plugin_env: &mut PluginEnv| {
|
||||
&mut plugin_env.wasi_ctx
|
||||
})
|
||||
.unwrap();
|
||||
zellij_exports(&mut linker);
|
||||
|
||||
let mut zellij = zellij_exports(store_mut, &plugin_env, &subscriptions);
|
||||
zellij.extend(&wasi);
|
||||
let instance = linker
|
||||
.instantiate(&mut store, module)
|
||||
.with_context(err_context)?;
|
||||
|
||||
let instance = Instance::new(store_mut, &module, &zellij).with_context(err_context)?;
|
||||
if let Some(func) = instance.get_func(&mut store, "_initialize") {
|
||||
func.typed::<(), ()>(&store)?.call(&mut store, ())?;
|
||||
}
|
||||
|
||||
wasi_env.initialize(store_mut, &instance)?;
|
||||
|
||||
Ok((store, instance, plugin_env, subscriptions))
|
||||
Ok((store, instance))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,18 @@
|
||||
use crate::plugins::plugin_worker::MessageToWorker;
|
||||
use crate::plugins::PluginId;
|
||||
use bytes::Bytes;
|
||||
use std::io::Write;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use wasmer::{Instance, Store};
|
||||
use wasmer_wasi::WasiEnv;
|
||||
use wasmtime::{Instance, Store};
|
||||
use wasmtime_wasi::preview1::WasiP1Ctx;
|
||||
use wasmtime_wasi::{
|
||||
HostInputStream, HostOutputStream, StdinStream, StdoutStream, StreamError, StreamResult,
|
||||
Subscribe,
|
||||
};
|
||||
|
||||
use crate::{thread_bus::ThreadSenders, ClientId};
|
||||
|
||||
@ -165,9 +171,9 @@ impl PluginMap {
|
||||
.iter()
|
||||
.filter(|(_, (running_plugin, _subscriptions, _workers))| {
|
||||
let running_plugin = running_plugin.lock().unwrap();
|
||||
let running_plugin_location = &running_plugin.plugin_env.plugin.location;
|
||||
let running_plugin_configuration =
|
||||
&running_plugin.plugin_env.plugin.userspace_configuration;
|
||||
let plugin_config = &running_plugin.store.data().plugin;
|
||||
let running_plugin_location = &plugin_config.location;
|
||||
let running_plugin_configuration = &plugin_config.userspace_configuration;
|
||||
running_plugin_location == plugin_location
|
||||
&& running_plugin_configuration == plugin_configuration
|
||||
})
|
||||
@ -188,9 +194,9 @@ impl PluginMap {
|
||||
> = HashMap::new();
|
||||
for ((plugin_id, client_id), (running_plugin, _, _)) in self.plugin_assets.iter() {
|
||||
let running_plugin = running_plugin.lock().unwrap();
|
||||
let running_plugin_location = &running_plugin.plugin_env.plugin.location;
|
||||
let running_plugin_configuration =
|
||||
&running_plugin.plugin_env.plugin.userspace_configuration;
|
||||
let plugin_config = &running_plugin.store.data().plugin;
|
||||
let running_plugin_location = &plugin_config.location;
|
||||
let running_plugin_configuration = &plugin_config.userspace_configuration;
|
||||
match cloned_plugin_assets.get_mut(running_plugin_location) {
|
||||
Some(location_map) => match location_map.get_mut(running_plugin_configuration) {
|
||||
Some(plugin_instances_info) => {
|
||||
@ -240,13 +246,10 @@ impl PluginMap {
|
||||
.find_map(|((p_id, _), (running_plugin, _, _))| {
|
||||
if *p_id == plugin_id {
|
||||
let running_plugin = running_plugin.lock().unwrap();
|
||||
let run_plugin_location = running_plugin.plugin_env.plugin.location.clone();
|
||||
let run_plugin_configuration = running_plugin
|
||||
.plugin_env
|
||||
.plugin
|
||||
.userspace_configuration
|
||||
.clone();
|
||||
let initial_cwd = running_plugin.plugin_env.plugin.initial_cwd.clone();
|
||||
let plugin_config = &running_plugin.store.data().plugin;
|
||||
let run_plugin_location = plugin_config.location.clone();
|
||||
let run_plugin_configuration = plugin_config.userspace_configuration.clone();
|
||||
let initial_cwd = plugin_config.initial_cwd.clone();
|
||||
Some(RunPlugin {
|
||||
_allow_exec_host_cmd: false,
|
||||
location: run_plugin_location,
|
||||
@ -262,13 +265,12 @@ impl PluginMap {
|
||||
|
||||
pub type Subscriptions = HashSet<EventType>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PluginEnv {
|
||||
pub plugin_id: PluginId,
|
||||
pub plugin: PluginConfig,
|
||||
pub permissions: Arc<Mutex<Option<HashSet<PermissionType>>>>,
|
||||
pub senders: ThreadSenders,
|
||||
pub wasi_env: WasiEnv,
|
||||
pub wasi_ctx: WasiP1Ctx,
|
||||
pub tab_index: Option<usize>,
|
||||
pub client_id: ClientId,
|
||||
#[allow(dead_code)]
|
||||
@ -282,6 +284,80 @@ 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>>,
|
||||
pub stdin_pipe: Arc<Mutex<VecDeque<u8>>>,
|
||||
pub stdout_pipe: Arc<Mutex<VecDeque<u8>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VecDequeInputStream(pub Arc<Mutex<VecDeque<u8>>>);
|
||||
|
||||
impl StdinStream for VecDequeInputStream {
|
||||
fn stream(&self) -> Box<dyn wasmtime_wasi::HostInputStream> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn isatty(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl HostInputStream for VecDequeInputStream {
|
||||
fn read(&mut self, size: usize) -> StreamResult<Bytes> {
|
||||
let mut inner = self.0.lock().unwrap();
|
||||
let len = std::cmp::min(size, inner.len());
|
||||
Ok(Bytes::from_iter(inner.drain(0..len)))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Subscribe for VecDequeInputStream {
|
||||
async fn ready(&mut self) {}
|
||||
}
|
||||
|
||||
pub struct WriteOutputStream<T>(pub Arc<Mutex<T>>);
|
||||
|
||||
impl<T> Clone for WriteOutputStream<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Write + Send + 'static> StdoutStream for WriteOutputStream<T> {
|
||||
fn stream(&self) -> Box<dyn HostOutputStream> {
|
||||
Box::new((*self).clone())
|
||||
}
|
||||
|
||||
fn isatty(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Write + Send + 'static> HostOutputStream for WriteOutputStream<T> {
|
||||
fn write(&mut self, bytes: Bytes) -> StreamResult<()> {
|
||||
self.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.write_all(&*bytes)
|
||||
.map_err(|e| StreamError::LastOperationFailed(e.into()))
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> StreamResult<()> {
|
||||
self.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.flush()
|
||||
.map_err(|e| StreamError::LastOperationFailed(e.into()))
|
||||
}
|
||||
|
||||
fn check_write(&mut self) -> StreamResult<usize> {
|
||||
Ok(usize::MAX)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: Send + 'static> Subscribe for WriteOutputStream<T> {
|
||||
async fn ready(&mut self) {}
|
||||
}
|
||||
|
||||
impl PluginEnv {
|
||||
@ -305,9 +381,8 @@ pub enum AtomicEvent {
|
||||
}
|
||||
|
||||
pub struct RunningPlugin {
|
||||
pub store: Store,
|
||||
pub store: Store<PluginEnv>,
|
||||
pub instance: Instance,
|
||||
pub plugin_env: PluginEnv,
|
||||
pub rows: usize,
|
||||
pub columns: usize,
|
||||
next_event_ids: HashMap<AtomicEvent, usize>,
|
||||
@ -315,17 +390,10 @@ pub struct RunningPlugin {
|
||||
}
|
||||
|
||||
impl RunningPlugin {
|
||||
pub fn new(
|
||||
store: Store,
|
||||
instance: Instance,
|
||||
plugin_env: PluginEnv,
|
||||
rows: usize,
|
||||
columns: usize,
|
||||
) -> Self {
|
||||
pub fn new(store: Store<PluginEnv>, instance: Instance, rows: usize, columns: usize) -> Self {
|
||||
RunningPlugin {
|
||||
store,
|
||||
instance,
|
||||
plugin_env,
|
||||
rows,
|
||||
columns,
|
||||
next_event_ids: HashMap::new(),
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::plugins::plugin_map::PluginEnv;
|
||||
use crate::plugins::zellij_exports::wasi_write_object;
|
||||
use wasmer::{Instance, Store};
|
||||
use wasmtime::{Instance, Store};
|
||||
|
||||
use zellij_utils::async_channel::{unbounded, Receiver, Sender};
|
||||
use zellij_utils::async_std::task;
|
||||
@ -13,24 +13,21 @@ pub struct RunningWorker {
|
||||
pub instance: Instance,
|
||||
pub name: String,
|
||||
pub plugin_config: PluginConfig,
|
||||
pub plugin_env: PluginEnv,
|
||||
store: Store,
|
||||
pub store: Store<PluginEnv>,
|
||||
}
|
||||
|
||||
impl RunningWorker {
|
||||
pub fn new(
|
||||
store: Store,
|
||||
store: Store<PluginEnv>,
|
||||
instance: Instance,
|
||||
name: &str,
|
||||
plugin_config: PluginConfig,
|
||||
plugin_env: PluginEnv,
|
||||
) -> Self {
|
||||
RunningWorker {
|
||||
store,
|
||||
instance,
|
||||
name: name.into(),
|
||||
plugin_config,
|
||||
plugin_env,
|
||||
}
|
||||
}
|
||||
pub fn send_message(&mut self, message: String, payload: String) -> Result<()> {
|
||||
@ -43,12 +40,11 @@ impl RunningWorker {
|
||||
let protobuf_bytes = protobuf_message.encode_to_vec();
|
||||
let work_function = self
|
||||
.instance
|
||||
.exports
|
||||
.get_function(&self.name)
|
||||
.get_typed_func::<(), ()>(&mut self.store, &self.name)
|
||||
.with_context(err_context)?;
|
||||
wasi_write_object(&self.plugin_env.wasi_env, &protobuf_bytes).with_context(err_context)?;
|
||||
wasi_write_object(self.store.data(), &protobuf_bytes).with_context(err_context)?;
|
||||
work_function
|
||||
.call(&mut self.store, &[])
|
||||
.call(&mut self.store, ())
|
||||
.with_context(err_context)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use insta::assert_snapshot;
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::tempdir;
|
||||
use wasmer::Store;
|
||||
use wasmtime::Engine;
|
||||
use zellij_utils::data::{
|
||||
BareKey, Event, KeyWithModifier, PermissionStatus, PermissionType, PluginCapabilities,
|
||||
};
|
||||
@ -247,7 +247,7 @@ fn create_plugin_thread(
|
||||
None,
|
||||
)
|
||||
.should_silently_fail();
|
||||
let store = Store::new(wasmer::Singlepass::default());
|
||||
let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap();
|
||||
let data_dir = PathBuf::from(tempdir().unwrap().path());
|
||||
let default_shell = PathBuf::from(".");
|
||||
let plugin_capabilities = PluginCapabilities::default();
|
||||
@ -270,7 +270,7 @@ fn create_plugin_thread(
|
||||
set_var("ZELLIJ_SESSION_NAME", "zellij-test");
|
||||
plugin_thread_main(
|
||||
plugin_bus,
|
||||
store,
|
||||
engine,
|
||||
data_dir,
|
||||
Box::new(Layout::default()),
|
||||
None,
|
||||
@ -337,7 +337,7 @@ fn create_plugin_thread_with_server_receiver(
|
||||
None,
|
||||
)
|
||||
.should_silently_fail();
|
||||
let store = Store::new(wasmer::Singlepass::default());
|
||||
let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap();
|
||||
let data_dir = PathBuf::from(tempdir().unwrap().path());
|
||||
let default_shell = PathBuf::from(".");
|
||||
let plugin_capabilities = PluginCapabilities::default();
|
||||
@ -349,7 +349,7 @@ fn create_plugin_thread_with_server_receiver(
|
||||
set_var("ZELLIJ_SESSION_NAME", "zellij-test");
|
||||
plugin_thread_main(
|
||||
plugin_bus,
|
||||
store,
|
||||
engine,
|
||||
data_dir,
|
||||
Box::new(Layout::default()),
|
||||
None,
|
||||
@ -422,7 +422,7 @@ fn create_plugin_thread_with_pty_receiver(
|
||||
None,
|
||||
)
|
||||
.should_silently_fail();
|
||||
let store = Store::new(wasmer::Singlepass::default());
|
||||
let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap();
|
||||
let data_dir = PathBuf::from(tempdir().unwrap().path());
|
||||
let default_shell = PathBuf::from(".");
|
||||
let plugin_capabilities = PluginCapabilities::default();
|
||||
@ -434,7 +434,7 @@ fn create_plugin_thread_with_pty_receiver(
|
||||
set_var("ZELLIJ_SESSION_NAME", "zellij-test");
|
||||
plugin_thread_main(
|
||||
plugin_bus,
|
||||
store,
|
||||
engine,
|
||||
data_dir,
|
||||
Box::new(Layout::default()),
|
||||
None,
|
||||
@ -502,7 +502,7 @@ fn create_plugin_thread_with_background_jobs_receiver(
|
||||
None,
|
||||
)
|
||||
.should_silently_fail();
|
||||
let store = Store::new(wasmer::Singlepass::default());
|
||||
let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap();
|
||||
let data_dir = PathBuf::from(tempdir().unwrap().path());
|
||||
let default_shell = PathBuf::from(".");
|
||||
let plugin_capabilities = PluginCapabilities::default();
|
||||
@ -514,7 +514,7 @@ fn create_plugin_thread_with_background_jobs_receiver(
|
||||
set_var("ZELLIJ_SESSION_NAME", "zellij-test");
|
||||
plugin_thread_main(
|
||||
plugin_bus,
|
||||
store,
|
||||
engine,
|
||||
data_dir,
|
||||
Box::new(Layout::default()),
|
||||
None,
|
||||
|
@ -15,7 +15,7 @@ use std::{
|
||||
str::FromStr,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use wasmer::{Module, Store, Value};
|
||||
use wasmtime::{Engine, Module};
|
||||
use zellij_utils::async_channel::Sender;
|
||||
use zellij_utils::async_std::task::{self, JoinHandle};
|
||||
use zellij_utils::consts::ZELLIJ_CACHE_DIR;
|
||||
@ -77,7 +77,7 @@ impl PluginRenderAsset {
|
||||
pub struct WasmBridge {
|
||||
connected_clients: Arc<Mutex<Vec<ClientId>>>,
|
||||
senders: ThreadSenders,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_dir: PathBuf,
|
||||
plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>,
|
||||
plugin_map: Arc<Mutex<PluginMap>>,
|
||||
@ -107,7 +107,7 @@ pub struct WasmBridge {
|
||||
impl WasmBridge {
|
||||
pub fn new(
|
||||
senders: ThreadSenders,
|
||||
store: Arc<Mutex<Store>>,
|
||||
engine: Engine,
|
||||
plugin_dir: PathBuf,
|
||||
path_to_default_shell: PathBuf,
|
||||
zellij_cwd: PathBuf,
|
||||
@ -125,7 +125,7 @@ impl WasmBridge {
|
||||
WasmBridge {
|
||||
connected_clients,
|
||||
senders,
|
||||
store,
|
||||
engine,
|
||||
plugin_dir,
|
||||
plugin_cache,
|
||||
plugin_map,
|
||||
@ -192,7 +192,7 @@ impl WasmBridge {
|
||||
let plugin_dir = self.plugin_dir.clone();
|
||||
let plugin_cache = self.plugin_cache.clone();
|
||||
let senders = self.senders.clone();
|
||||
let store = self.store.clone();
|
||||
let engine = self.engine.clone();
|
||||
let plugin_map = self.plugin_map.clone();
|
||||
let connected_clients = self.connected_clients.clone();
|
||||
let path_to_default_shell = self.path_to_default_shell.clone();
|
||||
@ -236,7 +236,7 @@ impl WasmBridge {
|
||||
plugin_dir,
|
||||
plugin_cache,
|
||||
senders.clone(),
|
||||
store,
|
||||
engine,
|
||||
plugin_map,
|
||||
size,
|
||||
connected_clients.clone(),
|
||||
@ -291,7 +291,7 @@ impl WasmBridge {
|
||||
drop(worker_sender.send(MessageToWorker::Exit));
|
||||
}
|
||||
let running_plugin = running_plugin.lock().unwrap();
|
||||
let cache_dir = running_plugin.plugin_env.plugin_own_data_dir.clone();
|
||||
let cache_dir = running_plugin.store.data().plugin_own_data_dir.clone();
|
||||
if let Err(e) = std::fs::remove_dir_all(cache_dir) {
|
||||
log::error!("Failed to remove cache dir for plugin: {:?}", e);
|
||||
}
|
||||
@ -330,7 +330,7 @@ impl WasmBridge {
|
||||
let plugin_dir = self.plugin_dir.clone();
|
||||
let plugin_cache = self.plugin_cache.clone();
|
||||
let senders = self.senders.clone();
|
||||
let store = self.store.clone();
|
||||
let engine = self.engine.clone();
|
||||
let plugin_map = self.plugin_map.clone();
|
||||
let connected_clients = self.connected_clients.clone();
|
||||
let path_to_default_shell = self.path_to_default_shell.clone();
|
||||
@ -346,7 +346,7 @@ impl WasmBridge {
|
||||
plugin_dir.clone(),
|
||||
plugin_cache.clone(),
|
||||
senders.clone(),
|
||||
store.clone(),
|
||||
engine.clone(),
|
||||
plugin_map.clone(),
|
||||
connected_clients.clone(),
|
||||
&mut loading_indication,
|
||||
@ -371,7 +371,7 @@ impl WasmBridge {
|
||||
plugin_dir.clone(),
|
||||
plugin_cache.clone(),
|
||||
senders.clone(),
|
||||
store.clone(),
|
||||
engine.clone(),
|
||||
plugin_map.clone(),
|
||||
connected_clients.clone(),
|
||||
&mut loading_indication,
|
||||
@ -423,7 +423,7 @@ impl WasmBridge {
|
||||
self.plugin_dir.clone(),
|
||||
self.plugin_cache.clone(),
|
||||
self.senders.clone(),
|
||||
self.store.clone(),
|
||||
self.engine.clone(),
|
||||
self.plugin_map.clone(),
|
||||
self.connected_clients.clone(),
|
||||
&mut loading_indication,
|
||||
@ -491,23 +491,17 @@ impl WasmBridge {
|
||||
let rendered_bytes = running_plugin
|
||||
.instance
|
||||
.clone()
|
||||
.exports
|
||||
.get_function("render")
|
||||
.map_err(anyError::new)
|
||||
.get_typed_func::<(i32, i32), ()>(
|
||||
&mut running_plugin.store,
|
||||
"render",
|
||||
)
|
||||
.and_then(|render| {
|
||||
render
|
||||
.call(
|
||||
&mut running_plugin.store,
|
||||
&[
|
||||
Value::I32(new_rows as i32),
|
||||
Value::I32(new_columns as i32),
|
||||
],
|
||||
)
|
||||
.map_err(anyError::new)
|
||||
})
|
||||
.and_then(|_| {
|
||||
wasi_read_string(&running_plugin.plugin_env.wasi_env)
|
||||
render.call(
|
||||
&mut running_plugin.store,
|
||||
(new_rows as i32, new_columns as i32),
|
||||
)
|
||||
})
|
||||
.and_then(|_| wasi_read_string(running_plugin.store.data()))
|
||||
.with_context(err_context);
|
||||
match rendered_bytes {
|
||||
Ok(rendered_bytes) => {
|
||||
@ -1078,12 +1072,13 @@ impl WasmBridge {
|
||||
};
|
||||
|
||||
running_plugin
|
||||
.plugin_env
|
||||
.store
|
||||
.data_mut()
|
||||
.set_permissions(HashSet::from_iter(permissions.clone()));
|
||||
|
||||
let mut permission_cache = PermissionCache::from_path_or_default(cache_path);
|
||||
permission_cache.cache(
|
||||
running_plugin.plugin_env.plugin.location.to_string(),
|
||||
running_plugin.store.data().plugin.location.to_string(),
|
||||
permissions,
|
||||
);
|
||||
|
||||
@ -1278,30 +1273,25 @@ pub fn apply_event_to_plugin(
|
||||
senders: ThreadSenders,
|
||||
) -> Result<()> {
|
||||
let instance = &running_plugin.instance;
|
||||
let plugin_env = &running_plugin.plugin_env;
|
||||
let rows = running_plugin.rows;
|
||||
let columns = running_plugin.columns;
|
||||
|
||||
let err_context = || format!("Failed to apply event to plugin {plugin_id}");
|
||||
match check_event_permission(plugin_env, event) {
|
||||
match check_event_permission(running_plugin.store.data(), event) {
|
||||
(PermissionStatus::Granted, _) => {
|
||||
let protobuf_event: ProtobufEvent = event
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|e| anyhow!("Failed to convert to protobuf: {:?}", e))?;
|
||||
let update = instance
|
||||
.exports
|
||||
.get_function("update")
|
||||
.get_typed_func::<(), i32>(&mut running_plugin.store, "update")
|
||||
.with_context(err_context)?;
|
||||
wasi_write_object(&plugin_env.wasi_env, &protobuf_event.encode_to_vec())
|
||||
wasi_write_object(running_plugin.store.data(), &protobuf_event.encode_to_vec())
|
||||
.with_context(err_context)?;
|
||||
let update_return = update
|
||||
.call(&mut running_plugin.store, &[])
|
||||
let should_render = update
|
||||
.call(&mut running_plugin.store, ())
|
||||
.with_context(err_context)?;
|
||||
let mut should_render = match update_return.get(0) {
|
||||
Some(Value::I32(n)) => *n == 1,
|
||||
_ => false,
|
||||
};
|
||||
let mut should_render = should_render == 1;
|
||||
if let Event::PermissionRequestResult(..) = event {
|
||||
// we always render in this case, otherwise the request permission screen stays on
|
||||
// screen
|
||||
@ -1309,18 +1299,11 @@ pub fn apply_event_to_plugin(
|
||||
}
|
||||
if rows > 0 && columns > 0 && should_render {
|
||||
let rendered_bytes = instance
|
||||
.exports
|
||||
.get_function("render")
|
||||
.map_err(anyError::new)
|
||||
.get_typed_func::<(i32, i32), ()>(&mut running_plugin.store, "render")
|
||||
.and_then(|render| {
|
||||
render
|
||||
.call(
|
||||
&mut running_plugin.store,
|
||||
&[Value::I32(rows as i32), Value::I32(columns as i32)],
|
||||
)
|
||||
.map_err(anyError::new)
|
||||
render.call(&mut running_plugin.store, (rows as i32, columns as i32))
|
||||
})
|
||||
.and_then(|_| wasi_read_string(&plugin_env.wasi_env))
|
||||
.and_then(|_| wasi_read_string(running_plugin.store.data()))
|
||||
.with_context(err_context)?;
|
||||
let pipes_to_block_or_unblock = pipes_to_block_or_unblock(running_plugin, None);
|
||||
let plugin_render_asset = PluginRenderAsset::new(
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -68,15 +68,15 @@ pub fn configure_logger() {
|
||||
.unwrap();
|
||||
|
||||
// Set the default logging level to "info" and log it to zellij.log file
|
||||
// Decrease verbosity for `wasmer_compiler_cranelift` module because it has a lot of useless info logs
|
||||
// Decrease verbosity for `wasmtime_wasi` module because it has a lot of useless info logs
|
||||
// For `zellij_server::logging_pipe`, we use custom format as we use logging macros to forward stderr output from plugins
|
||||
let config = Config::builder()
|
||||
.appender(Appender::builder().build("logFile", Box::new(log_file)))
|
||||
.appender(Appender::builder().build("logPlugin", Box::new(log_plugin)))
|
||||
.logger(
|
||||
Logger::builder()
|
||||
.appender("logFile")
|
||||
.build("wasmer_compiler_cranelift", LevelFilter::Warn),
|
||||
.appender("logPlugin")
|
||||
.build("wasmtime_wasi", LevelFilter::Warn),
|
||||
)
|
||||
.logger(
|
||||
Logger::builder()
|
||||
|
Loading…
Reference in New Issue
Block a user