# moonrockz/moon-lander-plugins/wasm

A service plugin in a WebAssembly module, on native hosts and on js hosts under
Node.js. The module can come from any source language that has an Extism PDK. The
contract, the order and the failures are in [the module guide](../README.mbt.md). A
module from MoonBit code uses [the guest package](../guest/README.mbt.md).

A program for wasm or wasm-gc that imports this package does not build, because the
Extism engine has native and js backends only. The other packages of the module still
build on those targets.

## Load a module

The application gives the module bytes. The package reads no file and opens no
connection:

```mbt check
///|
fn heartbeat_plugin(module_ : Bytes) -> @plugins_wasm.WasmPlugin raise {
  @plugins_wasm.WasmPlugin::new(module_)
  .memory(@plugins_wasm.MemoryLimit::pages(256))
  .config({ "region": "eu-west" })
}

///|
test "a wasm plugin is a plugin" {
  let plugin : &@plugins.Plugin = heartbeat_plugin(b"")
  ignore(plugin)
}
```

Give the value to `plugin_service` of [the hosting package](../hosting/README.mbt.md),
as each other plugin. The host checks the module when it connects, in its start hook.

## The exports

The module follows the Extism plugin ABI and exports one function `() -> i32` for each
operation. The input and the output of the call are the message bytes of the contract.
The return code is the status: 0 success, 1 a rejected event, 2 a failure. With 1 and 2,
the Extism error text is the message.

| Export | Operation |
| --- | --- |
| `moonlander_describe` | `describe` |
| `moonlander_start` | `start` |
| `moonlander_handle` | `handle` |
| `moonlander_tick` | `tick` |
| `moonlander_stop` | `stop` |

## Authority

A plugin has no network, file, environment or process access. A permitted host function
runs with the authority of the host, so the application decides what it may do. Each
setting starts at
"deny" or "empty", and the application gives each authority itself:

| Authority | Default | Method |
| --- | --- | --- |
| Extism `http_request` | Denied: the call fails, and no connection opens | `http(handler)`: the handler makes or refuses each request |
| Extism `config_get` | No values | `config(values)` |
| Extism `var_get` and `var_set` | No variables | `variables(limit)` |
| Functions of the application | None | `permit(function)`, once for each function |
| WASI clock and random source | None | `clock(clock)` and `random(source)` |
| WASI files, arguments and environment | None | None |

WASI output and the Extism log functions write to the log of the plugin: standard
output at the level information, standard error at the level warning. The host writes
these entries to its logger with the service name.

## Load errors

`WasmError` names the part of the module that breaks a rule. The host checks the exports
and the imports before any guest code runs. It then instantiates the module, which runs
the start function of the module, if it has one:

| Error | Cause |
| --- | --- |
| `InvalidModule` | The engine does not accept the bytes, for example a missing feature, a memory that needs more pages at the start than the memory limit, or a start function that fails |
| `MissingExport` | No export for an operation, such as `moonlander_tick` |
| `WrongExport` | An export of an operation that is not a function `() -> i32` |
| `UnknownImport` | An import that the host does not supply, such as `acme.launch` |
| `WrongImport` | A host function import of another type |

The hosting package reports a load error as `ConnectFailed`, and the host does not
start.

## Limits

| Limit | Native host | js host on Node.js |
| --- | --- | --- |
| Memory: `memory(MemoryLimit)`, 64 MiB by default, for the memory of the module and again for the kernel memory that holds the input and output of each call | Enforced | Enforced |
| Call limit of `plugin_service`, 5 seconds by default | Enforced: the engine ends the call at the deadline | Not enforced: `RunningPlugin::enforcement` is `NotEnforced` |

A call runs in the host process and blocks the event loop until it returns. A plugin
fails at its first fault, and the host does not call it again:

| Fault | `TransportFault` |
| --- | --- |
| The call, or the start of the guest runtime at the first call, passes the call limit | `LimitReached` |
| The plugin needs more memory than its limit | `MemoryLimitReached` |
| The plugin calls a host function that the application does not permit | `Denied`, with the name of the function |
| A trap, an exit, or a host function that fails, such as an HTTP handler that refuses a request | `Trapped`, with the cause |
