Inside a compiled image

A tinyscript image is a WebAssembly module, byte for byte. Here is every region it can hold, in the order the compiler writes them, and when each one is there.

See the regionsRun an image

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.

  • In every image
  • When the program uses it
  • In debug images only
  1. 1header\0asm, version 1The magic bytes and version 1: every image is a standard WebAssembly module.
  2. 2tinyscript.abicustom sectionThe ABI version, whether the program reads its input from memory, and the source types of the exports that take and return managed values.
  3. 3tinyscript.array-visitorcustom section, when image->array_visitorThe function the array methods call back through for each element.
  4. 4tinyscript.promisecustom section, when image->promise_driverThe functions that drive promise jobs and, in version 2, async generators.
  5. 5tinyscript.dispatchcustom section, when image->dispatchThe drivers of a call whose target is not known until run time: definition, own value and iterable.
  6. 6tinyscript.coercioncustom section, when adaptersFor each built-in import that converts its arguments, the adapter that runs ToPrimitive in the guest.
  7. 7tinyscript.functionscustom section, when image->table_count > TA_FIRST_GUEST_SLOTEach guest function in the table: its slot, what kind of callable it is, its arity and its name.
  8. 8tinyscript.regexpcustom section, when image->regexps.lengthThe regular expressions, compiled ahead of time to programs for the matcher.
  9. 9tinyscript.session-variablescustom section, when image->session_variable_countThe session variables and their slots, which code compiled at run time binds.
  10. 10tinyscript.namespacecustom section, when image->namespaces.lengthThe module namespace objects: each module’s exports, in code-unit order.
  11. 11typesection 1The function signatures, shared by imports and the program’s own functions.
  12. 12importsection 2, when image->import_count != 0The runtime’s built-ins the program calls, by name: the only way out of the module.
  13. 13functionsection 3The signature of each function the program defines.
  14. 14tablesection 4, when image->table_countThe function table, for calls whose target is a value.
  15. 15memorysection 5, when image->export_memory || image->data.length != 0The linear memory, when the program exports it or has data to place in it.
  16. 16tagsection 13, when image->exception_tagThe exception tag JavaScript’s throw raises: tinyscript.exception.
  17. 17globalsection 6, when image->binding_countThe program’s top-level bindings, each in its lane: a number, a 64-bit value or a handle.
  18. 18exportsection 7The program’s exports, which the host calls by name.
  19. 19startsection 8, when image->start_functionThe function that runs the module’s top-level code.
  20. 20elementsection 9, when image->table_countThe functions placed in the table.
  21. 21codesection 10The bodies of the functions: WebAssembly instructions the interpreter prepares once and runs.
  22. 22datasection 11, when image->data.length != 0The program’s literals, copied into memory when the image is prepared.
  23. 23tinyscript.positionscustom section, when image->positions.lengthWhere each instruction came from in the source, for error stacks.
  24. 24tinyscript.layoutscustom section, when image->layoutsThe shapes of the program’s records, which property reads try first.
  25. 25tinyscript.sourcescustom section, when image->debugThe source text of the program’s functions, for Function.prototype.toString and debugging.
  26. 26namecustom section, when image->debugThe 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, the C that writes them. A condition is the source's own test.

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.

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 runLearn the words