Merge branch 'fix/rhai-repl'

This commit is contained in:
Able 2022-02-20 05:08:28 -06:00
commit 5876cb6049
9 changed files with 107 additions and 38 deletions

View file

@ -12,12 +12,6 @@ run-args = [
"-cpu", "-cpu",
"Broadwell-v3", "Broadwell-v3",
"-serial", "-serial",
"stdio", "stdio",
"-smp", "-smp",

View file

@ -0,0 +1,17 @@
pub struct KeyEvent{
lctrl 1
rctrl 2
lalt 3
ralt 4
lsup 5
rsup 6
lshift 7
rshift 8
caps 9
down 10
# Keycodes
key 11-32
}

View file

@ -1,7 +1,8 @@
use crate::{ use crate::{
arch::{drivers::vga::WRITER, gdt}, arch::{drivers::vga::WRITER, gdt},
kernel_state::KERNEL_STATE, kernel_state::KERNEL_STATE,
print, println, KEYBUFF, print, println,
rhai_shell::KEYBUFF,
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -90,6 +91,7 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac
// Backspace // Backspace
8 => { 8 => {
WRITER.lock().backspace(); WRITER.lock().backspace();
KEYBUFF.lock().push(8_u8 as _);
// print!(" "); // print!(" ");
// WRITER.lock().backspace(); // WRITER.lock().backspace();
} }

View file

@ -7,7 +7,7 @@ lazy_static! {
} }
pub struct KernelInternalState { pub struct KernelInternalState {
hostname: String, pub hostname: String,
should_shutdown: bool, should_shutdown: bool,
} }

View file

@ -33,6 +33,7 @@ pub mod arch;
#[macro_use] #[macro_use]
pub mod print; pub mod print;
pub mod devices; pub mod devices;
pub mod rhai_shell;
pub mod wasm_jumploader; pub mod wasm_jumploader;
#[macro_use] #[macro_use]

View file

@ -22,7 +22,7 @@ impl log::Log for SimpleLogger {
let color; let color;
let time = TICK.load(Ordering::Relaxed) as f64; let time = TICK.load(Ordering::Relaxed) as f64;
let time_float = time / TIMER_INTERRUPT_HERTZ; let time_float = time;
match record.level() { match record.level() {
log::Level::Error => color = (Fg::Red, "$RED$"), log::Level::Error => color = (Fg::Red, "$RED$"),

View file

@ -48,12 +48,6 @@ macro_rules! print {
} }
#[macro_export] #[macro_export]
macro_rules! println { macro_rules! println {
// TODO: The panic here should not be here
() =>{
// ::core::writeln!($crate::print::Stdout, "\n")
panic![];
};
($($tt:tt)*) => { ($($tt:tt)*) => {
::core::writeln!($crate::print::Stdout, $($tt)*) ::core::writeln!($crate::print::Stdout, $($tt)*)
// panic![]; // panic![];

View file

@ -0,0 +1,81 @@
use alloc::vec::Vec;
pub fn rhai_shell() {
let engine = engine_construction();
let mut scope = rhai::Scope::new();
let mut buf = String::new();
print!("> ");
loop {
match x86_64::instructions::interrupts::without_interrupts(|| KEYBUFF.lock().pop()) {
Some('\n') => {
match engine.eval_with_scope::<rhai::Dynamic>(&mut scope, &buf) {
Ok(o) => println!("{o}"),
Err(e) => println!("Eval error: {e}"),
};
buf.clear();
print!("> ");
}
Some('\u{0008}') => {
buf.pop();
}
Some(chr) => buf.push(chr),
None => (),
}
}
}
lazy_static::lazy_static!(
pub static ref KEYBUFF: spin::Mutex<Vec<char>> = spin::Mutex::new(
Vec::new())
;
);
use alloc::string::{String, ToString};
use rhai::Engine;
use x86_64::instructions::interrupts::{disable, enable};
use crate::{
arch::{shutdown, sloop},
kmain::{tick, TICK},
systeminfo::{KERNEL_VERSION, RELEASE_TYPE},
KERNEL_STATE,
};
pub fn afetch() {
let kstate = KERNEL_STATE.lock();
use core::sync::atomic::Ordering::*;
disable();
let tick_time = TICK.load(Relaxed);
enable();
println!("OS: AbleOS");
println!("Host: {}", kstate.hostname);
println!("Kernel: AKern-{}-v{}", RELEASE_TYPE, KERNEL_VERSION);
println!("Uptime: {}", tick_time);
drop(kstate);
}
pub fn set_hostname(name: String) {
let mut kstate = KERNEL_STATE.lock();
kstate.hostname = name;
}
fn engine_construction() -> Engine {
let mut engine = rhai::Engine::new();
engine.on_print(|x| println!("{}", x));
engine.on_debug(|x, src, pos| {
let src = src.unwrap_or("unknown");
println!("DEBUG: {} at {:?}: {}", src, pos, x);
debug!("{} at {:?}: {}", src, pos, x);
});
engine.register_fn("afetch", afetch);
engine.register_fn("set_hostname", set_hostname);
engine.register_fn("shutdown", shutdown);
engine
}

View file

@ -1,3 +1,5 @@
use crate::rhai_shell::rhai_shell;
use { use {
crate::{ crate::{
devices::{pci_inner::DeviceClass, Device, DEVICE_TABLE}, devices::{pci_inner::DeviceClass, Device, DEVICE_TABLE},
@ -36,7 +38,7 @@ pub fn scratchpad() {
*/ */
// interp(); // interp();
// rhai_shell(); rhai_shell();
} }
pub struct PciIO {} pub struct PciIO {}
@ -92,25 +94,3 @@ impl ProcessMessage {
} }
} }
*/ */
pub fn rhai_shell() {
let mut engine = rhai::Engine::new();
let ret = engine.eval::<i64>("1 + 2");
match ret {
Ok(x) => println!("{}", x),
Err(e) => println!("{}", e),
}
loop {}
}
lazy_static::lazy_static!(
pub static ref KEYBUFF: spin::Mutex<Vec<char>> = spin::Mutex::new(
Vec::new())
;
);
use alloc::string::String;
use crate::arch::sloop;