ComputerCraft Beta Versions - Download and Discussion (1.74pr37, released June 22nd)
#461
Posted 09 April 2015 - 01:23 PM
#462
Posted 09 April 2015 - 01:26 PM
dan200, on 09 April 2015 - 01:23 PM, said:
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.
#463
Posted 09 April 2015 - 01:35 PM
#464
Posted 09 April 2015 - 01:37 PM
dan200, on 09 April 2015 - 01:23 PM, said:
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.
#465
Posted 09 April 2015 - 01:51 PM
Edited by wojbie, 09 April 2015 - 01:52 PM.
#466
Posted 09 April 2015 - 01:58 PM
#467
Posted 09 April 2015 - 02:01 PM
dan200, on 09 April 2015 - 01:58 PM, said:
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.
#468
Posted 09 April 2015 - 02:05 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:
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.
#469
Posted 09 April 2015 - 03:24 PM
Edited by MKlegoman357, 09 April 2015 - 03:25 PM.
#470
Posted 09 April 2015 - 03:56 PM
MKlegoman357, on 09 April 2015 - 03:24 PM, said:
#471
Posted 09 April 2015 - 09:27 PM
wojbie, on 09 April 2015 - 02:05 PM, said:
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.
#472
Posted 09 April 2015 - 09:31 PM
#473
Posted 09 April 2015 - 09:49 PM
Edited by apemanzilla, 09 April 2015 - 09:58 PM.
#474
Posted 10 April 2015 - 09:25 AM
dan200, on 09 April 2015 - 01:23 PM, said:
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.
#475
Posted 10 April 2015 - 09:35 AM
ElvishJerricco, on 10 April 2015 - 09:25 AM, said:
Edited by SquidDev, 10 April 2015 - 09:40 AM.
#476
Posted 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.
#477
Posted 10 April 2015 - 11:47 AM
ElvishJerricco, on 10 April 2015 - 09:25 AM, said:
dan200, on 09 April 2015 - 01:23 PM, said:
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()
#479
Posted 10 April 2015 - 12:36 PM
#480
Posted 10 April 2015 - 01:40 PM
dan200, on 10 April 2015 - 12:36 PM, said:
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.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users