# `Alternera.PluginWasmFramework.Runtime.Port.Compiler`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.1/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/runtime/port/compiler.ex#L1)

Compiles a module for `Runtime.Port` in a host process of its own
(`alternera_wasm_host compile IN OUT`), never in a serving one: a
compile then has a time limit (the process is killed past
`compile_timeout_ms/0`), a fault of the compiler on a hostile module
costs that process, and no serving process stalls behind a compile.

The process compiles the module, checks the interface's exports, runs
`alternera_abi` in a throwaway instance from the same pooling allocator
the serving processes use (so a module that does not fit their slots is
refused here) and writes the compiled form to the cache.

## The cache

A directory of compiled modules, `<digest>-<fingerprint>-<n>.cwasm`
(`Binary.fingerprint/0`: the binary and its engine settings; `n` makes
each compile's file its own, so discarding one never removes a file
another prepare of the same bytes uses). A serving
process loads a compiled file as native code without checking it, so
the directory is made fresh when the node first needs it, by a `mkdir`
that fails on any existing name (a symlink planted at a guessed name
included), then checked without following links to be a directory of
mode 0700. Nothing an earlier boot left is read: a boot compiles its
modules again. This user's directories of earlier boots whose node is
gone (its OS process no longer runs, or its pid is this node's own) are
removed then; a link is never followed. It goes under `:cache_dir` in the framework's env, by
default the system's temporary directory; a host whose temporary
directory is mounted `noexec` needs another, since the processes map
the files as code.

# `compiled`

```elixir
@type compiled() :: %{path: Path.t(), initialize?: boolean()}
```

A compiled module: where its file is and what calls need of it.

# `compile`

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

Compiles `wasm` (`digest` is its lower-case hex SHA-256) into the cache.
The errors are `Runtime.Wasmex.prepare/1`'s texts, so what the upload
shows does not depend on the runtime; the host's own words are logged.

# `compile_timeout_ms`

```elixir
@spec compile_timeout_ms() :: pos_integer()
```

How long a compile may take before its process is killed.

# `dir`

```elixir
@spec dir() :: {:ok, Path.t()} | {:error, String.t()}
```

The node's cache directory, made on first use.

---

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