Rename lune struct to runtime

This commit is contained in:
Filip Tibell 2024-01-14 10:41:18 +01:00
parent ef9550ced5
commit e5e83d1ad6
No known key found for this signature in database
9 changed files with 26 additions and 31 deletions

View file

@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
This unfortunately hurts ergonomics for quickly running scripts but is a necessary change to allow us to add more commands, such as the new `build` subcommand. This unfortunately hurts ergonomics for quickly running scripts but is a necessary change to allow us to add more commands, such as the new `build` subcommand.
- The `Lune` struct has been renamed to `Runtime` in the Lune rust crate.
### Added ### Added
- Added support for compiling single Lune scripts into standalone executables! ([#140]) - Added support for compiling single Lune scripts into standalone executables! ([#140])

View file

@ -5,7 +5,7 @@ use clap::Parser;
use directories::UserDirs; use directories::UserDirs;
use rustyline::{error::ReadlineError, DefaultEditor}; use rustyline::{error::ReadlineError, DefaultEditor};
use lune::Lune; use lune::Runtime;
const MESSAGE_WELCOME: &str = concat!("Lune v", env!("CARGO_PKG_VERSION")); const MESSAGE_WELCOME: &str = concat!("Lune v", env!("CARGO_PKG_VERSION"));
const MESSAGE_INTERRUPT: &str = "Interrupt: ^C again to exit"; const MESSAGE_INTERRUPT: &str = "Interrupt: ^C again to exit";
@ -38,7 +38,7 @@ impl ReplCommand {
let mut prompt_state = PromptState::Regular; let mut prompt_state = PromptState::Regular;
let mut source_code = String::new(); let mut source_code = String::new();
let mut lune_instance = Lune::new(); let mut lune_instance = Runtime::new();
loop { loop {
let prompt = match prompt_state { let prompt = match prompt_state {

View file

@ -7,7 +7,7 @@ use tokio::{
io::{stdin, AsyncReadExt as _}, io::{stdin, AsyncReadExt as _},
}; };
use lune::Lune; use lune::Runtime;
use super::utils::files::{discover_script_path_including_lune_dirs, strip_shebang}; use super::utils::files::{discover_script_path_including_lune_dirs, strip_shebang};
@ -41,7 +41,7 @@ impl RunCommand {
}; };
// Create a new lune object with all globals & run the script // Create a new lune object with all globals & run the script
let result = Lune::new() let result = Runtime::new()
.with_args(self.script_args) .with_args(self.script_args)
.run(&script_display_name, strip_shebang(script_contents)) .run(&script_display_name, strip_shebang(script_contents))
.await; .await;

View file

@ -1,6 +1,6 @@
use std::{env, process::ExitCode}; use std::{env, process::ExitCode};
use lune::Lune; use lune::Runtime;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use tokio::fs; use tokio::fs;
@ -68,7 +68,7 @@ pub async fn run_standalone(patched_bin: impl AsRef<[u8]>) -> Result<ExitCode> {
let args = env::args().skip(1).collect::<Vec<_>>(); let args = env::args().skip(1).collect::<Vec<_>>();
let meta = MetaChunk::from_bytes(patched_bin).expect("must be a standalone binary"); let meta = MetaChunk::from_bytes(patched_bin).expect("must be a standalone binary");
let result = Lune::new() let result = Runtime::new()
.with_args(args) .with_args(args)
.run("STANDALONE", meta.bytecode) .run("STANDALONE", meta.bytecode)
.await; .await;

View file

@ -6,4 +6,4 @@ pub mod roblox;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub use crate::lune::{Lune, LuneError}; pub use crate::lune::{Runtime, RuntimeError};

View file

@ -11,14 +11,12 @@ use crate::lune::util::formatting::pretty_format_luau_error;
An opaque error type for formatted lua errors. An opaque error type for formatted lua errors.
*/ */
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LuneError { pub struct RuntimeError {
error: LuaError, error: LuaError,
disable_colors: bool, disable_colors: bool,
} }
// TODO: Rename this struct to "RuntimeError" instead for impl RuntimeError {
// the next breaking release, it's a more fitting name
impl LuneError {
/** /**
Enables colorization of the error message when formatted using the [`Display`] trait. Enables colorization of the error message when formatted using the [`Display`] trait.
@ -57,7 +55,7 @@ impl LuneError {
} }
} }
impl From<LuaError> for LuneError { impl From<LuaError> for RuntimeError {
fn from(value: LuaError) -> Self { fn from(value: LuaError) -> Self {
Self { Self {
error: value, error: value,
@ -66,7 +64,7 @@ impl From<LuaError> for LuneError {
} }
} }
impl From<&LuaError> for LuneError { impl From<&LuaError> for RuntimeError {
fn from(value: &LuaError) -> Self { fn from(value: &LuaError) -> Self {
Self { Self {
error: value.clone(), error: value.clone(),
@ -75,7 +73,7 @@ impl From<&LuaError> for LuneError {
} }
} }
impl Display for LuneError { impl Display for RuntimeError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!( write!(
f, f,
@ -85,10 +83,8 @@ impl Display for LuneError {
} }
} }
impl Error for LuneError { impl Error for RuntimeError {
// TODO: Comment this out when we are ready to also re-export fn cause(&self) -> Option<&dyn Error> {
// `mlua` as part of our public library interface in Lune Some(&self.error)
// fn cause(&self) -> Option<&dyn Error> { }
// Some(&self.error)
// }
} }

View file

@ -11,19 +11,16 @@ pub(crate) mod util;
use self::scheduler::{LuaSchedulerExt, Scheduler}; use self::scheduler::{LuaSchedulerExt, Scheduler};
pub use error::LuneError; pub use error::RuntimeError;
// TODO: Rename this struct to "Runtime" instead for the
// next breaking release, it's a more fitting name and
// will probably be more obvious when browsing files
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Lune { pub struct Runtime {
lua: &'static Lua, lua: &'static Lua,
scheduler: &'static Scheduler<'static>, scheduler: &'static Scheduler<'static>,
args: Vec<String>, args: Vec<String>,
} }
impl Lune { impl Runtime {
/** /**
Creates a new Lune runtime, with a new Luau VM and task scheduler. Creates a new Lune runtime, with a new Luau VM and task scheduler.
*/ */
@ -70,7 +67,7 @@ impl Lune {
&mut self, &mut self,
script_name: impl AsRef<str>, script_name: impl AsRef<str>,
script_contents: impl AsRef<[u8]>, script_contents: impl AsRef<[u8]>,
) -> Result<ExitCode, LuneError> { ) -> Result<ExitCode, RuntimeError> {
let main = self let main = self
.lua .lua
.load(script_contents.as_ref()) .load(script_contents.as_ref())

View file

@ -1,7 +1,7 @@
use mlua::prelude::*; use mlua::prelude::*;
use super::formatting::format_label; use super::formatting::format_label;
use crate::LuneError; use crate::RuntimeError;
pub trait LuaEmitErrorExt { pub trait LuaEmitErrorExt {
fn emit_error(&self, err: LuaError); fn emit_error(&self, err: LuaError);
@ -10,6 +10,6 @@ pub trait LuaEmitErrorExt {
impl LuaEmitErrorExt for Lua { impl LuaEmitErrorExt for Lua {
fn emit_error(&self, err: LuaError) { fn emit_error(&self, err: LuaError) {
// NOTE: LuneError will pretty-format this error // NOTE: LuneError will pretty-format this error
eprintln!("{}\n{}", format_label("error"), LuneError::from(err)); eprintln!("{}\n{}", format_label("error"), RuntimeError::from(err));
} }
} }

View file

@ -5,7 +5,7 @@ use console::set_colors_enabled;
use console::set_colors_enabled_stderr; use console::set_colors_enabled_stderr;
use tokio::fs::read_to_string; use tokio::fs::read_to_string;
use crate::Lune; use crate::Runtime;
const ARGS: &[&str] = &["Foo", "Bar"]; const ARGS: &[&str] = &["Foo", "Bar"];
@ -20,7 +20,7 @@ macro_rules! create_tests {
// The rest of the test logic can continue as normal // The rest of the test logic can continue as normal
let full_name = format!("tests/{}.luau", $value); let full_name = format!("tests/{}.luau", $value);
let script = read_to_string(&full_name).await?; let script = read_to_string(&full_name).await?;
let mut lune = Lune::new().with_args( let mut lune = Runtime::new().with_args(
ARGS ARGS
.clone() .clone()
.iter() .iter()