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
56 lines
2.0 KiB
Rust
56 lines
2.0 KiB
Rust
use crate::{
|
|
pob::Context,
|
|
util::{change_working_directory, load_lua_file},
|
|
};
|
|
use mlua::{Function, IntoLuaMulti, Lua, MultiValue, Result as LuaResult, Value};
|
|
use std::env;
|
|
|
|
pub fn protected_call(l: &Lua, (func, args): (Function, MultiValue)) -> LuaResult<MultiValue> {
|
|
match func.call::<MultiValue>(args) {
|
|
// callers expect first return value to be Nil on success
|
|
Ok(return_values) => Ok(std::iter::once(Value::Nil).chain(return_values).collect()),
|
|
// otherwise it is set to error message.
|
|
Err(err) => err.to_string().into_lua_multi(l),
|
|
}
|
|
}
|
|
|
|
pub fn load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult<MultiValue> {
|
|
let script_dir = {
|
|
let ctx = l.app_data_ref::<Context>().unwrap();
|
|
ctx.script_dir.clone()
|
|
};
|
|
let mut module_path = script_dir.join(name);
|
|
if module_path.extension().is_none() {
|
|
module_path.set_extension("lua");
|
|
}
|
|
|
|
let current_dir = env::current_dir()?;
|
|
change_working_directory(script_dir.as_path())?;
|
|
let result = load_lua_file(l, &module_path).and_then(|chunk| chunk.call::<MultiValue>(args));
|
|
change_working_directory(current_dir)?;
|
|
result
|
|
}
|
|
|
|
pub fn protected_load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult<MultiValue> {
|
|
let script_dir = {
|
|
let ctx = l.app_data_ref::<Context>().unwrap();
|
|
ctx.script_dir.clone()
|
|
};
|
|
let mut module_path = script_dir.join(name);
|
|
if module_path.extension().is_none() {
|
|
module_path.set_extension("lua");
|
|
}
|
|
|
|
let current_dir = env::current_dir()?;
|
|
change_working_directory(script_dir.as_path())?;
|
|
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
|
|
}
|