mod.rs for vm
This commit is contained in:
parent
e06a4a6c72
commit
492a789109
15
src/lib.rs
15
src/lib.rs
@ -1,15 +1,8 @@
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
pub mod session;
|
||||
mod commands;
|
||||
mod ps;
|
||||
mod rc;
|
||||
pub mod commands;
|
||||
pub mod ps;
|
||||
pub mod rc;
|
||||
|
||||
#[path = "./luau/vm.rs"]
|
||||
mod vm;
|
||||
#[path = "./luau/alias.rs"]
|
||||
mod alias;
|
||||
#[path = "./luau/terminal.rs"]
|
||||
mod terminal;
|
||||
#[path = "./luau/shell.rs"]
|
||||
mod shell;
|
||||
pub mod vm;
|
@ -1,4 +1,4 @@
|
||||
use crate::{commands, ps, rc, vm::{self, LuauVm}};
|
||||
use crate::{commands, ps, rc, vm::{LuauVm, self}};
|
||||
use std::{fs, io::{self}};
|
||||
|
||||
pub struct Config {
|
||||
|
@ -1,38 +1,14 @@
|
||||
use mlua::{
|
||||
Lua as Luau,
|
||||
Result as lResult,
|
||||
Function,
|
||||
MultiValue,
|
||||
Table
|
||||
Function, Lua as Luau, MultiValue, Result as lResult, Table, Value
|
||||
};
|
||||
use crate::{sytem::System, terminal::TerminalColors, VERSION};
|
||||
use crate::vm::{shell::System, terminal::Terminal};
|
||||
use crate::VERSION;
|
||||
use color_print::{cformat, cprintln};
|
||||
use core::fmt;
|
||||
|
||||
trait Globals {
|
||||
fn warn(&self, luau_globals: &Table) -> lResult<()>;
|
||||
fn version(&self, luau_globals: &Table) -> lResult<()>;
|
||||
}
|
||||
impl Globals for LuauVm {
|
||||
fn 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())))
|
||||
.collect::<Vec<String>>();
|
||||
let back_to_luau_multi = luau_multi_values.into_iter()
|
||||
.map(|arg_v| mlua::Value::String(this.create_string(&arg_v).unwrap()))
|
||||
.collect::<MultiValue>();
|
||||
luau_print.call::<()>(back_to_luau_multi).unwrap();
|
||||
Ok(())
|
||||
})?)
|
||||
}
|
||||
|
||||
fn version(&self, luau_globals: &Table) -> lResult<()> {
|
||||
let luau_info = luau_globals.get::<String>("_VERSION")?;
|
||||
luau_globals.set("_VERSION", format!("{}, liblambdashell {}", luau_info, VERSION))
|
||||
}
|
||||
}
|
||||
mod shell;
|
||||
mod terminal;
|
||||
mod alias;
|
||||
|
||||
trait Helpers {
|
||||
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T>;
|
||||
@ -43,23 +19,47 @@ impl Helpers for LuauVm {
|
||||
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 fn new() -> Self {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self(Luau::new())
|
||||
}
|
||||
|
||||
fn set_shell_globals(&self) -> lResult<()> {
|
||||
let luau_globals = self.0.globals();
|
||||
self.warn(&luau_globals)?;
|
||||
self.version(&luau_globals)?;
|
||||
self.terminal(&luau_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)?;
|
@ -25,6 +25,7 @@ macro_rules! background_styles_luau {
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
trait Colors {
|
||||
fn background(&self, style_table: &Table) -> lResult<()>;
|
||||
fn foreground(&self, style_table: &Table) -> lResult<()>;
|
||||
@ -69,6 +70,7 @@ impl Colors for LuauVm {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
trait Write {
|
||||
fn write(&self, term_out_table: &Table) -> lResult<()>;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user