Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 5x 184x 179x 5x 99x 1x 98x 2x 96x 94x 2x 5x 119x 44x 44x 75x 5x 48x 48x 90x 90x 1x 89x 89x 93x 93x 52x 93x 93x 48x 9x 4x 5x 5x 1x 4x 48x 48x 52x 93x 52x 93x 52x 52x 60x 103x 60x 60x 4x 9x 9x 4x 4x 93x 93x 4x 52x 93x 52x 48x 5x 44x 44x 44x 47x 47x 47x 44x | import { GET_INTERFACE_GUARD } from '@endo/exo';
import type { Methods } from '@endo/exo';
import {
M,
getInterfaceGuardPayload,
getMethodGuardPayload,
} from '@endo/patterns';
import type { InterfaceGuard, MethodGuard, Pattern } from '@endo/patterns';
import type { Section } from './types.ts';
export type MethodGuardPayload = {
argGuards: Pattern[];
optionalArgGuards?: Pattern[];
restArgGuard?: Pattern;
returnGuard: Pattern;
};
/**
* Extract the typed method guard map from an interface guard.
*
* @param guard - The interface guard to inspect.
* @returns A record mapping method names to their guards.
*/
export const getInterfaceMethodGuards = (
guard: InterfaceGuard,
): Record<string, MethodGuard> =>
(
getInterfaceGuardPayload(guard) as unknown as {
methodGuards: Record<string, MethodGuard>;
}
).methodGuards;
/**
* Extract the typed payload from a method guard.
*
* @param guard - The method guard to inspect.
* @returns The guard's argument and return guard components.
*/
export const getMethodPayload = (guard: MethodGuard): MethodGuardPayload =>
getMethodGuardPayload(guard) as unknown as MethodGuardPayload;
/**
* Assemble a MethodGuard from its components.
*
* The @endo/patterns builder API requires a strict chain order:
* callWhen → optional → rest → returns. All four combinations of
* optional/rest presence are handled here so callers don't repeat this logic.
*
* @param base - Result of M.callWhen(...requiredArgs).
* @param optionals - Optional positional arg guards (may be empty).
* @param restGuard - Rest arg guard, or undefined if none.
* @param returnGuard - Return value guard.
* @returns The assembled MethodGuard.
*/
export const buildMethodGuard = (
base: ReturnType<typeof M.callWhen>,
optionals: Pattern[],
restGuard: Pattern | undefined,
returnGuard: Pattern,
): MethodGuard => {
if (optionals.length > 0 && restGuard !== undefined) {
return base
.optional(...optionals)
.rest(restGuard)
.returns(returnGuard);
} else if (optionals.length > 0) {
return base.optional(...optionals).returns(returnGuard);
} else if (restGuard === undefined) {
return base.returns(returnGuard);
}
return base.rest(restGuard).returns(returnGuard);
};
/**
* Naive union of guards via M.or — no pattern canonicalization.
*
* @param guards - Guards to union.
* @returns A single guard representing the union.
*/
const unionGuard = (guards: Pattern[]): Pattern => {
if (guards.length === 1) {
const [first] = guards;
return first;
}
return M.or(...guards);
};
/**
* Compute the union of all section guards — the open set covered by the sheafified facade.
*
* For each method name across all sections, collects the arg guards at each
* position and produces a union via M.or. Sections with fewer args than
* the maximum contribute to required args; the remainder become optional.
*
* @param name - The name for the collected interface guard.
* @param sections - The sections whose guards are collected.
* @returns An interface guard covering all sections.
*/
export const collectSheafGuard = <Core extends Methods>(
name: string,
sections: Section<Core>[],
): InterfaceGuard => {
const payloadsByMethod = new Map<string, MethodGuardPayload[]>();
for (const section of sections) {
const interfaceGuard = section[GET_INTERFACE_GUARD]?.();
if (!interfaceGuard) {
continue;
}
const { methodGuards } = getInterfaceGuardPayload(
interfaceGuard,
) as unknown as { methodGuards: Record<string, MethodGuard> };
for (const [methodName, methodGuard] of Object.entries(methodGuards)) {
const payload = getMethodGuardPayload(
methodGuard,
) as unknown as MethodGuardPayload;
if (!payloadsByMethod.has(methodName)) {
payloadsByMethod.set(methodName, []);
}
const existing = payloadsByMethod.get(methodName);
existing?.push(payload);
}
}
const getGuardAt = (
payload: MethodGuardPayload,
idx: number,
): Pattern | undefined => {
if (idx < payload.argGuards.length) {
return payload.argGuards[idx];
}
const optIdx = idx - payload.argGuards.length;
if (
payload.optionalArgGuards &&
optIdx < payload.optionalArgGuards.length
) {
return payload.optionalArgGuards[optIdx];
}
return payload.restArgGuard;
};
const unionMethodGuards: Record<string, MethodGuard> = {};
for (const [methodName, payloads] of payloadsByMethod) {
const minArity = Math.min(
...payloads.map((payload) => payload.argGuards.length),
);
const maxArity = Math.max(
...payloads.map(
(payload) =>
payload.argGuards.length + (payload.optionalArgGuards?.length ?? 0),
),
);
const requiredArgGuards = [];
for (let idx = 0; idx < minArity; idx++) {
requiredArgGuards.push(
unionGuard(payloads.map((payload) => payload.argGuards[idx])),
);
}
const optionalArgGuards = [];
for (let idx = minArity; idx < maxArity; idx++) {
const guards = payloads
.map((payload) => getGuardAt(payload, idx))
.filter((guard): guard is Pattern => guard !== undefined);
optionalArgGuards.push(unionGuard(guards));
}
const restArgGuards = payloads
.map((payload) => payload.restArgGuard)
.filter((restGuard): restGuard is Pattern => restGuard !== undefined);
const unionRestArgGuard =
restArgGuards.length > 0 ? unionGuard(restArgGuards) : undefined;
const returnGuard = unionGuard(
payloads.map((payload) => payload.returnGuard),
);
unionMethodGuards[methodName] = buildMethodGuard(
M.callWhen(...requiredArgGuards),
optionalArgGuards,
unionRestArgGuard,
returnGuard,
);
}
return M.interface(name, unionMethodGuards);
};
/**
* Upgrade all method guards in an interface guard to M.callWhen for async dispatch.
*
* @param resolvedGuard - The interface guard whose methods should be upgraded.
* @returns A record of async method guards keyed by method name.
*/
export const asyncifyMethodGuards = (
resolvedGuard: InterfaceGuard,
): Record<string, MethodGuard> => {
const resolvedMethodGuards = getInterfaceMethodGuards(resolvedGuard);
const asyncMethodGuards: Record<string, MethodGuard> = {};
for (const [methodName, methodGuard] of Object.entries(
resolvedMethodGuards,
)) {
const { argGuards, optionalArgGuards, restArgGuard, returnGuard } =
getMethodPayload(methodGuard);
const optionals = optionalArgGuards ?? [];
asyncMethodGuards[methodName] = buildMethodGuard(
M.callWhen(...argGuards),
optionals,
restArgGuard,
returnGuard,
);
}
return asyncMethodGuards;
};
|