<!-- https://tinyscript.tinyactors.dev/docs/anatomy/ -->
# Anatomy of an image · tinyscript

Kind: explanation. For: [Embedder](https://tinyscript.tinyactors.dev/audiences/embedder.xml), [Script author](https://tinyscript.tinyactors.dev/audiences/script-author.xml).
Voice: [Dario](https://tinyscript.tinyactors.dev/voices/explanation.xml).

> What a tinyscript image holds, region by region: a standard WebAssembly module plus custom sections that carry what the compiler knows.

A tinyscript image is a WebAssembly module, byte for byte. Open a real one below, section by section, with the C that writes and reads every byte.

[Open a real image](https://tinyscript.tinyactors.dev/docs/anatomy/index.md#browser) · [See the regions](https://tinyscript.tinyactors.dev/docs/anatomy/index.md#regions)

<a id="browser"></a>

## Open a real image

> A list of regions says what an image can hold. It does not say why `tinyscript.abi` exists when WebAssembly already declares every function's types.

So here are three real images, compiled with `tinyscript compile -g` at the commit this site pins. Pick a section and you get its bytes with every field marked, then the C that writes it, reads it and uses what it keeps.

Try `tinyscript.abi` in checkout.wasm first. WebAssembly says `total` takes an f64 and returns an i64. Then read what the section says that i64 holds, and which functions refuse an image without it.

Three images tinyscript compiled, drawn to scale. Each section shows what it says, its bytes with every field marked, and the C at the pinned commit that writes it, reads it and uses it.

<https://tinyscript.tinyactors.dev/docs/anatomy/#browser>

<a id="regions"></a>

## Standard sections, plus what the compiler knows

> A bytecode format of its own makes an engine a black box. Nothing but the engine can read it, so you take its word for what is inside.

I wanted images that ordinary tools can open. So the code, the imports and the exports live in WebAssembly's standard sections, and standard WebAssembly tools read them like any other module.

What WebAssembly has no words for goes into custom sections named `tinyscript.*`: which functions drive promises, how records are shaped, where each instruction came from. A WebAssembly engine skips them; tinyscript's interpreter uses them to skip work at run time.

1. `header` (\0asm, version 1): The magic bytes and version 1: every image is a standard WebAssembly module.
2. `tinyscript.abi` (custom section): The ABI version, whether the program reads its input from memory, and the source types of the exports that take and return managed values.
3. `tinyscript.array-visitor` (custom section, when `image->array_visitor`): The function the array methods call back through for each element.
4. `tinyscript.promise` (custom section, when `image->promise_driver`): The functions that drive promise jobs and, in version 2, async generators.
5. `tinyscript.dispatch` (custom section, when `image->dispatch`): The drivers of a call whose target is not known until run time: definition, own value and iterable.
6. `tinyscript.coercion` (custom section, when `adapters`): For each built-in import that converts its arguments, the adapter that runs ToPrimitive in the guest.
7. `tinyscript.functions` (custom section, when `image->table_count > TA_FIRST_GUEST_SLOT`): Each guest function in the table: its slot, what kind of callable it is, its arity and its name.
8. `tinyscript.regexp` (custom section, when `image->regexps.length`): The regular expressions, compiled ahead of time to programs for the matcher.
9. `tinyscript.session-variables` (custom section, when `image->session_variable_count`): The session variables and their slots, which code compiled at run time binds.
10. `tinyscript.namespace` (custom section, when `image->namespaces.length`): The module namespace objects: each module’s exports, in code-unit order.
11. `type` (section 1): The function signatures, shared by imports and the program’s own functions.
12. `import` (section 2, when `image->import_count != 0`): The runtime’s built-ins the program calls, by name: the only way out of the module.
13. `function` (section 3): The signature of each function the program defines.
14. `table` (section 4, when `image->table_count`): The function table, for calls whose target is a value.
15. `memory` (section 5, when `image->export_memory || image->data.length != 0`): The linear memory, when the program exports it or has data to place in it.
16. `tag` (section 13, when `image->exception_tag`): The exception tag JavaScript’s throw raises: tinyscript.exception.
17. `global` (section 6, when `image->binding_count`): The program’s top-level bindings, each in its lane: a number, a 64-bit value or a handle.
18. `export` (section 7): The program’s exports, which the host calls by name.
19. `start` (section 8, when `image->start_function`): The function that runs the module’s top-level code.
20. `element` (section 9, when `image->table_count`): The functions placed in the table.
21. `code` (section 10): The bodies of the functions: WebAssembly instructions the interpreter prepares once and runs.
22. `data` (section 11, when `image->data.length != 0`): The program’s literals, copied into memory when the image is prepared.
23. `tinyscript.positions` (custom section, when `image->positions.length`): Where each instruction came from in the source, for error stacks.
24. `tinyscript.layouts` (custom section, when `image->layouts`): The shapes of the program’s records, which property reads try first.
25. `tinyscript.sources` (custom section, when `image->debug`): The source text of the program’s functions, for Function.prototype.toString and debugging.
26. `name` (custom section, when `image->debug`): The standard name section: each function’s name, for tools that read WebAssembly.

The regions of an image, in order, read with tree-sitter from [`src/emit.c`](https://github.com/tinyactors-dev/tinyactors/blob/fb6c854cd344f2fcae2f3aeacbccc12df45ccae0/tinyscript/src/emit.c), the C that writes them. A condition is the source's own test.

<a id="imports"></a>

## Imports are the only way out

> A module that imports anything a host offers can reach anything a host has. Reading its imports tells you what it might do.

A tinyscript image imports only the runtime's built-ins, by name. Even a request to your host goes through one of them, so the import section is a complete list of what a program can ask for.

That is a list you can check before you run anything. I find it a much shorter conversation than a permissions file.

<a id="debug"></a>

## Debug images carry their source

> An error stack that points into bytecode is useless at three in the morning. A release image that carries every source file is wasteful.

So positions are written whenever a program has them, and the source text and the standard name section only in debug images. A release image stays small; a debug image explains itself to `Function.prototype.toString` and to your tools.

[Read how images run](https://tinyscript.tinyactors.dev/architecture/index.md#image) · [Learn the words](https://tinyscript.tinyactors.dev/glossary/index.md)
