mod.rs for vm
This commit is contained in:
0
src/vm/alias.rs
Normal file
0
src/vm/alias.rs
Normal file
76
src/vm/mod.rs
Normal file
76
src/vm/mod.rs
Normal file
@ -0,0 +1,76 @@
|
||||
use mlua::{
|
||||
Function, Lua as Luau, MultiValue, Result as lResult, Table, Value
|
||||
};
|
||||
use crate::vm::{shell::System, terminal::Terminal};
|
||||
use crate::VERSION;
|
||||
use color_print::{cformat, cprintln};
|
||||
use core::fmt;
|
||||
|
||||
mod shell;
|
||||
mod terminal;
|
||||
mod alias;
|
||||
|
||||
trait Helpers {
|
||||
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T>;
|
||||
fn luau_error<T>(&self, err: mlua::Error) -> Option<T>;
|
||||
}
|
||||
impl Helpers for LuauVm {
|
||||
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T> {
|
||||
println!("{err}");
|
||||
None
|
||||
}
|
||||
|
||||
fn luau_error<T>(&self, err: mlua::Error) -> Option<T> {
|
||||
cprintln!("<bold>====</>\n<r><bold>[!]:</> {err}</>\n<bold>====</>");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
trait Globals {
|
||||
fn global_warn(&self, luau_globals: &Table) -> lResult<()>;
|
||||
fn global_version(&self, luau_globals: &Table) -> lResult<()>;
|
||||
}
|
||||
impl Globals for LuauVm {
|
||||
fn global_warn(&self, luau_globals: &Table) -> lResult<()> {
|
||||
let luau_print = luau_globals.get::<Function>("print")?;
|
||||
luau_globals.set("warn", self.0.create_function(move |this, args: MultiValue| -> lResult<()> {
|
||||
let luau_multi_values = args.into_iter()
|
||||
.map(|value| cformat!("<y>{}</>", value.to_string().unwrap_or("<SHELL CONVERSION ERROR>".to_owned())))
|
||||
.map(|arg_v| Value::String(this.create_string(arg_v).unwrap()))
|
||||
.collect::<MultiValue>();
|
||||
luau_print.call::<()>(luau_multi_values).unwrap();
|
||||
Ok(())
|
||||
})?)
|
||||
}
|
||||
|
||||
fn global_version(&self, luau_globals: &Table) -> lResult<()> {
|
||||
let luau_info = luau_globals.get::<String>("_VERSION")?;
|
||||
luau_globals.set("_VERSION", format!("{}, liblambdashell {}", luau_info, VERSION))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LuauVm(pub Luau);
|
||||
impl LuauVm {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self(Luau::new())
|
||||
}
|
||||
|
||||
fn set_shell_globals(&self) -> lResult<()> {
|
||||
let luau_globals = self.0.globals();
|
||||
self.global_warn(&luau_globals)?;
|
||||
self.global_version(&luau_globals)?;
|
||||
self.global_terminal(&luau_globals)?;
|
||||
self.shell_globals(&luau_globals)?;
|
||||
luau_globals.set("getfenv", mlua::Nil)?;
|
||||
luau_globals.set("setfenv", mlua::Nil)?;
|
||||
self.0.sandbox(true)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn exec(&self, source: String) {
|
||||
match self.set_shell_globals() {
|
||||
Ok(()) => self.0.load(source).exec().map_or_else(|exec_err| self.luau_error(exec_err), Some),
|
||||
Err(globals_err) => self.option_display_none(globals_err),
|
||||
};
|
||||
}
|
||||
}
|
52
src/vm/shell.rs
Normal file
52
src/vm/shell.rs
Normal file
@ -0,0 +1,52 @@
|
||||
use mlua::{Result as lResult, Table, Value};
|
||||
use whoami::fallible;
|
||||
use crate::vm::LuauVm;
|
||||
|
||||
const DEFAULT_HOSTNAME: &str = "hostname";
|
||||
|
||||
pub trait PsPrompt {
|
||||
fn ps_prompt(&self) -> lResult<Table>;
|
||||
}
|
||||
impl PsPrompt for LuauVm {
|
||||
fn ps_prompt(&self) -> lResult<Table> {
|
||||
let prompt_table = self.0.create_table()?;
|
||||
let prompt_metatable = self.0.create_table()?;
|
||||
prompt_metatable.set("__index", self.0.create_function(|_, (table, index): (Table, Value)| -> lResult<String> {
|
||||
table.raw_get::<String>(index)
|
||||
})?)?;
|
||||
prompt_metatable.set("__newindex", self.0.create_function(|_, _: String| -> lResult<String> {
|
||||
Ok("placeholder".to_owned())
|
||||
})?)?;
|
||||
prompt_table.set("__metatable", mlua::Nil)?;
|
||||
prompt_table.set_metatable(Some(prompt_metatable));
|
||||
Ok(prompt_table)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait System {
|
||||
fn sys_details(&self, shell: &Table) -> lResult<()>;
|
||||
fn shell_globals(&self, luau_globals: &Table) -> lResult<()>;
|
||||
}
|
||||
impl System for LuauVm {
|
||||
fn sys_details(&self, shell: &Table) -> lResult<()> {
|
||||
let system = self.0.create_table()?;
|
||||
system.set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
|
||||
system.set("DEVICENAME", whoami::devicename().to_string())?;
|
||||
system.set("USERNAME", whoami::username().to_string())?;
|
||||
system.set("REALNAME", whoami::realname().to_string())?;
|
||||
system.set("PLATFORM", whoami::platform().to_string())?;
|
||||
system.set("DISTRO", whoami::distro().to_string())?;
|
||||
system.set("HOSTNAME", fallible::hostname().unwrap_or(DEFAULT_HOSTNAME.to_owned()))?;
|
||||
shell.set("SYSTEM", system)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn shell_globals(&self, luau_globals: &Table) -> lResult<()> {
|
||||
let shell = self.0.create_table()?;
|
||||
let ps_prompt = self.ps_prompt()?;
|
||||
self.sys_details(&shell)?;
|
||||
shell.set("PROMPT", ps_prompt)?;
|
||||
luau_globals.set("SHELL", shell)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
96
src/vm/terminal.rs
Normal file
96
src/vm/terminal.rs
Normal file
@ -0,0 +1,96 @@
|
||||
use mlua::{Result as lResult, Table};
|
||||
use crossterm::style::Stylize;
|
||||
use crate::vm::LuauVm;
|
||||
|
||||
macro_rules! foreground_styles_luau {
|
||||
($self:expr, $style_table:expr, $($color:ident)+) => {
|
||||
$(
|
||||
$style_table.set(stringify!($color).to_ascii_uppercase(), $self.0.create_function(|_, text: String| -> lResult<String> {
|
||||
Ok(text.$color().to_string())
|
||||
})?)?;
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! background_styles_luau {
|
||||
($self:expr, $style_table:expr, $($color:ident)+) => {
|
||||
$(
|
||||
match stringify!($color).split_once("_") {
|
||||
Some((_, color_name)) => $style_table.set(color_name.to_ascii_uppercase(), $self.0.create_function(|_, text: String| -> lResult<String> {
|
||||
Ok(text.$color().to_string())
|
||||
})?)?,
|
||||
None => panic!("Luau set error: {:?}. There was nothing to split from delimiter: \"_\"", stringify!($color)),
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
trait Colors {
|
||||
fn background(&self, style_table: &Table) -> lResult<()>;
|
||||
fn foreground(&self, style_table: &Table) -> lResult<()>;
|
||||
fn styling(&self, term_out_table: &Table) -> lResult<()>;
|
||||
}
|
||||
impl Colors for LuauVm {
|
||||
fn background(&self, style_table: &Table) -> lResult<()> {
|
||||
let foreground_table = self.0.create_table()?;
|
||||
foreground_styles_luau!(self, foreground_table,
|
||||
dark_grey dark_red dark_green dark_cyan
|
||||
dark_yellow dark_magenta dark_blue
|
||||
red grey black green yellow
|
||||
blue magenta cyan white
|
||||
underlined
|
||||
underline_dark_grey underline_dark_red underline_dark_green underline_dark_cyan
|
||||
underline_dark_yellow underline_dark_magenta underline_dark_blue underline_red
|
||||
underline_grey underline_black underline_green underline_yellow
|
||||
underline_blue underline_magenta underline_cyan underline_white
|
||||
bold
|
||||
);
|
||||
style_table.set("FOREGROUND", foreground_table)
|
||||
}
|
||||
|
||||
fn foreground(&self, style_table: &Table) -> lResult<()> {
|
||||
let background_table = self.0.create_table()?;
|
||||
background_styles_luau!(self, background_table,
|
||||
on_dark_grey on_dark_red on_dark_green on_dark_cyan
|
||||
on_dark_yellow on_dark_magenta on_dark_blue
|
||||
on_red on_grey on_black
|
||||
on_green on_yellow
|
||||
on_blue on_magenta
|
||||
on_cyan on_white
|
||||
);
|
||||
style_table.set("BACKGROUND", background_table)
|
||||
}
|
||||
|
||||
fn styling(&self, term_out_table: &Table) -> lResult<()> {
|
||||
let style_table = self.0.create_table()?;
|
||||
self.foreground(&style_table)?;
|
||||
self.background(&style_table)?;
|
||||
term_out_table.set("STYLE", style_table)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
trait Write {
|
||||
fn write(&self, term_out_table: &Table) -> lResult<()>;
|
||||
}
|
||||
impl Write for LuauVm {
|
||||
fn write(&self, term_out_table: &Table) -> lResult<()> {
|
||||
term_out_table.set("WRITE", self.0.create_function(|_, s: String| -> lResult<()> {
|
||||
print!("{s}");
|
||||
Ok(())
|
||||
})?)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Terminal {
|
||||
fn global_terminal(&self, luau_globals: &Table) -> lResult<()>;
|
||||
}
|
||||
impl Terminal for LuauVm {
|
||||
fn global_terminal(&self, luau_globals: &Table) -> lResult<()> {
|
||||
let term_table = self.0.create_table()?;
|
||||
let term_out_table = self.0.create_table()?;
|
||||
term_table.set("OUT", term_out_table)?;
|
||||
luau_globals.set("TERMINAL", &term_table)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user