What is an environment exactly?

Started by inteltoasters, Jul 29, 2025, 07:38 AM

Previous topic - Next topic

inteltoasters

I'm coding an OS for ComputerCraft, in the os.run method there is an option for a "environment".

What is that exactly, I'm just curious.
I love ComputerCraft.

NeonKatVT

Quote from: inteltoasters on Jul 29, 2025, 07:38 AMI'm coding an OS for ComputerCraft, in the os.run method there is an option for a "environment".

What is that exactly, I'm just curious.

I honestly tried to come up with an answer based on what I know. However I managed to figure out how to put in words for you.

When you use os.run in ComputerCraft, the environment parameter is a table that acts as the global scope for the program you're launching. Think of it as a custom "world" for your program.

What it is: This table dictates exactly what functions (like print, fs, io) and variables your program can access by default. It essentially becomes the program's _G (global) table.

Why use it?: It's an advanced feature for when you need "something special". This includes the following.

Isolation/Sandboxing: You can limit what a program can do, preventing it from accessing or modifying parts of your OS you don't want it to.

Custom Environments: You can provide only specific functions or data, creating a tailored execution context for a utility or a part of your OS.

Key Difference from shell.run: Unlike shell.run, os.run does not automatically give the launched program access to the shell API. If your program needs shell.run or other shell functions, you would have to manually add them to the environment table you pass.

Example: To run the default shell with a completely clean, minimal environment (no inherited variables or functions from the calling program), you'd use this on the code snippet below.
os.run({}, "/rom/programs/shell.lua")
In short, os.run's environment parameter gives you precise, low-level control over what a program can see and do, making it crucial for building robust operating systems and isolated applications in ComputerCraft, and is used by many in the community.
See my website! - A veteran of CC with expertise in the Lua programming language