|
| 1 | +/** |
| 2 | + * The binding environment one eval block sees (spec §4.3). |
| 3 | + * |
| 4 | + * Blocks share one bindings record — that is what lets a later block read what |
| 5 | + * an earlier one exported. But `renderChildren`, `render` and `useContent` |
| 6 | + * project content, and a projection settles its errors under the policy of the |
| 7 | + * block that started it, which differs per block. A `persist eval` block runs |
| 8 | + * on the invocation's eval-scope loop task, created long before that policy |
| 9 | + * existed, so the policy cannot come from the surrounding context. |
| 10 | + * |
| 11 | + * Each evaluation therefore gets its own facade over the shared record: the |
| 12 | + * three projecting bindings are bound to that evaluation's policy, everything |
| 13 | + * else reads and writes straight through. Nothing is swapped and nothing is |
| 14 | + * restored, so concurrent evaluations cannot race and work that outlives its |
| 15 | + * block keeps calling the closures it was given. |
| 16 | + */ |
| 17 | + |
| 18 | +import type { ErrorPolicy } from "./errors.ts"; |
| 19 | + |
| 20 | +/** |
| 21 | + * Bindings the facade re-binds per evaluation. The shared record holds the |
| 22 | + * invocation's own versions, which carry no policy. |
| 23 | + */ |
| 24 | +const PROJECTING = ["renderChildren", "render", "useContent"]; |
| 25 | + |
| 26 | +/** A projection binding as it is injected into the shared record. */ |
| 27 | +type Projector = (argument?: unknown, policy?: ErrorPolicy) => unknown; |
| 28 | + |
| 29 | +function isProjector(value: unknown): value is Projector { |
| 30 | + return typeof value === "function"; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Wrap `values` for one evaluation, binding the projecting operations to |
| 35 | + * `policy`. Reads of anything else, and every write, delete and enumeration, |
| 36 | + * reach the shared record — `serializeExports` and the transform's |
| 37 | + * `Object.keys` see exactly what they saw before. |
| 38 | + */ |
| 39 | +export function evaluationEnv( |
| 40 | + values: Record<string, unknown>, |
| 41 | + policy: ErrorPolicy, |
| 42 | +): Record<string, unknown> { |
| 43 | + const bound = new Map<string, unknown>(); |
| 44 | + |
| 45 | + return new Proxy(values, { |
| 46 | + get(target, key) { |
| 47 | + if (typeof key !== "string" || !PROJECTING.includes(key)) { |
| 48 | + return Reflect.get(target, key); |
| 49 | + } |
| 50 | + const existing = bound.get(key); |
| 51 | + if (existing) { |
| 52 | + return existing; |
| 53 | + } |
| 54 | + const projector = Reflect.get(target, key); |
| 55 | + if (!isProjector(projector)) { |
| 56 | + return projector; |
| 57 | + } |
| 58 | + const wrapper = (argument?: unknown) => projector(argument, policy); |
| 59 | + bound.set(key, wrapper); |
| 60 | + return wrapper; |
| 61 | + }, |
| 62 | + set(target, key, value) { |
| 63 | + return Reflect.set(target, key, value); |
| 64 | + }, |
| 65 | + deleteProperty(target, key) { |
| 66 | + return Reflect.deleteProperty(target, key); |
| 67 | + }, |
| 68 | + has(target, key) { |
| 69 | + return Reflect.has(target, key); |
| 70 | + }, |
| 71 | + ownKeys(target) { |
| 72 | + return Reflect.ownKeys(target); |
| 73 | + }, |
| 74 | + getOwnPropertyDescriptor(target, key) { |
| 75 | + return Reflect.getOwnPropertyDescriptor(target, key); |
| 76 | + }, |
| 77 | + }); |
| 78 | +} |
0 commit comments