Skip to content

SpecificationXI · Concurrency and asynchrony

11.4

Effects and capabilities

To touch the outside world —the console, files, the clock, the network— a program asks for a capability: a key that gives access to one single thing.

Markdown version

11.4.1 How it works

A capability is an opaque value that stands for a specific permission. There is one family per resource:

CapabilityWhat for
Consolewriting to the output and the error output, setting the exit code
Argumentsreading the program's arguments, with requestArguments()
Environmentreading environment variables, with requestEnvironment()
DirCap, FileCapfolders and files
Clock, Timerthe time and waits
NetworkCapHTTP 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.

Figure 11.4-1.The program does not touch the host directly: every resource is used through its key, which the host handed over when asked.

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
EndAlgorithm

Output

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 a Result.
  • It can be narrowed but not widened; once closed, it no longer works.