For a person or a model about to write an app; a model gets the same text from oad guide.

Contents

Code modules#

A code module is a small JavaScript function that the app carries in its definition and runs in a sandbox. It is the last resort: most apps need none.

What a module is#

A pure function from its input to its output, with no network, storage, clock, randomness, timers, host objects or capabilities: a date made without arguments fails ("this sandbox has no clock"), and so do Date.now and Math.random. Pass in what it needs: the time as env.now or env.today, a random number from random() in a let step before the call. Dates in a module are UTC. Its result is plain JSON of its output type.

When to write one, and when not#

Try expressions first: a host explains them to the person in plain words, the checks follow every name in them, and each is bounded by the cost budget. The checks cannot see into a module.

Write a module when:

  • the same logic would be written in several places with different inputs: expressions have no functions of your own;
  • the logic is a search or a parse whose steps depend on what it finds, such as the moves of a computer player, a scorer that looks ahead, or the fewest stamps that make a sum;
  • an expression comes near its cost budget and cannot be split.

Do not write one for arithmetic, text or a list that an expression says in a line, or for anything that needs the outside world: that is a capability (see capabilities).

Saying a thing once without code#

A derived state entry is the way to write logic once when it takes no input of its own: every place reads state.said. No parameters, a derived entry; parameters, a module.

Writing one#

The postage example finds the fewest stamps that pay a postage exactly:

JSON

{
  "description": "Finds the fewest stamps whose values add up to the postage exactly, largest first, trying every mix of the values given. It fails when no mix makes the postage.",
  "runtime": "js",
  "input": {
    "postage": { "type": "integer", "description": "The postage to pay, in cents." },
    "values": { "type": "integer[]", "description": "The values of the stamps to choose from, in cents." }
  },
  "output": "integer[]",
  "source": "export default function ({ postage, values }) { const best = [[]]; for (let a = 1; a <= postage; a++) { for (const v of values) { const rest = best[a - v]; if (v > 0 && v <= a && rest !== undefined && (best[a] === undefined || rest.length + 1 < best[a].length)) best[a] = [...rest, v]; } } if (best[postage] === undefined) throw new Error(`no mix of these stamps makes ${postage}`); return best[postage].sort((x, y) => y - x); }",
  "limits": { "timeMs": 50, "memoryMb": 16 }
}
  • runtime is "js": source is a module whose default export is the function, called with the input as one object.
  • input is a field map, as an action's, and output a type. Both are checked as values cross into and out of the sandbox.
  • Write the text in source. The hosts of these tools do not run a module given by address in module: its step fails with code.unsupported.

Every module needs a grant request that names it (code.noGrant without one). It may set only reason, rate, duration and required:

JSON

{ "stampMath": { "code": "fewestStamps", "reason": "Works out which of your stamps to use. It runs inside the app and reaches nothing else." } }

Calling one#

A code step runs it, with with for the input and as for the output:

JSON

{ "code": "fewestStamps", "with": { "postage": "=state.letterPostage", "values": "=state.stamps" }, "as": "picked", "orElse": "=error.code == 'code.failed' ? [] : null" }

The steps of an action, of a rule's do and of a flow transition's do can call a module. A view's handler cannot (view.handlerStep): run an action from it. Nor can an expression: a derive or a prop that names a module is reported (expression.unknownFunction) and fails when it runs. So an action runs the module and stores its result, and the view shows what is stored. In the example, stampLetter and stampParcel both call fewestStamps, each with its own postage, and both store the result in state.last, which the screen shows through state.said.

What a person is told#

Nobody is asked: a module reaches nothing, so its request is given without asking, and the person may turn it off in the app's permissions. A person reads the module's description (oad explain shows it with the input, the limits and the actions that run it) and the request's reason, never the source. Say truthfully what the module does, and when it fails.

Limits and failures#

A module may run 100 ms and use 32 MB unless limits asks for other values. A host may cap what it asks: the hosts of these tools give at most 10 seconds and 256 MB. A failure is the step's, with error.code:

Code When
code.limit It ran longer than its time, or needed more than its memory.
code.failed It threw (error.message holds its message), returned nothing or something that is not plain data, or returned a value that is not of its output type.
code.unsupported The host cannot run it. Where a chat cannot reach the tools, it shows a preview of the app, which refuses every module, says so in a line and offers "Open in browser", where the module runs.

Handle it on the step: orElse is the value of the as name when the step fails, and onError holds steps that run first, with error to read. Without either, the action fails and nothing it wrote is kept. The example tells the two apart: [] when no mix of stamps makes the postage, null when the module could not run, and state.said words each.

Next#