Compiling

Cresset has two ways to run a program. The interpreter reads your program and runs it directly; it is what cresset run uses, and what the playground uses. The compiler translates your program into JavaScript first, and then runs that. It is much faster, and it can package a program into a single file you can hand to someone else.

Both enforce exactly the same rules. The permission checker and the type checker run first, before anything is generated, so a program that is refused by the interpreter is refused by the compiler too, and a compiled program can still only do what you grant it.

Run a program compiled

cresset run program.crs --compiled

It behaves exactly like cresset run, but faster.

Build a standalone file

cresset build hello.crs
node hello.js

build writes hello.js: one file, with everything it needs inside it. You can copy it to another computer, as long as that computer has Node.js (version 18 or newer). It does not need Cresset installed.

The file takes the same permission flags as cresset run:

node report.js --allow read
node report.js --allow-all
node simulation.js --record run.json
node simulation.js --replay run.json
node tool.js --max-steps 1000000 -- these words go to args()

A built program with no flags can only print. If it asks for more, it refuses to start and says what it wanted:

error: this program asks for permissions you haven't granted: read
help: if you trust it, run with:  --allow print,read

Build a native executable

cresset build hello.crs --exe
./hello          # hello.exe on Windows

This makes one file that runs on its own: no Node.js, no Cresset. Give it to someone and they can double-click it. It takes the same permission flags (hello.exe --allow read) and enforces them the same way.

First time only. Packing needs a small official Node.js tool called postject (MIT license, about 5 MB). Cresset will not download anything unasked. The first build stops and tells you so; run it once with the extra flag:

cresset build hello.crs --exe --install-native-tools

The tool is kept in ~/.cresset/native (delete the folder to remove it), and later builds need no flag.

What you should know:

How much faster?

Measured by node tools/bench_compiled.js on the machine this was developed on (your numbers will differ, but the pattern should hold):

ProgramInterpreterCompiledFaster
Recursive fib(27)78 ms5 ms14x
A loop of 3 million steps with arithmetic200 ms20 ms10x
Sieve of primes up to 2 million868 ms224 ms4x
Records with methods, 500 thousand calls166 ms79 ms2x
Map with 300 thousand keys282 ms236 ms1.2x
All eight programs together2018 ms767 ms2.6x

Code that spends its time in arithmetic, loops and function calls gains the most. Code that spends its time inside built-in operations (maps, sorting, text) gains less, because that work was already done by fast built-in code.

What the compiler actually does

It turns each of your functions into a JavaScript function, with real local variables, direct calls from one function to another, and numbers and comparisons written inline. Everything that has a rule, a permission or an error message attached still goes through the same runtime code the interpreter uses, so the messages match.

You can look at what it makes. For this program:

fn fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

fn main() needs print:
    print(fib(20))

Try it in the playground →

the compiled fib looks like this (comments and a little bookkeeping removed):

function f_fib(v_n) {
  if (++R.steps > R.max) R.tooLong(1);
  if (++R.depth > 800) R.deep(1);
  try {
    if (R.lt(v_n, 2, 2)) { return v_n; }
    return R.add(f_fib(R.sub(v_n, 1, 4)), f_fib(R.sub(v_n, 2, 4)), 4);
  } finally { R.depth--; }
}

Two safety limits survive compilation: the step limit (--max-steps) and the 800-call recursion limit.

How it is kept correct

A second implementation is a place for bugs to hide, so the project's tests take it seriously:

What it is not

Be clear about what you are getting:

Next

How permissions work, in the compiler and out of it: Effects and permissions.