[lua] Capture module-level varargs into _G.arg for Sys.args()#12602
[lua] Capture module-level varargs into _G.arg for Sys.args()#12602jdonaldson wants to merge 1 commit intoHaxeFoundation:developmentfrom
Conversation
Environments like ComputerCraft pass script arguments via module-level varargs (...) rather than populating _G.arg. Add a one-liner at the top of generated output to capture ... into _G.arg when it is not already set, making Sys.args() work in these environments. Closes HaxeFoundation#10753
|
Sys.args() documentation states:
If Sys.args() can now contain values passed using loadfile instead of via the cli, then it is deviating from the specified behaviour. Also, just because Expand for sample lua code where this can occur-- wrapper.lua
local main = loadfile("main.lua")
main("load", "file", "args")-- main.lua
print("_G.arg = " .. table.concat(_G.arg, " "))
print("{...} = " .. table.concat({...}, " "))$ lua wrapper.lua real cli args
_G.arg = real cli args
{...} = load file argsI also think it's bad practice to overwrite a global value like We should also avoid injecting more unconditionally included lua code. Even if it's one line now, it builds up gradually and one of the most common complaints about the lua target is the boilerplate required for simple programs. It is especially something to consider when only some users need it but it is added to everyone's code. Using define feature here to conditionally generate the extra line avoids this problem. Maybe it's better to have a new method like |
Summary
...(varargs) rather than populating_G.arg. The generated Lua wraps everything in functions, so...becomes inaccessible andSys.args()returns an empty array.if _G.arg == nil then _G.arg = {...} end_G.arg, so the nil check makes this a no-op in normal environments.Test plan
make haxebuilds successfullySys.args()works with standardlua script.lua arg1 arg2Sys.args()works in ComputerCraft-like invocation (loadfile+ call with args, no_G.arg)