Function Storage

When you create a function in lua, how does the function get stored? Is it just some kind of reference to a position in the code, or is it stored in a table of some sort?

If it’s not a local function, it’s stored in the table _G.

By default. You can store a function in a specific table as well if you specify which.

Is it possible to manipulate the data stored for the function? For example, if one wanted to add a line of code to the end. Are the actual lines of code stored as strings, or what?

No. They’re stored as functions. If you’re wanting to do something additional with a function, you could do the following:

local OldFunction = SomeFunction

function SomeFunction(args)

No, that’s not possible, because functions are compiled to bytecode in Lua.

But, that bytecode is actually stored in the table containing the function correct?



On second thought, string.dump may return the bytecode, but editing it is useless and probably hard. Turning it back into a function is not possible in GLua as far as I know.

If I understand correctly, the function, in the lower level, is stored as just a string of lua bytecodes, which the lua VM/whatever-it’s-called executes when the function is called.

You can get the bytecode of a function by using the lua_dump function, though since there are NULLs in the string and C strings are NULL terminated, you can’t manipulate it without some extra work.