Repository navigation
Expand file tree
/
Copy pathanalysis.js
More file actions
962 lines (937 loc) · 48.8 KB
/
Copy pathanalysis.js
File metadata and controls
962 lines (937 loc) · 48.8 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZyoraLabs. Runtime Library Exception applies — see LICENSE.
import { NiralScriptError, sourceLocation } from "./errors.js";
import { ExpressionParser, patternBindings, patternExpressions } from "./expressions.js";
import { checkTypes } from "./types.js";
const EMPTY_CHILDREN = Object.freeze([]);
const CALLBACK_METHODS = new Set(["map", "filter", "find", "findIndex", "some", "every", "forEach", "reduce"]);
// Hoisted so the hot traversals test membership without allocating a literal per node.
const FUNCTION_LIKE = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression", "MethodDefinition"]);
const SCOPE_LIKE = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression", "ClassDeclaration", "ClassExpression", "MethodDefinition"]);
const INVOCATIONS = new Set(["CallExpression", "NewExpression"]);
const EFFECT_NODES = new Set(["CallExpression", "NewExpression", "MemberExpression", "AssignmentStatement"]);
const RECEIVERS = new Set(["ThisExpression", "Super"]);
// Leaf identifier/member targets written by an assignment, flattening destructuring patterns.
export function assignmentTargets(node) {
if (node.type === "ArrayPattern") return node.elements.filter(Boolean).flatMap(assignmentTargets);
if (node.type === "ObjectPattern") return node.properties.flatMap(property => assignmentTargets(property.value));
if (node.type === "RestElement") return assignmentTargets(node.argument);
return [node];
}
// Class and member decorators both evaluate in the scope that encloses the class.
export function decoratorExpressions(node) {
const own = node.decorators ?? EMPTY_CHILDREN;
const members = node.members.flatMap(member => member.decorators ?? EMPTY_CHILDREN);
return own.length || members.length ? [...own, ...members].map(decorator => decorator.expression) : EMPTY_CHILDREN;
}
export function analyzeNiralScript(program, { globals = [], moduleGlobals = globals, reserved = [], pureGlobals = [], pureMethods = {}, strictTypes = false, topLevelAwait = false, taintSources = [], taintExportedParams = false } = {}) {
const bindings = [];
const references = [];
const resolved = new WeakMap();
const initializedPatternReads = new WeakSet();
const declarations = new WeakMap();
const assignments = [];
const calls = [];
const arrows = [];
const loops = [];
const catches = [];
const throws = [];
const defaultSources = [];
const ambient = new Map([...globals].map(name => [name, { name, kind: "global" }]));
const root = { parent: { bindings: ambient }, bindings: new Map(), depth: 0, async: topLevelAwait };
let awaited = false;
const reservedNames = new Set(reserved);
const modules = new Map();
const imports = new Map();
const classes = [];
const classContexts = new WeakMap();
const receiverSources = new Map();
function fail(code, message, node) {
throw new NiralScriptError(code, message, sourceLocation(program, node));
}
function declare(pattern, kind, scope, end = pattern.end, declaration = null) {
for (const { id, shorthand, path } of patternBindings(pattern)) declareName(id, kind, scope, end, declaration, shorthand, path);
}
// A module exports a name when it declares it with `export`, or re-exports it from
// another module; re-exports are followed to the binding that holds the real value.
function resolveExport(target, name) {
const binding = target.bindings.get(name);
if (!binding || !declarations.get(binding)?.exported || binding.scope !== target) return null;
return binding.reexport ?? binding;
}
function declareName(id, kind, scope, end, declaration, shorthand, path) {
if (scope.bindings.has(id.name) || (scope === root && reservedNames.has(id.name))) {
fail("NRS073", `Duplicate binding '${id.name}'`, id);
}
const binding = { name: id.name, kind, id, shorthand, path, scope, end, index: bindings.length };
bindings.push(binding);
scope.bindings.set(id.name, binding);
if (declaration) declarations.set(binding, declaration);
}
function reference(node, scope, deferred, shorthand = false) {
const token = node.raw === undefined ? node : new ExpressionParser(program.source, program.filename, node.start, node.end).take();
let binding;
for (let current = scope; current && !binding; current = current.parent) binding = current.bindings.get(node.name);
if (!binding) fail("NRS073", `Unresolved name '${node.name}'`, token);
if (scope.pendingParameters?.has(binding)) fail("NRS074", `Cannot read '${node.name}' before parameter initialization`, token);
if (!deferred && binding.scope?.depth === scope.depth && !scope.initializedPatterns?.has(binding) && binding.kind !== "function" && binding.kind !== "parameter" && token.start < binding.end) {
fail("NRS074", `Cannot read '${node.name}' before initialization`, token);
}
references.push({ node: token, binding, shorthand, localName: node.name });
if (scope.initializedPatterns?.has(binding)) initializedPatternReads.add(node);
resolved.set(node, binding);
return binding;
}
function expression(node, scope, deferred, shorthand = false) {
if (!node) return;
if (node.type === "ArrowFunctionExpression") {
arrows.push(node);
const inner = { parent: scope, bindings: new Map(), depth: scope.depth + 1, owner: node.body, async: !!node.async, receiver: scope.receiver };
for (const param of node.params) declare(param, "parameter", inner);
parameterDefaults(node.params, inner);
if (node.body.type === "BlockStatement") body(node.body.body, inner);
else expression(node.body, inner, false);
return;
}
if (node.type === "AwaitExpression") {
if (!scope.async) fail("NRS072", scope.depth === 0 ? "Top-level await is not available here; move the await into an async function" : "Await is only valid inside an async function", node);
if (scope.depth === 0) awaited = true;
}
if (RECEIVERS.has(node.type)) {
if (!scope.receiver) fail("NRS072", `${node.type === "Super" ? "super" : "this"} is only valid in a class member`, node);
if (node.type === "Super" && !scope.receiver.declaration.superClass) fail("NRS072", "super requires a base class", node);
classContexts.set(node, scope.receiver);
return;
}
if (node.type === "CallExpression" && node.callee.type === "Super" && (scope.receiver?.member.kind !== "constructor" || scope.receiver.member.static)) fail("NRS072", "super() is only valid in a derived constructor", node);
if (node.type === "FunctionExpression") {
// optional name is scoped inside the function expression only, so it can recurse
const outer = { parent: scope, bindings: new Map(), depth: scope.depth, owner: scope.owner, async: scope.async, receiver: scope.receiver };
if (node.id) declareName(node.id, "function", outer, node.end, node);
block(node.body, outer, true, node.params, !!node.async);
return;
}
if (node.type === "ClassExpression") {
// optional name is scoped inside the class expression only
const inner = { parent: scope, bindings: new Map(), depth: scope.depth, owner: scope.owner, async: scope.async, receiver: scope.receiver };
if (node.id) declareName(node.id, "class", inner, node.end, node);
classes.push(node);
expression(node.superClass, scope, false);
for (const member of node.members) {
const receiver = { declaration: node, member };
classContexts.set(member, receiver);
if (member.type === "StaticBlock") block(member.body, scope, true, [], false, receiver);
else if (member.type === "MethodDefinition") block(member.body, scope, true, member.params, member.async, receiver);
else expression(member.value, { ...scope, receiver, depth: scope.depth + 1, async: false }, !member.static);
}
return;
}
if (node.type === "MetaProperty") {
if (!scope.depth && !scope.receiver) fail("NRS072", "new.target is only valid inside a function", node);
return;
}
if (node.type === "Identifier") return reference(node, scope, deferred, shorthand);
if (node.type === "MemberExpression") {
if (!node.computed && node.property.name.startsWith("#")) {
if (node.object.type === "Super") fail("NRS072", "super cannot access private members", node);
const declaration = scope.receiver?.declaration;
if (!declaration?.members.some(member => member.key?.name === node.property.name)) fail("NRS073", "Private member is not declared in this class", node.property);
}
expression(node.object, scope, deferred);
if (node.computed) expression(node.property, scope, deferred);
} else if (node.type === "Property") {
expression(node.value, scope, deferred, node.shorthand);
} else {
for (const child of children(node)) expression(child, scope, deferred);
}
}
function body(nodes, scope) {
for (const node of nodes) {
if (node.id && !["ForOfStatement", "ForInStatement", "TypeAliasDeclaration", "InterfaceDeclaration"].includes(node.type)) declare(node.id, node.kind ?? ({ InputDeclaration: "input", StateDeclaration: "state", DerivedDeclaration: "derive", FunctionDeclaration: "function", ClassDeclaration: "class" })[node.type], scope, node.end, node);
}
for (const node of nodes) {
if (node.type === "ExportNamedDeclaration" && node.source) {
const target = modules.get(node.target);
if (!target) fail("NRS078", "Module re-exports must be linked before analysis", node);
for (const specifier of node.specifiers) {
const source = resolveExport(target, specifier.imported.name);
if (!source) fail("NRS078", `Module does not export '${specifier.imported.name}'`, specifier.imported);
if (scope.bindings.has(specifier.local.name) || (scope === root && reservedNames.has(specifier.local.name))) fail("NRS073", `Duplicate binding '${specifier.local.name}'`, specifier.local);
const binding = { name: specifier.local.name, kind: "const", id: specifier.local, path: [], scope, end: specifier.local.end, index: bindings.length, reexport: source };
bindings.push(binding);
scope.bindings.set(specifier.local.name, binding);
declarations.set(binding, { exported: true });
imports.set(specifier, { binding: source, scope });
}
continue;
}
if (node.type !== "ImportDeclaration" || node.typeOnly) continue;
const target = modules.get(node.target);
if (!target) fail("NRS078", "Module imports must be linked before analysis", node);
for (const specifier of node.specifiers) {
const binding = resolveExport(target, specifier.imported.name);
if (!binding) fail("NRS078", `Module does not export '${specifier.imported.name}'`, specifier.imported);
if (scope.bindings.has(specifier.local.name) || (scope === root && reservedNames.has(specifier.local.name))) fail("NRS073", `Duplicate binding '${specifier.local.name}'`, specifier.local);
scope.bindings.set(specifier.local.name, binding);
imports.set(specifier, { binding, scope });
}
}
for (const node of nodes) statement(node, scope);
}
function block(node, scope, functionScope = false, params = [], async = false, receiver = functionScope ? null : scope.receiver) {
const inner = { parent: scope, bindings: new Map(), depth: scope.depth + Number(functionScope), owner: functionScope ? node : scope.owner, async: functionScope ? async : scope.async, receiver };
for (const param of params) declare(param, "parameter", inner);
parameterDefaults(params, inner);
body(node.body, inner);
}
function parameterDefaults(params, scope) {
scope.pendingParameters = new Set(params.flatMap(patternBindings).map(({ id }) => scope.bindings.get(id.name)));
scope.initializedPatterns = new Set();
function initialize(pattern) {
if (!pattern) return;
if (pattern.type === "AssignmentPattern") {
expression(pattern.right, scope, false);
defaultSources.push([pattern.left, pattern.right]);
initialize(pattern.left);
} else if (pattern.type === "RestElement") initialize(pattern.argument);
else if (pattern.type === "Identifier") {
const binding = scope.bindings.get(pattern.name);
scope.pendingParameters.delete(binding);
scope.initializedPatterns.add(binding);
} else for (const item of pattern.elements ?? pattern.properties) initialize(item?.value ?? item);
}
params.forEach(initialize);
scope.pendingParameters = null;
scope.initializedPatterns = null;
}
function statement(node, scope) {
switch (node.type) {
case "InputDeclaration":
if (scope !== root) fail("NRS072", "Inputs are only valid at component top level", node);
break;
case "ImportDeclaration":
case "ExportNamedDeclaration":
case "TypeAliasDeclaration":
case "InterfaceDeclaration":
case "BreakStatement":
case "ContinueStatement":
break;
case "StateDeclaration":
case "DerivedDeclaration":
case "VariableDeclaration":
expression(node.init, scope, node.type === "DerivedDeclaration");
parameterDefaults([node.id], scope);
break;
case "FunctionDeclaration":
block(node.body, scope, true, node.params, !!node.async);
break;
case "ClassDeclaration":
case "ClassExpression": {
classes.push(node);
for (const decorator of decoratorExpressions(node)) expression(decorator, scope, false);
expression(node.superClass, scope, false);
for (const member of node.members) {
const receiver = { declaration: node, member };
classContexts.set(member, receiver);
if (member.type === "StaticBlock") block(member.body, scope, true, [], false, receiver);
else if (member.type === "MethodDefinition") block(member.body, scope, true, member.params, member.async, receiver);
else expression(member.value, { ...scope, receiver, depth: scope.depth + 1, async: false }, !member.static);
if (member.kind === "constructor") {
function construction(node, states, nested = false) {
if (!node || !states.size) return states;
if (["FunctionDeclaration", "FunctionExpression", "ClassDeclaration", "ClassExpression"].includes(node.type)) return states;
if (node.type === "ArrowFunctionExpression") { construction(node.body, states, true); return states; }
if ((node.type === "ThisExpression" || node.type === "MemberExpression" && node.object.type === "Super") && states.has(false)) fail("NRS074", "Cannot use this or a super member before super()", node);
if (node.type === "ReturnStatement" && node.argument && node.argument.type !== "ThisExpression") fail("NRS072", "Class constructors initialize this; return replacement objects from a factory method", node);
if (node.type === "CallExpression" && node.callee.type === "Super") {
if (nested || states.has(true)) fail("NRS074", "super() must initialize a derived constructor exactly once on each path", node);
for (const argument of node.arguments) states = construction(argument, states, nested);
if (states.has(true)) fail("NRS074", "super() must initialize a derived constructor exactly once on each path", node);
return new Set([true]);
}
if (["IfStatement", "ConditionalExpression"].includes(node.type)) {
states = construction(node.test, states, nested);
return new Set([...construction(node.consequent, states, nested), ...construction(node.alternate, states, nested)]);
}
if (node.type === "LogicalExpression") {
states = construction(node.left, states, nested);
return new Set([...states, ...construction(node.right, states, nested)]);
}
if (["ForStatement", "ForOfStatement", "ForInStatement", "WhileStatement", "DoWhileStatement", "TryStatement"].includes(node.type)) {
for (const child of children(node)) construction(child, states, true);
return states;
}
if (node.type === "ReturnStatement" && states.has(false)) fail("NRS074", "Derived constructors must call super() before returning", node);
for (const child of children(node)) states = construction(child, states, nested);
return ["ReturnStatement", "ThrowStatement"].includes(node.type) ? new Set() : states;
}
for (const value of member.params.flatMap(patternExpressions)) construction(value, new Set([!node.superClass]), true);
if (construction(member.body, new Set([!node.superClass])).has(false)) fail("NRS074", "Derived constructors must call super() on every path", member);
}
}
break;
}
case "BlockStatement":
block(node, scope);
break;
case "ForOfStatement":
case "ForInStatement": {
loops.push(node);
const inner = { parent: scope, bindings: new Map(), depth: scope.depth, owner: scope.owner, async: scope.async, receiver: scope.receiver };
declare(node.id, node.kind, inner, node.right.end, node);
expression(node.right, inner, false);
parameterDefaults([node.id], inner);
block(node.body, inner);
break;
}
case "ForStatement": {
const inner = { parent: scope, bindings: new Map(), depth: scope.depth, owner: scope.owner, async: scope.async, receiver: scope.receiver };
if (node.init?.type === "VariableDeclaration") declare(node.init.id, node.init.kind, inner, node.init.end, node.init);
if (node.init) statement(node.init, inner);
expression(node.test, inner, false);
if (node.update) statement(node.update, inner);
block(node.body, inner);
break;
}
case "WhileStatement":
case "DoWhileStatement":
expression(node.test, scope, false);
block(node.body, scope);
break;
case "LabeledStatement":
statement(node.body, scope);
break;
case "IfStatement":
expression(node.test, scope, false);
statement(node.consequent, scope);
if (node.alternate) statement(node.alternate, scope);
break;
case "TryStatement":
statement(node.block, scope);
if (node.handler) statement(node.handler, scope);
if (node.finalizer) statement(node.finalizer, scope);
break;
case "CatchClause":
catches.push(node);
block(node.body, scope, false, node.param ? [node.param] : []);
break;
case "ThrowStatement":
throws.push(node);
expression(node.argument, scope, false);
break;
case "ReturnStatement":
if (scope.receiver?.member.type === "StaticBlock" && scope.owner === scope.receiver.member.body) fail("NRS072", "Static blocks cannot return", node);
expression(node.argument, scope, false);
break;
case "ExpressionStatement":
expression(node.expression, scope, false);
break;
case "AssignmentStatement": {
assignments.push(node);
expression(node.left, scope, false);
const destructured = ["ArrayPattern", "ObjectPattern"].includes(node.left.type);
for (const leaf of assignmentTargets(node.left)) {
let target = leaf;
while (target.type === "MemberExpression" || target.type === "ParenthesizedExpression") target = target.object ?? target.expression;
if (target.type !== "Identifier") continue;
const binding = references.findLast(entry => entry.node.start === target.start)?.binding;
if (!binding) continue;
const direct = leaf.type === "Identifier";
if (binding.kind === "derive" || (direct && !["state", "let", "parameter"].includes(binding.kind))) {
fail("NRS074", `Cannot assign to read-only binding '${target.name}'`, target);
}
if (destructured && direct && binding.kind === "state") fail("NRS074", `Reactive state '${target.name}' cannot be a destructuring target`, target);
}
expression(node.right, scope, false);
break;
}
default:
throw new TypeError(`Unsupported NiralScript declaration '${node.type}'`);
}
}
for (const module of program.modules ?? []) {
const scope = { parent: { bindings: new Map([...moduleGlobals].map(name => [name, { name, kind: "global" }])) }, bindings: new Map(), depth: 0, module: true, async: topLevelAwait };
body(module.body, scope);
modules.set(module.filename, scope);
}
body(program.body, root);
function children(node) {
switch (node.type) {
case "Identifier":
case "ThisExpression":
case "Super":
case "MetaProperty":
case "Literal":
case "TemplateElement":
case "ImportDeclaration":
case "ExportNamedDeclaration":
case "TypeAliasDeclaration":
case "InterfaceDeclaration":
case "InputDeclaration":
case "BreakStatement":
case "ContinueStatement":
return EMPTY_CHILDREN;
case "Program":
return node.modules ? [...node.modules, ...node.body] : node.body;
case "BlockStatement":
return node.body;
case "FunctionDeclaration":
case "FunctionExpression":
case "MethodDefinition":
case "ArrowFunctionExpression":
return [...node.params.flatMap(patternExpressions), node.body];
case "ClassDeclaration":
case "ClassExpression": return [node.superClass, ...decoratorExpressions(node), ...node.members].filter(Boolean);
case "StaticBlock": return [node.body];
case "PropertyDefinition": return node.value ? [node.value] : EMPTY_CHILDREN;
case "StateDeclaration":
case "DerivedDeclaration":
case "VariableDeclaration":
return [node.init, ...patternExpressions(node.id)];
case "IfStatement":
return node.alternate ? [node.test, node.consequent, node.alternate] : [node.test, node.consequent];
case "TryStatement":
return [node.block, node.handler, node.finalizer].filter(Boolean);
case "CatchClause":
return [...patternExpressions(node.param), node.body];
case "ThrowStatement":
case "ReturnStatement":
return node.argument ? [node.argument] : EMPTY_CHILDREN;
case "ExpressionStatement":
case "ParenthesizedExpression":
return [node.expression];
case "AssignmentStatement":
case "BinaryExpression":
case "LogicalExpression":
return [node.left, node.right];
case "ConditionalExpression":
return [node.test, node.consequent, node.alternate];
case "UnaryExpression":
case "AwaitExpression":
case "ContractExpression":
case "SpreadElement":
return [node.argument];
case "CastExpression":
return [node.expression];
case "ForOfStatement":
case "ForInStatement":
return [node.right, ...patternExpressions(node.id), node.body];
case "ForStatement":
return [node.init, node.test, node.body, node.update].filter(Boolean);
case "WhileStatement":
case "DoWhileStatement":
return [node.test, node.body];
case "LabeledStatement":
return [node.body];
case "MemberExpression":
return node.computed ? [node.object, node.property] : [node.object];
case "CallExpression":
case "NewExpression":
return [node.callee, ...node.arguments];
case "ArrayExpression":
case "ArrayPattern":
return node.elements.filter(Boolean);
case "ObjectExpression":
case "ObjectPattern":
return node.properties;
case "RestElement":
return [node.argument];
case "Property":
return [node.value];
case "TemplateLiteral":
return node.expressions;
default:
throw new TypeError(`Unsupported NiralScript node '${node.type}'`);
}
}
function unwrap(node) {
while (node?.type === "ParenthesizedExpression") node = node.expression;
return node;
}
function callable(node, seen = new Set()) {
node = unwrap(node);
if (node?.type === "Super") return constructorOf(baseOf(classContexts.get(node)?.declaration));
if (node?.type === "MemberExpression") {
const member = memberOf(node, seen);
return member?.type === "MethodDefinition" ? member : member?.value?.type === "ArrowFunctionExpression" ? member.value : null;
}
if (node?.type === "ArrowFunctionExpression") return node;
if (node?.type !== "Identifier") return null;
let binding = resolved.get(node);
while (binding?.reexport) binding = binding.reexport;
if (!binding || seen.has(binding)) return null;
seen.add(binding);
const declaration = declarations.get(binding);
if (binding.kind === "function") return declaration;
if (binding.kind === "const" && !binding.path?.length) return callable(declaration.init, seen);
if (binding.kind === "parameter" && !binding.path.length) {
const values = sources.get(binding) ?? [];
const targets = values.map(value => callable(value, new Set(seen)));
return targets.length && targets.every(target => target && target === targets[0]) ? targets[0] : null;
}
return null;
}
function collectCalls(node, parent) {
if (node.type === "Super" && !(parent?.type === "MemberExpression" && parent.object === node || parent?.type === "CallExpression" && parent.callee === node)) fail("NRS072", "super requires a member access or constructor call", node);
if (INVOCATIONS.has(node.type)) calls.push(node);
for (const child of children(node)) collectCalls(child, node);
}
collectCalls(program);
const sources = new Map();
const returns = new Map();
function classOf(node, seen = new Set()) {
node = unwrap(node);
if (!node || seen.has(node)) return null;
seen.add(node);
if (node.type === "ThisExpression") return classContexts.get(node)?.declaration;
if (node.type === "Super") return baseOf(classContexts.get(node)?.declaration);
if (node.type === "NewExpression") return classOf(node.callee, seen);
if (node.type === "CallExpression") {
const fn = callable(node.callee, new Set(seen));
if (fn) {
const candidates = returnValues(fn).map(value => classOf(value, new Set(seen)));
return candidates.length && candidates.every(value => value === candidates[0]) ? candidates[0] : null;
}
}
if (node.type === "Identifier") {
const binding = resolved.get(node);
const declaration = declarations.get(binding);
if (declaration?.type === "ClassDeclaration" || declaration?.type === "ClassExpression") return declaration;
const candidates = (sources.get(binding) ?? []).map(value => classOf(value, new Set(seen)));
return candidates.length && candidates.every(value => value === candidates[0]) ? candidates[0] : null;
}
return null;
}
function baseOf(declaration) { return declaration?.superClass ? classOf(declaration.superClass) : null; }
function constructorOf(declaration) { return declaration?.members.find(member => member.kind === "constructor") ?? (declaration ? constructorOf(baseOf(declaration)) : null); }
function isStaticReceiver(node) {
node = unwrap(node);
if (RECEIVERS.has(node?.type)) return !!classContexts.get(node)?.member.static;
return node?.type === "Identifier" && resolved.get(node)?.kind === "class";
}
function memberOf(node, resolving = new Set()) {
const key = node.computed ? node.property?.value : node.property?.name;
let declaration = classOf(node.object, resolving);
const isStatic = isStaticReceiver(node.object);
const seen = new Set();
while (declaration && !seen.has(declaration)) {
seen.add(declaration);
const member = declaration.members.find(member => member.static === isStatic && member.key?.name === String(key));
if (member) return member;
declaration = baseOf(declaration);
}
return null;
}
function retainedValues(declaration) {
const values = [];
for (let current = declaration; current; current = baseOf(current)) {
for (const member of current.members) if (member.type === "PropertyDefinition" && !member.static && member.value) values.push(member.value);
for (const assignment of assignments) if (assignment.left.type === "MemberExpression" && assignment.left.object.type === "ThisExpression" && classContexts.get(assignment.left.object)?.declaration === current) values.push(assignment.right);
}
return values;
}
function accessorOf(node, kind) {
const member = memberOf(node);
if (!member || !["get", "set"].includes(member.kind)) return null;
return classContexts.get(member).declaration.members.find(candidate => candidate.static === member.static && candidate.key?.name === member.key.name && candidate.kind === kind);
}
function collectAccessors(node, parent) {
if (node.type === "MemberExpression") {
const assignment = parent?.type === "AssignmentStatement" && parent.left === node;
const kind = assignment ? "set" : "get";
const fn = accessorOf(node, kind);
if (fn) calls.push({ type: "CallExpression", callee: node, arguments: assignment ? [parent.right] : [], accessor: fn });
}
for (const child of children(node)) collectAccessors(child, node);
}
const sourceNodes = new Map();
let sourceCount = 0;
function addSource(binding, value) {
if (!binding || !value) return;
if (!sources.has(binding)) sources.set(binding, []);
if (!sourceNodes.has(binding)) sourceNodes.set(binding, new Set());
if (sourceNodes.get(binding).has(value)) return;
sourceNodes.get(binding).add(value);
sourceCount++;
for (const key of binding.path) value = { type: "MemberExpression", object: value, property: { type: "Literal", value: key }, computed: true };
sources.get(binding).push(value);
}
for (const binding of bindings) addSource(binding, declarations.get(binding)?.init);
for (const assignment of assignments) {
if (assignment.left.type === "Identifier") addSource(resolved.get(assignment.left), assignment.right);
}
function parameterSource(pattern, value) {
if (pattern) for (const { id } of patternBindings(pattern)) addSource(bindings.find(binding => binding.id === id), value);
}
for (const [pattern, value] of defaultSources) parameterSource(pattern, value);
collectAccessors(program);
const argumentSources = new Map(calls.map(call => [call, call.arguments.flatMap(argument => argument.type === "SpreadElement" ? argument.argument.type === "ArrayExpression" ? argument.argument.elements : [{ type: "MemberExpression", object: argument.argument, computed: false }] : [argument])]));
for (const loop of loops) parameterSource(loop.id, { type: "MemberExpression", object: loop.right, computed: false });
for (const handler of catches) for (const thrown of throws) parameterSource(handler.param, thrown.argument);
const callbackValues = new Map();
for (const call of calls) {
const callee = unwrap(call.callee);
if (callee.type === "MemberExpression" && !callee.computed && CALLBACK_METHODS.has(callee.property.name)) {
const element = { type: "MemberExpression", object: callee.object, computed: false };
const indexValue = { type: "Literal", value: 0 };
callbackValues.set(call, callee.property.name === "reduce" ? [call.arguments[1] ?? element, element, indexValue, callee.object] : [element, indexValue, callee.object]);
}
}
let previousCount = -1;
while (previousCount !== sourceCount) {
previousCount = sourceCount;
for (const call of calls) {
const fn = call.accessor ?? (call.type === "NewExpression" ? constructorOf(classOf(call.callee)) : callable(call.callee));
if (fn && (call.type === "NewExpression" || call.callee.type === "MemberExpression")) {
if (!receiverSources.has(fn)) receiverSources.set(fn, new Set());
receiverSources.get(fn).add(call.type === "NewExpression" ? call : call.callee.object);
}
if (fn) fn.params.forEach((param, index) => {
const args = argumentSources.get(call);
for (const value of patternExpressions(param)) parameterSource(param, value);
const values = call.arguments.some(argument => argument.type === "SpreadElement") || param.type === "RestElement" ? args : [args[index]];
for (const value of values) parameterSource(param, value);
});
const callback = callbackValues.has(call) ? callable(call.arguments[0]) : null;
if (callback) {
callback.params.forEach((param, index) => parameterSource(param, callbackValues.get(call)[index]));
if (unwrap(call.callee).property.name === "reduce") for (const value of returnValues(callback)) parameterSource(callback.params[0], value);
}
}
}
for (const call of calls) {
if (resolved.get(unwrap(call.callee))?.kind === "parameter" && !callable(call.callee)) fail("NRS075", "Invoked callback parameters require one statically known function", call.callee);
}
// A class decorator may replace the class, so the binding only holds the decorated value
// after the declaration finishes. Static initializers run before that and would hit the TDZ.
for (const node of classes) {
if (!node.decorators?.length) continue;
const selfReference = child => {
if (!child || FUNCTION_LIKE.has(child.type)) return;
if (child.type === "Identifier" && declarations.get(resolved.get(child)) === node) {
fail("NRS074", `A decorated class cannot be referenced by its own static initializers`, child);
}
for (const nested of children(child)) selfReference(nested);
};
for (const member of node.members) {
if (!member.static) continue;
selfReference(member.type === "StaticBlock" ? member.body : member.value);
}
}
function returnValues(fn) {
if (!returns.has(fn)) {
if (fn.type === "ArrowFunctionExpression" && fn.body.type !== "BlockStatement") {
returns.set(fn, [fn.body]);
return returns.get(fn);
}
const values = [];
function collect(node) {
if (SCOPE_LIKE.has(node.type)) return;
if (node.type === "ReturnStatement") {
if (node.argument) values.push(node.argument);
return;
}
for (const child of children(node)) collect(child);
}
collect(fn.body);
returns.set(fn, values);
}
return returns.get(fn);
}
function readOnlyOrigin(node, seen = new Set()) {
node = unwrap(node);
if (!node) return null;
if (seen.has(node)) return null;
seen.add(node);
if (node.type === "ThisExpression") {
for (const receiver of receiverSources.get(classContexts.get(node)?.member) ?? []) {
const origin = readOnlyOrigin(receiver, new Set(seen));
if (origin) return origin;
}
}
if (node.type === "NewExpression") {
for (const value of [...node.arguments, ...retainedValues(classOf(node.callee))]) {
const origin = readOnlyOrigin(value, new Set(seen));
if (origin) return origin;
}
}
if (node.type === "Identifier") {
const binding = resolved.get(node);
if (!binding || seen.has(binding)) return null;
if (binding.kind === "derive" || binding.kind === "input" || binding.kind === "global") return binding;
seen.add(binding);
for (const source of sources.get(binding) ?? []) {
const origin = readOnlyOrigin(source, new Set(seen));
if (origin) return origin;
}
} else if (node.type === "CallExpression") {
const callee = unwrap(node.callee);
if (callee.type === "MemberExpression" && !callee.computed) {
if (["slice", "concat", "filter", "find", "at"].includes(callee.property.name)) {
const origin = readOnlyOrigin(callee.object, new Set(seen));
if (origin) return origin;
if (callee.property.name === "concat") for (const value of node.arguments) {
const origin = readOnlyOrigin(value, new Set(seen));
if (origin) return origin;
}
}
if (["map", "reduce"].includes(callee.property.name)) {
const callback = callable(node.arguments[0]);
if (callback && !seen.has(callback)) {
seen.add(callback);
for (const value of returnValues(callback)) {
const origin = readOnlyOrigin(value, new Set(seen));
if (origin) return origin;
}
}
if (callee.property.name === "reduce") return readOnlyOrigin(node.arguments[1], seen);
}
}
const fn = callable(node.callee);
if (fn && !seen.has(fn)) {
seen.add(fn);
for (const value of returnValues(fn)) {
const origin = readOnlyOrigin(value, new Set(seen));
if (origin) return origin;
}
}
} else if (node.type === "MemberExpression") {
const getter = accessorOf(node, "get");
if (getter && !seen.has(getter)) {
seen.add(getter);
for (const value of returnValues(getter)) {
const origin = readOnlyOrigin(value, new Set(seen));
if (origin) return origin;
}
}
return readOnlyOrigin(node.object, seen);
}
else if (["ConditionalExpression", "LogicalExpression", "ArrayExpression", "ObjectExpression", "Property", "SpreadElement", "AwaitExpression"].includes(node.type)) {
for (const child of children(node)) {
const origin = readOnlyOrigin(child, new Set(seen));
if (origin) return origin;
}
}
return null;
}
for (const assignment of assignments) {
if (assignment.left.type !== "MemberExpression") continue;
if (assignment.left.object.type === "ThisExpression" && classContexts.get(assignment.left.object)?.member.kind === "constructor") continue;
const origin = readOnlyOrigin(assignment.left);
if (origin) fail("NRS074", `Cannot mutate an alias of read-only binding '${origin.name}'`, assignment.left);
}
function literalKind(node, seen = new Set()) {
node = unwrap(node);
if (!node) return null;
if (node.type === "CallExpression") {
const callee = unwrap(node.callee);
if (callee.type === "MemberExpression" && !callee.computed && ["map", "filter", "slice", "concat"].includes(callee.property.name) && literalKind(callee.object, seen) === "array") return "array";
}
if (node.type === "ArrayExpression") return "array";
if (node.type === "TemplateLiteral" || (node.type === "Literal" && typeof node.value === "string")) return "string";
if (node.type !== "Identifier") return null;
const binding = resolved.get(node);
if (!binding || seen.has(binding)) return null;
seen.add(binding);
const values = sources.get(binding) ?? [];
const kinds = values.map(value => literalKind(value, new Set(seen)));
return kinds.length && kinds.every(kind => kind === kinds[0]) ? kinds[0] : null;
}
const pure = new Set(pureGlobals);
const methods = Object.fromEntries(Object.entries(pureMethods).map(([kind, names]) => [kind, new Set(names)]));
function origins(node, seen = new Set()) {
node = unwrap(node);
if (!node) return seen;
if (node.type === "MemberExpression") return origins(node.object, seen);
if (node.type === "CallExpression") {
const fn = callable(node.callee);
if (fn && !seen.has(fn)) {
seen.add(fn);
for (const value of returnValues(fn)) origins(value, seen);
}
const callee = unwrap(node.callee);
if (callee.type === "MemberExpression" && !callee.computed && ["map", "filter", "slice", "concat", "find", "at", "reduce"].includes(callee.property.name)) {
origins(callee.object, seen);
for (const value of node.arguments) origins(value, seen);
const callback = callable(node.arguments[0]);
if (callback && !seen.has(callback)) {
seen.add(callback);
for (const value of returnValues(callback)) origins(value, seen);
}
}
return seen;
}
if (["ConditionalExpression", "LogicalExpression", "ArrayExpression", "ObjectExpression", "Property", "SpreadElement", "AwaitExpression"].includes(node.type)) {
for (const child of children(node)) origins(child, seen);
return seen;
}
const binding = resolved.get(node);
if (binding && !seen.has(binding)) {
seen.add(binding);
for (const value of sources.get(binding) ?? []) origins(value, seen);
}
return seen;
}
const modifiedObjects = assignments.filter(node => node.left.type === "MemberExpression").map(node => origins(node.left));
function intrinsic(node) {
node = unwrap(node);
if (node.type === "Identifier") {
const binding = resolved.get(node);
return binding?.kind === "global" && pure.has(binding.name);
}
if (node.type !== "MemberExpression" || node.computed) return false;
const object = unwrap(node.object);
const binding = resolved.get(object);
const aliases = origins(object);
if (modifiedObjects.some(modified => [...aliases].some(alias => modified.has(alias)))) return false;
return (binding?.kind === "global" && pure.has(`${binding.name}.${node.property.name}`)) || methods[literalKind(object)]?.has(node.property.name);
}
function invoked(call) {
if (call.type === "NewExpression") {
const declaration = classOf(call.callee);
const targets = [];
const constructor = constructorOf(declaration);
if (constructor) targets.push(constructor);
for (let current = declaration; current; current = baseOf(current)) {
for (const field of current.members.filter(member => member.type === "PropertyDefinition" && !member.static && member.value)) targets.push({ params: [], body: field.value });
}
return targets;
}
if (call.type === "MemberExpression") {
const member = accessorOf(call, "get");
return member ? [member] : [];
}
if (call.type === "AssignmentStatement" && call.left.type === "MemberExpression") {
const setter = accessorOf(call.left, "set");
return setter ? [setter] : [];
}
if (call.type === "AssignmentStatement") return [];
const direct = callable(call.callee);
if (direct) {
const targets = [direct];
if (call.callee.type === "MemberExpression" && call.callee.object.type !== "Super") {
const context = classContexts.get(direct);
if (context) for (const declaration of classes) {
for (let base = baseOf(declaration); base; base = baseOf(base)) if (base === context.declaration) {
const override = declaration.members.find(member => member.type === "MethodDefinition" && member.static === direct.static && member.key.name === direct.key.name);
if (override) targets.push(override);
}
}
}
return targets;
}
const callee = unwrap(call.callee);
if (callee.type === "MemberExpression" && !callee.computed && CALLBACK_METHODS.has(callee.property.name) && intrinsic(callee)) {
const callback = callable(call.arguments[0]);
if (!callback) fail("NRS075", "Collection callbacks must be statically known functions", call);
return [callback];
}
return [];
}
for (const call of calls) {
const callee = unwrap(call.callee);
if (callee.type !== "MemberExpression") continue;
const origin = readOnlyOrigin(callee.object);
if (["derive", "input"].includes(origin?.kind) && !intrinsic(callee) && !callable(callee)) fail("NRS074", "Cannot call an unproven method on a read-only value or its alias", callee);
}
const dependencies = new Map();
for (const binding of bindings.filter(binding => binding.kind === "derive")) {
const dependsOn = new Set();
const visited = new Set();
function inspect(node, owner = null) {
if (!node || SCOPE_LIKE.has(node.type)) return;
if (node.type === "AwaitExpression") fail("NRS075", "Derived evaluation must remain synchronous", node);
if (node.type === "ThrowStatement") fail("NRS075", "Derived evaluation cannot explicitly throw", node);
if (node.type === "Identifier") {
const target = resolved.get(node);
if (target?.kind === "derive") dependsOn.add(target);
return;
}
if (node.type === "AssignmentStatement") {
const target = resolved.get(node.left);
const construction = node.left.type === "MemberExpression" && node.left.object.type === "ThisExpression" && classContexts.get(node.left.object)?.member.kind === "constructor";
if (!construction && (node.left.type !== "Identifier" || !owner || target?.scope?.owner !== owner || !["let", "parameter"].includes(target.kind))) {
fail("NRS075", "Derived evaluation cannot mutate captured bindings or objects", node.left);
}
}
if (EFFECT_NODES.has(node.type)) {
const targets = invoked(node);
const constructed = node.type === "NewExpression" ? classOf(node.callee) : null;
if (constructed && decoratorExpressions(constructed).length) fail("NRS075", "Derived evaluation cannot construct a decorated class", node.callee);
if (INVOCATIONS.has(node.type) && !targets.length && !constructed && !intrinsic(node.callee)) fail("NRS075", "Derived evaluation requires a provably pure function call", node.callee);
for (const fn of targets) if (!visited.has(fn)) {
const host = classContexts.get(fn)?.declaration;
if (host && decoratorExpressions(host).length) fail("NRS075", "Derived evaluation cannot call a member of a decorated class", node.callee);
if (fn.async) fail("NRS075", "Derived evaluation cannot invoke async functions", node.callee);
visited.add(fn);
for (const value of fn.params.flatMap(patternExpressions)) inspect(value, fn.body);
inspect(fn.body, fn.body);
}
}
for (const child of children(node)) inspect(child, owner);
}
inspect(declarations.get(binding).init);
dependencies.set(binding, dependsOn);
}
const done = new Set();
function visit(binding, path = []) {
if (path.includes(binding)) fail("NRS076", `Derived dependency cycle: ${[...path, binding].map(item => item.name).join(" -> ")}`, binding.id);
if (done.has(binding)) return;
for (const dependency of dependencies.get(binding) ?? []) visit(dependency, [...path, binding]);
done.add(binding);
}
for (const binding of dependencies.keys()) visit(binding);
function initialized(node, position, seen = new Set(), active = new Map(), owner = null) {
if (!node || FUNCTION_LIKE.has(node.type)) return;
if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
for (const decorator of decoratorExpressions(node)) initialized(decorator, position, seen, active, owner);
initialized(node.superClass, position, seen, active, owner);
for (const member of node.members) if (member.static) {
if (member.type === "PropertyDefinition") initialized(member.value, node.end, seen, active, owner);
else if (member.type === "StaticBlock") initialized(member.body, node.end, seen, active, member.body);
}
return;
}
if (owner) active.set(owner, node.start);
if (node.type === "BlockStatement" && !owner) {
for (const child of node.body) initialized(child, child.start, seen, active);
return;
}
if (["ForOfStatement", "ForInStatement"].includes(node.type) && !owner) {
initialized(node.right, position, seen, active);
initialized(node.body, node.body.start, seen, active);
return;
}
if (node.type === "ForStatement" && !owner) {
for (const child of [node.init, node.test, node.body, node.update].filter(Boolean)) initialized(child, child.start, seen, active);
return;
}
if (node.type === "Identifier") {
const binding = resolved.get(node);
const currentPosition = binding?.scope?.depth === 0 ? position : active.get(binding?.scope?.owner);
if (initializedPatternReads.has(node)) return;
if (currentPosition !== undefined && !["function", "parameter"].includes(binding.kind)) {
if (binding.end > currentPosition) fail("NRS074", `Call reads '${binding.name}' before initialization`, node);
if (binding.kind === "derive" && !seen.has(binding)) {
seen.add(binding);
initialized(declarations.get(binding).init, position, seen, active);
}
}
return;
}
if (EFFECT_NODES.has(node.type)) {
for (const fn of invoked(node)) if (!seen.has(fn)) {
seen.add(fn);
for (const value of fn.params.flatMap(patternExpressions)) initialized(value, position, seen, new Map(active), fn.body);
initialized(fn.body, position, seen, new Map(active), fn.body);
}
}
for (const child of children(node)) initialized(child, position, seen, active, owner);
}
for (const module of [...(program.modules ?? []), program]) {
for (const node of module.body) initialized(node, node.start);
}
const analysis = { bindings, references, root, imports, arrows, resolved, declarations, classes, classContexts, classOf, memberOf, constructorOf, baseOf, isStaticReceiver, topLevelAwait: awaited };
checkTypes(program, analysis, { strictTypes, taintSources, taintExportedParams });
return analysis;
}