Page 1 of 1

Allow loadfile() when require() would be allowed

Posted: Fri May 09, 2025 1:12 pm
by i142857
I'd like the Lua function loadfile to work, with the same restrictions as require and load (starting at mod root, not in event handlers, etc., only in text mode, only with .lua extension, etc.). I would be OK with an extra restriction that prevents loadfile from loading files in other mods, if you're concerned, but really the basic cross-mod require can already do a lot of stuff, so I'm not sure this is much worse.

For our own files, this is equivalent to changing the target .lua module to return a large string of all the Lua code, then calling require and passing that to load, so it doesn't let us do anything new, but I really don't want to write the code in a large multi-line string.

Why:
I have a test framework in my mod for running tests. I can run tests both outside Factorio, in normal lua5.2, and inside Factorio. It's nice to be able to run the tests in the real runtime. But some of my tests need to control Factorio globals, such as "game" or "script", and I do so by using loadfile with a custom environment. I could try to mess with _G but that's a lot riskier, and not really a good Lua practice, at least in 5.2. Those tests currently can't run inside Factorio and I'd like to fix that.

Thank you for your consideration :any-quality:

Re: Allow loadfile() when require() would be allowed

Posted: Fri May 09, 2025 1:30 pm
by boskid
What exactly makes `require()` unusable in your case?

Re: Allow loadfile() when require() would be allowed

Posted: Fri May 09, 2025 1:39 pm
by i142857
require doesn't take an environment, it always loads the module with the current environment/globals. In my tests I need to control the environment, so that I can replace things like "game" and "script". loadfile (and load) have an optional third parameter that is the environment to load the module with.

Re: Allow loadfile() when require() would be allowed

Posted: Sat May 10, 2025 8:08 am
by curiosity
i142857 wrote: Fri May 09, 2025 1:12 pm Why:
I have a test framework in my mod for running tests. I can run tests both outside Factorio, in normal lua5.2, and inside Factorio. It's nice to be able to run the tests in the real runtime. But some of my tests need to control Factorio globals, such as "game" or "script", and I do so by using loadfile with a custom environment. I could try to mess with _G but that's a lot riskier, and not really a good Lua practice, at least in 5.2. Those tests currently can't run inside Factorio and I'd like to fix that.
Have you tried making an instrument mod? Sure, it doesn't let you test multiplayer, but I don't think multiplayer-specific tests would need this.

Re: Allow loadfile() when require() would be allowed

Posted: Sat May 10, 2025 2:26 pm
by i142857
Yeah, there are several elaborate workarounds like that that I can do, but I asked because they're kind of unreasonably complicated, and it's just kind of weird to allow load() (which also can override the environment) and not loadfile(). To be honest if load() hadn't been allowed I wouldn't have asked :)