I have been trying to improve the error reporting in some of my code and came across another slightly rough area of Lua. I have been using luaL_loadstring to load text from a Windows EXE named resource. Errors get reported something like:
[string: “x = x + 1”]:1: attempt to perform arithmetic on global ‘x’ (a nil value)
I tried using luaL_loadbuffer, specifying the name of the resource as the last parameter (chunk name). But it still insists on adding the inappropriate ‘string’ prefix and enclosing my supplied name in quotes:
[string: “myres”]:1: attempt to perform arithmetic on global ‘x’ (a nil value)
Even using lua_load with a custom lua_Reader function, the unwanted prefixing is still done, so it seems to be impossible to get “under” this unwanted processing with the published API.
The only way I could get control of the format of the bit in square brackets was to use an error handler in lua_pcall. But even this is made needlessly complex by supplying the error message already composed. I then have to parse the error description part off the end with some heuristic string processing (can I be sure there will never be a colon in the error description?) before reassembling it with information from lua_getinfo to get the format I want:
[myres]:1: attempt to perform arithmetic on global ‘x’ (a nil value)
It would be much better if the error handler received the components of the message as individual parameters and allowed you to compose them any way you want. It would also be good to have a numeric code corresponding to the error description for easy translation or rewording.
However in my view, if a chunk name is supplied explicitly to lua_loadbuffer or lua_load, it should be used as supplied without the prefixing or quoting – if we want those we can supply them as part of the name.