# `Alternera.PluginWasmFramework.HostCalls`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.1/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/host_calls.ex#L1)

The host functions a language step's guest calls through
`alternera.host_call`, served in the process that runs the step
(`Runtime.call/5`), each through the plugin SDK only:
`Plugin.Provisional.Store`, `Plugin.Provisional.Reads` (over
`Plugin.Host`) and `Plugin.Provisional.Calls`.

An operation's argument is a JSON array of its positional arguments, as
a script passes them, and its answer a JSON array of what it returns:

| Operation | Arguments | Answer |
|---|---|---|
| `store.get` | store, key, default? | `[value]`: the value, the default or null |
| `store.put` | store, key, value, opts? | `[true]`, or `[false, "store_key_exists"]` with `if_absent` |
| `store.update` | store, key, opts | `[value]`, or `[false, "store_key_not_found"]` |
| `store.delete` | store, key | `[deleted]` |
| `store.list` | store, opts? | `[entries, truncated]`, each entry `{key, value, age}` |
| `tenant.var` | name | `[value]` |
| `tenant.secret` | name | `["{{secret.NAME}}"]`: the reference, never the value |
| `pipeline.call` | name, inputs? | `[result, exchange]`: the callee's result (true when it has none) and the `req`, `res` and `vars` the script reads from then on |
| `log` | message | `[]` |

The store options and their checks are Script's (`ttl`, `if_absent`;
`mode`, `value`, `by`, `create_if_missing`, `initial`; `prefix`,
`limit`), and so are the failures: a miss or a failed condition is an
answer, anything else an error of the store's type
(`store_value_too_large`, `store_full`, `store_invalid_value`,
`store_type_mismatch`, `store_unavailable`) that the guest raises in
the script, so a script may catch it, and that fails the step with that
type when it does not. A store value read that is larger than the
call's memory limit is refused with `store_value_too_large` before it
is copied in.

A value read from a store the tenant marked secret (`get`, `list`, the
value `update` returns) is noted (`Plugin.Provisional.SecretReads`): the step then marks
every place it wrote as secret, and scrubs or withholds the texts of
its failures, as Script does.

`pipeline.call` runs the pipeline in this process with the runner's
handle (`Calls`), within the call's deadline, and is refused after it:
the callee gets the exchange as the step got it plus what earlier calls
wrote, never the script's own edits. A body the callee changed becomes
the step's own body from then on (what `body_read` gives). Its failure
is an error of the callee's type, scrubbed of what the run read.

A module uploaded into a step (`uses` set) names its own parameters in
the first argument of a store, `pipeline.call`, `tenant.var` or
`tenant.secret` call; `Uses.translate/3` turns them into the names its
step binds and refuses what the module did not declare (`not_declared`)
or the step did not bind (`not_bound`), before anything is read.

`log` writes the message, scrubbed of what the run read from secret
stores and cut to 1024 bytes, to the proxy's log at info with the
tenant, pipeline and step; at most 50 lines a run, the rest are
dropped and counted. It is the guest's only way out: its standard
streams go nowhere (both runtimes give it an empty WASI context).

# `t`

```elixir
@type t() :: %Alternera.PluginWasmFramework.HostCalls{
  access: term(),
  after_call: term(),
  bodies: term(),
  call_error: term(),
  called?: term(),
  calls: term(),
  context: term(),
  deadline: term(),
  dropped_logs: term(),
  files: term(),
  logged: term(),
  marks: term(),
  max_value_bytes: term(),
  own: term(),
  tenant_vars: term(),
  uses: term()
}
```

A run's host state: what it may reach (`access`, the store tenant and
whether writes are a dry run; `tenant_vars`, the scope its variables
resolve in), what it read from secret stores (`marks`), the pipeline
calls (`calls`, a `Calls` handle or nil; `after_call`, what the step
makes of a call's reply for the script), the bodies `body_read` gives
(`bodies`, by side, a binary, a `Plugin.Body` handle or nil; `own`, the
side a call's body replaces), the files those reads keep open (`files`,
closed by `close/1`) and its log count.

# `body`

```elixir
@spec body(:request | :response, non_neg_integer(), non_neg_integer(), t()) ::
  {binary() | :none, t()}
```

Serves `body_read` (`Runtime.host/0`'s `body`): a range of a held body,
or `:none` when the step holds none or it cannot be read (logged).

# `call`

```elixir
@spec call(String.t(), binary(), t()) ::
  {{:ok, iodata()} | {:error, String.t(), String.t()}, t()}
```

Serves `op` with its JSON `arg` (`Runtime.host/0`'s `call`).

# `close`

```elixir
@spec close(t()) :: :ok
```

Closes the files the run's body reads opened.

# `held_body`

```elixir
@spec held_body(t(), :request | :response) ::
  binary() | Alternera.Plugin.Body.t() | nil
```

The body `which` as the run left it: after a `pipeline.call` it is the callee's.

# `new`

```elixir
@spec new(keyword()) :: t()
```

The host state for one run.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
