Jump to content




Program Migration

computer utility lua

  • You cannot reply to this topic
30 replies to this topic

#21 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 07 November 2015 - 09:10 PM

View PostCreator, on 07 November 2015 - 08:54 PM, said:

That wouldn't work since the program only takes the content from the files. Where would it get the env tables from?

getfenv() returns the environment of the current function which is the "main" program. But that would still cause conflicts because getfenv returns a pointer to the env, not a copy of the env. You have to copy (or even "deep copy") the env to get two independent tables.

#22 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 07 November 2015 - 09:18 PM

Yeah, but why bother?

#23 CoderPuppy

  • Members
  • 121 posts

Posted 07 November 2015 - 10:02 PM

View PostH4X0RZ, on 07 November 2015 - 09:10 PM, said:

View PostCreator, on 07 November 2015 - 08:54 PM, said:

That wouldn't work since the program only takes the content from the files. Where would it get the env tables from?

getfenv() returns the environment of the current function which is the "main" program. But that would still cause conflicts because getfenv returns a pointer to the env, not a copy of the env. You have to copy (or even "deep copy") the env to get two independent tables.

Or use setmetatable({}, { __index = getfenv() }) to create a descended table.

#24 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 08 November 2015 - 09:02 AM

View PostCreator, on 07 November 2015 - 08:54 PM, said:

That wouldn't work since the program only takes the content from the files. Where would it get the env tables from?
Generate new ones with inheriting _G

newenv= setmetatable({},{__index = _G})

Or get the current env with getfenv()

#25 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 08 November 2015 - 09:03 AM

The question is why? What does this bring?

#26 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 08 November 2015 - 09:09 AM

View PostCreator, on 08 November 2015 - 09:03 AM, said:

The question is why? What does this bring?
You avoid conflicts:
func1 = function()
 test = "hi"
 for i=1,15 do
  coroutine.yield()
 end
 A = #test
end
func2 = function()
 coroutine.yield()
 test = 5
end


#27 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 08 November 2015 - 09:32 AM

This is why you use locals.

#28 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 08 November 2015 - 11:05 AM

View PostCreator, on 08 November 2015 - 09:32 AM, said:

This is why you use locals.
But you never know if you migrate some programs, which sometimes aren't yours

#29 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 08 November 2015 - 11:14 AM

Then it's their problem. And to copy tables do this:
local function tablecopy(source,destination)
  source = type(source) == "table" and source or {}
  for i,v in pairs(source) do
    if type(v) == "table" then
	  destination[i] = {}
	  tablecopy(source[i],destination[i])
    else
	  destination[i] = v
    end
  end
end
env1 = tablecopy(getfenv(),{})
env2 = tablecopy(getfenv(),{})

Disclaimer: I am not responsible for recursive tables.

#30 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 08 November 2015 - 11:16 AM

View PostCreator, on 08 November 2015 - 11:14 AM, said:

Then it's their problem. And to copy tables do this:
local function tablecopy(source,destination)
  source = type(source) == "table" and source or {}
  for i,v in pairs(source) do
    if type(v) == "table" then
	  destination[i] = {}
	  tablecopy(source[i],destination[i])
    else
	  destination[i] = v
    end
  end
end
env1 = tablecopy(getfenv(),{})
env2 = tablecopy(getfenv(),{})

Disclaimer: I am not responsible for recursive tables.
No metatable support, no recursion support

Edited by Konlab, 08 November 2015 - 11:18 AM.


#31 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 08 November 2015 - 11:20 AM

You're actually right.
local function tablecopy(source,destination)
  source = type(source) == "table" and source or {}
  for i,v in pairs(source) do
    if type(v) == "table" then
	  destination[i] = {}
	  local mt = getmetatable(v)
	  if mt and type(mt) == "table" then
	    setmetatable(destnation[i],mt)
	  end
	  tablecopy(v,destination[i])
    else
	  destination[i] = v
    end
  end
end

env1 = tablecopy(getfenv(),{})
env2 = tablecopy(getfenv(),{})







2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users