Working SHELL.PROMPT = and SHELL.PROMPT, SHELL is now a userdata

This commit is contained in:
rhpidfyre 2025-01-10 12:05:35 -05:00
parent 97b08d24e3
commit d95366bebe
5 changed files with 65 additions and 70 deletions

View File

@ -11,10 +11,4 @@ home = "0.5.9"
mlua = { version = "0.10.0", features = ["luau-jit", "vendored"] } mlua = { version = "0.10.0", features = ["luau-jit", "vendored"] }
thiserror = "2.0.9" thiserror = "2.0.9"
uzers = "0.12.1" uzers = "0.12.1"
whoami = "1.5.2" whoami = "1.5.2"
[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1

View File

@ -1,27 +1,28 @@
pub const DEFAULT_PS: &str = concat!("lambdashell-", env!("CARGO_PKG_VERSION"), " "); pub const DEFAULT_PS: &str = concat!("lambdashell-", env!("CARGO_PKG_VERSION"), " ");
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct Ps(String); pub struct Ps(String);
impl Ps { impl Ps {
pub fn set(prompt: String) -> Self { pub const fn set(prompt: String) -> Self {
Self(prompt) Self(prompt)
} }
pub fn get(self) -> String { //rustc: `std::string::String::as_str` is not yet stable as a const fn
self.0 pub fn get(&self) -> &str {
self.0.as_str()
}
pub fn modify(&mut self, prompt: String) {
self.0 = prompt
} }
pub fn display(&self) { pub fn display(&self) {
print!("{}", self.0); print!("{}", self.0);
} }
pub fn working_dir_name(&self) -> String { pub fn working_dir_name(&self) -> String {
std::env::current_dir().map_or("?".to_owned(), |path| { std::env::current_dir().map_or("?".to_owned(), |path| path.file_name().map_or("?".to_owned(), |name| {
path.file_name().map_or("?".to_owned(), |name| { match name.to_os_string() == whoami::username_os() {
let name_os_string = name.to_os_string(); true => "~".to_owned(),
match name_os_string == whoami::username_os() && name_os_string != "root" { false => name.to_string_lossy().to_string(),
true => "~".to_owned(), }
false => name.to_string_lossy().to_string(), }))
}
})
})
} }
} }

View File

@ -1,4 +1,4 @@
use std::{fs, io::{self}}; use std::{cell::RefCell, fs, io::{self}, rc::Rc};
use core::fmt; use core::fmt;
use crate::{ use crate::{
@ -14,16 +14,16 @@ pub struct LambdaShell {
terminate: bool, terminate: bool,
config: Config, config: Config,
vm: LuauVm, vm: LuauVm,
ps: Ps, ps: Rc<RefCell<Ps>>,
} }
impl LambdaShell { impl LambdaShell {
pub fn create(config: Config) -> Self { pub fn create(config: Config) -> Self {
let ps = Ps::set(ps::DEFAULT_PS.to_owned()); let ps = Rc::new(RefCell::new(Ps::set(ps::DEFAULT_PS.to_owned())));
Self { Self {
vm: vm::LuauVm::new(ps.to_owned()), ps: Rc::clone(&ps),
vm: vm::LuauVm::new(ps),
terminate: false, terminate: false,
config, config,
ps
} }
} }
@ -48,12 +48,12 @@ impl LambdaShell {
fs::read_to_string(conf_file).map_or_display(|luau_conf| self.vm_exec(luau_conf)); fs::read_to_string(conf_file).map_or_display(|luau_conf| self.vm_exec(luau_conf));
} }
} }
self.ps.display();
self.ps.borrow().display();
loop { loop {
if self.terminate { break } else { if self.terminate { break } else {
match self.wait() { match self.wait() {
Ok(()) => self.ps.display(), Ok(()) => self.ps.borrow().display(),
Err(flush_err) => self.error(flush_err), Err(flush_err) => self.error(flush_err),
} }
} }

View File

@ -1,5 +1,6 @@
use mlua::{Function, Lua as Luau, MultiValue, Result as lResult, Table, Value}; use mlua::{Function, Lua as Luau, MultiValue, Result as lResult, Table, Value};
use color_print::{cformat, ceprintln}; use color_print::{cformat, ceprintln};
use std::{cell::RefCell, rc::Rc};
use core::fmt; use core::fmt;
use shell::Shell; use shell::Shell;
@ -34,7 +35,7 @@ impl Globals for LuauVm {
fn global_warn(&self, luau_globals: &Table) -> lResult<()> { fn global_warn(&self, luau_globals: &Table) -> lResult<()> {
let luau_print = luau_globals.get::<Function>("print")?; let luau_print = luau_globals.get::<Function>("print")?;
luau_globals.set("warn", self.vm.create_function(move |this, args: MultiValue| -> lResult<()> { luau_globals.raw_set("warn", self.vm.create_function(move |this, args: MultiValue| -> lResult<()> {
let luau_multi_values = args.into_iter() let luau_multi_values = args.into_iter()
.map(|value| cformat!("<bold,y>{}</>", value.to_string().unwrap_or(Self::CONV_ERROR.to_owned()))) .map(|value| cformat!("<bold,y>{}</>", value.to_string().unwrap_or(Self::CONV_ERROR.to_owned())))
.map(|arg_v| Value::String(this.create_string(arg_v).unwrap())) .map(|arg_v| Value::String(this.create_string(arg_v).unwrap()))
@ -46,27 +47,30 @@ impl Globals for LuauVm {
fn global_version(&self, luau_globals: &Table) -> lResult<()> { fn global_version(&self, luau_globals: &Table) -> lResult<()> {
let luau_info = luau_globals.get::<String>("_VERSION")?; let luau_info = luau_globals.get::<String>("_VERSION")?;
luau_globals.set("_VERSION", format!("{luau_info}, liblambdashell {}", Self::LIB_VERSION)) luau_globals.raw_set("_VERSION", format!("{luau_info}, liblambdashell {}", Self::LIB_VERSION))
} }
} }
pub struct LuauVm { pub struct LuauVm {
vm: Luau, vm: Luau,
ps: Ps, ps: Rc<RefCell<Ps>>
} }
impl LuauVm { impl LuauVm {
pub(crate) fn new(ps: Ps) -> Self { pub(crate) fn new(ps: Rc<RefCell<Ps>>) -> Self {
Self { vm: Luau::new(), ps } Self {
vm: Luau::new(),
ps
}
} }
fn set_shell_globals(&self) -> lResult<()> { fn set_shell_globals(&self) -> lResult<()> {
let luau_globals = self.vm.globals(); let luau_globals = self.vm.globals();
self.global_shell(&luau_globals)?;
self.global_terminal(&luau_globals)?;
self.global_warn(&luau_globals)?; self.global_warn(&luau_globals)?;
self.global_version(&luau_globals)?; self.global_version(&luau_globals)?;
self.global_terminal(&luau_globals)?; luau_globals.raw_set("getfenv", mlua::Nil)?;
self.global_shell(&luau_globals)?; luau_globals.raw_set("setfenv", mlua::Nil)?;
luau_globals.set("getfenv", mlua::Nil)?;
luau_globals.set("setfenv", mlua::Nil)?;
self.vm.sandbox(true)?; self.vm.sandbox(true)?;
Ok(()) Ok(())
} }

View File

@ -1,28 +1,8 @@
use mlua::{Result as lResult, Table}; use mlua::{MetaMethod, Result as lResult, Table, UserData, UserDataFields, UserDataMethods};
use std::{cell::RefCell, rc::Rc};
use whoami::fallible; use whoami::fallible;
use crate::vm::LuauVm; use crate::{ps::Ps, vm::LuauVm};
trait PsPrompt {
fn ps_prompt(&self) -> lResult<Table>;
}
impl PsPrompt for LuauVm {
fn ps_prompt(&self) -> lResult<Table> {
let prompt_table = self.vm.create_table()?;
let prompt_metatable = self.vm.create_table()?;
let ps_owned = self.ps.to_owned();
prompt_metatable.set("__index", self.vm.create_function(move |_, (s, s1): (Table, String)| -> lResult<String> {
Ok(ps_owned.clone().get())
})?)?;
prompt_metatable.set("__newindex", self.vm.create_function(|_, _: String| -> lResult<String> {
Ok("placeholder".to_owned())
})?)?;
// prompt_table.set("__metatable", mlua::Nil)?;
prompt_table.set_metatable(Some(prompt_metatable));
prompt_table.set_readonly(false);
Ok(prompt_table)
}
}
trait System { trait System {
const DEFAULT_HOSTNAME: &str; const DEFAULT_HOSTNAME: &str;
@ -33,26 +13,42 @@ impl System for LuauVm {
fn sys_details(&self) -> lResult<Table> { fn sys_details(&self) -> lResult<Table> {
let system = self.vm.create_table()?; let system = self.vm.create_table()?;
system.set("DESKTOP_ENV", whoami::desktop_env().to_string())?; system.raw_set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
system.set("DEVICENAME", whoami::devicename().to_string())?; system.raw_set("DEVICENAME", whoami::devicename().to_string())?;
system.set("USERNAME", whoami::username().to_string())?; system.raw_set("USERNAME", whoami::username().to_string())?;
system.set("REALNAME", whoami::realname().to_string())?; system.raw_set("REALNAME", whoami::realname().to_string())?;
system.set("PLATFORM", whoami::platform().to_string())?; system.raw_set("PLATFORM", whoami::platform().to_string())?;
system.set("DISTRO", whoami::distro().to_string())?; system.raw_set("DISTRO", whoami::distro().to_string())?;
system.set("HOSTNAME", fallible::hostname().unwrap_or(Self::DEFAULT_HOSTNAME.to_owned()))?; system.raw_set("ARCH", whoami::arch().to_string())?;
system.raw_set("HOSTNAME", fallible::hostname().unwrap_or(Self::DEFAULT_HOSTNAME.to_owned()))?;
Ok(system) Ok(system)
} }
} }
struct ShellUserdata(Rc<RefCell<Ps>>);
impl UserData for ShellUserdata {
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("PROMPT", |_, this| Ok(this.0.borrow().get().to_owned()));
}
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (tindex, tvalue): (String, String)| -> lResult<()> {
if tindex == "PROMPT" {
this.0.borrow_mut().modify(tvalue);
}
Ok(())
});
}
}
pub trait Shell { pub trait Shell {
fn global_shell(&self, luau_globals: &Table) -> lResult<()>; fn global_shell(&self, luau_globals: &Table) -> lResult<()>;
} }
impl Shell for LuauVm { impl Shell for LuauVm {
fn global_shell(&self, luau_globals: &Table) -> lResult<()> { fn global_shell(&self, luau_globals: &Table) -> lResult<()> {
let shell = self.vm.create_table()?; // let shell = self.vm.create_table()?;
shell.set("SYSTEM", self.sys_details()?)?; // shell.set("SYSTEM", self.sys_details()?)?;
shell.set("PROMPT", self.ps_prompt()?)?; // shell.set("PROMPT", self.ps_prompt()?)?;
luau_globals.set("SHELL", shell)?; luau_globals.set("SHELL", ShellUserdata(Rc::clone(&self.ps)))?;
Ok(()) Ok(())
} }
} }