1
votes

I've compiled my lua code using luac53 and have the bytecode file. The problem is loadfile doesn't seem to want to load it and returns nil.

local func = loadfile("file.txt")//Returns nil

Load also returns nil after the following:

local file = io.open("file.txt", "rb")
local str = file:read("*a")
file:close()
local func = load(str, "bt")//Returns nil

The file is being loaded as a string here, but neither load or loadfile are doing anything. Does anyone know why this is happening?

1
This isn't your problem, but generally speaking, a .txt file should be a text file of some sort. Sticking compiled Lua bytecode into a "text file" is bad form. - Nicol Bolas
I've also tried loading the bytecode from a standard .lua file to no avail either. This is really confusing to me. - user8160888
Like I said, that's not your problem. Your real problem probably has to do with the current directory when executing the script (that is, the file isn't in the directory that is presently "current") or some other pathing issue. But there's no way to know without being on your actual computer. - Nicol Bolas
When using the load method, str loaded from the file just file. Lua is deciding to be difficult to me today I guess :(. - user8160888

1 Answers

2
votes

Standard Lua practice for functions that issue errors is to return nil followed by an error message. The Lua file you provide is probably generating an error at load time, so it is returning that. But you never look for a second return value.

assert can be useful here, since it takes a value to test and a string to print if there is an error. So if you did assert(loadstring(...)), it would print the error that loadstring returned, if it errored out. As a bonus, assert will return the first parameter it is given, so if loadstring succeeded, assert(loadstring(...)) will return the loaded chunk.