mirror of
https://github.com/lune-org/lune.git
synced 2025-04-10 21:40:54 +01:00
Move runtime struct into runtime module
This commit is contained in:
parent
c8dcea0a21
commit
b4b7440130
4 changed files with 109 additions and 104 deletions
|
@ -8,4 +8,4 @@ pub use lune_roblox as roblox;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
pub use crate::rt::{Runtime, RuntimeError};
|
pub use crate::rt::{Runtime, RuntimeError, RuntimeResult};
|
||||||
|
|
|
@ -1,104 +1,5 @@
|
||||||
#![allow(clippy::missing_panics_doc)]
|
mod result;
|
||||||
|
mod runtime;
|
||||||
|
|
||||||
use std::{
|
pub use self::result::{RuntimeError, RuntimeResult};
|
||||||
process::ExitCode,
|
pub use self::runtime::Runtime;
|
||||||
rc::Rc,
|
|
||||||
sync::{
|
|
||||||
atomic::{AtomicBool, Ordering},
|
|
||||||
Arc,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use mlua::Lua;
|
|
||||||
use mlua_luau_scheduler::Scheduler;
|
|
||||||
|
|
||||||
use lune_std::inject_globals;
|
|
||||||
|
|
||||||
mod error;
|
|
||||||
|
|
||||||
pub use error::RuntimeError;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Runtime {
|
|
||||||
lua: Rc<Lua>,
|
|
||||||
args: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Runtime {
|
|
||||||
/**
|
|
||||||
Creates a new Lune runtime, with a new Luau VM.
|
|
||||||
*/
|
|
||||||
#[must_use]
|
|
||||||
#[allow(clippy::new_without_default)]
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let lua = Rc::new(Lua::new());
|
|
||||||
|
|
||||||
lua.set_app_data(Rc::downgrade(&lua));
|
|
||||||
lua.set_app_data(Vec::<String>::new());
|
|
||||||
|
|
||||||
inject_globals(&lua).expect("Failed to inject globals");
|
|
||||||
|
|
||||||
Self {
|
|
||||||
lua,
|
|
||||||
args: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Sets arguments to give in `process.args` for Lune scripts.
|
|
||||||
*/
|
|
||||||
#[must_use]
|
|
||||||
pub fn with_args<V>(mut self, args: V) -> Self
|
|
||||||
where
|
|
||||||
V: Into<Vec<String>>,
|
|
||||||
{
|
|
||||||
self.args = args.into();
|
|
||||||
self.lua.set_app_data(self.args.clone());
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Runs a Lune script inside of the current runtime.
|
|
||||||
|
|
||||||
This will preserve any modifications to global values / context.
|
|
||||||
|
|
||||||
# Errors
|
|
||||||
|
|
||||||
This function will return an error if the script fails to run.
|
|
||||||
*/
|
|
||||||
pub async fn run(
|
|
||||||
&mut self,
|
|
||||||
script_name: impl AsRef<str>,
|
|
||||||
script_contents: impl AsRef<[u8]>,
|
|
||||||
) -> Result<ExitCode, RuntimeError> {
|
|
||||||
// Create a new scheduler for this run
|
|
||||||
let sched = Scheduler::new(&self.lua);
|
|
||||||
|
|
||||||
// Add error callback to format errors nicely + store status
|
|
||||||
let got_any_error = Arc::new(AtomicBool::new(false));
|
|
||||||
let got_any_inner = Arc::clone(&got_any_error);
|
|
||||||
sched.set_error_callback(move |e| {
|
|
||||||
got_any_inner.store(true, Ordering::SeqCst);
|
|
||||||
eprintln!("{}", RuntimeError::from(e));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Load our "main" thread
|
|
||||||
let main = self
|
|
||||||
.lua
|
|
||||||
.load(script_contents.as_ref())
|
|
||||||
.set_name(script_name.as_ref());
|
|
||||||
|
|
||||||
// Run it on our scheduler until it and any other spawned threads complete
|
|
||||||
sched.push_thread_back(main, ())?;
|
|
||||||
sched.run().await;
|
|
||||||
|
|
||||||
// Return the exit code - default to FAILURE if we got any errors
|
|
||||||
Ok(sched.get_exit_code().unwrap_or({
|
|
||||||
if got_any_error.load(Ordering::SeqCst) {
|
|
||||||
ExitCode::FAILURE
|
|
||||||
} else {
|
|
||||||
ExitCode::SUCCESS
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,6 +7,8 @@ use mlua::prelude::*;
|
||||||
|
|
||||||
use lune_utils::fmt::ErrorComponents;
|
use lune_utils::fmt::ErrorComponents;
|
||||||
|
|
||||||
|
pub type RuntimeResult<T, E = RuntimeError> = Result<T, E>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
An opaque error type for formatted lua errors.
|
An opaque error type for formatted lua errors.
|
||||||
*/
|
*/
|
102
crates/lune/src/rt/runtime.rs
Normal file
102
crates/lune/src/rt/runtime.rs
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
#![allow(clippy::missing_panics_doc)]
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
process::ExitCode,
|
||||||
|
rc::Rc,
|
||||||
|
sync::{
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use mlua::Lua;
|
||||||
|
use mlua_luau_scheduler::Scheduler;
|
||||||
|
|
||||||
|
use lune_std::inject_globals;
|
||||||
|
|
||||||
|
use super::{RuntimeError, RuntimeResult};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Runtime {
|
||||||
|
lua: Rc<Lua>,
|
||||||
|
args: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Runtime {
|
||||||
|
/**
|
||||||
|
Creates a new Lune runtime, with a new Luau VM.
|
||||||
|
*/
|
||||||
|
#[must_use]
|
||||||
|
#[allow(clippy::new_without_default)]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let lua = Rc::new(Lua::new());
|
||||||
|
|
||||||
|
lua.set_app_data(Rc::downgrade(&lua));
|
||||||
|
lua.set_app_data(Vec::<String>::new());
|
||||||
|
|
||||||
|
inject_globals(&lua).expect("Failed to inject globals");
|
||||||
|
|
||||||
|
Self {
|
||||||
|
lua,
|
||||||
|
args: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets arguments to give in `process.args` for Lune scripts.
|
||||||
|
*/
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_args<V>(mut self, args: V) -> Self
|
||||||
|
where
|
||||||
|
V: Into<Vec<String>>,
|
||||||
|
{
|
||||||
|
self.args = args.into();
|
||||||
|
self.lua.set_app_data(self.args.clone());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Runs a Lune script inside of the current runtime.
|
||||||
|
|
||||||
|
This will preserve any modifications to global values / context.
|
||||||
|
|
||||||
|
# Errors
|
||||||
|
|
||||||
|
This function will return an error if the script fails to run.
|
||||||
|
*/
|
||||||
|
pub async fn run(
|
||||||
|
&mut self,
|
||||||
|
script_name: impl AsRef<str>,
|
||||||
|
script_contents: impl AsRef<[u8]>,
|
||||||
|
) -> RuntimeResult<ExitCode> {
|
||||||
|
// Create a new scheduler for this run
|
||||||
|
let sched = Scheduler::new(&self.lua);
|
||||||
|
|
||||||
|
// Add error callback to format errors nicely + store status
|
||||||
|
let got_any_error = Arc::new(AtomicBool::new(false));
|
||||||
|
let got_any_inner = Arc::clone(&got_any_error);
|
||||||
|
sched.set_error_callback(move |e| {
|
||||||
|
got_any_inner.store(true, Ordering::SeqCst);
|
||||||
|
eprintln!("{}", RuntimeError::from(e));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Load our "main" thread
|
||||||
|
let main = self
|
||||||
|
.lua
|
||||||
|
.load(script_contents.as_ref())
|
||||||
|
.set_name(script_name.as_ref());
|
||||||
|
|
||||||
|
// Run it on our scheduler until it and any other spawned threads complete
|
||||||
|
sched.push_thread_back(main, ())?;
|
||||||
|
sched.run().await;
|
||||||
|
|
||||||
|
// Return the exit code - default to FAILURE if we got any errors
|
||||||
|
Ok(sched.get_exit_code().unwrap_or({
|
||||||
|
if got_any_error.load(Ordering::SeqCst) {
|
||||||
|
ExitCode::FAILURE
|
||||||
|
} else {
|
||||||
|
ExitCode::SUCCESS
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue