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

Are all APIs loaded into _G and are they modificable?

Started by thecrimulo, 20 July 2016 - 05:38 PM
thecrimulo #1
Posted 20 July 2016 - 07:38 PM
I think the title says it all. Are all APIs, like http, textutils, fs or shell loaded into _G, and you can modify them all using _G[''][''] = …?
Are there other things in that global variable? os.loadAPI uses it to load custom APIs? They are some questions I've come through while trying to modify some functions, and I'm still trying to guess it.

Thanks,
~ thecrimulo
Lyqyd #2
Posted 20 July 2016 - 07:42 PM
Everything loaded with os.loadAPI ends up as a table in _G, and these can be modified. Other things, like shell and multishell are provided directly in the environment of programs run inside the shell. These are not in _G (and should never be put in _G), and can also be modified.
thecrimulo #3
Posted 20 July 2016 - 07:45 PM
And when they are not in _G, can I modify them directly with api.function = function()…?
Lyqyd #4
Posted 20 July 2016 - 07:49 PM
Yep.
Sewbacca #5
Posted 20 July 2016 - 09:03 PM
Everything loaded with os.loadAPI ends up as a table in _G, and these can be modified. Other things, like shell and multishell are provided directly in the environment of programs run inside the shell. These are not in _G (and should never be put in _G), and can also be modified.

Why shell and multishell should never be put in _G?
KingofGamesYami #6
Posted 20 July 2016 - 09:04 PM
Why shell and multishell should never be put in _G?

A new shell and multishell instance is required for every program, so that things like shell.getRunningProgram return correct information.
Sewbacca #7
Posted 20 July 2016 - 09:41 PM
Why shell and multishell should never be put in _G?

A new shell and multishell instance is required for every program, so that things like shell.getRunningProgram return correct information.
Thanks!
Lyqyd #8
Posted 21 July 2016 - 12:01 AM
KoGY's explanation is correct, but more accurately phrased: the shell and multishell APIs must be provided by the appropriate shell (if any) that a given piece of code is run from. A program run by another program will end up with the same shell instance as its parent, and can manipulate it (which makes it possible to create a program to assign a shell alias, for instance).