@tsplc/compiler 0.5.4 → 0.5.6
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/bun-runtime.js +14 -2
- package/dist/bun-runtime.js.map +4 -4
- package/dist/codesys.js +46 -2
- package/dist/codesys.js.map +3 -3
- package/dist/collections.js +395 -0
- package/dist/collections.js.map +10 -0
- package/dist/elaborate-capture.js +64 -5
- package/dist/elaborate-capture.js.map +4 -4
- package/dist/index.js +5345 -1074
- package/dist/index.js.map +32 -25
- package/dist/ir.js +122 -1
- package/dist/ir.js.map +6 -6
- package/package.json +14 -9
- package/src/codesys/numeric.ts +61 -0
- package/src/frontend/control-flow.ts +752 -0
- package/src/frontend/coroutines.ts +171 -7
- package/src/frontend/elaborate/array-searches.ts +216 -0
- package/src/frontend/elaborate/assignments.ts +352 -0
- package/src/frontend/elaborate/calls.ts +488 -49
- package/src/frontend/elaborate/capture-protocol.ts +19 -0
- package/src/frontend/elaborate/capture.ts +82 -3
- package/src/frontend/elaborate/dialect.ts +118 -4
- package/src/frontend/elaborate/expressions.ts +927 -80
- package/src/frontend/elaborate/filtered-arrays.ts +607 -0
- package/src/frontend/elaborate/frame.ts +44 -1
- package/src/frontend/elaborate/generics.ts +137 -0
- package/src/frontend/elaborate/graph.ts +91 -0
- package/src/frontend/elaborate/imports.ts +14 -15
- package/src/frontend/elaborate/index.ts +30 -5
- package/src/frontend/elaborate/initializers.ts +15 -1
- package/src/frontend/elaborate/iterators.ts +1677 -0
- package/src/frontend/elaborate/layouts.ts +41 -19
- package/src/frontend/elaborate/model.ts +64 -8
- package/src/frontend/elaborate/references.ts +749 -0
- package/src/frontend/elaborate/specialize.ts +459 -29
- package/src/frontend/elaborate/statements.ts +552 -133
- package/src/frontend/elaborate/storage.ts +151 -10
- package/src/frontend/elaborate/values.ts +72 -0
- package/src/frontend/elaborate/writes.ts +302 -1
- package/src/frontend/intrinsics.ts +32 -0
- package/src/frontend/module-graph.ts +2 -1
- package/src/frontend/positions.ts +12 -5
- package/src/frontend/sdk.ts +7 -1
- package/src/frontend/typescript.ts +5 -1
- package/src/ir/core-model.ts +2 -6
- package/src/ir/fold.ts +30 -1
- package/src/ir/index.ts +5 -0
- package/src/ir/model.ts +53 -2
- package/src/ir/scalars.ts +126 -0
- package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
- package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
- package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
- package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
- package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
- package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
- package/stdlib/collections.ts +514 -0
- package/types/index.d.ts +18 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
/** The checker's queries whose answers can name a generic class's type parameters. */
|
|
4
|
+
const typeQueries = new Set<string | symbol>([
|
|
5
|
+
"getTypeAtLocation",
|
|
6
|
+
"getTypeOfSymbol",
|
|
7
|
+
"getTypeOfSymbolAtLocation",
|
|
8
|
+
"getReturnTypeOfSignature",
|
|
9
|
+
"getTypeFromTypeNode",
|
|
10
|
+
"getAwaitedType",
|
|
11
|
+
"getNonNullableType",
|
|
12
|
+
"getIndexTypeOfType",
|
|
13
|
+
"getBaseTypeOfLiteralType",
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
const isTypeReference = (type: ts.Type): type is ts.TypeReference =>
|
|
17
|
+
Boolean(type.flags & ts.TypeFlags.Object) &&
|
|
18
|
+
"objectFlags" in type &&
|
|
19
|
+
typeof type.objectFlags === "number" &&
|
|
20
|
+
Boolean(type.objectFlags & ts.ObjectFlags.Reference);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Generic classes specialize per instance. While an instance's methods are specialized, the checker
|
|
24
|
+
* the elaborator reads through answers with that instance's type arguments in place of its class's
|
|
25
|
+
* type parameters, so `Ring<number>`'s `T[]` lays out as `number[]`.
|
|
26
|
+
*/
|
|
27
|
+
export class GenericTypes {
|
|
28
|
+
readonly checker: ts.TypeChecker;
|
|
29
|
+
private readonly scopes: Map<ts.Type, ts.Type>[] = [];
|
|
30
|
+
|
|
31
|
+
constructor(private readonly base: ts.TypeChecker) {
|
|
32
|
+
this.checker = new Proxy(base, {
|
|
33
|
+
get: (target, property, receiver) => {
|
|
34
|
+
const value: unknown = Reflect.get(target, property, receiver);
|
|
35
|
+
if (typeof value !== "function") {
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
if (!typeQueries.has(property)) {
|
|
39
|
+
return value.bind(target);
|
|
40
|
+
}
|
|
41
|
+
return (...args: unknown[]) => {
|
|
42
|
+
const result: unknown = value.apply(target, args);
|
|
43
|
+
return isType(result) ? this.substitute(result) : result;
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Runs `work` with the type arguments of `instanceType`, a generic class's instance, in scope. */
|
|
50
|
+
within<T>(
|
|
51
|
+
declaration: ts.ClassLikeDeclaration | undefined,
|
|
52
|
+
instanceType: ts.Type | undefined,
|
|
53
|
+
work: () => T,
|
|
54
|
+
): T {
|
|
55
|
+
const parameters = declaration?.typeParameters ?? [];
|
|
56
|
+
if (!parameters.length || !instanceType || !isTypeReference(instanceType)) {
|
|
57
|
+
return work();
|
|
58
|
+
}
|
|
59
|
+
const args = this.base.getTypeArguments(instanceType);
|
|
60
|
+
const scope = new Map(
|
|
61
|
+
parameters.flatMap((parameter, index): [ts.Type, ts.Type][] => {
|
|
62
|
+
const arg = args[index];
|
|
63
|
+
return arg
|
|
64
|
+
? [[this.base.getTypeAtLocation(parameter), this.substitute(arg)]]
|
|
65
|
+
: [];
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
this.scopes.push(scope);
|
|
69
|
+
try {
|
|
70
|
+
return work();
|
|
71
|
+
} finally {
|
|
72
|
+
this.scopes.pop();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private construct(
|
|
77
|
+
name: "getUnionType" | "createArrayType",
|
|
78
|
+
argument: ts.Type | readonly ts.Type[],
|
|
79
|
+
): ts.Type {
|
|
80
|
+
const made = typeConstructor(this.base, name)(argument);
|
|
81
|
+
if (!isType(made)) {
|
|
82
|
+
throw new Error(`${name} made no type.`);
|
|
83
|
+
}
|
|
84
|
+
return made;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A type with the type parameters in scope replaced, through unions and arrays. */
|
|
88
|
+
substitute(type: ts.Type): ts.Type {
|
|
89
|
+
if (!this.scopes.length) {
|
|
90
|
+
return type;
|
|
91
|
+
}
|
|
92
|
+
if (type.flags & ts.TypeFlags.TypeParameter) {
|
|
93
|
+
for (let index = this.scopes.length - 1; index >= 0; index--) {
|
|
94
|
+
const found = this.scopes[index]?.get(type);
|
|
95
|
+
if (found) {
|
|
96
|
+
return found;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return type;
|
|
100
|
+
}
|
|
101
|
+
if (type.isUnion()) {
|
|
102
|
+
const members = type.types.map((member) => this.substitute(member));
|
|
103
|
+
return members.some((member, index) => member !== type.types[index])
|
|
104
|
+
? this.construct("getUnionType", members)
|
|
105
|
+
: type;
|
|
106
|
+
}
|
|
107
|
+
if (this.base.isArrayType(type) && isTypeReference(type)) {
|
|
108
|
+
const [element] = this.base.getTypeArguments(type);
|
|
109
|
+
const replaced = element && this.substitute(element);
|
|
110
|
+
return replaced && replaced !== element
|
|
111
|
+
? this.construct("createArrayType", replaced)
|
|
112
|
+
: type;
|
|
113
|
+
}
|
|
114
|
+
return type;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Calls one of the checker's type constructors. TypeScript 6 has them on every checker but leaves
|
|
120
|
+
* them out of its public declarations, so they are looked up by name.
|
|
121
|
+
*/
|
|
122
|
+
const typeConstructor = (
|
|
123
|
+
checker: ts.TypeChecker,
|
|
124
|
+
name: "getUnionType" | "createArrayType",
|
|
125
|
+
): ((argument: unknown) => unknown) => {
|
|
126
|
+
const found: unknown = Reflect.get(checker, name);
|
|
127
|
+
if (typeof found !== "function") {
|
|
128
|
+
throw new Error(`This TypeScript's checker has no ${name}.`);
|
|
129
|
+
}
|
|
130
|
+
return (argument) => found.call(checker, argument);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const isType = (value: unknown): value is ts.Type =>
|
|
134
|
+
typeof value === "object" &&
|
|
135
|
+
value !== null &&
|
|
136
|
+
"flags" in value &&
|
|
137
|
+
"checker" in value;
|
|
@@ -14,6 +14,7 @@ import { z } from "zod";
|
|
|
14
14
|
import {
|
|
15
15
|
type CapturedGraph,
|
|
16
16
|
type CaptureRequest,
|
|
17
|
+
type ClosureSite,
|
|
17
18
|
capturedGraphSchema,
|
|
18
19
|
} from "./capture-protocol";
|
|
19
20
|
|
|
@@ -71,15 +72,105 @@ export const bindingNames = (source: ts.SourceFile) => {
|
|
|
71
72
|
return found;
|
|
72
73
|
};
|
|
73
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Every arrow and function expression not bound at module level, which setup may store, with the
|
|
77
|
+
* names it captures from the functions around it, and `this` when it reads it. Setup notes what each
|
|
78
|
+
* captured when it made it, so the PLC can call it with those values.
|
|
79
|
+
*/
|
|
80
|
+
export const closureSites = (
|
|
81
|
+
source: ts.SourceFile,
|
|
82
|
+
checker: ts.TypeChecker,
|
|
83
|
+
): ClosureSite[] => {
|
|
84
|
+
const sites: ClosureSite[] = [];
|
|
85
|
+
const visit = (node: ts.Node, enclosing: ts.Node | undefined): void => {
|
|
86
|
+
const closure = ts.isArrowFunction(node) || ts.isFunctionExpression(node);
|
|
87
|
+
// A module-level const's function is captured by its name instead.
|
|
88
|
+
if (closure && !moduleFunction(node)) {
|
|
89
|
+
sites.push({
|
|
90
|
+
start: node.getStart(source),
|
|
91
|
+
end: node.end,
|
|
92
|
+
captures: enclosing ? captures(node, enclosing, checker) : [],
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const scope =
|
|
96
|
+
ts.isFunctionLike(node) || ts.isClassLike(node) ? node : enclosing;
|
|
97
|
+
ts.forEachChild(node, (child) => visit(child, scope));
|
|
98
|
+
};
|
|
99
|
+
visit(source, undefined);
|
|
100
|
+
return sites;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/** The names a closure reads that the functions around it declare, and `this` if an arrow reads it. */
|
|
104
|
+
const captures = (
|
|
105
|
+
closure: ts.ArrowFunction | ts.FunctionExpression,
|
|
106
|
+
enclosing: ts.Node,
|
|
107
|
+
checker: ts.TypeChecker,
|
|
108
|
+
): string[] => {
|
|
109
|
+
const names = new Set<string>();
|
|
110
|
+
const visit = (node: ts.Node): void => {
|
|
111
|
+
if (
|
|
112
|
+
node.kind === ts.SyntaxKind.ThisKeyword &&
|
|
113
|
+
ts.isArrowFunction(closure)
|
|
114
|
+
) {
|
|
115
|
+
names.add("this");
|
|
116
|
+
}
|
|
117
|
+
if (ts.isIdentifier(node) && !isPropertyName(node)) {
|
|
118
|
+
// A shorthand property reads the variable it is named for.
|
|
119
|
+
const symbol = ts.isShorthandPropertyAssignment(node.parent)
|
|
120
|
+
? checker.getShorthandAssignmentValueSymbol(node.parent)
|
|
121
|
+
: checker.getSymbolAtLocation(node);
|
|
122
|
+
const declaration = symbol?.valueDeclaration;
|
|
123
|
+
if (
|
|
124
|
+
declaration &&
|
|
125
|
+
!within(declaration, closure) &&
|
|
126
|
+
within(declaration, enclosing) &&
|
|
127
|
+
!ts.isClassLike(declaration)
|
|
128
|
+
) {
|
|
129
|
+
names.add(node.text);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (!ts.isTypeNode(node)) {
|
|
133
|
+
ts.forEachChild(node, visit);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
visit(closure.body);
|
|
137
|
+
return [...names];
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** A function a module-level const names, which capture finds by that name. */
|
|
141
|
+
const moduleFunction = (
|
|
142
|
+
node: ts.ArrowFunction | ts.FunctionExpression,
|
|
143
|
+
): boolean => {
|
|
144
|
+
const declaration = node.parent;
|
|
145
|
+
const statement = declaration?.parent?.parent;
|
|
146
|
+
return (
|
|
147
|
+
ts.isVariableDeclaration(declaration) &&
|
|
148
|
+
statement !== undefined &&
|
|
149
|
+
ts.isVariableStatement(statement) &&
|
|
150
|
+
ts.isSourceFile(statement.parent)
|
|
151
|
+
);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const within = (node: ts.Node, container: ts.Node): boolean =>
|
|
155
|
+
node.pos >= container.pos && node.end <= container.end;
|
|
156
|
+
|
|
157
|
+
const isPropertyName = (node: ts.Identifier): boolean =>
|
|
158
|
+
(ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) ||
|
|
159
|
+
(ts.isPropertyAssignment(node.parent) && node.parent.name === node);
|
|
160
|
+
|
|
74
161
|
/** Runs the program's setup in a Bun subprocess and returns the object graph it built. */
|
|
75
162
|
export const captureGraph = (
|
|
76
163
|
entry: string,
|
|
77
164
|
sources: readonly ts.SourceFile[],
|
|
165
|
+
checker: ts.TypeChecker,
|
|
78
166
|
): CapturedGraph => {
|
|
79
167
|
const files = sources.filter((source) => !source.isDeclarationFile);
|
|
80
168
|
const request: CaptureRequest = {
|
|
81
169
|
entry,
|
|
82
170
|
files: files.map((source) => source.fileName),
|
|
171
|
+
closures: Object.fromEntries(
|
|
172
|
+
files.map((source) => [source.fileName, closureSites(source, checker)]),
|
|
173
|
+
),
|
|
83
174
|
bindings: Object.fromEntries(
|
|
84
175
|
files.map((source) => [source.fileName, bindingNames(source)]),
|
|
85
176
|
),
|
|
@@ -31,20 +31,25 @@ export class ModuleImports {
|
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* What the generated entry module imports besides the generated types: the storage modules,
|
|
34
|
-
* kept modules' bindings and the SDK functions scan code
|
|
34
|
+
* kept modules' bindings, and the SDK functions scan code and generated conversions call.
|
|
35
35
|
*/
|
|
36
36
|
export class EntryImports {
|
|
37
37
|
/** Storage modules generated code reads through a namespace import, by import name. */
|
|
38
38
|
readonly namespaces = new Map<string, string>();
|
|
39
39
|
/** Bindings of kept modules, read by their own names. */
|
|
40
40
|
readonly kept = new ModuleImports();
|
|
41
|
-
/** `@tsplc/compiler/clock
|
|
42
|
-
readonly
|
|
43
|
-
/** Conversions generated code calls from `@tsplc/compiler/types`. */
|
|
44
|
-
readonly conversions = new Set<string>();
|
|
41
|
+
/** SDK functions by the module that declares them, such as `@tsplc/compiler/clock`. */
|
|
42
|
+
private readonly sdk = new Map<string, Set<string>>();
|
|
45
43
|
/** Whether scan code calls `telemetry`. */
|
|
46
44
|
telemetry = false;
|
|
47
45
|
|
|
46
|
+
/** Records a call of `name`, which `module` of the SDK declares. */
|
|
47
|
+
addSdk(module: string, name: string): void {
|
|
48
|
+
const names = this.sdk.get(module) ?? new Set<string>();
|
|
49
|
+
names.add(name);
|
|
50
|
+
this.sdk.set(module, names);
|
|
51
|
+
}
|
|
52
|
+
|
|
48
53
|
lines(entryFile: string): string[] {
|
|
49
54
|
return [
|
|
50
55
|
...[...this.namespaces].map(
|
|
@@ -52,19 +57,13 @@ export class EntryImports {
|
|
|
52
57
|
`import * as ${name} from "${importPath(entryFile, file)}";`,
|
|
53
58
|
),
|
|
54
59
|
...this.kept.lines(entryFile, "import"),
|
|
55
|
-
...
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
: []),
|
|
60
|
+
...[...this.sdk].map(
|
|
61
|
+
([module, names]) =>
|
|
62
|
+
`import { ${[...names].join(", ")} } from "${module}";`,
|
|
63
|
+
),
|
|
60
64
|
...(this.telemetry
|
|
61
65
|
? [`import { telemetry } from "@tsplc/compiler/telemetry";`]
|
|
62
66
|
: []),
|
|
63
|
-
...(this.conversions.size
|
|
64
|
-
? [
|
|
65
|
-
`import { ${[...this.conversions].join(", ")} } from "@tsplc/compiler/types";`,
|
|
66
|
-
]
|
|
67
|
-
: []),
|
|
68
67
|
];
|
|
69
68
|
}
|
|
70
69
|
}
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
import { inSdk, namesSdk } from "../sdk";
|
|
20
20
|
import type { CompileDebug } from "../typescript";
|
|
21
21
|
import type { CapturedValue } from "./capture-protocol";
|
|
22
|
+
import { GenericTypes } from "./generics";
|
|
22
23
|
import { captureGraph } from "./graph";
|
|
23
24
|
import { importPath } from "./imports";
|
|
24
25
|
import { Model, type Root } from "./model";
|
|
@@ -229,21 +230,34 @@ export const lowerElaboration = (
|
|
|
229
230
|
"/",
|
|
230
231
|
);
|
|
231
232
|
const rootModule = (item: Root) => `${directory}/${item.name}.ts`;
|
|
233
|
+
// Failures statements recorded before one escaped: often its cause, as a skipped declaration's name is then unknown.
|
|
234
|
+
let recorded = (): readonly SpecializeError[] => [];
|
|
232
235
|
|
|
233
236
|
try {
|
|
234
237
|
const declarations = retainedDeclarations(runtime, checker, sdk);
|
|
235
|
-
const graph = captureGraph(entryFile, runtime);
|
|
236
|
-
|
|
238
|
+
const graph = captureGraph(entryFile, runtime, checker);
|
|
239
|
+
// Generic classes specialize per instance, through a checker that knows each one's type arguments.
|
|
240
|
+
const generics = new GenericTypes(checker);
|
|
241
|
+
const model = new Model(graph, generics.checker, program, entryFile);
|
|
237
242
|
const specializer = new Specializer(
|
|
238
243
|
model,
|
|
239
|
-
|
|
244
|
+
generics,
|
|
240
245
|
sdk,
|
|
241
246
|
runtime,
|
|
242
247
|
isKept,
|
|
243
248
|
rootModule,
|
|
244
249
|
);
|
|
250
|
+
recorded = () => specializer.failures;
|
|
245
251
|
const main = findEntry(entrySource, entry.export);
|
|
246
252
|
const body = specializer.entry(main);
|
|
253
|
+
if (specializer.failures.length) {
|
|
254
|
+
diagnostics.push(
|
|
255
|
+
...specializer.failures.map((cause) =>
|
|
256
|
+
failure(cause, positions, debug),
|
|
257
|
+
),
|
|
258
|
+
);
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
247
261
|
|
|
248
262
|
// Exported data the hardware, HMI and server address is stored whether or not code reads it.
|
|
249
263
|
for (const item of model.roots) {
|
|
@@ -347,7 +361,10 @@ export const lowerElaboration = (
|
|
|
347
361
|
}
|
|
348
362
|
}
|
|
349
363
|
} catch (cause) {
|
|
350
|
-
diagnostics.push(
|
|
364
|
+
diagnostics.push(
|
|
365
|
+
...recorded().map((earlier) => failure(earlier, positions, debug)),
|
|
366
|
+
failure(cause, positions, debug),
|
|
367
|
+
);
|
|
351
368
|
}
|
|
352
369
|
return result;
|
|
353
370
|
};
|
|
@@ -387,7 +404,15 @@ const storageGlobals = (
|
|
|
387
404
|
const node = model.node(item.value);
|
|
388
405
|
if (!node || !storage.flattened(item)) {
|
|
389
406
|
const declared = checker.getTypeAtLocation(item.declaration.name);
|
|
390
|
-
|
|
407
|
+
// An array of instances stores each instance's state, not its class.
|
|
408
|
+
const instances =
|
|
409
|
+
node?.captured.kind === "array" &&
|
|
410
|
+
node.captured.items.some(
|
|
411
|
+
(value) => model.node(value)?.captured.kind === "instance",
|
|
412
|
+
);
|
|
413
|
+
const layout =
|
|
414
|
+
(instances && node && storage.storageType(node)) ||
|
|
415
|
+
layouts.declaredText(item.declaration, declared, item.value);
|
|
391
416
|
global(
|
|
392
417
|
item.name,
|
|
393
418
|
layout,
|
|
@@ -43,6 +43,10 @@ export class Initializers {
|
|
|
43
43
|
if (node.captured.kind === "array") {
|
|
44
44
|
const element = this.model.elementType(declared);
|
|
45
45
|
const items = node.captured.items;
|
|
46
|
+
// Instances of different classes are stored side by side, a field each.
|
|
47
|
+
if (this.storage.mixedStates(node)) {
|
|
48
|
+
return `{ ${items.map((item, index) => `item${index}: ${element ? this.valueText(item, element) : this.literal(item)}`).join(", ")} }`;
|
|
49
|
+
}
|
|
46
50
|
if (!items.length) {
|
|
47
51
|
return `defaultValue<${layout ?? this.layouts.text(declared, value)}>()`;
|
|
48
52
|
}
|
|
@@ -80,6 +84,10 @@ export class Initializers {
|
|
|
80
84
|
}
|
|
81
85
|
const value = captured.fields[name] ?? { kind: "undefined" as const };
|
|
82
86
|
const inner = this.model.node(value);
|
|
87
|
+
// A reference starts as the id of the object setup gave it, or -1 for none.
|
|
88
|
+
if (this.storage.referenceField(node, name)) {
|
|
89
|
+
return String(inner ? inner.id : -1);
|
|
90
|
+
}
|
|
83
91
|
if (inner?.captured.kind === "instance") {
|
|
84
92
|
return this.instanceInitializer(inner);
|
|
85
93
|
}
|
|
@@ -89,7 +97,13 @@ export class Initializers {
|
|
|
89
97
|
(item) => this.model.node(item)?.captured.kind === "instance",
|
|
90
98
|
)
|
|
91
99
|
) {
|
|
92
|
-
|
|
100
|
+
const states = inner.captured.items.map((item) =>
|
|
101
|
+
this.instanceInitializer(this.model.node(item)!),
|
|
102
|
+
);
|
|
103
|
+
// Instances of different classes are stored side by side, a field each.
|
|
104
|
+
return this.storage.mixedStates(inner)
|
|
105
|
+
? `{ ${states.map((state, index) => `item${index}: ${state}`).join(", ")} }`
|
|
106
|
+
: `[${states.join(", ")}]`;
|
|
93
107
|
}
|
|
94
108
|
return this.fieldInitializer(node, name, value, layout);
|
|
95
109
|
}
|