<!-- https://tinyscript.tinyactors.dev/docs/embedding/ -->
# Embedding · tinyscript

Kind: explanation. For: [Embedder](https://tinyscript.tinyactors.dev/audiences/embedder.xml), [Agent builder](https://tinyscript.tinyactors.dev/audiences/agent-builder.xml), [Platform builder](https://tinyscript.tinyactors.dev/audiences/platform.xml).
Voice: [Dario](https://tinyscript.tinyactors.dev/voices/explanation.xml).

> How your application talks to a tinyscript VM: it runs each call for some fuel, and a script reaches the outside world only through messages your host answers.

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 loop](https://tinyscript.tinyactors.dev/docs/embedding/index.md#loop) · [Look inside an image](https://tinyscript.tinyactors.dev/docs/anatomy/index.md)

<a id="loop"></a>

## 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.

| Status | What happened | What you do |
|---|---|---|
| `YIELD` | The fuel ran out mid-call | Run again to continue where it stopped, or abandon the call |
| `EFFECT` | The script asked the host for something | Read the request, then reply when you have the answer |
| `SUSPENDED` | A generator yielded a value | Resume it with next, return or throw |
| `DONE` | The call returned | Read the result |
| `THROWN` | The call threw and nothing caught it | Read the exception |

- **A run never outlives its fuel**: A run that spends its fuel returns `YIELD`; running again continues the same call, and any other call into the realm abandons it. Source: [tinyscript, `include/tinyscript.h`, `ta_tinyscript_run`](https://github.com/tinyactors-dev/tinyactors/blob/fb6c854cd344f2fcae2f3aeacbccc12df45ccae0/tinyscript/include/tinyscript.h)

<a id="messages"></a>

## 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. **Script in the VM, Request**: The script calls the host by method name. The run returns `EFFECT`.

```javascript
const rows = request("queue.read", [{queue, after: cursor}, 32]).rows;
```

2. **Script in the VM, On the wire**: The request as CBOR in your packet buffer: kind 1, request 7, the method and its arguments.

```text
[1, 7, "queue.read", [{"queue": "orders", "after": 4120}, 32]]
```

3. **Your host, Reply**: Your answer to request 7, passed to `ta_tinyscript_reply`. A stale or malformed reply is refused and the call stays parked.

```text
[1, 7, {"rows": [...]}]
```

4. **Your host, Or a throw**: The request throws that error inside the script, where a try block can catch it.

```text
[2, 7, {"name": "TypeError", "message": "no such queue"}]
```

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`](https://github.com/tinyactors-dev/tinyactors/blob/fb6c854cd344f2fcae2f3aeacbccc12df45ccae0/tinyscript/README.md)

<a id="buffers"></a>

## 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.

<a id="everywhere"></a>

## 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.

| Build | For | Published as |
|---|---|---|
| WebAssembly | Browsers and restricted contexts | A WebAssembly module and its JavaScript host |
| napi | Servers, through Node-API | A native addon |
| Swift | Apple platforms | A Swift package, from GitHub releases |
| Kotlin | Android | A binding, from GitHub releases |
| Amalgamation | Any C or C++ project | One C file and one header, in the style of SQLite |

- **Five builds, one host protocol**: Every build speaks the same host protocol: requests and replies as messages through the packet and scratch buffers. Source: [tinyscript's specification, decision `d-distribution`](https://github.com/tinyactors-dev/tinyactors/blob/fb6c854cd344f2fcae2f3aeacbccc12df45ccae0/tinyscript/spec/tinyscript.xml)

[Look inside an image](https://tinyscript.tinyactors.dev/docs/anatomy/index.md) · [Read the architecture](https://tinyscript.tinyactors.dev/architecture/index.md)
