11.4.1 How it works
A capability is an opaque value that stands for a specific permission. There is one family per resource:
| Capability | What for |
|---|---|
Console | writing to the output and the error output, setting the exit code |
Arguments | reading the program's arguments, with requestArguments() |
Environment | reading environment variables, with requestEnvironment() |
DirCap, FileCap | folders and files |
Clock, Timer | the time and waits |
NetworkCap | HTTP requests |
They are requested with a request… function and return a Result: the host grants the family if it supports it, or returns an IoError. With Propagate the error is simply returned to the caller.
On the left, the program with three capabilities: Console, FileCap and Clock. On the right, the host with the terminal, the disk and the time. Each capability is an arrow to its resource.
ProgramWarning through the error output
Subroutine Avisar() As Result<Integer, IoError>
Define consola As Console <- Propagate requestConsole();
Discard consola.writeError("Falta el nombre\n");
Return consola.setExitCode(2);
EndSubroutine
Algorithm Revisar
Write "Revisando";
Switch Avisar() Do
Success(codigo): Write "Código de salida: ", codigo;
Failure(problema): Write problema.code;
EndSwitch
EndAlgorithmOutput
Revisando Falta el nombre Código de salida: 2
The program finishes normally.
Capabilities cannot be forged or manipulated: each one carries a slot, a generation and some rights. They can be narrowed —a subdirectory, a single web origin— but never widened. Closing a capability invalidates it: using it afterwards gives the error CapacidadCerrada.
In short
- A capability is the key to an outside resource.
- It is requested with
request…and arrives as aResult. - It can be narrowed but not widened; once closed, it no longer works.