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
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.
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.
| Stage | What it makes | How often |
|---|---|---|
| Parse | One syntax tree per source, with parsers generated from JavaScript and TypeScript grammars | Once per compile |
| Scan and resolve | Side tables: functions, imports, scopes, captured variables and the call graph | Once per compile |
| Lower | An intermediate representation per function, then its WebAssembly bytes | Once per function |
| Link | The image, with direct calls linked and drivers added | Once per compile |
| Prepare | A validated, decoded image with register blocks | Once per image |
| Run | Calls, in a realm per session | Every call |
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
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.
| Section | What it carries |
|---|---|
tinyscript.abi | How each exported function takes and returns values |
tinyscript.positions | The source line and column of every call and throw |
tinyscript.session-variables | The session variables the program reads |
tinyscript.layouts | The field layouts the compiler predicted |
tinyscript.sources | The source text, in debug builds |
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.
A heterogeneous value uses a tagged i64 word, while known single-type paths continue to use native lanes.
Source:tinyscript, spec/tinyscript.xml, Runtime-typed JavaScript values
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.
A prediction verifies the live own key at the suggested slot, and a miss takes ordinary lookup or assignment.
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.
Before register blocks, the numeric loop retired 150,900,935 instructions; after them, 72,903,466.
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.
A prepared module is shared across isolated caller-owned realms, and an export call in one realm sees that realm's object identities.
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.
The compiler reports UNSUPPORTED with not lowered yet: and what it met, instead of guessing.