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:
- The file is large, about 89 MB. It contains a copy of the Node.js runtime, because that is what runs your program. It is not machine code written from scratch.
- It is for the kind of computer that built it. Build on Windows to get a Windows program; macOS and Linux need to be built on macOS and Linux. (Those two paths are written but have not been tested yet.)
- It is not code-signed. Windows SmartScreen or an antivirus may warn about a file you just made and nobody has seen before. That is normal for unsigned programs, and signing is something you buy and do yourself.
- Start-up is about the same as
cresset run(around 75 ms here after the first run; the very first run of a new file can be slower while your antivirus looks at it).
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):
| Program | Interpreter | Compiled | Faster |
|---|---|---|---|
Recursive fib(27) | 78 ms | 5 ms | 14x |
| A loop of 3 million steps with arithmetic | 200 ms | 20 ms | 10x |
| Sieve of primes up to 2 million | 868 ms | 224 ms | 4x |
| Records with methods, 500 thousand calls | 166 ms | 79 ms | 2x |
| Map with 300 thousand keys | 282 ms | 236 ms | 1.2x |
| All eight programs together | 2018 ms | 767 ms | 2.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))
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:
- The entire test suite can be run through the compiler instead of the interpreter (
CRESSET_BACKEND=compiled node tests/run_tests.js), and every program in it must give the same output and the same errors. - Every example program and every runnable program in these docs is run both ways, with random numbers and the clock replayed from the first run, and compared exactly.
- Packages, shared types, methods, permissions given to packages, and built standalone files each have their own tests.
What it is not
Be clear about what you are getting:
- It compiles to JavaScript, not to machine code. A built program runs on Node.js.
--exehides that by packing Node.js inside the file, but the program is still JavaScript underneath. Real machine-code output is a bigger project that isn't done. - The playground and the REPL use the interpreter. The playground's security policy forbids running generated code in the page, and the REPL needs the interpreter's step-by-step control.
cresset testuses the interpreter.
Next
How permissions work, in the compiler and out of it: Effects and permissions.