How tinyscript runs a script

tinyscript is a compiler and an interpreter in one C library: the compiler turns source text into a WebAssembly image, and the interpreter runs that image inside your application. After reading, you can judge where its speed comes from and what it does not do yet.

Follow the pathLearn the words

Source becomes an image before it runs

An application that evaluates a chart's expressions on every event cannot afford to parse them each time. Two evaluators, one for source and one for compiled code, would also disagree on edge cases.

tinyscript has one way in: its compiler. The compiler parses each source file into a syntax tree, resolves names across the program, and lowers each function into WebAssembly, callees first.

The result is an image, an ordinary WebAssembly module. There is no second evaluator that reads source or walks a tree, so code built at run time with new Function goes through the same compiler. Before the image runs, preparation reads it once.

StageWhat it makesHow often
ParseOne syntax tree per source, with parsers generated from JavaScript and TypeScript grammarsOnce per compile
Scan and resolveSide tables: functions, imports, scopes, captured variables and the call graphOnce per compile
LowerAn intermediate representation per function, then its WebAssembly bytesOnce per function
LinkThe image, with direct calls linked and drivers addedOnce per compile
PrepareA validated, decoded image with register blocksOnce per image
RunCalls, in a realm per sessionEvery call

The compiler is the only way in

There is no second evaluator that reads source or walks a syntax tree; new Function reaches the same compiler at run time.

Source:tinyscript, docs/compiler.md

An image is standard WebAssembly

A compiled program in a private format can only be read with the engine that made it. Debugging a failed call then means trusting that engine's own tools.

An image is a WebAssembly module any standard tool can read. What tinyscript needs beyond standard WebAssembly travels in custom sections, which other hosts may ignore.

One section says how each export takes and returns values. Another maps every call and throw to its source line and column, so an error names where it happened. A debug build adds the source text itself.

SectionWhat it carries
tinyscript.abiHow each exported function takes and returns values
tinyscript.positionsThe source line and column of every call and throw
tinyscript.session-variablesThe session variables the program reads
tinyscript.layoutsThe field layouts the compiler predicted
tinyscript.sourcesThe source text, in debug builds

Values keep their native representation

An engine that boxes every number pays for a tag check on each addition, in every iteration of a checksum loop.

The compiler gives each value a representation: number, boolean, handle, dynamic or void. A number it can prove stays a plain binary64 value in WebAssembly, with no tag.

A value whose type it cannot prove, such as TypeScript's any or a union, becomes a dynamic word: a tagged 64-bit value. Because a function's best representation depends on its callees, the compiler compiles callees first and keeps a summary of each.

Shapes are predictions the run time checks

An event handler reads the same fields of every event it gets. Looking each field up by name on every read costs a search the program's types could have answered.

A TypeScript record type, or a layout the host supplies for plain JavaScript, tells the compiler the order of an object's fields. It compiles a read as a guess at a slot.

The run time checks the key in that slot. On a miss it takes ordinary JavaScript lookup, inherited properties included, so an object of another shape is never read wrong. A type is a hint here, not a promise.

Preparation turns numeric code into register blocks

WebAssembly describes a stack machine. An interpreter that runs it instruction by instruction moves values on and off the stack for every addition in a numeric loop.

Preparation runs once per image. It validates the module, decodes its instructions into a compact form and finds stretches of numeric code it can turn into register blocks.

A register block keeps constants and temporaries in registers and carries a loop's values straight into its next assignment. When a fuel slice ends inside a block, the call resumes from the original WebAssembly instructions. Every session that runs the same program shares the prepared image.

One prepared image runs in many realms

A host with thousands of sessions cannot compile and prepare a program for each one. Sharing one copy is only safe if no session can see another's objects.

The interpreter runs prepared code for a session in that session's realm: its globals and its heap. Realms share the prepared image, and each keeps its own objects.

Within a call, the interpreter counts fuel, so your application bounds how long it may run. When the program needs the clock or sends a message, it reports an effect and parks until your application replies.

One tree and one IR, not a pipeline

The compiler also runs while programs run, for new Function, so every pass it makes is time and memory a running program pays.

We lower from the parser's tree because the grammar has already settled precedence and nesting. One intermediate representation follows, because some decisions, such as whether a value is boxed, come after the code that depends on them.

We chose not to go into SSA form, as compilers such as Go's do: turning SSA back into WebAssembly's structured stack machine needs a stackifier, and preparation already builds register blocks. Constant folding, removing dead branches and inlining go into that one IR.

What this design does not fix yet: tinyscript compiles a profile of JavaScript, and a construct with no case fails to compile. The reference says how far each feature goes.

Read the reference