2024-09-18 18:47:38 -07:00
|
|
|
use crate::context::Context;
|
|
|
|
|
2024-09-17 17:17:12 -07:00
|
|
|
use super::vector3::Vector3;
|
|
|
|
use super::cframe::CFrame;
|
|
|
|
|
2024-09-17 17:16:57 -07:00
|
|
|
pub struct Runner{
|
|
|
|
lua:mlua::Lua,
|
2024-09-16 18:54:04 -07:00
|
|
|
}
|
2024-09-18 18:47:38 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error{
|
2024-09-20 18:01:25 -07:00
|
|
|
Lua{
|
|
|
|
source:String,
|
|
|
|
error:mlua::Error
|
|
|
|
},
|
2024-09-18 18:47:38 -07:00
|
|
|
Script(crate::script::Error),
|
|
|
|
/// If the lua.remove_app_data function fails
|
|
|
|
RemoveAppData,
|
|
|
|
}
|
|
|
|
impl Error{
|
|
|
|
pub fn print(self){
|
|
|
|
match self{
|
2024-09-20 18:01:25 -07:00
|
|
|
Self::Lua{source,error:mlua::Error::RuntimeError(s)}=>{
|
|
|
|
println!("lua error: {s}\nsource:{source}");
|
2024-09-18 18:47:38 -07:00
|
|
|
},
|
|
|
|
other=>println!("{:?}",other),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-16 18:54:04 -07:00
|
|
|
|
2024-09-17 17:16:57 -07:00
|
|
|
fn init(lua:&mlua::Lua)->mlua::Result<()>{
|
2024-09-17 17:17:12 -07:00
|
|
|
lua.sandbox(true)?;
|
|
|
|
|
|
|
|
//global environment
|
|
|
|
let globals=lua.globals();
|
|
|
|
|
|
|
|
//Vector3
|
|
|
|
{
|
|
|
|
let vector3_table=lua.create_table()?;
|
|
|
|
|
|
|
|
//Vector3.new
|
|
|
|
vector3_table.raw_set("new",
|
|
|
|
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
|
|
|
|
Ok(ctx.create_userdata(Vector3::new(x,y,z)))
|
|
|
|
)?
|
|
|
|
)?;
|
|
|
|
|
|
|
|
globals.set("Vector3",vector3_table)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
//CFrame
|
|
|
|
{
|
|
|
|
let cframe_table=lua.create_table()?;
|
|
|
|
|
|
|
|
//CFrame.new
|
|
|
|
cframe_table.raw_set("new",
|
|
|
|
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
|
|
|
|
Ok(ctx.create_userdata(CFrame::new(x,y,z)))
|
|
|
|
)?
|
|
|
|
)?;
|
|
|
|
|
|
|
|
//CFrame.Angles
|
|
|
|
cframe_table.raw_set("Angles",
|
|
|
|
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
|
|
|
|
Ok(ctx.create_userdata(CFrame::angles(x,y,z)))
|
|
|
|
)?
|
|
|
|
)?;
|
|
|
|
|
|
|
|
globals.set("CFrame",cframe_table)?;
|
|
|
|
}
|
|
|
|
|
2024-09-16 19:01:16 -07:00
|
|
|
Ok(())
|
2024-09-16 18:54:04 -07:00
|
|
|
}
|
2024-09-17 17:16:57 -07:00
|
|
|
|
|
|
|
impl Runner{
|
|
|
|
pub fn new()->mlua::Result<Self>{
|
|
|
|
let runner=Self{
|
|
|
|
lua:mlua::Lua::new(),
|
|
|
|
};
|
|
|
|
init(&runner.lua)?;
|
|
|
|
Ok(runner)
|
|
|
|
}
|
2024-09-20 13:51:17 -07:00
|
|
|
pub fn run_script(&self,script:crate::script::Script,context:&mut Context)->Result<(),Error>{
|
2024-09-21 12:53:03 -07:00
|
|
|
let (name,source)=script.name_source(context).map_err(Error::Script)?;
|
|
|
|
self.lua.globals().set("script",super::instance::Instance::from(script)).map_err(|error|Error::Lua{source:source.clone(),error})?;
|
2024-09-20 13:51:17 -07:00
|
|
|
//this makes set_app_data shut up about the lifetime
|
|
|
|
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
|
2024-09-20 18:01:25 -07:00
|
|
|
let r=self.lua.load(source.as_str())
|
2024-09-20 13:51:17 -07:00
|
|
|
.set_name(name)
|
2024-09-20 18:01:25 -07:00
|
|
|
.exec().map_err(|error|Error::Lua{source,error});
|
2024-09-20 13:51:17 -07:00
|
|
|
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
|
2024-09-20 18:01:25 -07:00
|
|
|
r?;
|
2024-09-20 13:51:17 -07:00
|
|
|
Ok(())
|
2024-09-17 17:16:57 -07:00
|
|
|
}
|
|
|
|
}
|