gdp-wired 0.46.0 → 0.48.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/README.md +18 -5
- package/dist/core/carry.d.ts +15 -0
- package/dist/core/carry.js +651 -0
- package/dist/core/cli.d.ts +4 -1
- package/dist/core/cli.js +31 -11
- package/dist/core/count-cases.d.ts +24 -0
- package/dist/core/count-cases.js +320 -0
- package/dist/core/count-cert-grow.d.ts +4 -0
- package/dist/core/count-cert-grow.js +37 -0
- package/dist/core/count-cert-transport.d.ts +3 -0
- package/dist/core/count-cert-transport.js +58 -0
- package/dist/core/count-completion.d.ts +9 -0
- package/dist/core/count-completion.js +27 -0
- package/dist/core/count-cubes.d.ts +8 -0
- package/dist/core/count-cubes.js +69 -0
- package/dist/core/count-demand.d.ts +37 -0
- package/dist/core/count-demand.js +361 -0
- package/dist/core/count-facts.d.ts +52 -0
- package/dist/core/count-facts.js +247 -0
- package/dist/core/count-focus-proof.d.ts +8 -0
- package/dist/core/count-focus-proof.js +140 -0
- package/dist/core/count-focused.d.ts +22 -0
- package/dist/core/count-focused.js +676 -0
- package/dist/core/count-ranges.d.ts +5 -0
- package/dist/core/count-ranges.js +65 -0
- package/dist/core/count-recovery/action-guard.d.ts +5 -0
- package/dist/core/count-recovery/action-guard.js +51 -0
- package/dist/core/count-recovery/boolean-core.d.ts +16 -0
- package/dist/core/count-recovery/boolean-core.js +258 -0
- package/dist/core/count-recovery/carried-case.d.ts +2 -0
- package/dist/core/count-recovery/carried-case.js +24 -0
- package/dist/core/count-recovery/conditional-check-cache.d.ts +23 -0
- package/dist/core/count-recovery/conditional-check-cache.js +130 -0
- package/dist/core/count-recovery/conditional-cuts.d.ts +6 -0
- package/dist/core/count-recovery/conditional-cuts.js +318 -0
- package/dist/core/count-recovery/conditional-proof.d.ts +19 -0
- package/dist/core/count-recovery/conditional-proof.js +306 -0
- package/dist/core/count-recovery/derivability-closure.d.ts +14 -0
- package/dist/core/count-recovery/derivability-closure.js +533 -0
- package/dist/core/count-recovery/empty-count-source.d.ts +2 -0
- package/dist/core/count-recovery/empty-count-source.js +40 -0
- package/dist/core/count-recovery/field-bound-lemmas.d.ts +23 -0
- package/dist/core/count-recovery/field-bound-lemmas.js +640 -0
- package/dist/core/count-recovery/field-case.d.ts +5 -0
- package/dist/core/count-recovery/field-case.js +89 -0
- package/dist/core/count-recovery/field-demand.d.ts +46 -0
- package/dist/core/count-recovery/field-demand.js +47 -0
- package/dist/core/count-recovery/field-only.d.ts +3 -0
- package/dist/core/count-recovery/field-only.js +18 -0
- package/dist/core/count-recovery/fold-path.d.ts +6 -0
- package/dist/core/count-recovery/fold-path.js +35 -0
- package/dist/core/count-recovery/frame-lemmas.d.ts +9 -0
- package/dist/core/count-recovery/frame-lemmas.js +120 -0
- package/dist/core/count-recovery/guard-cases.d.ts +12 -0
- package/dist/core/count-recovery/guard-cases.js +138 -0
- package/dist/core/count-recovery/key-closure.d.ts +14 -0
- package/dist/core/count-recovery/key-closure.js +62 -0
- package/dist/core/count-recovery/leaf-allowance.d.ts +4 -0
- package/dist/core/count-recovery/leaf-allowance.js +7 -0
- package/dist/core/count-recovery/lift-count-proxies.d.ts +6 -0
- package/dist/core/count-recovery/lift-count-proxies.js +175 -0
- package/dist/core/count-recovery/live-dependencies.d.ts +6 -0
- package/dist/core/count-recovery/live-dependencies.js +76 -0
- package/dist/core/count-recovery/member-inclusions.d.ts +11 -0
- package/dist/core/count-recovery/member-inclusions.js +168 -0
- package/dist/core/count-recovery/member-premises.d.ts +29 -0
- package/dist/core/count-recovery/member-premises.js +114 -0
- package/dist/core/count-recovery/natural-cases.d.ts +6 -0
- package/dist/core/count-recovery/natural-cases.js +59 -0
- package/dist/core/count-recovery/ops.d.ts +6 -0
- package/dist/core/count-recovery/ops.js +8 -0
- package/dist/core/count-recovery/pre-state-source.d.ts +8 -0
- package/dist/core/count-recovery/pre-state-source.js +18 -0
- package/dist/core/count-recovery/rebase-path.d.ts +9 -0
- package/dist/core/count-recovery/rebase-path.js +70 -0
- package/dist/core/count-recovery/rename-proof.d.ts +2 -0
- package/dist/core/count-recovery/rename-proof.js +102 -0
- package/dist/core/count-recovery/row-cases.d.ts +16 -0
- package/dist/core/count-recovery/row-cases.js +291 -0
- package/dist/core/count-recovery/scalar-guards.d.ts +7 -0
- package/dist/core/count-recovery/scalar-guards.js +291 -0
- package/dist/core/count-recovery/source-memo.d.ts +12 -0
- package/dist/core/count-recovery/source-memo.js +67 -0
- package/dist/core/count-select.d.ts +16 -0
- package/dist/core/count-select.js +24 -0
- package/dist/core/count-support.d.ts +598 -0
- package/dist/core/count-support.js +2080 -0
- package/dist/core/count-unions.d.ts +2 -0
- package/dist/core/count-unions.js +139 -0
- package/dist/core/count-witness-flags.d.ts +9 -0
- package/dist/core/count-witness-flags.js +37 -0
- package/dist/core/count-write-frames.d.ts +12 -0
- package/dist/core/count-write-frames.js +206 -0
- package/dist/core/counts.d.ts +20 -2
- package/dist/core/counts.js +807 -156
- package/dist/core/dsl.d.ts +50 -6
- package/dist/core/dsl.js +307 -65
- package/dist/core/effect-schema.d.ts +90 -0
- package/dist/core/effect-schema.js +500 -0
- package/dist/core/effect.d.ts +3 -7
- package/dist/core/effect.js +10 -65
- package/dist/core/exists.d.ts +1 -1
- package/dist/core/exists.js +80 -15
- package/dist/core/extract-check.d.ts +7 -1
- package/dist/core/extract-check.js +161 -19
- package/dist/core/extract-go-check.js +24 -4
- package/dist/core/extract-go.d.ts +7 -0
- package/dist/core/extract-go.js +29 -2
- package/dist/core/extract-read.d.ts +31 -9
- package/dist/core/extract-read.js +36 -11
- package/dist/core/extract-rs.js +58 -14
- package/dist/core/extract-shapes.d.ts +1 -0
- package/dist/core/extract-shapes.js +190 -27
- package/dist/core/extract-standalone.d.ts +3 -0
- package/dist/core/extract-standalone.js +265 -0
- package/dist/core/extract.d.ts +1 -0
- package/dist/core/extract.js +25 -5
- package/dist/core/field-laws.d.ts +2 -0
- package/dist/core/field-laws.js +100 -0
- package/dist/core/finite-frontiers.d.ts +5 -0
- package/dist/core/finite-frontiers.js +116 -0
- package/dist/core/finite-houdini.d.ts +52 -0
- package/dist/core/finite-houdini.js +460 -0
- package/dist/core/finite-model.d.ts +13 -0
- package/dist/core/finite-model.js +68 -0
- package/dist/core/finite-tiny.d.ts +72 -0
- package/dist/core/finite-tiny.js +1052 -0
- package/dist/core/infer.d.ts +1 -1
- package/dist/core/infer.js +32 -9
- package/dist/core/laws.d.ts +2 -1
- package/dist/core/laws.js +76 -19
- package/dist/core/lemma-cuts.d.ts +38 -0
- package/dist/core/lemma-cuts.js +365 -0
- package/dist/core/machine.d.ts +39 -2
- package/dist/core/machine.js +240 -35
- package/dist/core/mutate.d.ts +55 -0
- package/dist/core/mutate.js +210 -0
- package/dist/core/native-domain.d.ts +6 -0
- package/dist/core/native-domain.js +114 -0
- package/dist/core/native-law.d.ts +3 -0
- package/dist/core/native-law.js +174 -0
- package/dist/core/quorum-laws.d.ts +5 -0
- package/dist/core/quorum-laws.js +239 -0
- package/dist/core/relational.js +10 -2
- package/dist/core/relevance.d.ts +4 -0
- package/dist/core/relevance.js +79 -0
- package/dist/core/rs/Cargo.toml +1 -1
- package/dist/core/rs/src/main.rs +458 -9
- package/dist/core/runtime-wall.d.ts +3 -0
- package/dist/core/runtime-wall.js +4 -0
- package/dist/core/sample-step.d.ts +16 -0
- package/dist/core/sample-step.js +28 -0
- package/dist/core/source-check.d.ts +44 -0
- package/dist/core/source-check.js +126 -0
- package/dist/core/source-diagnostic.d.ts +1 -0
- package/dist/core/source-diagnostic.js +34 -0
- package/dist/core/support-budget.d.ts +2 -0
- package/dist/core/support-budget.js +6 -0
- package/dist/core/system.js +2 -0
- package/dist/core/wall.d.ts +6 -0
- package/dist/core/wall.js +23 -0
- package/dist/core/watch.js +4 -0
- package/dist/kernel/cdcl.d.ts +4 -0
- package/dist/kernel/cdcl.js +20 -5
- package/dist/kernel/kernel.d.ts +10 -0
- package/dist/kernel/kernel.js +56 -3
- package/dist/kernel/model-sequence.d.ts +2 -2
- package/dist/kernel/model-sequence.js +4 -3
- package/dist/kernel/model-values.d.ts +2 -0
- package/dist/kernel/model-values.js +60 -0
- package/dist/kernel/prove.d.ts +10 -0
- package/dist/kernel/prove.js +56 -8
- package/dist/kernel/prover-rows.d.ts +29 -0
- package/dist/kernel/prover-rows.js +114 -0
- package/dist/lean-kernel/emit.d.ts +39 -2
- package/dist/lean-kernel/emit.js +136 -24
- package/dist/lean-kernel/lean-terms.d.ts +1 -0
- package/dist/lean-kernel/lean-terms.js +13 -5
- package/dist/lean-kernel/library.js +4 -0
- package/dist/lean-kernel/qalpha.d.ts +7 -0
- package/dist/lean-kernel/qalpha.js +40 -0
- package/package.json +5 -1
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
// symbolically (core/extract-read.ts) into a Model: the state's variables, where each sits in the code's own values,
|
|
5
5
|
// the step's writes, what the code refuses (its throws) and how a request is made of the code.
|
|
6
6
|
import ts from 'typescript';
|
|
7
|
+
import { standaloneClosure } from "./extract-standalone.js";
|
|
7
8
|
import * as I from "./extract-ir.js";
|
|
8
|
-
import { Exec, Retype, memberName, mergeOps, normalize, program, refuse, shapeOf, unparen } from "./extract-read.js";
|
|
9
|
+
import { Exec, Retype, lazyKey, memberName, mergeOps, normalize, program, refuse, shapeOf, unparen } from "./extract-read.js";
|
|
9
10
|
const at0 = (sf) => sf.statements[0] ?? sf;
|
|
10
11
|
export function setupOf(code, file, typed = {}, files = {}) {
|
|
11
12
|
const root = file.slice(0, file.length - (file.split('/').pop().length)), extra = Object.fromEntries(Object.entries(files).map(([p, c]) => [normalize(`${root}${p}`), c]));
|
|
@@ -99,8 +100,8 @@ function materialize(ex, v, layout, p, at, out = { leaves: new Map(), maps: new
|
|
|
99
100
|
// An untyped input written here: of this field's sort, and the step read again.
|
|
100
101
|
const e = v.t === 'num' || v.t === 'bool' ? v.e : v.t === 'str' ? v.e : undefined;
|
|
101
102
|
const name = e && (e.op === 'var' || e.op === 'svar') ? e.name : undefined, lz = name ? ex.s.lazy.get(name) : undefined;
|
|
102
|
-
if (lz && !ex.s.lazySorts.has(
|
|
103
|
-
throw new Retype(
|
|
103
|
+
if (lz && !ex.s.lazySorts.has(lazyKey(lz, lz.field)))
|
|
104
|
+
throw new Retype(lazyKey(lz, lz.field), sort);
|
|
104
105
|
refuse(at, `${layout.name} is set to a ${v.t}`);
|
|
105
106
|
}
|
|
106
107
|
out.leaves.set(layout.name, sort.k === 'choice' ? { t: 'choice', e: ex.asChoice(v, sort.values, at), values: sort.values } : v);
|
|
@@ -356,20 +357,30 @@ function literalsOf(body, param, s) {
|
|
|
356
357
|
return out;
|
|
357
358
|
}
|
|
358
359
|
const OTHER = '@@other';
|
|
359
|
-
function argOf(ex, param, prefix, body) {
|
|
360
|
+
function argOf(ex, param, prefix, body, ordinal = 0) {
|
|
360
361
|
const s = ex.s, t = s.checker.getTypeAtLocation(param), pname = ts.isIdentifier(param.name) ? param.name.text : 'arg';
|
|
361
362
|
const lits = ts.isIdentifier(param.name) ? literalsOf(body, param.name.text, s) : new Map();
|
|
362
363
|
// Untyped (JavaScript): its fields as the code reads them, numbers unless compared with string literals.
|
|
363
364
|
if (t.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
365
|
+
// The scope and ordinal are semantic identity; the prefix is only a readable name.
|
|
366
|
+
const root = [prefix, ordinal];
|
|
367
|
+
if (ordinal)
|
|
368
|
+
prefix = `${prefix}arg${ordinal}_`;
|
|
369
|
+
const lazy = { prefix, root };
|
|
370
|
+
for (const [path, xs] of lits) {
|
|
371
|
+
const key = lazyKey(lazy, path);
|
|
372
|
+
if (!s.lazySorts.has(key))
|
|
373
|
+
s.lazySorts.set(key, { k: 'choice', values: [...xs, OTHER] });
|
|
374
|
+
}
|
|
375
|
+
const id = ex.id(`input.${prefix}${pname}`);
|
|
368
376
|
s.notes.add(`${pname} is untyped: its fields are inputs as the code reads them (numbers, or a choice among the literals it is compared with and ${OTHER})`);
|
|
369
377
|
const disc = [...lits.keys()].find(k => !k.includes('_'));
|
|
370
|
-
const arg = { k: 'lazy',
|
|
371
|
-
const
|
|
372
|
-
|
|
378
|
+
const arg = { k: 'lazy', ...lazy };
|
|
379
|
+
const key = disc && lazyKey(lazy, disc);
|
|
380
|
+
if (key)
|
|
381
|
+
(s.lazySelections ??= new Set()).add(key);
|
|
382
|
+
const events = key ? [...lits.get(disc), OTHER].map(value => ({ select: { [key]: value }, arg })) : [{ select: {}, arg }];
|
|
383
|
+
return { v: { t: 'ref', id }, events, heap: [[id, { k: 'rec', fields: new Map(), origin: 'input', lazy }]] };
|
|
373
384
|
}
|
|
374
385
|
const members = t.isUnion() && t.types.every(m => m.flags & ts.TypeFlags.Object) ? t.types : [t];
|
|
375
386
|
if (members.length === 1 && !(t.flags & ts.TypeFlags.Object)) {
|
|
@@ -413,7 +424,7 @@ function argOf(ex, param, prefix, body) {
|
|
|
413
424
|
vars.set(key, x);
|
|
414
425
|
return Exec.valueOf(x);
|
|
415
426
|
};
|
|
416
|
-
const id = `input.${prefix}${pname}
|
|
427
|
+
const id = ex.id(`input.${prefix}${pname}`), v = make({ k: 'rec', fields: [...all] }, [], id);
|
|
417
428
|
const template = (m) => {
|
|
418
429
|
const of = (sh, path, present) => (sh.k === 'rec'
|
|
419
430
|
? { k: 'rec', fields: sh.fields.filter(([f]) => path.length || present(f)).map(([f, fs]) => [f, of(fs, [...path, f], present)]) }
|
|
@@ -432,6 +443,155 @@ function argOf(ex, param, prefix, body) {
|
|
|
432
443
|
return { v, events, heap };
|
|
433
444
|
}
|
|
434
445
|
const sortOf = (sh, lits) => (sh.k === 'str' && lits.length ? { k: 'choice', values: [...lits, OTHER] } : sh.k === 'num' || sh.k === 'bool' || sh.k === 'choice' || sh.k === 'str' ? sh : (() => { throw new Error('a compound sort'); })());
|
|
446
|
+
// Experimental opt-in: a pure, standalone scalar function. No third-party call or state is synthesized.
|
|
447
|
+
export function readStandalone(code, file, opts) {
|
|
448
|
+
const s = setupOf(code, file, opts.typed, opts.files), fn = findFunction(s.sf, opts.entry);
|
|
449
|
+
if (opts.entry === 'undefined' || opts.entry === '__cov')
|
|
450
|
+
refuse(fn, 'standalone: reserved entry name');
|
|
451
|
+
const bindings = standaloneClosure(s, fn);
|
|
452
|
+
s.spans = [[fn.getStart(), fn.end]];
|
|
453
|
+
const ex = new Exec(s, true), heap = new Map(), args = [];
|
|
454
|
+
const domains = {}, limits = [];
|
|
455
|
+
let p = { pc: [], locals: new Map(), heap, wrote: [], nows: 0 };
|
|
456
|
+
for (const param of fn.parameters) {
|
|
457
|
+
const name = ts.isIdentifier(param.name) ? param.name.text : 'arg', t = s.checker.getTypeAtLocation(param), parts = t.isUnion() ? t.types : [t];
|
|
458
|
+
if (!t.isUnion() && (t.flags & ts.TypeFlags.Object)) {
|
|
459
|
+
const sh = shapeOf(t, s.checker, param);
|
|
460
|
+
if (sh.k !== 'rec' || !sh.fields.length || sh.fields.some(([key, field]) => ['__proto__', 'prototype', 'constructor'].includes(key) || (field.k !== 'bool' && field.k !== 'choice') || (field.k === 'choice' && (field.values.length > 64 || field.values.some(x => !x)))))
|
|
461
|
+
refuse(param, 'standalone: record inputs need boolean or finite string choice fields');
|
|
462
|
+
s.notes.add('standalone record inputs are immutable plain-data snapshots: own data properties only, no accessors or Proxies');
|
|
463
|
+
const a = argOf(ex, param, `${name}_`, fn.body ?? fn);
|
|
464
|
+
if (a.events.length !== 1 || Object.keys(a.events[0].select).length)
|
|
465
|
+
refuse(param, 'standalone: record input has multiple variants');
|
|
466
|
+
for (const [id, object] of a.heap)
|
|
467
|
+
p.heap.set(id, object);
|
|
468
|
+
args.push(a.events[0].arg);
|
|
469
|
+
p = ex.bind(param.name, a.v, p);
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
const missing = parts.filter(x => !!(x.flags & ts.TypeFlags.Undefined)), rest = parts.filter(x => !(x.flags & ts.TypeFlags.Undefined));
|
|
473
|
+
if (parts.some(x => !!(x.flags & ts.TypeFlags.Null)))
|
|
474
|
+
refuse(param, 'standalone: null input is outside the subset');
|
|
475
|
+
const bool = rest.length > 0 && rest.every(x => !!(x.flags & ts.TypeFlags.BooleanLike));
|
|
476
|
+
const num = rest.length > 0 && rest.every(x => !!(x.flags & ts.TypeFlags.NumberLike));
|
|
477
|
+
const choice = rest.length > 0 && rest.every(x => !!(x.flags & ts.TypeFlags.StringLiteral));
|
|
478
|
+
if ((!bool && !num && !choice) || (missing.length && !bool))
|
|
479
|
+
refuse(param, 'standalone: only numeric, boolean, finite string choice, or boolean | undefined inputs');
|
|
480
|
+
const values = choice ? [...new Set(rest.map(x => x.value))] : [];
|
|
481
|
+
if (choice && (values.length > 64 || values.some(x => !x)))
|
|
482
|
+
refuse(param, 'standalone: string literal domain must contain at most 64 nonempty choices');
|
|
483
|
+
const x = ex.newVar(name, choice ? { k: 'choice', values } : { k: bool ? 'bool' : 'num' }, 'input');
|
|
484
|
+
let v = Exec.valueOf(x);
|
|
485
|
+
if (num && rest.every(t => !!(t.flags & ts.TypeFlags.NumberLiteral))) {
|
|
486
|
+
const values = [...new Set(rest.map(t => t.value))].sort((a, b) => a - b);
|
|
487
|
+
if (!values.every(Number.isSafeInteger) || values.length > 64)
|
|
488
|
+
refuse(param, 'standalone: numeric literal domain must contain at most 64 safe integers');
|
|
489
|
+
domains[x.name] = values;
|
|
490
|
+
limits.push(I.or(...values.map(n => I.cmp('=', v.e, I.num(n)))));
|
|
491
|
+
}
|
|
492
|
+
if (missing.length) {
|
|
493
|
+
const m = ex.newVar(`${name}_missing`, { k: 'bool' }, 'input');
|
|
494
|
+
v = { t: 'opt', has: I.not(Exec.valueOf(m).e), v };
|
|
495
|
+
args.push({ k: 'maybe-bool', name: x.name, missing: m.name });
|
|
496
|
+
}
|
|
497
|
+
else
|
|
498
|
+
args.push({ k: 'input', name: x.name });
|
|
499
|
+
p = ex.bind(param.name, v, p);
|
|
500
|
+
}
|
|
501
|
+
const sig = s.checker.getSignatureFromDeclaration(fn), rt = s.checker.getReturnTypeOfSignature(sig), parts = rt.isUnion() ? rt.types : [rt];
|
|
502
|
+
if (!rt.isUnion() && (rt.flags & ts.TypeFlags.Object)) {
|
|
503
|
+
const shape = shapeOf(rt, s.checker, fn);
|
|
504
|
+
if (shape.k !== 'rec' || !shape.fields.length || shape.fields.some(([key, field]) => ['__proto__', 'prototype', 'constructor'].includes(key) || (field.k !== 'bool' && field.k !== 'num')))
|
|
505
|
+
refuse(fn, 'standalone: record results need scalar boolean or numeric fields');
|
|
506
|
+
const { layout } = build(ex, shape, [`${opts.entry}_result`], p.heap, 'fresh', fn);
|
|
507
|
+
if (layout.k !== 'rec')
|
|
508
|
+
throw new Error('standalone: record layout expected');
|
|
509
|
+
const fields = layout.fields.map(([f, sub]) => {
|
|
510
|
+
if (sub.k !== 'scalar')
|
|
511
|
+
return refuse(fn, 'standalone: nested result field');
|
|
512
|
+
return [f, sub.name];
|
|
513
|
+
});
|
|
514
|
+
let outs;
|
|
515
|
+
if (ts.isBlock(fn.body)) {
|
|
516
|
+
const flow = ex.block(fn.body.statements, p);
|
|
517
|
+
if (flow.live && ex.feasible(flow.live))
|
|
518
|
+
refuse(fn, 'standalone: record result missing on a path');
|
|
519
|
+
outs = flow.rets;
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
const [v, q] = ex.ev(fn.body, p);
|
|
523
|
+
outs = [[q, v]];
|
|
524
|
+
}
|
|
525
|
+
outs = outs.filter(([q]) => ex.feasible(q));
|
|
526
|
+
if (!outs.length)
|
|
527
|
+
refuse(fn, 'standalone: no reachable return');
|
|
528
|
+
const final = joinAll(ex, outs.map(([q, v]) => [q, materialize(ex, v, layout, q, fn)]), fn);
|
|
529
|
+
const init = {};
|
|
530
|
+
for (const [, n] of fields)
|
|
531
|
+
init[n] = termOf(Exec.defaultOf(s.vars.get(n).sort));
|
|
532
|
+
const model = finish(s, { name: opts.entry, shape: 'functions', file, entry: opts.entry,
|
|
533
|
+
layout: { k: 'rec', fields: [] }, init, final, refuse: I.or(refusalOf(ex), I.not(I.and(...limits))), pre: I.T,
|
|
534
|
+
events: [{ select: {}, call: opts.entry, args, names: fn.parameters.map(x => ts.isIdentifier(x.name) ? x.name.text : '__gdp_destructured') }], outputs: fields.map(([, n]) => n) });
|
|
535
|
+
model.standalone = true;
|
|
536
|
+
model.kind = 'a standalone function with a scalar record result';
|
|
537
|
+
model.standaloneDomains = domains;
|
|
538
|
+
model.standaloneBindings = bindings;
|
|
539
|
+
model.results = { [opts.entry]: { k: 'rec', fields } };
|
|
540
|
+
model.notes.push('standalone function: closed scalar inputs and record result; numbers are safe integers', `closed reachable bindings: ${bindings.join(', ')}`);
|
|
541
|
+
return model;
|
|
542
|
+
}
|
|
543
|
+
const nullable = parts.some(x => !!(x.flags & ts.TypeFlags.Null));
|
|
544
|
+
if (nullable && parts.some(x => !!(x.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Void))))
|
|
545
|
+
refuse(fn, 'standalone: a result cannot be both null and undefined');
|
|
546
|
+
const rest = parts.filter(x => !(x.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Void | ts.TypeFlags.Null)));
|
|
547
|
+
const bool = rest.length > 0 && rest.every(x => !!(x.flags & ts.TypeFlags.BooleanLike));
|
|
548
|
+
const num = rest.length > 0 && rest.every(x => !!(x.flags & ts.TypeFlags.NumberLike));
|
|
549
|
+
const choice = rest.length > 0 && rest.every(x => !!(x.flags & ts.TypeFlags.StringLiteral));
|
|
550
|
+
if (!bool && !num && !choice)
|
|
551
|
+
refuse(fn, 'standalone: result must be numeric, boolean, or a finite string choice, optionally absent');
|
|
552
|
+
const values = choice ? [...new Set(rest.map(x => x.value))] : [];
|
|
553
|
+
if (choice && (values.length > 64 || values.some(x => !x)))
|
|
554
|
+
refuse(fn, 'standalone: string literal result must contain at most 64 nonempty choices');
|
|
555
|
+
const val = ex.newVar(`${opts.entry}_result`, choice ? { k: 'choice', values } : { k: bool ? 'bool' : 'num' }, 'state');
|
|
556
|
+
const absent = ex.newVar(`${opts.entry}_result_missing`, { k: 'bool' }, 'state');
|
|
557
|
+
let outs;
|
|
558
|
+
if (ts.isBlock(fn.body)) {
|
|
559
|
+
const flow = ex.block(fn.body.statements, p);
|
|
560
|
+
outs = [...flow.rets, ...(flow.live ? [[flow.live, { t: 'undef' }]] : [])];
|
|
561
|
+
}
|
|
562
|
+
else {
|
|
563
|
+
const [v, q] = ex.ev(fn.body, p);
|
|
564
|
+
outs = [[q, v]];
|
|
565
|
+
}
|
|
566
|
+
outs = outs.filter(([q]) => ex.feasible(q));
|
|
567
|
+
if (!outs.length)
|
|
568
|
+
refuse(fn, 'standalone: no reachable return');
|
|
569
|
+
const final = joinAll(ex, outs.map(([q, v]) => {
|
|
570
|
+
if ((v.t === 'undef' || v.t === 'opt') && !!v.null !== nullable)
|
|
571
|
+
refuse(fn, 'standalone: result absence differs from its declared null or undefined type');
|
|
572
|
+
const has = v.t === 'undef' ? I.F : v.t === 'opt' ? v.has : I.T;
|
|
573
|
+
const inner = v.t === 'undef' ? undefined : v.t === 'opt' ? v.v : v;
|
|
574
|
+
// Choice variables are indexed by their own domain. Do not silently copy
|
|
575
|
+
// an index into a result with a different domain or ordering.
|
|
576
|
+
if (inner?.t === 'choice' && val.sort.k === 'choice' && JSON.stringify(inner.values) !== JSON.stringify(val.sort.values))
|
|
577
|
+
refuse(fn, 'standalone: choice result domain differs from returned choice');
|
|
578
|
+
const there = inner ? outValue(ex, inner, val, { ...q, pc: [...q.pc, has] }, fn) : Exec.defaultOf(val.sort);
|
|
579
|
+
const sv = { leaves: new Map(), maps: new Map() };
|
|
580
|
+
sv.leaves.set(val.name, ex.mergeScalar(has, there, Exec.defaultOf(val.sort), fn));
|
|
581
|
+
sv.leaves.set(absent.name, { t: 'bool', e: I.not(has) });
|
|
582
|
+
return [q, sv];
|
|
583
|
+
}), fn);
|
|
584
|
+
const model = finish(s, { name: opts.entry, shape: 'functions', file, entry: opts.entry,
|
|
585
|
+
layout: { k: 'rec', fields: [] }, init: { [val.name]: termOf(Exec.defaultOf(val.sort)), [absent.name]: termOf(Exec.defaultOf(absent.sort)) },
|
|
586
|
+
final, refuse: I.or(refusalOf(ex), I.not(I.and(...limits))), pre: I.T, events: [{ select: {}, call: opts.entry, args, names: fn.parameters.map(x => ts.isIdentifier(x.name) ? x.name.text : '__gdp_destructured') }], outputs: [val.name, absent.name] });
|
|
587
|
+
model.standalone = true;
|
|
588
|
+
model.kind = 'a standalone scalar function';
|
|
589
|
+
model.standaloneDomains = domains;
|
|
590
|
+
model.standaloneBindings = bindings;
|
|
591
|
+
model.notes.push('standalone scalar function: separate arguments and optional result; numbers are safe integers, not arbitrary JavaScript numbers', `closed reachable bindings: ${bindings.join(', ')}`, ...Object.entries(domains).map(([n, xs]) => `declared numeric input domain ${n}: ${xs.join(', ')}; other values are outside the claim`));
|
|
592
|
+
model.results = { [opts.entry]: { k: 'maybe', name: val.name, missing: absent.name, null: nullable } };
|
|
593
|
+
return model;
|
|
594
|
+
}
|
|
435
595
|
export function readFunctions(code, file, opts) {
|
|
436
596
|
// Read again while an untyped input turns out to be of another sort than a number.
|
|
437
597
|
for (let typed = { ...opts.typed };;) {
|
|
@@ -512,10 +672,10 @@ function readFunctionsOnce(code, file, opts) {
|
|
|
512
672
|
let p = { pc: sel.op === 'const' ? [] : [sel], locals: new Map(), heap: h, wrote: [], nows: 0 };
|
|
513
673
|
p = ex.bind(f.parameters[0].name, stateV, p);
|
|
514
674
|
const combos = [{ select: ev ? { [ev.name]: names[i] } : {}, args: [{ k: 'state' }] }];
|
|
515
|
-
for (const param of f.parameters.slice(1)) {
|
|
675
|
+
for (const [ordinal, param] of f.parameters.slice(1).entries()) {
|
|
516
676
|
if (param.dotDotDotToken)
|
|
517
677
|
refuse(param, 'a rest parameter');
|
|
518
|
-
const a = argOf(ex, param, multi ? `${names[i]}_` : '', f.body ?? f);
|
|
678
|
+
const a = argOf(ex, param, multi ? `${names[i]}_` : '', f.body ?? f, ordinal);
|
|
519
679
|
for (const [id, o] of a.heap)
|
|
520
680
|
p.heap.set(id, o);
|
|
521
681
|
p = ex.bind(param.name, a.v, p);
|
|
@@ -541,15 +701,6 @@ function readFunctionsOnce(code, file, opts) {
|
|
|
541
701
|
for (const c of combos)
|
|
542
702
|
events.push({ select: c.select, call: names[i], args: c.args, names: f.parameters.map(x => x.name.getText()) });
|
|
543
703
|
});
|
|
544
|
-
// An untyped record's discriminant, chosen by name now that the variable is made.
|
|
545
|
-
for (const e of events)
|
|
546
|
-
for (const [k, value] of Object.entries(e.select))
|
|
547
|
-
if (k.startsWith('$lazy:')) {
|
|
548
|
-
delete e.select[k];
|
|
549
|
-
const v = [...s.lazy].find(([, l]) => `$lazy:${l.prefix}${l.field}` === k);
|
|
550
|
-
if (v)
|
|
551
|
-
e.select[v[0]] = value;
|
|
552
|
-
}
|
|
553
704
|
const final = joinAll(ex0, outs, first);
|
|
554
705
|
// A default export by its function's own name.
|
|
555
706
|
const own = ts.isFunctionDeclaration(first) ? first.name?.text : ts.isVariableDeclaration(first.parent) && ts.isIdentifier(first.parent.name) ? first.parent.name.text : undefined;
|
|
@@ -557,6 +708,18 @@ function readFunctionsOnce(code, file, opts) {
|
|
|
557
708
|
refuse: I.or(...throws), pre: I.T, events, outputs: [], ...(ev ? { event: ev.name } : {}), ...(initSource ? { initSource } : {}) });
|
|
558
709
|
}
|
|
559
710
|
function finish(s, m) {
|
|
711
|
+
// Resolve synthetic discriminants through the same structured identity as sort inference.
|
|
712
|
+
const lazyVars = new Map([...s.lazy].map(([name, field]) => [lazyKey(field, field.field), name]));
|
|
713
|
+
for (const event of m.events)
|
|
714
|
+
for (const key of s.lazySelections ?? []) {
|
|
715
|
+
const value = event.select[key];
|
|
716
|
+
if (value === undefined)
|
|
717
|
+
continue;
|
|
718
|
+
delete event.select[key];
|
|
719
|
+
const name = lazyVars.get(key);
|
|
720
|
+
if (name)
|
|
721
|
+
event.select[name] = value;
|
|
722
|
+
}
|
|
560
723
|
const vars = [...s.vars.values()];
|
|
561
724
|
const writes = {};
|
|
562
725
|
for (const [n, v] of m.final.leaves)
|
|
@@ -1041,10 +1204,10 @@ function attempt(code, file, opts, written) {
|
|
|
1041
1204
|
const name = memberName(m.name), ex = new Exec(s), sel = I.is(evL, name);
|
|
1042
1205
|
let p = { pc: [sel], locals: new Map(), heap: new Map(stateHeap), wrote: [], nows: 0 };
|
|
1043
1206
|
const args = [];
|
|
1044
|
-
for (const param of m.parameters) {
|
|
1207
|
+
for (const [ordinal, param] of m.parameters.entries()) {
|
|
1045
1208
|
if (param.dotDotDotToken || !ts.isIdentifier(param.name))
|
|
1046
1209
|
refuse(param, 'a rest or destructured argument');
|
|
1047
|
-
const a = argOf(ex, param, `${name}_`, m.body ?? m);
|
|
1210
|
+
const a = argOf(ex, param, `${name}_`, m.body ?? m, ordinal);
|
|
1048
1211
|
if (a.events.length !== 1)
|
|
1049
1212
|
refuse(param, 'an argument that is a union of records');
|
|
1050
1213
|
for (const [id, o] of a.heap)
|
|
@@ -1364,10 +1527,10 @@ function factoryAttempt(code, file, opts, written, fxOf) {
|
|
|
1364
1527
|
const ex = new Exec(s), sel = I.is(evL, f.name);
|
|
1365
1528
|
let p = { pc: [sel], locals: new Map(), heap: new Map(stateHeap), wrote: [], nows: 0 };
|
|
1366
1529
|
const args = [];
|
|
1367
|
-
for (const param of f.decl.parameters) {
|
|
1530
|
+
for (const [ordinal, param] of f.decl.parameters.entries()) {
|
|
1368
1531
|
if (param.dotDotDotToken || !ts.isIdentifier(param.name))
|
|
1369
1532
|
refuse(param, 'a rest or destructured argument');
|
|
1370
|
-
const a = argOf(ex, param, `${f.name}_`, f.decl.body ?? f.decl);
|
|
1533
|
+
const a = argOf(ex, param, `${f.name}_`, f.decl.body ?? f.decl, ordinal);
|
|
1371
1534
|
if (a.events.length !== 1)
|
|
1372
1535
|
refuse(param, 'an argument that is a union of records');
|
|
1373
1536
|
for (const [id, o] of a.heap)
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// The opt-in scalar reader's closed binding boundary. No module code is run here.
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { refuse, unparen } from "./extract-read.js";
|
|
4
|
+
const reserved = (name) => ['undefined', '__cov', '$join', 'this'].includes(name) || name.startsWith('__gdp');
|
|
5
|
+
export function standaloneClosure(s, entry) {
|
|
6
|
+
const bindings = new Map(), active = new Set(), done = new Set(), reached = new Set();
|
|
7
|
+
const symbol = (id) => s.checker.getSymbolAtLocation(id) ?? refuse(id, 'standalone: unresolved binding');
|
|
8
|
+
const add = (id, decl, order) => {
|
|
9
|
+
if (reserved(id.text))
|
|
10
|
+
refuse(id, ts.isFunctionDeclaration(decl) ? 'standalone: reserved function name' : 'standalone: reserved module binding');
|
|
11
|
+
const sym = symbol(id);
|
|
12
|
+
if (bindings.has(sym))
|
|
13
|
+
refuse(id, 'standalone: duplicate module binding');
|
|
14
|
+
bindings.set(sym, { decl, order });
|
|
15
|
+
};
|
|
16
|
+
// Declarations of functions and types do not run their bodies on module load. Only
|
|
17
|
+
// inert const initializers may accompany them. Value imports and re-exports can run
|
|
18
|
+
// another module and are deliberately not this frontend's boundary.
|
|
19
|
+
for (const [order, st] of s.sf.statements.entries()) {
|
|
20
|
+
if (ts.isInterfaceDeclaration(st) || ts.isTypeAliasDeclaration(st) || ts.isEmptyStatement(st))
|
|
21
|
+
continue;
|
|
22
|
+
if (ts.isImportDeclaration(st)) {
|
|
23
|
+
const c = st.importClause;
|
|
24
|
+
if (c?.isTypeOnly || (c && !c.name && c.namedBindings && ts.isNamedImports(c.namedBindings) && c.namedBindings.elements.length > 0 && c.namedBindings.elements.every(x => x.isTypeOnly)))
|
|
25
|
+
continue;
|
|
26
|
+
refuse(st, 'standalone: only erased type imports are supported');
|
|
27
|
+
}
|
|
28
|
+
if (ts.isFunctionDeclaration(st) && st.name) {
|
|
29
|
+
add(st.name, st, order);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (ts.isFunctionDeclaration(st) && st === entry)
|
|
33
|
+
continue;
|
|
34
|
+
if (ts.isVariableStatement(st) && st.declarationList.flags & ts.NodeFlags.Const) {
|
|
35
|
+
for (const d of st.declarationList.declarations) {
|
|
36
|
+
if (!ts.isIdentifier(d.name) || !d.initializer)
|
|
37
|
+
refuse(d, 'standalone: module constants need a simple binding and initializer');
|
|
38
|
+
add(d.name, d, order);
|
|
39
|
+
}
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
refuse(st, 'standalone: executable module statement or unsupported declaration');
|
|
43
|
+
}
|
|
44
|
+
const functionOf = (b) => {
|
|
45
|
+
if (ts.isFunctionDeclaration(b.decl))
|
|
46
|
+
return b.decl;
|
|
47
|
+
const e = b.decl.initializer && unparen(b.decl.initializer);
|
|
48
|
+
return e && (ts.isArrowFunction(e) || ts.isFunctionExpression(e)) ? e : undefined;
|
|
49
|
+
};
|
|
50
|
+
const inert = (raw, before) => {
|
|
51
|
+
const e = unparen(raw);
|
|
52
|
+
if (ts.isAsExpression(e) || ts.isTypeAssertionExpression(e) || ts.isSatisfiesExpression(e)) {
|
|
53
|
+
inert(e.expression, before);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (ts.isNumericLiteral(e)) {
|
|
57
|
+
if (!Number.isSafeInteger(Number(e.text)))
|
|
58
|
+
refuse(e, 'standalone: constants must be safe integers');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (ts.isStringLiteral(e) || ts.isNoSubstitutionTemplateLiteral(e) || [ts.SyntaxKind.TrueKeyword, ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.NullKeyword].includes(e.kind))
|
|
62
|
+
return;
|
|
63
|
+
if (ts.isIdentifier(e)) {
|
|
64
|
+
if (e.text === 'undefined' && !s.checker.getSymbolAtLocation(e)?.valueDeclaration)
|
|
65
|
+
return;
|
|
66
|
+
const b = bindings.get(symbol(e));
|
|
67
|
+
if (!b || b.order >= before.order || functionOf(b))
|
|
68
|
+
refuse(e, 'standalone: constant initializer must reference an earlier inert constant');
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (ts.isArrayLiteralExpression(e)) {
|
|
72
|
+
for (const x of e.elements) {
|
|
73
|
+
if (ts.isSpreadElement(x) || ts.isOmittedExpression(x))
|
|
74
|
+
refuse(x, 'standalone: array initializer spread or hole');
|
|
75
|
+
inert(x, before);
|
|
76
|
+
}
|
|
77
|
+
;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (ts.isPrefixUnaryExpression(e) && [ts.SyntaxKind.PlusToken, ts.SyntaxKind.MinusToken, ts.SyntaxKind.ExclamationToken].includes(e.operator)) {
|
|
81
|
+
inert(e.operand, before);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
refuse(e, 'standalone: module initializer is not inert');
|
|
85
|
+
};
|
|
86
|
+
for (const b of bindings.values())
|
|
87
|
+
if (ts.isVariableDeclaration(b.decl) && !functionOf(b))
|
|
88
|
+
inert(b.decl.initializer, b);
|
|
89
|
+
const constant = (b, at) => {
|
|
90
|
+
if (active.has(b.decl))
|
|
91
|
+
refuse(at, 'standalone: recursive constant');
|
|
92
|
+
if (done.has(b.decl))
|
|
93
|
+
return;
|
|
94
|
+
if (!ts.isVariableDeclaration(b.decl))
|
|
95
|
+
refuse(at, 'standalone: function used as a value');
|
|
96
|
+
active.add(b.decl);
|
|
97
|
+
expression(b.decl.initializer, new Set());
|
|
98
|
+
active.delete(b.decl);
|
|
99
|
+
done.add(b.decl);
|
|
100
|
+
reached.add(b.decl.name.getText());
|
|
101
|
+
};
|
|
102
|
+
const expression = (raw, locals) => {
|
|
103
|
+
const e = unparen(raw);
|
|
104
|
+
if (ts.isAsExpression(e) || ts.isTypeAssertionExpression(e) || ts.isSatisfiesExpression(e)) {
|
|
105
|
+
expression(e.expression, locals);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (ts.isNumericLiteral(e) || ts.isStringLiteral(e) || ts.isNoSubstitutionTemplateLiteral(e) || e.kind === ts.SyntaxKind.TrueKeyword || e.kind === ts.SyntaxKind.FalseKeyword || e.kind === ts.SyntaxKind.NullKeyword)
|
|
109
|
+
return;
|
|
110
|
+
if (ts.isIdentifier(e)) {
|
|
111
|
+
const sym = s.checker.getSymbolAtLocation(e);
|
|
112
|
+
if (e.text === 'undefined' && !sym?.valueDeclaration)
|
|
113
|
+
return;
|
|
114
|
+
if (sym && locals.has(sym))
|
|
115
|
+
return;
|
|
116
|
+
const b = sym && bindings.get(sym);
|
|
117
|
+
if (!b)
|
|
118
|
+
refuse(e, 'standalone: non-input identifier or captured binding');
|
|
119
|
+
if (functionOf(b))
|
|
120
|
+
refuse(e, 'standalone: non-input identifier (function used as a value)');
|
|
121
|
+
constant(b, e);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (ts.isObjectLiteralExpression(e)) {
|
|
125
|
+
for (const prop of e.properties) {
|
|
126
|
+
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name) && !['__proto__', 'prototype', 'constructor'].includes(prop.name.text))
|
|
127
|
+
expression(prop.initializer, locals);
|
|
128
|
+
else if (ts.isShorthandPropertyAssignment(prop) && !prop.objectAssignmentInitializer && !['__proto__', 'prototype', 'constructor'].includes(prop.name.text))
|
|
129
|
+
expression(prop.name, locals);
|
|
130
|
+
else
|
|
131
|
+
refuse(prop, 'standalone: record literals need plain fields');
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (ts.isPropertyAccessExpression(e)) {
|
|
136
|
+
if (e.questionDotToken || ['__proto__', 'prototype', 'constructor'].includes(e.name.text))
|
|
137
|
+
refuse(e, 'standalone: unsafe property access');
|
|
138
|
+
expression(e.expression, locals);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (ts.isCallExpression(e)) {
|
|
142
|
+
if (e.questionDotToken || !ts.isIdentifier(e.expression))
|
|
143
|
+
refuse(e, 'standalone: only direct closed helper calls are supported');
|
|
144
|
+
const b = bindings.get(symbol(e.expression)), fn = b && functionOf(b);
|
|
145
|
+
if (!fn || locals.has(symbol(e.expression)))
|
|
146
|
+
refuse(e, 'standalone: call is not a closed helper');
|
|
147
|
+
if (e.arguments.length !== fn.parameters.length)
|
|
148
|
+
refuse(e, 'standalone: helper arguments must match its parameters');
|
|
149
|
+
for (const x of e.arguments)
|
|
150
|
+
expression(x, locals);
|
|
151
|
+
functionBody(fn);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (ts.isPrefixUnaryExpression(e) && [ts.SyntaxKind.PlusToken, ts.SyntaxKind.MinusToken, ts.SyntaxKind.ExclamationToken].includes(e.operator)) {
|
|
155
|
+
expression(e.operand, locals);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (ts.isBinaryExpression(e) && [ts.SyntaxKind.PlusToken, ts.SyntaxKind.MinusToken, ts.SyntaxKind.AsteriskToken, ts.SyntaxKind.PercentToken, ts.SyntaxKind.SlashToken, ts.SyntaxKind.LessThanToken, ts.SyntaxKind.LessThanEqualsToken, ts.SyntaxKind.GreaterThanToken, ts.SyntaxKind.GreaterThanEqualsToken, ts.SyntaxKind.EqualsEqualsToken, ts.SyntaxKind.EqualsEqualsEqualsToken, ts.SyntaxKind.ExclamationEqualsToken, ts.SyntaxKind.ExclamationEqualsEqualsToken, ts.SyntaxKind.AmpersandAmpersandToken, ts.SyntaxKind.BarBarToken, ts.SyntaxKind.QuestionQuestionToken].includes(e.operatorToken.kind)) {
|
|
159
|
+
expression(e.left, locals);
|
|
160
|
+
expression(e.right, locals);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (ts.isConditionalExpression(e)) {
|
|
164
|
+
expression(e.condition, locals);
|
|
165
|
+
expression(e.whenTrue, locals);
|
|
166
|
+
expression(e.whenFalse, locals);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (ts.isNonNullExpression(e)) {
|
|
170
|
+
expression(e.expression, locals);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
refuse(e, 'standalone: effects or unsupported expression');
|
|
174
|
+
};
|
|
175
|
+
const statement = (st, locals) => {
|
|
176
|
+
if (ts.isBlock(st)) {
|
|
177
|
+
st.statements.forEach(x => statement(x, locals));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (ts.isReturnStatement(st)) {
|
|
181
|
+
if (st.expression)
|
|
182
|
+
expression(st.expression, locals);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (ts.isVariableStatement(st) && (st.declarationList.flags & ts.NodeFlags.Const)) {
|
|
186
|
+
for (const d of st.declarationList.declarations) {
|
|
187
|
+
if (!d.initializer)
|
|
188
|
+
refuse(d, 'standalone: local const needs an initializer');
|
|
189
|
+
expression(d.initializer, locals);
|
|
190
|
+
bindLocals(d.name, locals);
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (ts.isIfStatement(st)) {
|
|
195
|
+
expression(st.expression, locals);
|
|
196
|
+
statement(st.thenStatement, locals);
|
|
197
|
+
if (st.elseStatement)
|
|
198
|
+
statement(st.elseStatement, locals);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (ts.isSwitchStatement(st)) {
|
|
202
|
+
expression(st.expression, locals);
|
|
203
|
+
for (const c of st.caseBlock.clauses) {
|
|
204
|
+
if (ts.isCaseClause(c))
|
|
205
|
+
expression(c.expression, locals);
|
|
206
|
+
c.statements.forEach(x => statement(x, locals));
|
|
207
|
+
}
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if ((ts.isBreakStatement(st) && !st.label) || ts.isEmptyStatement(st))
|
|
211
|
+
return;
|
|
212
|
+
refuse(st, 'standalone: effects or unsupported statement');
|
|
213
|
+
};
|
|
214
|
+
const bindLocals = (name, locals, role = 'local') => {
|
|
215
|
+
if (ts.isObjectBindingPattern(name)) {
|
|
216
|
+
for (const el of name.elements) {
|
|
217
|
+
if (el.dotDotDotToken || el.initializer || (el.propertyName && !ts.isIdentifier(el.propertyName)))
|
|
218
|
+
refuse(el, 'standalone: unsupported destructuring');
|
|
219
|
+
if (el.propertyName && ['__proto__', 'prototype', 'constructor'].includes(el.propertyName.getText()))
|
|
220
|
+
refuse(el, 'standalone: unsafe property');
|
|
221
|
+
bindLocals(el.name, locals, role);
|
|
222
|
+
}
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (!ts.isIdentifier(name))
|
|
226
|
+
refuse(name, 'standalone: only identifier or record bindings are supported');
|
|
227
|
+
const id = name;
|
|
228
|
+
if (reserved(id.text))
|
|
229
|
+
refuse(id, `standalone: reserved ${role} ${id.text}`);
|
|
230
|
+
const sym = symbol(id);
|
|
231
|
+
if (locals.has(sym))
|
|
232
|
+
refuse(id, 'standalone: duplicate local binding');
|
|
233
|
+
locals.add(sym);
|
|
234
|
+
};
|
|
235
|
+
const functionBody = (fn) => {
|
|
236
|
+
if (active.has(fn))
|
|
237
|
+
refuse(fn, 'standalone: recursion is outside the subset');
|
|
238
|
+
if (done.has(fn))
|
|
239
|
+
return;
|
|
240
|
+
if (!fn.body || fn.asteriskToken || fn.modifiers?.some(m => m.kind === ts.SyntaxKind.AsyncKeyword))
|
|
241
|
+
refuse(fn, 'standalone: async, generator or missing body');
|
|
242
|
+
if ((ts.isFunctionDeclaration(fn) || ts.isFunctionExpression(fn)) && fn.name && reserved(fn.name.text))
|
|
243
|
+
refuse(fn.name, 'standalone: reserved function name');
|
|
244
|
+
const locals = new Set();
|
|
245
|
+
for (const p of fn.parameters) {
|
|
246
|
+
if (p.dotDotDotToken || p.initializer)
|
|
247
|
+
refuse(p, 'standalone: rest and defaults are outside the subset');
|
|
248
|
+
if (ts.isObjectBindingPattern(p.name) && p.name.elements.some(el => !!el.propertyName || !ts.isIdentifier(el.name)))
|
|
249
|
+
refuse(p, 'standalone: record parameters need unrenamed flat bindings');
|
|
250
|
+
bindLocals(p.name, locals, 'parameter');
|
|
251
|
+
}
|
|
252
|
+
active.add(fn);
|
|
253
|
+
if (ts.isBlock(fn.body))
|
|
254
|
+
statement(fn.body, locals);
|
|
255
|
+
else
|
|
256
|
+
expression(fn.body, locals);
|
|
257
|
+
active.delete(fn);
|
|
258
|
+
done.add(fn);
|
|
259
|
+
const b = [...bindings.values()].find(x => functionOf(x) === fn);
|
|
260
|
+
if (b)
|
|
261
|
+
reached.add(b.decl.name.getText());
|
|
262
|
+
};
|
|
263
|
+
functionBody(entry);
|
|
264
|
+
return [...reached].sort();
|
|
265
|
+
}
|
package/dist/core/extract.d.ts
CHANGED