Talk to a VM in messages

Your application never hands tinyscript a file handle, a socket or a clock. It runs a call for some fuel and reads back a status; when the script wants something, it parks on a message and waits for your answer.

See the loopLook inside an image

One loop drives every call

A script you did not write can loop forever, block on I/O or throw. Your event loop has to keep turning through all three.

So tinyscript never takes the thread. You call ta_tinyscript_run with an amount of fuel, and it comes back with a status before the fuel is gone.

Each status tells you exactly one thing to do next. I like that a lot: there is no callback that fires while you are not looking.

StatusWhat happenedWhat you do
YIELDThe fuel ran out mid-callRun again to continue where it stopped, or abandon the call
EFFECTThe script asked the host for somethingRead the request, then reply when you have the answer
SUSPENDEDA generator yielded a valueResume it with next, return or throw
DONEThe call returnedRead the result
THROWNThe call threw and nothing caught itRead the exception

The outside world is a message away

A sandbox that blocks file and network access has to find every way out. Miss one, and a plugin reads your environment.

tinyscript has one way out, and you write the other end of it. A script imports request from tinyscript:host and calls it with a method name and its arguments.

The VM encodes that as CBOR in a packet buffer you gave it, parks the call and returns EFFECT. You decide whether to answer, how and when; the call resumes at the same expression with your value.

  1. RequestScript in the VM · 1
    const rows = request("queue.read", [{queue, after: cursor}, 32]).rows;
    The script calls the host by method name. The run returns EFFECT.
  2. On the wireScript in the VM · 2
    [1, 7, "queue.read", [{"queue": "orders", "after": 4120}, 32]]
    The request as CBOR in your packet buffer: kind 1, request 7, the method and its arguments.
  3. ReplyYour host · 3
    [1, 7, {"rows": [...]}]
    Your answer to request 7, passed to ta_tinyscript_reply. A stale or malformed reply is refused and the call stays parked.
  4. Or a throwYour host · 4
    [2, 7, {"name": "TypeError", "message": "no such queue"}]
    The request throws that error inside the script, where a try block can catch it.
One request and its two possible answers, as tinyscript's README and specification define the wire.

Every effect crosses the message boundary

The core has no ambient filesystem, clock, output or random source; a call parks on a request and resumes on a validated reply with the same request number.

Source:tinyscript, README.md

Your buffers, your memory

An engine that allocates for itself can grow until the process falls over. One hostile tenant then costs every other tenant.

tinyscript calls no allocator of its own. The compiler, the image and each session work inside buffers you hand over, and a buffer that is too short is a status, never a truncation.

The packet and scratch buffers for requests are yours too: ta_tinyscript_set_host_buffers gives them to a realm. Because a session is just bytes you own, you can copy it, store it and put it under a microscope.

The same protocol in every build

Most engines speak one API in C, another in Node.js and a third in Swift. A host written for one is rewritten for the next.

tinyscript's engine is freestanding and deterministic, so it runs anywhere a C compiler or a WebAssembly engine does. Every build ships the same engine and speaks the same messages; only the language around them changes.

The C library with its CLI ships today as development snapshots. The other builds come from the same source, as the specification records them.

BuildForPublished as
WebAssemblyBrowsers and restricted contextsA WebAssembly module and its JavaScript host
napiServers, through Node-APIA native addon
SwiftApple platformsA Swift package, from GitHub releases
KotlinAndroidA binding, from GitHub releases
AmalgamationAny C or C++ projectOne C file and one header, in the style of SQLite

Look inside an imageRead the architecture