Merge pull request from GhostedGaming/main
Added Lua standard library
This commit is contained in:
commit
7093448efe
3 changed files with 43 additions and 10 deletions
|
@ -20,6 +20,7 @@ regex = "1.11.1"
|
||||||
rustyline = { version = "15.0.0", features = ["derive", "rustyline-derive"] }
|
rustyline = { version = "15.0.0", features = ["derive", "rustyline-derive"] }
|
||||||
tokio = { version = "1.42.0", features = ["macros", "parking_lot", "rt", "rt-multi-thread"] }
|
tokio = { version = "1.42.0", features = ["macros", "parking_lot", "rt", "rt-multi-thread"] }
|
||||||
|
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
debug-assertions = true
|
debug-assertions = true
|
||||||
|
|
||||||
|
|
9
engine/src/core/repl/Re-exports.rs
Normal file
9
engine/src/core/repl/Re-exports.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
pub use renderer::*;
|
||||||
|
pub use window::*;
|
||||||
|
pub use crate::core::*;
|
||||||
|
pub use material::*;
|
||||||
|
pub use effect::*;
|
||||||
|
pub use light::*;
|
||||||
|
pub use geometry::*;
|
||||||
|
pub use object::*;
|
||||||
|
pub use control::*;
|
|
@ -1,4 +1,4 @@
|
||||||
use mlua::{Lua, MultiValue, Number};
|
use mlua::{Function, Lua, LuaOptions, MultiValue, Number, Value::Nil};
|
||||||
use rustyline::{error::ReadlineError, DefaultEditor};
|
use rustyline::{error::ReadlineError, DefaultEditor};
|
||||||
use crate::core::repl::handler::Command;
|
use crate::core::repl::handler::Command;
|
||||||
|
|
||||||
|
@ -9,27 +9,39 @@ impl Command for ZLua {
|
||||||
fn execute(&self, _args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
fn execute(&self, _args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
|
||||||
let time = chrono::Local::now().format("%H:%M:%S.%3f").to_string();
|
let time = chrono::Local::now().format("%H:%M:%S.%3f").to_string();
|
||||||
let prompt = format!("[{}/{}] {}", time, "ZLUA", ">>\t");
|
let prompt = format!("[{}/{}] {}", time, "ZLUA", ">>\t");
|
||||||
let lua = Lua::new();
|
let lua = Lua::new_with(
|
||||||
|
mlua::StdLib::ALL_SAFE,
|
||||||
|
LuaOptions::default()
|
||||||
|
)?;
|
||||||
let globals = lua.globals();
|
let globals = lua.globals();
|
||||||
|
//This just adds 2 numbers together
|
||||||
let add = lua.create_function(|_, (number1,number2):(i32,i32)|{
|
let add = lua.create_function(|_, (number1,number2):(i32,i32)|{
|
||||||
let result = number1 + number2;
|
let result = number1 + number2;
|
||||||
println!("{result}");
|
println!("{result}");
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
|
globals.set("add", add)?;
|
||||||
|
|
||||||
let is_equal = lua.create_function(|_, (list1, list2): (i32, i32)| {
|
let is_equal = lua.create_function(|_, (list1, list2): (i32, i32)| {
|
||||||
// This function just checks whether two string lists are equal, and in an inefficient way.
|
if list1 == 0 || list2 == 0 {
|
||||||
// Lua callbacks return `mlua::Result`, an Ok value is a normal return, and an Err return
|
return Err(mlua::Error::RuntimeError("Zero values not allowed".to_string()));
|
||||||
// turns into a Lua 'error'. Again, any type that is convertible to Lua may be returned.
|
}
|
||||||
Ok(list1 == list2)
|
Ok(list1 == list2)
|
||||||
})?;
|
})?;
|
||||||
globals.set("isequal", is_equal)?;
|
globals.set("isEqual", is_equal)?;
|
||||||
|
|
||||||
let log = lua.create_function(|_, (msg,): (String,)| {
|
let log = lua.create_function(|_, (msg,): (String,)| {
|
||||||
println!("{}", msg);
|
println!("{}", msg);
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
globals.set("log", log)?;
|
globals.set("log", log)?;
|
||||||
|
|
||||||
|
let fail_safe = lua.create_function(|_,()|{
|
||||||
|
println!("Failed");
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
globals.set("failSafe",fail_safe)?;
|
||||||
let mut editor = DefaultEditor::new().expect("Failed to create editor");
|
let mut editor = DefaultEditor::new().expect("Failed to create editor");
|
||||||
globals.set("add", add)?;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut prompt = &prompt;
|
let mut prompt = &prompt;
|
||||||
|
@ -47,6 +59,15 @@ impl Command for ZLua {
|
||||||
|
|
||||||
match lua.load(&line).eval::<MultiValue>() {
|
match lua.load(&line).eval::<MultiValue>() {
|
||||||
Ok(values) => {
|
Ok(values) => {
|
||||||
|
for value in &values {
|
||||||
|
match value {
|
||||||
|
mlua::Value::Nil => println!("Got nil value"),
|
||||||
|
mlua::Value::Number(n) => println!("Got number: {}", n),
|
||||||
|
mlua::Value::String(s) => println!("Got string: {}", s.to_str()?),
|
||||||
|
mlua::Value::Boolean(b) => println!("Got boolean: {}", b),
|
||||||
|
_ => eprintln!("Got unexpected type: {:#?}", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
editor.add_history_entry(line).unwrap();
|
editor.add_history_entry(line).unwrap();
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
|
@ -68,9 +89,11 @@ impl Command for ZLua {
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("error: {}", e);
|
eprintln!("Error: {} at line {}", e, line.lines().count());
|
||||||
|
eprintln!("Input that caused error: {}", line);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue