diff options
| author | Brooks J Rady <b.j.rady@gmail.com> | 2021-04-27 14:57:54 +0100 |
|---|---|---|
| committer | Brooks J Rady <b.j.rady@gmail.com> | 2021-04-27 14:57:54 +0100 |
| commit | 2814c3027271d15b4af345bdde03591bb189cbce (patch) | |
| tree | 192290907893d4454c23360f1c5f6f66118af30d /src | |
| parent | fix(ci): formatting tweak (diff) | |
| download | sl0ppij-2814c3027271d15b4af345bdde03591bb189cbce.tar.gz sl0ppij-2814c3027271d15b4af345bdde03591bb189cbce.tar.xz sl0ppij-2814c3027271d15b4af345bdde03591bb189cbce.zip | |
feat(plugin): added the `get_plugin_ids()` query function
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/mod.rs | 10 | ||||
| -rw-r--r-- | src/common/wasm_vm.rs | 59 |
2 files changed, 44 insertions, 25 deletions
diff --git a/src/common/mod.rs b/src/common/mod.rs index 79e48780..d525a3fe 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -40,8 +40,7 @@ use pty_bus::{PtyBus, PtyInstruction}; use screen::{Screen, ScreenInstruction}; use serde::{Deserialize, Serialize}; use utils::consts::ZELLIJ_IPC_PIPE; -use wasm_vm::PluginEnv; -use wasm_vm::{wasi_stdout, wasi_write_string, zellij_imports, PluginInstruction}; +use wasm_vm::{wasi_stdout, wasi_write_json, zellij_exports, PluginEnv, PluginInstruction}; use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value}; use wasmer_wasi::{Pipe, WasiState}; use zellij_tile::data::{EventType, ModeInfo}; @@ -506,7 +505,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) { subscriptions: Arc::new(Mutex::new(HashSet::new())), }; - let zellij = zellij_imports(&store, &plugin_env); + let zellij = zellij_exports(&store, &plugin_env); let instance = Instance::new(&module, &zellij.chain_back(wasi)).unwrap(); let start = instance.exports.get_function("_start").unwrap(); @@ -525,10 +524,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) { let event_type = EventType::from_str(&event.to_string()).unwrap(); if (pid.is_none() || pid == Some(i)) && subs.contains(&event_type) { let update = instance.exports.get_function("update").unwrap(); - wasi_write_string( - &plugin_env.wasi_env, - &serde_json::to_string(&event).unwrap(), - ); + wasi_write_json(&plugin_env.wasi_env, &event); update.call(&[]).unwrap(); } } diff --git a/src/common/wasm_vm.rs b/src/common/wasm_vm.rs index 29b110ae..ef3df61a 100644 --- a/src/common/wasm_vm.rs +++ b/src/common/wasm_vm.rs @@ -1,11 +1,13 @@ +use serde::Serialize; use std::{ collections::HashSet, path::PathBuf, + process, sync::{mpsc::Sender, Arc, Mutex}, }; use wasmer::{imports, Function, ImportObject, Store, WasmerEnv}; use wasmer_wasi::WasiEnv; -use zellij_tile::data::{Event, EventType}; +use zellij_tile::data::{Event, EventType, PluginIds}; use super::{ pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext, @@ -32,17 +34,26 @@ pub struct PluginEnv { // Plugin API --------------------------------------------------------------------------------------------------------- -pub fn zellij_imports(store: &Store, plugin_env: &PluginEnv) -> ImportObject { - imports! { - "zellij" => { - "host_subscribe" => Function::new_native_with_env(store, plugin_env.clone(), host_subscribe), - "host_unsubscribe" => Function::new_native_with_env(store, plugin_env.clone(), host_unsubscribe), - "host_open_file" => Function::new_native_with_env(store, plugin_env.clone(), host_open_file), - "host_set_invisible_borders" => Function::new_native_with_env(store, plugin_env.clone(), host_set_invisible_borders), - "host_set_max_height" => Function::new_native_with_env(store, plugin_env.clone(), host_set_max_height), - "host_set_selectable" => Function::new_native_with_env(store, plugin_env.clone(), host_set_selectable), +pub fn zellij_exports(store: &Store, plugin_env: &PluginEnv) -> ImportObject { + macro_rules! zellij_export { + ($($host_function:ident),+ $(,)?) => { + imports! { + "zellij" => { + $("$host_function" => Function::new_native_with_env(store, plugin_env.clone(), $host_function),)+ + } + } } } + + zellij_export! { + host_subscribe, + host_unsubscribe, + host_set_invisible_borders, + host_set_max_height, + host_set_selectable, + host_get_plugin_ids, + host_open_file, + } } fn host_subscribe(plugin_env: &PluginEnv) { @@ -57,14 +68,6 @@ fn host_unsubscribe(plugin_env: &PluginEnv) { subscriptions.retain(|k| !old.contains(k)); } -fn host_open_file(plugin_env: &PluginEnv) { - let path = PathBuf::from(wasi_stdout(&plugin_env.wasi_env).lines().next().unwrap()); - plugin_env - .send_pty_instructions - .send(PtyInstruction::SpawnTerminal(Some(path))) - .unwrap(); -} - fn host_set_selectable(plugin_env: &PluginEnv, selectable: i32) { let selectable = selectable != 0; plugin_env @@ -98,6 +101,22 @@ fn host_set_invisible_borders(plugin_env: &PluginEnv, invisible_borders: i32) { .unwrap() } +fn host_get_plugin_ids(plugin_env: &PluginEnv) { + let ids = PluginIds { + plugin_id: plugin_env.plugin_id, + zellij_pid: process::id(), + }; + wasi_write_json(&plugin_env.wasi_env, &ids); +} + +fn host_open_file(plugin_env: &PluginEnv) { + let path = PathBuf::from(wasi_stdout(&plugin_env.wasi_env).lines().next().unwrap()); + plugin_env + .send_pty_instructions + .send(PtyInstruction::SpawnTerminal(Some(path))) + .unwrap(); +} + // Helper Functions --------------------------------------------------------------------------------------------------- // FIXME: Unwrap city @@ -114,3 +133,7 @@ pub fn wasi_write_string(wasi_env: &WasiEnv, buf: &str) { let wasi_file = state.fs.stdin_mut().unwrap().as_mut().unwrap(); writeln!(wasi_file, "{}\r", buf).unwrap(); } + +pub fn wasi_write_json(wasi_env: &WasiEnv, object: &impl Serialize) { + wasi_write_string(wasi_env, &serde_json::to_string(&object).unwrap()); +} |
