This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Lion4ever's profile picture

Local-Statement Checker aka Read-Only Tables

Started by Lion4ever, 31 May 2015 - 05:39 PM
Lion4ever #1
Posted 31 May 2015 - 07:39 PM
This program makes tables read-only using metatables.

This way you can easily find if and where a table is modified. (e.g. to prevent something like os.pullEvent = nil)

By default this program blocks the enviorment it was started in ( getfenv() ). This way you can tell if you forgot a "local"-statement in your code because if you did, it rises an error.


variable = "value"
-- yourprogram:1: attempt to create global string "variable" in the current environment
local variable = "value"
-- works fine

Using local for variables that are used only by your program is not only slightly faster but considered a better coding-style as well.

Program: pastebin get YqyTgCLy checkLocal

Usage: checkLocal [optional name of program to test]
Edited on 31 May 2015 - 05:40 PM
Pyuu #2
Posted 31 May 2015 - 09:06 PM
Feels more like an API.
Code looks complex, pretty much goes over my head… unless it's the variable naming that is doing that.
Neat.
flaghacker #3
Posted 31 May 2015 - 09:36 PM
Using local for variables that are used only by your program is not only slightly faster but considered a better coding-style as well.

Naming your variables something clear is too :P/>.

Neat program, certainly seems usefull.
Lion4ever #4
Posted 01 June 2015 - 09:33 AM
Naming your variables something clear is too :P/>.

Feels more like an API.
Code looks complex, pretty much goes over my head… unless it's the variable naming that is doing that.
Neat.
I am gonna rename the variables later today. :)/>

The check for local-statements part can not be an API since you would require you to change the programm you want to test.
However an API to offer new read-only table with something like makeNewReadOnlyFrom(sourceTable) would be useful, too. I can add that if you want me to.
Edited on 01 June 2015 - 07:34 AM
Lion4ever #5
Posted 01 June 2015 - 07:32 PM
I changed all variable names now. It should be clear what they do now.

@Mayushii I hope you can understand it now :P/> If not feel free to ask.
Cranium #6
Posted 01 June 2015 - 08:30 PM
So if I'm making an API for use within ComputerCraft, this would essentially crash it every time?
Bomb Bloke #7
Posted 02 June 2015 - 01:00 AM
Yes, that's the stated behaviour; this is intended for checking regular scripts, not APIs.
MKlegoman357 #8
Posted 02 June 2015 - 11:58 AM
I remember a similar program which instead of erroring just logged any changes made to specific tables and/or environment.
Lion4ever #9
Posted 02 June 2015 - 06:48 PM
So if I'm making an API for use within ComputerCraft, this would essentially crash it every time?
Using the following line (default):
local blockTables = {getfenv()}
(first line of the program)

os.loadAPI() is not running in the same enviroment and therefor still works.
With getfenv(2) you can still access and change _G. This program ist not a sandbox.


With this however follwoing line _G is blocked too and neither os.loadAPI nor getfenv(2) can change it:
local blockTables = {_G,getfenv()}
(Second line of program)

So if you uncomment this line and run my program without a parameter, you can not load any APIs anymore.
Like that it is a bit more like a sandbox, but i do not garantie that there is no way around this. I dont know any ways.
Lion4ever #10
Posted 02 June 2015 - 06:59 PM
Yes, that's the stated behaviour; this is intended for checking regular scripts, not APIs.

No, it does not crash not even if you load the API with shell.run(), because that is a new program with a new enviroment which is not blocked. From there you can even change _G if you use the programs default.

But you are right, this is not intended for checking APIs, because everything the API leaves behind is possibly needed by programs using the API.

I remember a similar program which instead of erroring just logged any changes made to specific tables and/or environment.

Yeah, i wanted to have the line where the change came from and i do not know an other way to get that Information.