From 055de5c4eacea61f0bb9e18bc13a8349ef8e2c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sat, 15 Mar 2025 17:53:20 +0100 Subject: [PATCH] introduce shulkerscript module in lua --- src/transpile/lua.rs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/transpile/lua.rs b/src/transpile/lua.rs index 8e1e089..f67b553 100644 --- a/src/transpile/lua.rs +++ b/src/transpile/lua.rs @@ -98,12 +98,37 @@ mod enabled { fn add_globals(&self, lua: &Lua, scope: &Arc) -> 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()) + let shulkerscript_globals = { + let table = lua.create_table()?; + + let (location_path, location_start, location_end) = { + let span = self.span(); + let file = span.source_file(); + let path = file.path().to_owned(); + let start_location = span.start_location(); + let end_location = span.end_location().unwrap_or_else(|| { + let line_amount = file.line_amount(); + let column = file.get_line(line_amount).expect("line amount used").len(); + + crate::base::source_file::Location { + line: line_amount, + column, + } + }); + + (path, start_location, end_location) + }; + + table.set("file_path", location_path.to_string_lossy())?; + table.set("start_line", location_start.line)?; + table.set("start_column", location_start.column)?; + table.set("end_line", location_end.line)?; + table.set("end_column", location_end.column)?; + + table.set("version", crate::VERSION)?; + + table }; - globals.set("shu_location", location.to_string_lossy())?; if let Some(inputs) = self.inputs() { for x in inputs.elements() { @@ -170,6 +195,8 @@ mod enabled { } } + globals.set("shulkerscript", shulkerscript_globals)?; + Ok(()) }