From 4b997447b5c0c4d42cd92052432a6a8bb0ff0f21 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:45:11 +0200 Subject: [PATCH] Strip `#@` script headers before loading Lua LuaJIT now only treats a leading `#!` line as a shebang and interprets other lines beginning with `#` as line number directives. https://github.com/LuaJIT/LuaJIT/commit/c41d727dbffa5ec4f346dd9be0f4c64740f124aa --- src/pob.rs | 4 ++-- src/pob/api/lua.rs | 20 ++++++++++++-------- src/pob/subscript.rs | 7 ++++++- src/util.rs | 34 ++++++++++++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/pob.rs b/src/pob.rs index 1208f30..1c2a67b 100644 --- a/src/pob.rs +++ b/src/pob.rs @@ -13,7 +13,7 @@ use crate::{ pob::subscript::{NativeMultiValue, SubscriptEvent, SubscriptManager}, renderer::textures::TextureManager, stage::{StageEvent, StageFrameOutput, StageTransition}, - util::{append_lua_package_dir, change_working_directory}, + util::{append_lua_package_dir, change_working_directory, load_lua_file}, window::WindowState, }; use std::{ @@ -133,7 +133,7 @@ impl PathOfBuilding { /// Loads and runs `Launch.lua`, then calls PoB's `OnInit` handler. fn launch(lua: &Lua, script_dir: &Path) -> LuaResult<()> { change_working_directory(script_dir)?; - lua.load(script_dir.join("Launch.lua")).exec()?; + load_lua_file(lua, &script_dir.join("Launch.lua"))?.exec()?; { profiling::scope!("OnInit"); api::on_init(lua)?; diff --git a/src/pob/api/lua.rs b/src/pob/api/lua.rs index 318b895..6b5db72 100644 --- a/src/pob/api/lua.rs +++ b/src/pob/api/lua.rs @@ -1,4 +1,7 @@ -use crate::{pob::Context, util::change_working_directory}; +use crate::{ + pob::Context, + util::{change_working_directory, load_lua_file}, +}; use mlua::{Function, IntoLuaMulti, Lua, MultiValue, Result as LuaResult, Value}; use std::env; @@ -23,7 +26,7 @@ pub fn load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult(args); + let result = load_lua_file(l, &module_path).and_then(|chunk| chunk.call::(args)); change_working_directory(current_dir)?; result } @@ -40,12 +43,13 @@ pub fn protected_load_module(l: &Lua, (name, args): (String, MultiValue)) -> Lua let current_dir = env::current_dir()?; change_working_directory(script_dir.as_path())?; - let result = match l.load(module_path).call::(args) { - // on success, callers expect a Nil followed by return values - Ok(res) => Ok(std::iter::once(Value::Nil).chain(res).collect()), - // otherwise it is set to error message. - Err(err) => err.to_string().into_lua_multi(l), - }; + let result = + match load_lua_file(l, &module_path).and_then(|chunk| chunk.call::(args)) { + // on success, callers expect a Nil followed by return values + Ok(res) => Ok(std::iter::once(Value::Nil).chain(res).collect()), + // otherwise it is set to error message. + Err(err) => err.to_string().into_lua_multi(l), + }; change_working_directory(current_dir)?; result } diff --git a/src/pob/subscript.rs b/src/pob/subscript.rs index 66a1e2b..c7ce439 100644 --- a/src/pob/subscript.rs +++ b/src/pob/subscript.rs @@ -1,4 +1,7 @@ -use crate::{pob::modules, util::append_lua_package_dir}; +use crate::{ + pob::modules, + util::{append_lua_package_dir, fix_script_header}, +}; use anyhow::{Result, anyhow}; use mlua::{Integer, IntoLuaMulti, Lua, MultiValue, Number, Value}; use std::{ @@ -184,6 +187,8 @@ impl Subscript { )?; } + let mut script_text = script_text.into_bytes(); + fix_script_header(&mut script_text); let result = lua.load(script_text).call::(arguments)?; result.try_into() }); diff --git a/src/util.rs b/src/util.rs index 74bb245..a925b72 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,7 @@ use ahash::AHasher; -use mlua::{Lua, Result as LuaResult, Table}; +use mlua::{Lua, Result as LuaResult, Table, chunk::Chunk}; use std::{ - env, + env, fs, hash::{Hash, Hasher}, path::{Path, PathBuf}, }; @@ -79,3 +79,33 @@ pub fn append_lua_package_dir(lua: &Lua, path: &Path) -> LuaResult<()> { append_lua_package_path(lua, &format!("{path}/?/init.lua"))?; Ok(()) } + +/// Rewrites PoB's `#@