* 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
81 lines
2.5 KiB
Rust
81 lines
2.5 KiB
Rust
use crate::plugins::plugin_map::PluginEnv;
|
|
use crate::plugins::zellij_exports::wasi_write_object;
|
|
use wasmtime::{Instance, Store};
|
|
|
|
use zellij_utils::async_channel::{unbounded, Receiver, Sender};
|
|
use zellij_utils::async_std::task;
|
|
use zellij_utils::errors::prelude::*;
|
|
use zellij_utils::input::plugins::PluginConfig;
|
|
use zellij_utils::plugin_api::message::ProtobufMessage;
|
|
use zellij_utils::prost::Message;
|
|
|
|
pub struct RunningWorker {
|
|
pub instance: Instance,
|
|
pub name: String,
|
|
pub plugin_config: PluginConfig,
|
|
pub store: Store<PluginEnv>,
|
|
}
|
|
|
|
impl RunningWorker {
|
|
pub fn new(
|
|
store: Store<PluginEnv>,
|
|
instance: Instance,
|
|
name: &str,
|
|
plugin_config: PluginConfig,
|
|
) -> Self {
|
|
RunningWorker {
|
|
store,
|
|
instance,
|
|
name: name.into(),
|
|
plugin_config,
|
|
}
|
|
}
|
|
pub fn send_message(&mut self, message: String, payload: String) -> Result<()> {
|
|
let err_context = || format!("Failed to send message to worker");
|
|
let protobuf_message = ProtobufMessage {
|
|
name: message,
|
|
payload,
|
|
..Default::default()
|
|
};
|
|
let protobuf_bytes = protobuf_message.encode_to_vec();
|
|
let work_function = self
|
|
.instance
|
|
.get_typed_func::<(), ()>(&mut self.store, &self.name)
|
|
.with_context(err_context)?;
|
|
wasi_write_object(self.store.data(), &protobuf_bytes).with_context(err_context)?;
|
|
work_function
|
|
.call(&mut self.store, ())
|
|
.with_context(err_context)?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub enum MessageToWorker {
|
|
Message(String, String), // message, payload
|
|
Exit,
|
|
}
|
|
|
|
pub fn plugin_worker(mut worker: RunningWorker) -> Sender<MessageToWorker> {
|
|
let (sender, receiver): (Sender<MessageToWorker>, Receiver<MessageToWorker>) = unbounded();
|
|
task::spawn({
|
|
async move {
|
|
loop {
|
|
match receiver.recv().await {
|
|
Ok(MessageToWorker::Message(message, payload)) => {
|
|
if let Err(e) = worker.send_message(message, payload) {
|
|
log::error!("Failed to send message to worker: {:?}", e);
|
|
}
|
|
},
|
|
Ok(MessageToWorker::Exit) => {
|
|
break;
|
|
},
|
|
Err(e) => {
|
|
log::error!("Failed to receive worker message on channel: {:?}", e);
|
|
break;
|
|
},
|
|
}
|
|
}
|
|
}
|
|
});
|
|
sender
|
|
}
|