Retrieved article excerpt
Open article · Retrieved 2026-11-03T09:22:33.719121+00:00
# [What djs is for](https://earendil-works.github.io/djs/introduction.html#what-djs-is-for)
djs is a small JavaScript and TypeScript engine written in Rust. It exists for one job:
running the scripts that a language model writes in pi’s **codemode**, inside a
WebAssembly sandbox, in place of QuickJS. This guide explains how it works and, above
all, why it is built the way it is. Almost every unusual decision in djs follows from
that one job, so this page starts there.
> **In short.** A codemode script is short, calls slow tools, moves a lot of data, and
> must be stoppable and resumable at any moment. djs therefore keeps *all* of its state
> in one byte array that it can copy, never uses the Rust stack for JavaScript calls,
> lets the host read values straight out of memory, and compiles source to bytecode in
> one pass.
## [A codemode script](https://earendil-works.github.io/djs/introduction.html#a-codemode-script)
Instead of calling tools one at a time, the model writes a small program that calls
them, combines their results and returns what it wants to show:
liveeditable · runs in djs-wasm in your browser`Ctrl+↵` run · `Tab` indent · `Esc` leaveResetRun `Ctrl+↵`
```
// A stand-in for a tool the host provides; in pi it is `tools.search`.
const search = async (q) => [
{ name: "djs", stars: 120, lang: "Rust" },
{ name: "quickjs", stars: 9000, lang: "C" },
];
const hits = await search("small js engines");
const rust = hits.filter((h) => h.lang === "Rust");
console.log(`${rust.length} of ${hits.length} in Rust:`, rust.map((h) => h.name).join(", "));
return rust;
```
OutputBytecodeHeapSuspend & snapshot
```
1 of 2 in Rust: djs
```
```
function 0 <anonymous>: params=2 locals=4 stack=6 captures=0
handler [3, 94) -> 94 depth 0
0 async_start loc0 (<state>)
3 fclosure child0
8 set_loc1 (search) ; 6:20
9 undefined
10 push_const8 #0 ("small js engines")
12 call1 ; 6:26
13 await loc0 (<state>) ; 6:14
16 set_loc2 (hits) ; 7:14
17 get_method "filter" ic0 ; 7:18
24 fclosure child1
29 call1 ; 7:25
30 put_loc3 (rust)
31 get_arg1 ; 8:1
32 get_method "log" ic1 ; 8:8
39 get_loc3 (rust) ; 8:16
40 get_field "length" ic2 ; 8:20
47 to_string ; 8:14
48 push_const8 #1 (" of ")
50 get_loc2 (hits) ; 8:34
51 get_field "length" ic3 ; 8:38
58 to_string ; 8:32
59 push_const8 #2 (" in Rust:")
61 concat 4
64 get_loc3 (rust) ; 8:58
65 get_method "map" ic4 ; 8:62
72 fclosure child2
77 call1 ; 8:66
78 get_method "join" ic5 ; 8:81
85 push_const8 #3 (", ")
87 call1 ; 8:86
88 call2 ; 8:12
89 drop
90 get_loc3 (rust) ; 9:8
91 async_return loc0 (<state>)
94 async_reject loc0 (<state>)
function 0.0 search: params=1 locals=1 stack=3 captures=0
handler [3, 68) -> 68 depth 0
0 async_start loc0 (<state>)
3 object
4 push_const8 #1 ("djs")
6 define_field "name" ic0
13 push_i8 120
15 define_field "stars" ic1
22 push_const8 #0 ("Rust")
24 define_field "lang" ic2
31 object
32 push_const8 #2 ("quickjs")
34 define_field "name" ic3
41 push_i32 9000
46 define_field "stars" ic4
53 push_const8 #3 ("C")
55 define_field "lang" ic5
62 array_from 2
65 async_return loc0 (<state>)
68 async_reject loc0 (<state>)
function 0.1 <anonymous>: params=1 locals=0 stack=2 captures=0
0 get_arg0 ; 7:33
1 get_field "lang" ic0 ; 7:34
8 push_const8 #0 ("Rust")
10 strict_eq ; 7:40
11 return
function 0.2 <anonymous>: params=1 locals=0 stack=1 captures=0
0 get_arg0 ; 8:74
1 get_field "name" ic0 ; 8:75
8 return
```
Run the example to walk the VM's arena.
Run the example to see it suspend and snapshot.
Such scripts have a particular profile, and each part of it became a design goal:
| What a codemode script does | What djs needs to do | Goal |
| --- | --- | --- |
| Receives tool results: long strings, arrays of similar objects, base64 images | Get data in and out with as little copying and conversion as possible | [cheap data passing](https://earendil-works.github.io/djs/goals.html#cheap-data-passing) |
| Runs for milliseconds, once, then is thrown away | Start in microseconds; compile fast; keep the wasm module small | [fast startup in wasm](https://earendil-works.github.io/djs/goals.html#fast-startup-in-wasm) |
| Is written fresh every time | Compile in one pass, straight to bytecode | [no syntax tree](https://earendil-works.github.io/djs/goals.html#no-syntax-tree) |
| Waits for tools that may take minutes | Save the whole VM while it waits, restore it elsewhere | [snapshots](https://earendil-works.github.io/djs/goals.html#snapshots) |
| Must not hog the host, may loop forever, may be cancelled | Stop between any two instructions and resume later, even inside a `map` callback or a getter | [suspend anywhere](https://earendil-works.github.io/djs/goals.html#suspend-anywhere) |
The goals are in priority order, and the next chapter, [what djs optimizes
for](https://earendil-works.github.io/djs/goals.html), explains each of them. Two more constraints shape everything: **wasm32
is the target that counts** (memory and speed are judged in V8’s wasm tier, [D016](https://earendil-works.github.io/djs/decisions.html#d016 "wasm32 is the primary target for performance and memory design")), and
**behavior matches QuickJS**, down to error messages, because models read those messages
and pi’s tests expect them.
## [The shape of the system](https://earendil-works.github.io/djs/introduction.html#the-shape-of-the-system)
The parts of djs and the arena they share
Host (JavaScript)
pi's codemode, a Sandbox
script source
JS or TypeScript
tool functions
answered later
wire encoder
values in
heap reader
values out, read in place
djs-core: Rust, no\_std, in wasm
compiler
tokens to bytecode
interpreter
fuel, no recursion
copying GC
compacts the arena
pending host calls
and the inbox
function templates
bytecode, constants
value stack, frames
every call
realm, builtins
the base snapshot
job queue
promise reactions
objects, strings
shapes, ropes, arrays
atoms
property keys
handles
values the host holds
The arena: one byte array with all VM state, linked by u32 offsets
a snapshot is a copy of these bytes
calls
results
reads
The parts of djs and the arena they share
- The **compiler** turns source text (JavaScript or TypeScript; types are erased while
parsing) into bytecode in a single pass, without building a syntax tree.
- The **interpreter** runs that bytecode. JavaScript calls do not recurse on the Rust
stack: every frame lives in the arena, so the interpreter can return to the host after
any instruction and pick up exactly where it stopped.
- The **arena** holds everything: objects, strings, bytecode, the value stack, the call
frames, the promise job queue, the pending tool calls. Values refer to each other by
`u32` offsets, never by pointers, so the arena can move, be copied and be read from
the outside.
- The **garbage collector** compacts the arena in place, which keeps snapshots small and
makes the heap image of a program a deterministic function of the program.
- The **builtins** (`Array.prototype.map`, `JSON.parse`, …) are written in a strict
subset of TypeScript and compiled twice: to bytecode that ships in the base snapshot,
and, for the hot ones, to Rust state machines that can suspend in the middle.
- The **host** (pi’s codemode, or the `Sandbox` class of the `@djs/host` package) sends
values in through a compact wire format and reads results by walking the arena
directly, with a reader generated from the same layout tables as the engine.
## [What djs is not](https://earendil-works.github.io/djs/introduction.html#what-djs-is-not)
djs is always in strict mode, has no ES modules, no `Intl` and no JIT, among other
things left out on purpose; [the non-goals](https://earendil-works.github.io/djs/goals.html#non-goals) lists them and why.
## [How to read this guide](https://earendil-works.github.io/djs/introduction.html#how-to-read-this-guide)
The tour goes from the bottom up: the arena and values, cells and objects, the compiler,
the interpreter, suspension, the collector, snapshots, the builtins, determinism, and
the host interface. Each chapter explains its **trade-offs**: what the design costs, and
the alternatives that were not taken. The chapters explain each decision where it
matters; the decision’s ID ([D1050](https://earendil-works.github.io/djs/decisions.html#d1050 "Value encoding")) links to its entry in the [decision
log](https://earendil-works.github.io/djs/decisions.html), the record kept while djs was built, with the full reasoning and
the measurements behind it.
Code samples marked *live* run in djs-wasm in your browser on the docs site. The output
shown under each sample was produced by running it in this build of djs when the guide
was checked, so it cannot drift from what djs actually does.
Decisions on this page:
- [D016](https://earendil-works.github.io/djs/decisions.html#d016 "wasm32 is the primary target for performance and memory design"): wasm32 is the primary target for performance and memory design
- [D008](https://earendil-works.github.io/djs/decisions.html#d008 "Codemode top level is a function body"): Codemode top level is a function body
- [D1100](https://earendil-works.github.io/djs/decisions.html#d1100 "Difftest compares what pi's codemode shows the model"): Difftest compares what pi’s codemode shows the model