Debugging Luau scripts runtime errors #317
-
I use luau as a DSL and let users run luau scripts. When a Luau runtime error is encountered, I send the info to the debug log so users can troubleshoot their scripts. Is there any way to make the error message say what line in the Luau script caused the error? Right now, I even setup the Luau compiler to be more debug friendly when debug logging is on, i.e. let luau = Lua::new();
// see Compiler settings here: https://docs.rs/mlua/latest/mlua/struct.Compiler.html#
let luau_compiler = if log_enabled!(log::Level::Debug) || log_enabled!(log::Level::Trace) {
// debugging is on, set more debugging friendly compiler settings
// so we can see more error details in the logfile
mlua::Compiler::new()
.set_optimization_level(0)
.set_debug_level(2)
.set_coverage_level(2)
} else {
// use more performant compiler settings
mlua::Compiler::new()
.set_optimization_level(2)
.set_debug_level(1)
.set_coverage_level(0)
};
// set default Luau compiler
luau.set_compiler(luau_compiler.clone()); I do get the Luau runtime error message, but there is no line number info for the script. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Do you have a script example please? I tried to execute the following Lua file: function throw_error()
error("blah")
end
throw_error() using optimized compiler: let compiler = mlua::Compiler::new()
.set_optimization_level(2)
.set_debug_level(1)
.set_coverage_level(0);
lua.set_compiler(compiler);
let err = lua.load(Path::new("file.lua")).exec().err().unwrap();
println!("{err}"); and got error:
|
Beta Was this translation helpful? Give feedback.
-
Awesome! I was loading my scripts to a string, compiling it to byte code, and then running I did so as I created a Luau dialect for my project for processing CSV files, with discrete BEGIN, MAIN and END blocks - e.g. BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables, setup functions
-- and load additional Lua libraries as required
running_total = 0;
grand_total = 0;
amount_array = {};
adjusted_array = {};
function margin(x: number, y: number ): number
return x * y;
end
function sum(numbers_array: table): number
local sum: number = 0;
for _, v in ipairs(numbers_array) do
sum = sum + v;
end
return sum;
end
}!
-- this is the MAIN script loop, which is executed for each row
-- note how we use the _IDX special variable to get the row index
amount_array[_IDX] = Amount;
running_total = running_total + Amount;
adjusted_array[_IDX] = Amount + margin(Amount, 0.25);
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
END {
-- and this is the END block, which is executed once at the end
grand_total = running_total;
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
adjusted_total = sum(adjusted_array);
-- note how we use the _ROWCOUNT special variable to get the number of rows
-- the value returned from the END script is sent to stderr
return (`Min/Max: {min_amount}/{max_amount} Grand total of {_ROWCOUNT} rows: {grand_total} adjusted: {adjusted_total}`);
}! I'll change it up so I can leverage what you demonstrated above. Perhaps, I can just pre-parse my Luau Dialect and just get rid of the BEGIN and END wrappers and just load the different subscripts separately. WDYT? Am I using Luau/mlua correctly? |
Beta Was this translation helpful? Give feedback.
Do you have a script example please?
I tried to execute the following Lua file:
using optimized compiler:
and got error: