ComputerCraft Beta Versions - Download and...
dan200 09 Apr 2015
Do you really change functions environments except just after creating them in your program? I'm struggling to see the use case.
apemanzilla 09 Apr 2015
dan200, on 09 April 2015 - 01:23 PM, said:
Do you really change functions environments except just after creating them in your program? I'm struggling to see the use case.
It's more for use in operating systems and other high-level programs where sandboxing is an issue. You may want to sandbox code someone else has created, perhaps replacing os.shutdown and os.reboot with functions that don't do anything.
You can also instead just swap the global environment temporarily, but there are other cases where it can be useful.
Edited by apemanzilla, 09 April 2015 - 01:26 PM.
dan200 09 Apr 2015
But surely the users programs are loaded from files? Meaning you can safely sandbox them on load time.
SquidDev 09 Apr 2015
dan200, on 09 April 2015 - 01:23 PM, said:
Do you really change functions environments except just after creating them in your program? I'm struggling to see the use case.
The only time I've ever needed to do it was porting over the Busted framework. This sets the local environment on the fly so tests do not have access to the main suite, etc... Though it should be possible to just rewrite the entire file's table to do the same thing. I don't see any harm in having get/setfenv reimplemented as if we want to sandbox, we can always remove them.
Wojbie 09 Apr 2015
Wait does that mean we will [eventually] get Loved/Hated Goto! This will be interesting.
Edited by wojbie, 09 April 2015 - 01:52 PM.
Edited by wojbie, 09 April 2015 - 01:52 PM.
apemanzilla 09 Apr 2015
dan200, on 09 April 2015 - 01:58 PM, said:
Wait, they added goto to lua? What a time to be alive.
Yeah, and a bunch of other cool stuff in Lua 5.3. Goto is from 5.2 however.
while true do goto Test end ::Test:: print("done!")
Edited by apemanzilla, 09 April 2015 - 02:06 PM.
Wojbie 09 Apr 2015
One of places i can see Goto getting usefull in CC? Excavate program. All those
Is Lua 5.3 backwards compatible with 5.2?
Wait never mind that was stupid question. Of course its not. 5.2 looks nicer anyways.
Edited by wojbie, 09 April 2015 - 02:10 PM.
if done then break endparts in nested loops replaced with one label and few gotos. No need for repeated breaks!
apemanzilla, on 09 April 2015 - 02:01 PM, said:
Yeah, and a bunch of other cool stuff in Lua 5.3. Goto is from 5.2 however.
Wait never mind that was stupid question. Of course its not. 5.2 looks nicer anyways.
Edited by wojbie, 09 April 2015 - 02:10 PM.
MKlegoman357 09 Apr 2015
Although I don't like the function environments change in Lua 5.2 it is still a very nice change. Actually, would it be possible to port it straight to Lua 5.3, it adds some new interesting things. Anyway, in addition to Lua 5.2 I'd like to see full support of the standard Lua libraries. I really don't see why they are not full, almost every one of them can be rewritten in plain CC Lua. It would really help when you want to use a 'native' Lua library written by other people, you wouldn't have to modify them anymore. Of course, there shouldn't be any support for C libraries.
Edited by MKlegoman357, 09 April 2015 - 03:25 PM.
Edited by MKlegoman357, 09 April 2015 - 03:25 PM.
apemanzilla 09 Apr 2015
MKlegoman357, on 09 April 2015 - 03:24 PM, said:
Although I don't like the function environments change in Lua 5.2 it is still a very nice change. Actually, would it be possible to port it straight to Lua 5.3, it adds some new interesting things. Anyway, in addition to Lua 5.2 I'd like to see full support of the standard Lua libraries. I really don't see why they are not full, almost every one of them can be rewritten in plain CC Lua. It would really help when you want to use a 'native' Lua library written by other people, you wouldn't have to modify them anymore. Of course, there shouldn't be any support for C libraries.
Bomb Bloke 09 Apr 2015
wojbie, on 09 April 2015 - 02:05 PM, said:
One of places i can see Goto getting usefull in CC? Excavate program. All those
if done then break endparts in nested loops replaced with one label and few gotos. No need for repeated breaks!
You've already got the option of bundling the whole nested loop structure into a function, which you can then simply return from at any point.
Goto is, of course, a more elegant solution. Relatively speaking.
ElvishJerricco 10 Apr 2015
dan200, on 09 April 2015 - 01:23 PM, said:
Do you really change functions environments except just after creating them in your program? I'm struggling to see the use case.
LuaLua is one case where it's necessary. It's a language level extension on Lua for CC, and its runtime depends on being able to change function environments.
Also, there are some weird use-cases where you want to bundle an API with a program. For example, in Grin, I have several APIs I need to use that I have written to work as ordinary CC APIs. But one of Grin's features is its ability to work from a simple "pastebin run" command, so it all needs to be one file. My solution is to keep the APIs in the git repo, and use a text substitution system to insert the APIs within functions that get their environments set.
function json_api() @JSON@ end local json = {} setfenv(json_api, json) json_api()
and then the text substitution system replaces @JSON@ with the contents of the json API file.
It's kinda dirty, but the point is that there are tons of odd-ball use-cases for function environments.
Then again I guess solutions that require forcibly changing environments are kind of like this.
SquidDev 10 Apr 2015
ElvishJerricco, on 10 April 2015 - 09:25 AM, said:
-snip-
Edited by SquidDev, 10 April 2015 - 09:40 AM.
Lupus590 10 Apr 2015
Like any change, there will be some toothaches to get round if/when CC goes to Lua 5.2
Ok, I can't hold it in... I AM REALLY EXCITED FOR A LUA UPDATE!!!!
Edited by Lupus590, 10 April 2015 - 11:25 AM.
Ok, I can't hold it in... I AM REALLY EXCITED FOR A LUA UPDATE!!!!
Edited by Lupus590, 10 April 2015 - 11:25 AM.
dan200 10 Apr 2015
ElvishJerricco, on 10 April 2015 - 09:25 AM, said:
dan200, on 09 April 2015 - 01:23 PM, said:
Do you really change functions environments except just after creating them in your program? I'm struggling to see the use case.
LuaLua is one case where it's necessary. It's a language level extension on Lua for CC, and its runtime depends on being able to change function environments.
Also, there are some weird use-cases where you want to bundle an API with a program. For example, in Grin, I have several APIs I need to use that I have written to work as ordinary CC APIs. But one of Grin's features is its ability to work from a simple "pastebin run" command, so it all needs to be one file. My solution is to keep the APIs in the git repo, and use a text substitution system to insert the APIs within functions that get their environments set.
function json_api() @JSON@ end local json = {} setfenv(json_api, json) json_api()
and then the text substitution system replaces @JSON@ with the contents of the json API file.
It's kinda dirty, but the point is that there are tons of odd-ball use-cases for function environments.
Then again I guess solutions that require forcibly changing environments are kind of like this.
You can achieve your example in Lua 5.2 like so:
local json = {} do local _ENV = json function json_api() ... end end json_api()
ElvishJerricco 10 Apr 2015
dan200 10 Apr 2015
Lua 5.2 has environments, you just can't change them after the function is created. You set them either using the 4th parameter to load (when compiling a function from code), or using the _ENV pattern I showed in the previous post (when declaring a new function). Everyone concerned they're losing something they absolutely need would do well to read what the lua manual has to say on the topic: http://www.lua.org/m...manual.html#2.2
ElvishJerricco 10 Apr 2015
dan200, on 10 April 2015 - 12:36 PM, said:
Lua 5.2 has environments, you just can't change them after the function is created. You set them either using the 4th parameter to load (when compiling a function from code), or using the _ENV pattern I showed in the previous post (when declaring a new function). Everyone concerned they're losing something they absolutely need would do well to read what the lua manual has to say on the topic: http://www.lua.org/m...manual.html#2.2
I know very well how to use environments with 5.2. The issue is definitely minor and very niche, I'm not arguing against that. I'm merely pointing out that minor and niche is non-zero, and I have personally found several uses for setting environments on non-top-level-functions. Again, it's not major, so moving to 5.2 isn't the end of the world or anything. But it does take some justification.