Jump to content




Local-Statement Checker aka Read-Only Tables


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

#1 Lion4ever

  • Members
  • 91 posts

Posted 31 May 2015 - 06: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 by Lion4ever, 31 May 2015 - 06:40 PM.


#2 Pyuu

  • Members
  • 203 posts

Posted 31 May 2015 - 08: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.

#3 flaghacker

  • Members
  • 655 posts

Posted 31 May 2015 - 08:36 PM

View PostLion4ever, on 31 May 2015 - 06:39 PM, said:

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.

#4 Lion4ever

  • Members
  • 91 posts

Posted 01 June 2015 - 08:33 AM

View Postflaghacker, on 31 May 2015 - 08:36 PM, said:

Naming your variables something clear is too :P.

View PostMayushii, on 31 May 2015 - 08:06 PM, said:

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 by Lion4ever, 01 June 2015 - 08:34 AM.


#5 Lion4ever

  • Members
  • 91 posts

Posted 01 June 2015 - 06: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.

#6 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 01 June 2015 - 07:30 PM

So if I'm making an API for use within ComputerCraft, this would essentially crash it every time?

#7 Bomb Bloke

    Hobbyist Coder

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

Posted 02 June 2015 - 12:00 AM

Yes, that's the stated behaviour; this is intended for checking regular scripts, not APIs.

#8 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 02 June 2015 - 10:58 AM

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

#9 Lion4ever

  • Members
  • 91 posts

Posted 02 June 2015 - 05:48 PM

View PostCranium, on 01 June 2015 - 07:30 PM, said:

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.

#10 Lion4ever

  • Members
  • 91 posts

Posted 02 June 2015 - 05:59 PM

View PostBomb Bloke, on 02 June 2015 - 12:00 AM, said:

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.

View PostMKlegoman357, on 02 June 2015 - 10:58 AM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users