kernel / core / itx / src / context / lent-names.tsmain at 949894f
// lent-names.ts — A LENT NAME NEVER TAKES A NAME OF THE PLATFORM'S. A lend is a rule of the
// context's table that points at a live stub, and a rule is read before a built-in root. So a stub
// lent as `itx.secrets` would get every call of `itx.secrets.set(…)` made after it, with the clear
// value, and one lent as `itx.config.x` would stand in for the mind's own code. The one who lends
// is outside the mind: a bridge with a key of its own, or a process a member started.
import {
canonicalItxExpressionPrefix,
type ItxExpressionInput,
itxExpressionStepName,
normalizedItxExpression,
parseItxExpressionPrefix,
} from "iterate/expression";
/** Whether a resolved call is the stub lent as `match` and nothing past it:
* `itx.builtins.rpcStubs.get(match)`, which is how the table names a lent stub. A call that goes
* on from that stub (`….get(match).inner`) came through another rule, and is not this lend. */
export function isLendOf(resolved: ItxExpressionInput, match: ItxExpressionInput): boolean {
const [scope, builtins, stubs, get, ...further] = normalizedItxExpression(resolved);
return (
further.length === 0 &&
scope === "itx" &&
builtins === "builtins" &&
stubs === "rpcStubs" &&
Array.isArray(get) &&
get[0] === "get" &&
get[1] === canonicalItxExpressionPrefix(match)
);
}
/** WHETHER A NAME IS FREE FOR A KEY'S LEND, BY A CONTEXT'S ROWS: no row is at the name, above it or
* under it, but the stub lent as this very name before. A row with no target is a row: the mind
* masked the name, and a lend under a mask would answer where the mind said that nothing does. A
* row under the name would be left as an island in what the key lends. The bare row (`itx`
* alone) is a context's link or its jail and no name.
*
* A lend of this very name is free in this table's terms, whoever lent it. Who may take a live
* lend is decided at the context's attach (rpc-stubs.ts `appendAtAttach`), in one turn with the
* row's write: a read of the table here and the lend after it are two steps. */
export function nameIsFree(
own: ItxExpressionInput,
rows: readonly { match: ItxExpressionInput; target: ItxExpressionInput | null }[],
): boolean {
const namesOf = (name: ItxExpressionInput) =>
parseItxExpressionPrefix(name).map((step) => itxExpressionStepName(step));
const mine = namesOf(own);
return rows.every(({ match, target }) => {
const theirs = namesOf(match);
if (theirs.length < 2) return true;
const shared = Math.min(mine.length, theirs.length);
if (!mine.slice(0, shared).every((name, index) => name === theirs[index])) return true;
return theirs.length === mine.length && !!target && isLendOf(target, own);
});
}
/** Why a live stub is not lent at `match`, or null when it may be. `roots` are the names that are
* the platform's at the mind's root (`implicitRootsAt("/")`): the kernel's and the host's. */
export function lendRefusal(match: ItxExpressionInput, roots: ReadonlySet<string>): string | null {
const steps = parseItxExpressionPrefix(match);
const name = itxExpressionStepName(steps[1]);
if (steps[0] !== "itx" || !name) return "a lend names what it lends, as itx.<name>";
// `builtins` is how the table itself names a root, and `config` is the mind's own code
if (name === "builtins" || name === "config" || roots.has(name))
return `itx.${name} is the platform's own name, and a lent stub never takes it: lend under a name of your own`;
return null;
}