@telorun/templating 0.117.0 → 0.119.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cel/analyze.d.ts +6 -0
- package/dist/cel/analyze.d.ts.map +1 -1
- package/dist/cel/analyze.js +21 -0
- package/dist/cel/compile.d.ts +18 -3
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +40 -15
- package/dist/engine.d.ts +4 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.js +1 -1
- package/dist/engines/hole-analysis.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/package.json +3 -3
- package/src/cel/analyze.ts +23 -0
- package/src/cel/compile.ts +56 -13
- package/src/engine.ts +4 -0
- package/src/engines/cel.ts +1 -1
- package/src/engines/hole-analysis.ts +1 -1
- package/src/index.ts +7 -1
package/dist/cel/analyze.d.ts
CHANGED
|
@@ -27,6 +27,10 @@ export declare const INDEX_SEGMENT = "[*]";
|
|
|
27
27
|
/** Chain segment for "each element of the collection reached so far": what a
|
|
28
28
|
* comprehension's iteration variable stands for. */
|
|
29
29
|
export declare const EACH_SEGMENT = "[]";
|
|
30
|
+
/** Chain segment for "whether the member exists": the last segment of a chain a
|
|
31
|
+
* presence test (`has(a.b)`) reads, which depends on the KEYS of what the chain
|
|
32
|
+
* before it reaches and on none of its values. */
|
|
33
|
+
export declare const PRESENCE_SEGMENT = "?";
|
|
30
34
|
/**
|
|
31
35
|
* What an expression READS of the values its roots name, as member chains —
|
|
32
36
|
* {@link extractAccessChains} with a comprehension's iteration variable followed
|
|
@@ -38,6 +42,8 @@ export declare const EACH_SEGMENT = "[]";
|
|
|
38
42
|
* its elements: the chain ends in {@link EACH_SEGMENT}.
|
|
39
43
|
* - A bare use of the variable reads the element whole, so the range is read
|
|
40
44
|
* whole, as it is by any other use of the collection.
|
|
45
|
+
* - A presence test reads only whether the member exists: `has(a.b)` reads
|
|
46
|
+
* `a.?` ({@link PRESENCE_SEGMENT}), never the value of `a.b`.
|
|
41
47
|
*
|
|
42
48
|
* A variable whose range is not a plain chain (a call's result, another macro's
|
|
43
49
|
* output) names nothing a root holds, and a read through it contributes nothing.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analyze.d.ts","sourceRoot":"","sources":["../../src/cel/analyze.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,OAAO,EACb,MAAM,cAAc,CAAC;AAItB;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,EAAE,EAAE,CAI7D;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAS5F;AA8FD;;+DAE+D;AAC/D,eAAO,MAAM,aAAa,QAAQ,CAAC;AA6BnC;qDACqD;AACrD,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC
|
|
1
|
+
{"version":3,"file":"analyze.d.ts","sourceRoot":"","sources":["../../src/cel/analyze.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,OAAO,EACb,MAAM,cAAc,CAAC;AAItB;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,EAAE,EAAE,CAI7D;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAS5F;AA8FD;;+DAE+D;AAC/D,eAAO,MAAM,aAAa,QAAQ,CAAC;AA6BnC;qDACqD;AACrD,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC;;mDAEmD;AACnD,eAAO,MAAM,gBAAgB,MAAM,CAAC;AAEpC;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,EAAE,EAAE,CAI3D;AA4ED;+DAC+D;AAC/D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;CACrC;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,OAAO,GAAG,gBAAgB,EAAE,CAI3E;AA4BD,UAAU,aAAa;IACrB,2EAA2E;IAC3E,IAAI,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,MAAM,EAAE,MAAM,CAAC;CAChB;AA0BD;;sBAEsB;AACtB,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjC,MAAM,GAAG,IAAI,CAIf;AA0DD;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,OAAO,EACb,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjC,aAAa,EAAE,CAIjB;AA2ED;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,MAAM,EAAE,EACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,MAAM,GAAG,IAAI,CA2Cf"}
|
package/dist/cel/analyze.js
CHANGED
|
@@ -152,6 +152,10 @@ function extractChain(node, boundVars) {
|
|
|
152
152
|
/** Chain segment for "each element of the collection reached so far": what a
|
|
153
153
|
* comprehension's iteration variable stands for. */
|
|
154
154
|
export const EACH_SEGMENT = "[]";
|
|
155
|
+
/** Chain segment for "whether the member exists": the last segment of a chain a
|
|
156
|
+
* presence test (`has(a.b)`) reads, which depends on the KEYS of what the chain
|
|
157
|
+
* before it reaches and on none of its values. */
|
|
158
|
+
export const PRESENCE_SEGMENT = "?";
|
|
155
159
|
/**
|
|
156
160
|
* What an expression READS of the values its roots name, as member chains —
|
|
157
161
|
* {@link extractAccessChains} with a comprehension's iteration variable followed
|
|
@@ -163,6 +167,8 @@ export const EACH_SEGMENT = "[]";
|
|
|
163
167
|
* its elements: the chain ends in {@link EACH_SEGMENT}.
|
|
164
168
|
* - A bare use of the variable reads the element whole, so the range is read
|
|
165
169
|
* whole, as it is by any other use of the collection.
|
|
170
|
+
* - A presence test reads only whether the member exists: `has(a.b)` reads
|
|
171
|
+
* `a.?` ({@link PRESENCE_SEGMENT}), never the value of `a.b`.
|
|
166
172
|
*
|
|
167
173
|
* A variable whose range is not a plain chain (a call's result, another macro's
|
|
168
174
|
* output) names nothing a root holds, and a read through it contributes nothing.
|
|
@@ -203,6 +209,14 @@ function visitReads(node, chains, bound) {
|
|
|
203
209
|
chains.push(chain[chain.length - 1] === EACH_SEGMENT ? chain.slice(0, -1) : chain);
|
|
204
210
|
return;
|
|
205
211
|
}
|
|
212
|
+
const presence = presenceOperand(node);
|
|
213
|
+
if (presence) {
|
|
214
|
+
const owner = readChainOf(presence, bound);
|
|
215
|
+
if (owner !== null) {
|
|
216
|
+
chains.push([...owner, PRESENCE_SEGMENT]);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
206
220
|
const moduleCall = moduleCallOf(node);
|
|
207
221
|
if (moduleCall) {
|
|
208
222
|
for (const arg of moduleCall.args)
|
|
@@ -230,6 +244,13 @@ function visitReads(node, chains, bound) {
|
|
|
230
244
|
for (const child of childNodes(node))
|
|
231
245
|
visitReads(child, chains, bound);
|
|
232
246
|
}
|
|
247
|
+
/** What a presence test `has(<operand>.<member>)` asks the keys of. */
|
|
248
|
+
function presenceOperand(node) {
|
|
249
|
+
if (node.kind !== "call" || node.name !== "has" || node.args.length !== 1)
|
|
250
|
+
return undefined;
|
|
251
|
+
const arg = node.args[0];
|
|
252
|
+
return arg.kind === "select" && !arg.optional ? arg.operand : undefined;
|
|
253
|
+
}
|
|
233
254
|
/**
|
|
234
255
|
* Every member access whose base is a module call, longest first — the chains
|
|
235
256
|
* {@link extractAccessChains} deliberately skips, since their root is a value
|
package/dist/cel/compile.d.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* silently answering a different question. What is left here is reading the result.
|
|
12
12
|
*/
|
|
13
13
|
import { type CompiledValue } from "@telorun/sdk";
|
|
14
|
-
import { type CelEnvironment, type CelExpression, type NamespaceImplementation } from "@telorun/cel";
|
|
14
|
+
import { type CelEnvironment, type CelExpression, type CelProgram, type NamespaceImplementation } from "@telorun/cel";
|
|
15
15
|
import { type ModuleCallDispatch } from "./module-call.js";
|
|
16
16
|
/**
|
|
17
17
|
* Whether an expression's text, written twice in one value, means one value both times: it
|
|
@@ -23,6 +23,21 @@ import { type ModuleCallDispatch } from "./module-call.js";
|
|
|
23
23
|
* unreadable thing is one ranged diagnostic and the tree keeps the longest prefix.
|
|
24
24
|
*/
|
|
25
25
|
export declare function repeatableExpression(expr: string, environment: CelEnvironment): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* One compiled expression whose program a host may replace with an equivalent one compiled
|
|
28
|
+
* against the same environment — the kernel swaps in the program from an emitted module.
|
|
29
|
+
*/
|
|
30
|
+
export interface CelProgramSite {
|
|
31
|
+
readonly source: string;
|
|
32
|
+
readonly environment: CelEnvironment;
|
|
33
|
+
use(program: CelProgram): void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The environment an expression at a site with these module names compiles against: the
|
|
37
|
+
* environment itself when it already declares all of them, otherwise a clone carrying them. A host compiling many expressions of one site builds it once with this, so
|
|
38
|
+
* they all share one environment.
|
|
39
|
+
*/
|
|
40
|
+
export declare function siteEnvironment(environment: CelEnvironment, moduleNames?: ReadonlySet<string>): CelEnvironment;
|
|
26
41
|
/**
|
|
27
42
|
* Compile one CEL expression into a `CompiledValue`. The `!cel` engine compiles its whole
|
|
28
43
|
* scalar with it, and the tags with holes compile each hole.
|
|
@@ -32,9 +47,9 @@ export declare function repeatableExpression(expr: string, environment: CelEnvir
|
|
|
32
47
|
* references below, the analyzer's passes, the kernel's dependency walk — sees the resolved
|
|
33
48
|
* shape rather than re-deriving it from the source text.
|
|
34
49
|
*/
|
|
35
|
-
export declare function compileExpression(expr: string, environment: CelEnvironment, moduleNames?: ReadonlySet<string
|
|
50
|
+
export declare function compileExpression(expr: string, environment: CelEnvironment, moduleNames?: ReadonlySet<string>, programSites?: CelProgramSite[]): CompiledValue;
|
|
36
51
|
/** A `CompiledValue` over an expression already read against its site's names. */
|
|
37
|
-
export declare function compiledValueOf(expr: string, parsed: CelExpression, environment: CelEnvironment): CompiledValue;
|
|
52
|
+
export declare function compiledValueOf(expr: string, parsed: CelExpression, environment: CelEnvironment, programSites?: CelProgramSite[]): CompiledValue;
|
|
38
53
|
/**
|
|
39
54
|
* A scope's module functions as the engine's own per-evaluation dispatch — **the one
|
|
40
55
|
* adapter**, so every host that binds a table converts its arguments the same way.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/cel/compile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAe,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAKL,KAAK,cAAc,EACnB,KAAK,aAAa,EAElB,KAAK,uBAAuB,EAC7B,MAAM,cAAc,CAAC;AACtB,OAAO,EAGL,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAwC1B;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,cAAc,GAAG,OAAO,CAIvF;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,cAAc,EAC3B,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sour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,GAAG,SAAS,CAAC,GAAG,SAAS,CAMxF"}
|
package/dist/cel/compile.js
CHANGED
|
@@ -62,31 +62,54 @@ export function repeatableExpression(expr, environment) {
|
|
|
62
62
|
return !callsNonDeterministic(parsed.root) && !callsOnBareReceiver(parsed.root);
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* `moduleNames` are the declaring module's own names; a call whose receiver is one of them
|
|
69
|
-
* is resolved to a `qcall` as the expression is READ, so every later reader — the root
|
|
70
|
-
* references below, the analyzer's passes, the kernel's dependency walk — sees the resolved
|
|
71
|
-
* shape rather than re-deriving it from the source text.
|
|
65
|
+
* The environment an expression at a site with these module names compiles against: the
|
|
66
|
+
* environment itself when it already declares all of them, otherwise a clone carrying them. A host compiling many expressions of one site builds it once with this, so
|
|
67
|
+
* they all share one environment.
|
|
72
68
|
*/
|
|
73
|
-
export function
|
|
69
|
+
export function siteEnvironment(environment, moduleNames) {
|
|
74
70
|
// Only the names CEL can read as a namespace: a set holding `my-module` is refused WHOLE
|
|
75
71
|
// at registration, and a name that cannot be one resolves no call anyway.
|
|
76
72
|
const namespaces = moduleNames === undefined ? NO_NAMESPACES : celNamespaceNames(moduleNames);
|
|
77
|
-
if (namespaces.size === 0)
|
|
78
|
-
return
|
|
79
|
-
|
|
73
|
+
if (namespaces.size === 0)
|
|
74
|
+
return environment;
|
|
75
|
+
// An environment already declaring every one of the names resolves the tree as a clone
|
|
76
|
+
// would, since the clone inherits its declarations.
|
|
77
|
+
const declared = new Set(environment.namespaces());
|
|
78
|
+
if ([...namespaces].every((name) => declared.has(name)))
|
|
79
|
+
return environment;
|
|
80
80
|
// A clone carrying the site's names: the tree is resolved under them, and the environment
|
|
81
81
|
// that compiles it must declare the same set or it refuses the tree.
|
|
82
82
|
const site = environment.clone();
|
|
83
83
|
for (const name of namespaces)
|
|
84
84
|
site.registerNamespace(name);
|
|
85
|
-
return
|
|
85
|
+
return site;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Compile one CEL expression into a `CompiledValue`. The `!cel` engine compiles its whole
|
|
89
|
+
* scalar with it, and the tags with holes compile each hole.
|
|
90
|
+
*
|
|
91
|
+
* `moduleNames` are the declaring module's own names; a call whose receiver is one of them
|
|
92
|
+
* is resolved to a `qcall` as the expression is READ, so every later reader — the root
|
|
93
|
+
* references below, the analyzer's passes, the kernel's dependency walk — sees the resolved
|
|
94
|
+
* shape rather than re-deriving it from the source text.
|
|
95
|
+
*/
|
|
96
|
+
export function compileExpression(expr, environment, moduleNames, programSites) {
|
|
97
|
+
const site = siteEnvironment(environment, moduleNames);
|
|
98
|
+
return compiledValueOf(expr, site.parse(expr), site, programSites);
|
|
86
99
|
}
|
|
87
100
|
/** A `CompiledValue` over an expression already read against its site's names. */
|
|
88
|
-
export function compiledValueOf(expr, parsed, environment) {
|
|
89
|
-
|
|
101
|
+
export function compiledValueOf(expr, parsed, environment, programSites) {
|
|
102
|
+
// Where a host may swap in an emitted program, the closure is built only if nothing was
|
|
103
|
+
// swapped in by the first evaluation. An unreadable expression is still compiled — and so
|
|
104
|
+
// refused — here, where it is written.
|
|
105
|
+
let program = programSites && parsed.diagnostics.length === 0 ? undefined : environment.compile(parsed);
|
|
106
|
+
programSites?.push({
|
|
107
|
+
source: expr,
|
|
108
|
+
environment,
|
|
109
|
+
use: (replacement) => {
|
|
110
|
+
program = replacement;
|
|
111
|
+
},
|
|
112
|
+
});
|
|
90
113
|
// As written — `CompiledValue.calls` is the qualified spellings, not their shapes.
|
|
91
114
|
const calls = qualifiedCalls(parsed.root).map((call) => call.qualifiedName);
|
|
92
115
|
return {
|
|
@@ -99,7 +122,9 @@ export function compiledValueOf(expr, parsed, environment) {
|
|
|
99
122
|
// the owning scope bound on the activation. Evaluating without it reaches no function at
|
|
100
123
|
// all: the engine would answer its own `unbound_function` for every qualified call,
|
|
101
124
|
// whatever the kernel bound, so the whole dispatch is silently dead.
|
|
102
|
-
call: (ctx) => hostValueOf(program.evaluate(ctx, {
|
|
125
|
+
call: (ctx) => hostValueOf((program ??= environment.compile(parsed)).evaluate(ctx, {
|
|
126
|
+
namespaceFunction: dispatchFrom(ctx),
|
|
127
|
+
})),
|
|
103
128
|
};
|
|
104
129
|
}
|
|
105
130
|
/**
|
package/dist/engine.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CelEnvironment } from "@telorun/cel";
|
|
2
2
|
import type { CompiledValue } from "@telorun/sdk";
|
|
3
|
+
import type { CelProgramSite } from "./cel/compile.js";
|
|
3
4
|
import type { ModuleCallFlags } from "./cel/diagnose.js";
|
|
4
5
|
import type { ModuleCallTypeResolver } from "./cel/module-call.js";
|
|
5
6
|
/** Compile-time environment passed to `engine.compile`. Engines that need to
|
|
@@ -19,6 +20,9 @@ export interface CompileEnv {
|
|
|
19
20
|
* before module functions existed.
|
|
20
21
|
*/
|
|
21
22
|
readonly moduleNames?: ReadonlySet<string>;
|
|
23
|
+
/** Receives every CEL expression compiled here, so a host can replace each
|
|
24
|
+
* program with one from an emitted module. */
|
|
25
|
+
readonly programSites?: CelProgramSite[];
|
|
22
26
|
}
|
|
23
27
|
/** Analyze-time environment passed to `engine.analyze`. The walker resolves
|
|
24
28
|
* the path-specific effective context (kernel globals merged in, x-telo-context
|
package/dist/engine.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAEnE;;wEAEwE;AACxE,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,cA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EAAE,UAAU,GAAG,aAAa,CAAC;IAExD;;;;;;;;;;;;uBAYmB;IACnB,UAAU,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,eAAe,EAAE,CAAC;IAExD;;;;;;;;;;;;;;;;;2CAiBuC;IACvC,YAAY,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEzC;;;;;;qDAMiD;IACjD,iBAAiB,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,gBAAgB,EAAE,CAAC;IAEhE;;;;;;;;;iEAS6D;IAC7D,UAAU,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC;CAC9D"}
|
package/dist/engines/cel.js
CHANGED
|
@@ -243,7 +243,7 @@ export const celEngine = {
|
|
|
243
243
|
name: "cel",
|
|
244
244
|
language: "cel",
|
|
245
245
|
compile(source, env) {
|
|
246
|
-
return compileExpression(source, env.celEnv, env.moduleNames);
|
|
246
|
+
return compileExpression(source, env.celEnv, env.moduleNames, env.programSites);
|
|
247
247
|
},
|
|
248
248
|
analyze(source, env) {
|
|
249
249
|
return analyzeCelExpression(source, env);
|
|
@@ -12,7 +12,7 @@ export function requireHoles(tag, source) {
|
|
|
12
12
|
/** Every hole compiled as its own CEL expression, with the roots, module calls
|
|
13
13
|
* and volatility of all of them carried together. */
|
|
14
14
|
export function compileHoles(holes, env) {
|
|
15
|
-
const compiled = holes.map((h) => compileExpression(h.expr, env.celEnv, env.moduleNames));
|
|
15
|
+
const compiled = holes.map((h) => compileExpression(h.expr, env.celEnv, env.moduleNames, env.programSites));
|
|
16
16
|
const refs = new Set();
|
|
17
17
|
const calls = new Set();
|
|
18
18
|
let volatile = false;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { buildCelEnvironment, buildCelLanguageEnvironment, celBuiltinFunctions, celFunctionCatalog, type CelHandlers, } from "./cel/environment.js";
|
|
2
|
-
export { compileExpression, namespaceDispatchOf } from "./cel/compile.js";
|
|
2
|
+
export { compileExpression, namespaceDispatchOf, siteEnvironment, type CelProgramSite, } from "./cel/compile.js";
|
|
3
3
|
export { interpolationShape, literalFragments, readInterpolationHoles, type HoleReading, type InterpolationHole, type InterpolationShape, } from "./cel/interpolation-holes.js";
|
|
4
|
-
export { EACH_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
4
|
+
export { EACH_SEGMENT, PRESENCE_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
5
5
|
export { auditCalls, explainUnresolved, type CallAudit, type ModuleCallFlags, } from "./cel/diagnose.js";
|
|
6
6
|
export { celNamespaceNames, MODULE_CALL_DISPATCH_KEY, moduleCallNames, moduleCallOf, type ModuleCallDispatch, type ModuleCallTypeResolver, } from "./cel/module-call.js";
|
|
7
7
|
export { walkCelExpressions } from "./cel/walk.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,EAClB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,EAClB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,EACf,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB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EAChB,eAAe,EACf,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,UAAU,EACV,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,YAAY,EACZ,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,9 +3,9 @@ export { buildCelEnvironment, buildCelLanguageEnvironment, celBuiltinFunctions,
|
|
|
3
3
|
// become the engine's namespace seam — exported because the analyzer evaluates
|
|
4
4
|
// rule conditions and function bodies through the same table, and a second
|
|
5
5
|
// adapter is what let it convert arguments differently from the kernel.
|
|
6
|
-
export { compileExpression, namespaceDispatchOf } from "./cel/compile.js";
|
|
6
|
+
export { compileExpression, namespaceDispatchOf, siteEnvironment, } from "./cel/compile.js";
|
|
7
7
|
export { interpolationShape, literalFragments, readInterpolationHoles, } from "./cel/interpolation-holes.js";
|
|
8
|
-
export { EACH_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
8
|
+
export { EACH_SEGMENT, PRESENCE_SEGMENT, extractAccessChains, extractReadChains, findNullableAccessIssues, INDEX_SEGMENT, validateChainAgainstSchema, } from "./cel/analyze.js";
|
|
9
9
|
export { auditCalls, explainUnresolved, } from "./cel/diagnose.js";
|
|
10
10
|
// The rewrite, its reader, and the activation key the kernel binds a scope's
|
|
11
11
|
// dispatch table under. The unbound message and the receiver and bound-name
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/templating",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.119.0",
|
|
4
4
|
"description": "Telo Templating - Engine registry and shared CEL core for Telo manifests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telo",
|
|
@@ -39,14 +39,14 @@
|
|
|
39
39
|
"re2js": "2.8.3",
|
|
40
40
|
"uuid": "14.0.1",
|
|
41
41
|
"yaml": "^2.8.3",
|
|
42
|
-
"@telorun/cel": "0.
|
|
42
|
+
"@telorun/cel": "0.119.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/d3-format": "^3.0.4",
|
|
46
46
|
"@types/node": "^20.0.0",
|
|
47
47
|
"typescript": "^5.0.0",
|
|
48
48
|
"vitest": "^2.1.8",
|
|
49
|
-
"@telorun/sdk": "0.
|
|
49
|
+
"@telorun/sdk": "0.119.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@telorun/sdk": "*"
|
package/src/cel/analyze.ts
CHANGED
|
@@ -171,6 +171,11 @@ function extractChain(node: CelNode, boundVars: ReadonlySet<string>): string[] |
|
|
|
171
171
|
* comprehension's iteration variable stands for. */
|
|
172
172
|
export const EACH_SEGMENT = "[]";
|
|
173
173
|
|
|
174
|
+
/** Chain segment for "whether the member exists": the last segment of a chain a
|
|
175
|
+
* presence test (`has(a.b)`) reads, which depends on the KEYS of what the chain
|
|
176
|
+
* before it reaches and on none of its values. */
|
|
177
|
+
export const PRESENCE_SEGMENT = "?";
|
|
178
|
+
|
|
174
179
|
/**
|
|
175
180
|
* What an expression READS of the values its roots name, as member chains —
|
|
176
181
|
* {@link extractAccessChains} with a comprehension's iteration variable followed
|
|
@@ -182,6 +187,8 @@ export const EACH_SEGMENT = "[]";
|
|
|
182
187
|
* its elements: the chain ends in {@link EACH_SEGMENT}.
|
|
183
188
|
* - A bare use of the variable reads the element whole, so the range is read
|
|
184
189
|
* whole, as it is by any other use of the collection.
|
|
190
|
+
* - A presence test reads only whether the member exists: `has(a.b)` reads
|
|
191
|
+
* `a.?` ({@link PRESENCE_SEGMENT}), never the value of `a.b`.
|
|
185
192
|
*
|
|
186
193
|
* A variable whose range is not a plain chain (a call's result, another macro's
|
|
187
194
|
* output) names nothing a root holds, and a read through it contributes nothing.
|
|
@@ -224,6 +231,15 @@ function visitReads(node: CelNode, chains: string[][], bound: BoundRanges): void
|
|
|
224
231
|
return;
|
|
225
232
|
}
|
|
226
233
|
|
|
234
|
+
const presence = presenceOperand(node);
|
|
235
|
+
if (presence) {
|
|
236
|
+
const owner = readChainOf(presence, bound);
|
|
237
|
+
if (owner !== null) {
|
|
238
|
+
chains.push([...owner, PRESENCE_SEGMENT]);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
227
243
|
const moduleCall = moduleCallOf(node);
|
|
228
244
|
if (moduleCall) {
|
|
229
245
|
for (const arg of moduleCall.args) visitReads(arg, chains, bound);
|
|
@@ -250,6 +266,13 @@ function visitReads(node: CelNode, chains: string[][], bound: BoundRanges): void
|
|
|
250
266
|
for (const child of childNodes(node)) visitReads(child, chains, bound);
|
|
251
267
|
}
|
|
252
268
|
|
|
269
|
+
/** What a presence test `has(<operand>.<member>)` asks the keys of. */
|
|
270
|
+
function presenceOperand(node: CelNode): CelNode | undefined {
|
|
271
|
+
if (node.kind !== "call" || node.name !== "has" || node.args.length !== 1) return undefined;
|
|
272
|
+
const arg = node.args[0]!;
|
|
273
|
+
return arg.kind === "select" && !arg.optional ? arg.operand : undefined;
|
|
274
|
+
}
|
|
275
|
+
|
|
253
276
|
/** A member access on a module call's result: the call, and the members read
|
|
254
277
|
* off it in order (`Billing.total(xs).amount` → `amount`). */
|
|
255
278
|
export interface CallResultAccess {
|
package/src/cel/compile.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
type CelEnvironment,
|
|
20
20
|
type CelExpression,
|
|
21
21
|
type CelNode,
|
|
22
|
+
type CelProgram,
|
|
22
23
|
type NamespaceImplementation,
|
|
23
24
|
} from "@telorun/cel";
|
|
24
25
|
import {
|
|
@@ -80,6 +81,40 @@ export function repeatableExpression(expr: string, environment: CelEnvironment):
|
|
|
80
81
|
return !callsNonDeterministic(parsed.root) && !callsOnBareReceiver(parsed.root);
|
|
81
82
|
}
|
|
82
83
|
|
|
84
|
+
/**
|
|
85
|
+
* One compiled expression whose program a host may replace with an equivalent one compiled
|
|
86
|
+
* against the same environment — the kernel swaps in the program from an emitted module.
|
|
87
|
+
*/
|
|
88
|
+
export interface CelProgramSite {
|
|
89
|
+
readonly source: string;
|
|
90
|
+
readonly environment: CelEnvironment;
|
|
91
|
+
use(program: CelProgram): void;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The environment an expression at a site with these module names compiles against: the
|
|
96
|
+
* environment itself when it already declares all of them, otherwise a clone carrying them. A host compiling many expressions of one site builds it once with this, so
|
|
97
|
+
* they all share one environment.
|
|
98
|
+
*/
|
|
99
|
+
export function siteEnvironment(
|
|
100
|
+
environment: CelEnvironment,
|
|
101
|
+
moduleNames?: ReadonlySet<string>,
|
|
102
|
+
): CelEnvironment {
|
|
103
|
+
// Only the names CEL can read as a namespace: a set holding `my-module` is refused WHOLE
|
|
104
|
+
// at registration, and a name that cannot be one resolves no call anyway.
|
|
105
|
+
const namespaces = moduleNames === undefined ? NO_NAMESPACES : celNamespaceNames(moduleNames);
|
|
106
|
+
if (namespaces.size === 0) return environment;
|
|
107
|
+
// An environment already declaring every one of the names resolves the tree as a clone
|
|
108
|
+
// would, since the clone inherits its declarations.
|
|
109
|
+
const declared = new Set(environment.namespaces());
|
|
110
|
+
if ([...namespaces].every((name) => declared.has(name))) return environment;
|
|
111
|
+
// A clone carrying the site's names: the tree is resolved under them, and the environment
|
|
112
|
+
// that compiles it must declare the same set or it refuses the tree.
|
|
113
|
+
const site = environment.clone();
|
|
114
|
+
for (const name of namespaces) site.registerNamespace(name);
|
|
115
|
+
return site;
|
|
116
|
+
}
|
|
117
|
+
|
|
83
118
|
/**
|
|
84
119
|
* Compile one CEL expression into a `CompiledValue`. The `!cel` engine compiles its whole
|
|
85
120
|
* scalar with it, and the tags with holes compile each hole.
|
|
@@ -93,18 +128,10 @@ export function compileExpression(
|
|
|
93
128
|
expr: string,
|
|
94
129
|
environment: CelEnvironment,
|
|
95
130
|
moduleNames?: ReadonlySet<string>,
|
|
131
|
+
programSites?: CelProgramSite[],
|
|
96
132
|
): CompiledValue {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const namespaces = moduleNames === undefined ? NO_NAMESPACES : celNamespaceNames(moduleNames);
|
|
100
|
-
if (namespaces.size === 0) {
|
|
101
|
-
return compiledValueOf(expr, environment.parse(expr), environment);
|
|
102
|
-
}
|
|
103
|
-
// A clone carrying the site's names: the tree is resolved under them, and the environment
|
|
104
|
-
// that compiles it must declare the same set or it refuses the tree.
|
|
105
|
-
const site = environment.clone();
|
|
106
|
-
for (const name of namespaces) site.registerNamespace(name);
|
|
107
|
-
return compiledValueOf(expr, site.parse(expr), site);
|
|
133
|
+
const site = siteEnvironment(environment, moduleNames);
|
|
134
|
+
return compiledValueOf(expr, site.parse(expr), site, programSites);
|
|
108
135
|
}
|
|
109
136
|
|
|
110
137
|
/** A `CompiledValue` over an expression already read against its site's names. */
|
|
@@ -112,8 +139,20 @@ export function compiledValueOf(
|
|
|
112
139
|
expr: string,
|
|
113
140
|
parsed: CelExpression,
|
|
114
141
|
environment: CelEnvironment,
|
|
142
|
+
programSites?: CelProgramSite[],
|
|
115
143
|
): CompiledValue {
|
|
116
|
-
|
|
144
|
+
// Where a host may swap in an emitted program, the closure is built only if nothing was
|
|
145
|
+
// swapped in by the first evaluation. An unreadable expression is still compiled — and so
|
|
146
|
+
// refused — here, where it is written.
|
|
147
|
+
let program: CelProgram | undefined =
|
|
148
|
+
programSites && parsed.diagnostics.length === 0 ? undefined : environment.compile(parsed);
|
|
149
|
+
programSites?.push({
|
|
150
|
+
source: expr,
|
|
151
|
+
environment,
|
|
152
|
+
use: (replacement) => {
|
|
153
|
+
program = replacement;
|
|
154
|
+
},
|
|
155
|
+
});
|
|
117
156
|
// As written — `CompiledValue.calls` is the qualified spellings, not their shapes.
|
|
118
157
|
const calls = qualifiedCalls(parsed.root).map((call) => call.qualifiedName);
|
|
119
158
|
return {
|
|
@@ -127,7 +166,11 @@ export function compiledValueOf(
|
|
|
127
166
|
// all: the engine would answer its own `unbound_function` for every qualified call,
|
|
128
167
|
// whatever the kernel bound, so the whole dispatch is silently dead.
|
|
129
168
|
call: (ctx: Record<string, unknown>) =>
|
|
130
|
-
hostValueOf(
|
|
169
|
+
hostValueOf(
|
|
170
|
+
(program ??= environment.compile(parsed)).evaluate(ctx as never, {
|
|
171
|
+
namespaceFunction: dispatchFrom(ctx),
|
|
172
|
+
}),
|
|
173
|
+
),
|
|
131
174
|
};
|
|
132
175
|
}
|
|
133
176
|
|
package/src/engine.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CelEnvironment } from "@telorun/cel";
|
|
2
2
|
import type { CompiledValue } from "@telorun/sdk";
|
|
3
|
+
import type { CelProgramSite } from "./cel/compile.js";
|
|
3
4
|
import type { ModuleCallFlags } from "./cel/diagnose.js";
|
|
4
5
|
import type { ModuleCallTypeResolver } from "./cel/module-call.js";
|
|
5
6
|
|
|
@@ -20,6 +21,9 @@ export interface CompileEnv {
|
|
|
20
21
|
* before module functions existed.
|
|
21
22
|
*/
|
|
22
23
|
readonly moduleNames?: ReadonlySet<string>;
|
|
24
|
+
/** Receives every CEL expression compiled here, so a host can replace each
|
|
25
|
+
* program with one from an emitted module. */
|
|
26
|
+
readonly programSites?: CelProgramSite[];
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
/** Analyze-time environment passed to `engine.analyze`. The walker resolves
|
package/src/engines/cel.ts
CHANGED
|
@@ -270,7 +270,7 @@ export const celEngine: TemplatingEngine = {
|
|
|
270
270
|
language: "cel",
|
|
271
271
|
|
|
272
272
|
compile(source, env) {
|
|
273
|
-
return compileExpression(source, env.celEnv, env.moduleNames);
|
|
273
|
+
return compileExpression(source, env.celEnv, env.moduleNames, env.programSites);
|
|
274
274
|
},
|
|
275
275
|
|
|
276
276
|
analyze(source, env) {
|
|
@@ -28,7 +28,7 @@ export function compileHoles(
|
|
|
28
28
|
readonly calls: readonly string[];
|
|
29
29
|
readonly volatile: boolean;
|
|
30
30
|
} {
|
|
31
|
-
const compiled = holes.map((h) => compileExpression(h.expr, env.celEnv, env.moduleNames));
|
|
31
|
+
const compiled = holes.map((h) => compileExpression(h.expr, env.celEnv, env.moduleNames, env.programSites));
|
|
32
32
|
const refs = new Set<string>();
|
|
33
33
|
const calls = new Set<string>();
|
|
34
34
|
let volatile = false;
|
package/src/index.ts
CHANGED
|
@@ -9,7 +9,12 @@ export {
|
|
|
9
9
|
// become the engine's namespace seam — exported because the analyzer evaluates
|
|
10
10
|
// rule conditions and function bodies through the same table, and a second
|
|
11
11
|
// adapter is what let it convert arguments differently from the kernel.
|
|
12
|
-
export {
|
|
12
|
+
export {
|
|
13
|
+
compileExpression,
|
|
14
|
+
namespaceDispatchOf,
|
|
15
|
+
siteEnvironment,
|
|
16
|
+
type CelProgramSite,
|
|
17
|
+
} from "./cel/compile.js";
|
|
13
18
|
export {
|
|
14
19
|
interpolationShape,
|
|
15
20
|
literalFragments,
|
|
@@ -20,6 +25,7 @@ export {
|
|
|
20
25
|
} from "./cel/interpolation-holes.js";
|
|
21
26
|
export {
|
|
22
27
|
EACH_SEGMENT,
|
|
28
|
+
PRESENCE_SEGMENT,
|
|
23
29
|
extractAccessChains,
|
|
24
30
|
extractReadChains,
|
|
25
31
|
findNullableAccessIssues,
|