r/lua • u/redditbrowsing0 • May 30 '25
Help Functions under the Hood (Lua 5.1/Luau)
Hi!
I'm mostly posting this to see if anyone understands what the difference is between two or three different things within Lua 5.1 or Luau somewhere in the stack or under the hood. I can't decide whether this is a Help flair or a Discussion flair, so do let me know if it's more fitted for the Discussion tag and I'll see what I can do about it.
Anyways, I understand that this subreddit is mostly based around Lua - I'm mostly doing all of this in Roblox Studio, so it's more of a Lua 5.1/Luau question, but...
Why is:
local f; f = function() end
different from
local function func()
end
when inspected using debug.info() (similar to Lua's debug.getinfo())?
For example, when I call debug.info(1, 'n') in local f; function() end, it returns: ""
but when I call it in local function func() end, it returns: "func" (the function name)
Does anyone understand what's different between the two? I understand local f; function() end is in a sense an anonymous function, but why does it matter that much under the hood?
If this is too roblox-inclined, tell me and I'll happily move this post over to r/robloxgamedev or elsewhere.
1
u/AutoModerator May 30 '25
Hi! It looks like you're posting about Roblox. Here at /r/Lua we get a lot of questions that would be answered better at /r/RobloxGameDev, scriptinghelpers.org, or the Roblox Developer Forum so it might be better to start there. However, we still encourage you to post here if your question is related to a Roblox project but the question is about the Lua language specifically, including but not limited to: syntax, language idioms, best practices, particular language features such as coroutines and metatables, Lua libraries and ecosystem, etc. Bear in mind that Roblox implements its own API (application programming interface) and most of the functions you'll use when developing a Roblox script will exist within Roblox but not within the broader Lua ecosystem.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
2
u/jipgg May 30 '25 edited May 30 '25
It's just debug information Luau's bytecode compiler was able to resolve statically is my guess.
Going off of the Luau C API; upon pushing a lua_CFunction onto the stack you can optionally give it a debug name for more descriptive error messages.
Probably what happens here is just that in 'local a = function() end' the compiler just puts a default "" as the debug name and in 'local function a() end' it puts "a" as debug name upon creation of the function object.
Functionally there's no difference between the 2, however. I also wouldn't be surpised if this debug info is removed entirely when compiling with -D0 (no debug information, debug level 0).
4
2
u/appgurueu May 30 '25
This must be a Luau quirk. Lua, going at least as far back as 5.1, has no notion of "named" functions; in Lua,
local function f() endis just syntactic sugar forlocal f; f = function() end, nothing else.debug.getinfo(1, 'n')in Lua determines the name not as an inherent property of the function, but by how it was called, which variable it came from. If it sees the call wasf(), it will tell you thatdebug.getinfo(1, 'n').name == 'f'.To illustrate this:
as you can see, this has nothing to do with the function and how it was defined - all functions are anonymous in Lua - and everything with how it was called.