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
This commit is contained in:
meehl committed 2026-10-21 17:45:11 +02:00
1 parent a1e92fa94e
commit 4b997447b5
4 files changed
+52 -13

No files matched your search

+2 -2
View File
@@ -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)?;
+12 -8
View File
@@ -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<Mul
let current_dir = env::current_dir()?;
change_working_directory(script_dir.as_path())?;
let result = l.load(module_path).call::<MultiValue>(args);
let result = load_lua_file(l, &module_path).and_then(|chunk| chunk.call::<MultiValue>(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::<MultiValue>(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::<MultiValue>(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
}
+6 -1
View File
@@ -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::<MultiValue>(arguments)?;
result.try_into()
});
+32 -2
View File
@@ -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 `#@ <label>` script header into a Lua comment.
///
/// Some Path of Building scripts start with a line such as `#@ <optional_text>`. Recent LuaJIT
/// versions interpret a leading `#` as a line number directive and reject this header so we
/// replace them with a comment.
///
/// commit that introduced changes to the lexer to enable parsing of line number directives:
/// <https://github.com/LuaJIT/LuaJIT/commit/c41d727dbffa5ec4f346dd9be0f4c64740f124aa>
pub fn fix_script_header(source: &mut [u8]) {
// skip utf8-bom
let start = if source.starts_with(b"\xEF\xBB\xBF") {
3
} else {
0
};
if source[start..].starts_with(b"#@") {
source[start..start + 2].copy_from_slice(b"--");
}
}
/// Loads a Lua script from `path` and applies `fix_script_header`.
///
/// The chunk name is set to `@<path>` to match `Lua::load`'s behavior for paths, so error messages
/// keep referring to the file.
pub fn load_lua_file<'lua>(lua: &'lua Lua, path: &Path) -> LuaResult<Chunk<'lua>> {
let mut source = fs::read(path)?;
fix_script_header(&mut source);
Ok(lua.load(source).set_name(format!("@{}", path.display())))
}