ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: hugeblank on Oct 19, 2018, 06:24 AM

Title: Copying standard tables (_G/_ENV)
Post by: hugeblank on Oct 19, 2018, 06:24 AM
Right, so I'm perfectly aware that _G contains itself, as does _ENV. But which one would I want to copy in the case that I want to set a functions environment using, say, a modified fs API? And how would I go about doing it so that the environment above it isn't affected? Is it as simple as a shallow copy?
Title: Copying standard tables (_G/_ENV)
Post by: Lupus590 on Oct 19, 2018, 04:54 PM
When making a new environment you can use metatables to skip the copy part (reference implementation (https://github.com/lupus590/CC-Random-Code/blob/ffb1c2e7c07045b2b5c48a9959625adac5cbf13e/src/runtime%20string%20compiler.lua)), that way your new environment only contains what it changed from the previous. If you want to sandbox, however, you will need to copy the table contents.