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