improve lua integration by allowing more flexible return types and introducing globals

This commit is contained in:
Moritz Hölting 2024-09-20 14:55:48 +02:00
parent 0cccee936e
commit 61b8f1ffb9
6 changed files with 81 additions and 16 deletions

View File

@ -31,6 +31,7 @@ getset = "0.1.2"
itertools = "0.13.0" itertools = "0.13.0"
mlua = { version = "0.9.7", features = ["lua54", "vendored"], optional = true } mlua = { version = "0.9.7", features = ["lua54", "vendored"], optional = true }
path-absolutize = "3.1.1" path-absolutize = "3.1.1"
pathdiff = "0.2.1"
serde = { version = "1.0.197", features = ["derive", "rc"], optional = true } serde = { version = "1.0.197", features = ["derive", "rc"], optional = true }
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, optional = true, rev = "aff342a64a94981af942223345b5a5f105212957" } shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, optional = true, rev = "aff342a64a94981af942223345b5a5f105212957" }
strsim = "0.11.1" strsim = "0.11.1"

View File

@ -291,7 +291,7 @@ fn write_error_line(
(line_number == start_line && index >= start_location.column) (line_number == start_line && index >= start_location.column)
|| (line_number == end_line || (line_number == end_line
&& (index + 1) && (index + 1)
< end_location <= end_location
.map_or(usize::MAX, |end_location| end_location.column)) .map_or(usize::MAX, |end_location| end_location.column))
|| (line_number > start_line && line_number < end_line) || (line_number > start_line && line_number < end_line)
} else { } else {

View File

@ -136,6 +136,12 @@ impl SourceFile {
None None
} }
} }
/// Get the relative path of the source file from the current working directory.
#[must_use]
pub fn path_relative(&self) -> Option<PathBuf> {
pathdiff::diff_paths(&self.path, std::env::current_dir().ok()?)
}
} }
/// Represents a range of characters in a source file. /// Represents a range of characters in a source file.

View File

@ -128,6 +128,20 @@ impl Display for LuaRuntimeError {
impl std::error::Error for LuaRuntimeError {} impl std::error::Error for LuaRuntimeError {}
#[cfg(feature = "lua")]
impl LuaRuntimeError {
pub fn from_lua_err(err: &mlua::Error, span: Span) -> Self {
let err_string = err.to_string();
Self {
error_message: err_string
.strip_prefix("runtime error: ")
.unwrap_or(&err_string)
.to_string(),
code_block: span,
}
}
}
/// An error that occurs when a function declaration is missing. /// An error that occurs when a function declaration is missing.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnexpectedExpression(pub Expression); pub struct UnexpectedExpression(pub Expression);

View File

@ -2,7 +2,7 @@
#[cfg(feature = "lua")] #[cfg(feature = "lua")]
mod enabled { mod enabled {
use mlua::Lua; use mlua::{Lua, Value};
use crate::{ use crate::{
base::{self, source_file::SourceElement, Handler}, base::{self, source_file::SourceElement, Handler},
@ -16,7 +16,10 @@ mod enabled {
/// # Errors /// # Errors
/// - If Lua code evaluation is disabled. /// - If Lua code evaluation is disabled.
#[tracing::instrument(level = "debug", name = "eval_lua", skip_all, ret)] #[tracing::instrument(level = "debug", name = "eval_lua", skip_all, ret)]
pub fn eval_string(&self, handler: &impl Handler<base::Error>) -> TranspileResult<String> { pub fn eval_string(
&self,
handler: &impl Handler<base::Error>,
) -> TranspileResult<Option<String>> {
tracing::debug!("Evaluating Lua code"); tracing::debug!("Evaluating Lua code");
let lua = Lua::new(); let lua = Lua::new();
@ -24,7 +27,7 @@ mod enabled {
let name = { let name = {
let span = self.span(); let span = self.span();
let file = span.source_file(); let file = span.source_file();
let path = file.path(); let path = file.path_relative().unwrap_or_else(|| file.path().clone());
let start = span.start_location(); let start = span.start_location();
let end = span.end_location().unwrap_or_else(|| { let end = span.end_location().unwrap_or_else(|| {
@ -43,24 +46,62 @@ mod enabled {
) )
}; };
self.add_globals(&lua).unwrap();
let lua_result = lua let lua_result = lua
.load(self.code()) .load(self.code())
.set_name(name) .set_name(name)
.eval::<String>() .eval::<Value>()
.map_err(|err| { .map_err(|err| {
let err_string = err.to_string(); let err =
let err = TranspileError::from(LuaRuntimeError { TranspileError::from(LuaRuntimeError::from_lua_err(&err, self.span()));
error_message: err_string
.strip_prefix("runtime error: ")
.unwrap_or(&err_string)
.to_string(),
code_block: self.span(),
});
handler.receive(crate::Error::from(err.clone())); handler.receive(crate::Error::from(err.clone()));
err err
})?; })?;
Ok(lua_result) self.handle_lua_result(dbg!(lua_result)).map_err(|err| {
handler.receive(err.clone());
err
})
}
fn add_globals(&self, lua: &Lua) -> mlua::Result<()> {
let globals = lua.globals();
let location = {
let span = self.span();
let file = span.source_file();
file.path_relative().unwrap_or_else(|| file.path().clone())
};
globals.set("shu_location", location.to_string_lossy())?;
Ok(())
}
fn handle_lua_result(&self, value: Value) -> TranspileResult<Option<String>> {
match value {
Value::Nil => Ok(None),
Value::String(s) => Ok(Some(s.to_string_lossy().into_owned())),
Value::Integer(i) => Ok(Some(i.to_string())),
Value::Number(n) => Ok(Some(n.to_string())),
Value::Function(f) => self.handle_lua_result(f.call(()).map_err(|err| {
TranspileError::LuaRuntimeError(LuaRuntimeError::from_lua_err(
&err,
self.span(),
))
})?),
Value::Boolean(_)
| Value::Error(_)
| Value::Table(_)
| Value::Thread(_)
| Value::UserData(_)
| Value::LightUserData(_) => {
Err(TranspileError::LuaRuntimeError(LuaRuntimeError {
code_block: self.span(),
error_message: format!("invalid return type {}", value.type_name()),
}))
}
}
} }
} }
} }
@ -79,7 +120,10 @@ mod disabled {
/// ///
/// # Errors /// # Errors
/// - If Lua code evaluation is disabled. /// - If Lua code evaluation is disabled.
pub fn eval_string(&self, handler: &impl Handler<base::Error>) -> TranspileResult<String> { pub fn eval_string(
&self,
handler: &impl Handler<base::Error>,
) -> TranspileResult<Option<String>> {
handler.receive(TranspileError::LuaDisabled); handler.receive(TranspileError::LuaDisabled);
tracing::error!("Lua code evaluation is disabled"); tracing::error!("Lua code evaluation is disabled");
Err(TranspileError::LuaDisabled) Err(TranspileError::LuaDisabled)

View File

@ -355,7 +355,7 @@ impl Transpiler {
Ok(Some(Command::Raw(string.str_content().to_string()))) Ok(Some(Command::Raw(string.str_content().to_string())))
} }
Expression::Primary(Primary::Lua(code)) => { Expression::Primary(Primary::Lua(code)) => {
Ok(Some(Command::Raw(code.eval_string(handler)?))) Ok(code.eval_string(handler)?.map(Command::Raw))
} }
}, },
Statement::Block(_) => { Statement::Block(_) => {