Jump to content




ComputerCraft Beta Versions - Download and Discussion (1.74pr37, released June 22nd)


525 replies to this topic

#461 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 09 April 2015 - 01:23 PM

Do you really change functions environments except just after creating them in your program? I'm struggling to see the use case.

#462 apemanzilla

  • Members
  • 1,421 posts

Posted 09 April 2015 - 01:26 PM

View Postdan200, 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.


#463 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 09 April 2015 - 01:35 PM

But surely the users programs are loaded from files? Meaning you can safely sandbox them on load time.

#464 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 09 April 2015 - 01:37 PM

View Postdan200, 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.

#465 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 09 April 2015 - 01:51 PM

Wait does that mean we will [eventually] get Loved/Hated Goto! This will be interesting.

Edited by wojbie, 09 April 2015 - 01:52 PM.


#466 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 09 April 2015 - 01:58 PM

Wait, they added goto to lua? What a time to be alive.

#467 apemanzilla

  • Members
  • 1,421 posts

Posted 09 April 2015 - 02:01 PM

View Postdan200, 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.


#468 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 09 April 2015 - 02:05 PM

One of places i can see Goto getting usefull in CC? Excavate program. All those
if done then
  break
end
parts in nested loops replaced with one label and few gotos. No need for repeated breaks!


View Postapemanzilla, 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.
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.


#469 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 09 April 2015 - 03:24 PM

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.


#470 apemanzilla

  • Members
  • 1,421 posts

Posted 09 April 2015 - 03:56 PM

View PostMKlegoman357, 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.
LuaJ only supports up to Lua 5.2 at the moment.

#471 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 April 2015 - 09:27 PM

View Postwojbie, 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
end
parts 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 CodeWafels

  • Members
  • 6 posts

Posted 09 April 2015 - 09:31 PM

what are the new things in this devbeta?

#473 apemanzilla

  • Members
  • 1,421 posts

Posted 09 April 2015 - 09:49 PM

Edit: Nvm, I derped.

Edited by apemanzilla, 09 April 2015 - 09:58 PM.


#474 ElvishJerricco

  • Members
  • 803 posts

Posted 10 April 2015 - 09:25 AM

View Postdan200, 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.

#475 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 10 April 2015 - 09:35 AM

View PostElvishJerricco, on 10 April 2015 - 09:25 AM, said:

-snip-
This is another case which is bugging me. In the case of Grin you have direct control over the source so you can always just add a local _ENV = setmetatable({}, {__index = getfenv()}) at the top of functions. I'm all for this change to Lua 5.2 though!

Edited by SquidDev, 10 April 2015 - 09:40 AM.


#476 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 10 April 2015 - 11:25 AM

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.


#477 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 10 April 2015 - 11:47 AM

View PostElvishJerricco, on 10 April 2015 - 09:25 AM, said:

View Postdan200, 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()


#478 ElvishJerricco

  • Members
  • 803 posts

Posted 10 April 2015 - 12:25 PM

View Postdan200, on 10 April 2015 - 11:47 AM, said:

- snip -

Well, like I said, I'm sure my arguments for environments are just bad solutions to odd problems. But I have found enormous amounts of use for them.

#479 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 10 April 2015 - 12:36 PM

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

#480 ElvishJerricco

  • Members
  • 803 posts

Posted 10 April 2015 - 01:40 PM

View Postdan200, 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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users