@oseo/compiler 0.0.0-stage → 0.1.0-canary.368
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/LICENSE +674 -0
- package/README.md +59 -2
- package/dist/agent-programs.d.ts +28 -0
- package/dist/agent-programs.d.ts.map +1 -0
- package/dist/agent-programs.js +134 -0
- package/dist/agent-programs.js.map +1 -0
- package/dist/agent-templates.d.ts +33 -0
- package/dist/agent-templates.d.ts.map +1 -0
- package/dist/agent-templates.js +45 -0
- package/dist/agent-templates.js.map +1 -0
- package/dist/compile.d.ts +20 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +75 -0
- package/dist/compile.js.map +1 -0
- package/dist/fragments.d.ts +72 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +158 -0
- package/dist/fragments.js.map +1 -0
- package/dist/hir-build.d.ts +80 -0
- package/dist/hir-build.d.ts.map +1 -0
- package/dist/hir-build.js +3077 -0
- package/dist/hir-build.js.map +1 -0
- package/dist/hir-print.d.ts +10 -0
- package/dist/hir-print.d.ts.map +1 -0
- package/dist/hir-print.js +271 -0
- package/dist/hir-print.js.map +1 -0
- package/dist/hir.d.ts +1246 -0
- package/dist/hir.d.ts.map +1 -0
- package/dist/hir.js +147 -0
- package/dist/hir.js.map +1 -0
- package/dist/host-scripts.d.ts +14 -0
- package/dist/host-scripts.d.ts.map +1 -0
- package/dist/host-scripts.js +204 -0
- package/dist/host-scripts.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/mir-build.d.ts +8 -0
- package/dist/mir-build.d.ts.map +1 -0
- package/dist/mir-build.js +5113 -0
- package/dist/mir-build.js.map +1 -0
- package/dist/mir-print.d.ts +7 -0
- package/dist/mir-print.d.ts.map +1 -0
- package/dist/mir-print.js +65 -0
- package/dist/mir-print.js.map +1 -0
- package/dist/mir-specialize.d.ts +7 -0
- package/dist/mir-specialize.d.ts.map +1 -0
- package/dist/mir-specialize.js +182 -0
- package/dist/mir-specialize.js.map +1 -0
- package/dist/mir.d.ts +571 -0
- package/dist/mir.d.ts.map +1 -0
- package/dist/mir.js +1 -0
- package/dist/module-builtin.d.ts +20 -0
- package/dist/module-builtin.d.ts.map +1 -0
- package/dist/module-builtin.js +57 -0
- package/dist/module-builtin.js.map +1 -0
- package/dist/module-compile.d.ts +64 -0
- package/dist/module-compile.d.ts.map +1 -0
- package/dist/module-compile.js +690 -0
- package/dist/module-compile.js.map +1 -0
- package/dist/module-json.d.ts +7 -0
- package/dist/module-json.d.ts.map +1 -0
- package/dist/module-json.js +88 -0
- package/dist/module-json.js.map +1 -0
- package/dist/modules.d.ts +87 -0
- package/dist/modules.d.ts.map +1 -0
- package/dist/modules.js +734 -0
- package/dist/modules.js.map +1 -0
- package/dist/native-fragments.d.ts +36 -0
- package/dist/native-fragments.d.ts.map +1 -0
- package/dist/native-fragments.js +110 -0
- package/dist/native-fragments.js.map +1 -0
- package/dist/native.d.ts +282 -0
- package/dist/native.d.ts.map +1 -0
- package/dist/native.js +47 -0
- package/dist/native.js.map +1 -0
- package/dist/regexp-execute.d.ts +74 -0
- package/dist/regexp-execute.d.ts.map +1 -0
- package/dist/regexp-execute.js +480 -0
- package/dist/regexp-execute.js.map +1 -0
- package/dist/regexp-matcher.d.ts +279 -0
- package/dist/regexp-matcher.d.ts.map +1 -0
- package/dist/regexp-matcher.js +1098 -0
- package/dist/regexp-matcher.js.map +1 -0
- package/dist/regexp-parse.d.ts +22 -0
- package/dist/regexp-parse.d.ts.map +1 -0
- package/dist/regexp-parse.js +1440 -0
- package/dist/regexp-parse.js.map +1 -0
- package/dist/regexp-print.d.ts +24 -0
- package/dist/regexp-print.d.ts.map +1 -0
- package/dist/regexp-print.js +147 -0
- package/dist/regexp-print.js.map +1 -0
- package/dist/regexp.d.ts +324 -0
- package/dist/regexp.d.ts.map +1 -0
- package/dist/regexp.js +22 -0
- package/dist/regexp.js.map +1 -0
- package/dist/source.d.ts +38 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +10 -0
- package/dist/source.js.map +1 -0
- package/dist/syntax.d.ts +1232 -0
- package/dist/syntax.d.ts.map +1 -0
- package/dist/syntax.js +1 -0
- package/package.json +32 -3
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
import { buildSeededHir, hirBindingIdentifiers, hirBindingPatternHasAwait, hirExpressionHasAwait, sourceDiagnostic } from "./hir-build.js";
|
|
2
|
+
import { buildMir } from "./mir-build.js";
|
|
3
|
+
import { builtinModuleExports, moduleNodeRange, moduleNodeSyntax } from "./module-builtin.js";
|
|
4
|
+
import { linkModuleGraph } from "./modules.js";
|
|
5
|
+
|
|
6
|
+
//#region src/module-compile.ts
|
|
7
|
+
function includePropertiesWhen(properties) {
|
|
8
|
+
return properties() ?? {};
|
|
9
|
+
}
|
|
10
|
+
function moduleProgramBody(module) {
|
|
11
|
+
const items = [...module.body];
|
|
12
|
+
for (const [index, entry] of module.exports.entries()) {
|
|
13
|
+
if (entry.kind !== "default") continue;
|
|
14
|
+
const bindingName = `*default:${index}*`;
|
|
15
|
+
if ("parameters" in entry.declaration) {
|
|
16
|
+
items.push({
|
|
17
|
+
...entry.declaration,
|
|
18
|
+
bindingName
|
|
19
|
+
});
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
let initializer = entry.declaration;
|
|
23
|
+
if (entry.declaration.kind === "function" && entry.declaration.functionValue.name == null) initializer = {
|
|
24
|
+
...entry.declaration,
|
|
25
|
+
inferredName: "default"
|
|
26
|
+
};
|
|
27
|
+
else if (entry.declaration.kind === "class" && entry.declaration.constructorFunction.name == null) initializer = {
|
|
28
|
+
...entry.declaration,
|
|
29
|
+
inferredName: "default"
|
|
30
|
+
};
|
|
31
|
+
items.push({
|
|
32
|
+
...includePropertiesWhen(() => {
|
|
33
|
+
if (entry.byteRange == null) return void 0;
|
|
34
|
+
return { byteRange: entry.byteRange };
|
|
35
|
+
}),
|
|
36
|
+
hint: void 0,
|
|
37
|
+
initializer,
|
|
38
|
+
kind: "const",
|
|
39
|
+
name: bindingName,
|
|
40
|
+
range: entry.range
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return items.toSorted((left, right) => (left.byteRange?.start ?? Number.MAX_SAFE_INTEGER) - (right.byteRange?.start ?? Number.MAX_SAFE_INTEGER));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The top-level declaration that binds one name, used to locate a
|
|
47
|
+
* duplicate-declaration diagnostic. A lexical declaration list is
|
|
48
|
+
* searched by declarator, so the diagnostic names the declarator rather
|
|
49
|
+
* than the whole module.
|
|
50
|
+
*/
|
|
51
|
+
function declarationNamed(items, name) {
|
|
52
|
+
for (const item of items) {
|
|
53
|
+
if (item.kind === "declaration-list") {
|
|
54
|
+
const declarator = declarationNamed(item.declarations, name);
|
|
55
|
+
if (declarator != null) return declarator;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if ((item.kind === "const" || item.kind === "let" || item.kind === "function") && item.name === name) return item;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function isSourceRange(value) {
|
|
62
|
+
if (value == null || typeof value !== "object") return false;
|
|
63
|
+
const candidate = value;
|
|
64
|
+
return candidate.start != null && typeof candidate.start.line === "number" && typeof candidate.start.column === "number" && candidate.end != null && typeof candidate.end.line === "number" && typeof candidate.end.column === "number";
|
|
65
|
+
}
|
|
66
|
+
function isNonNullObject(value) {
|
|
67
|
+
return value !== null && typeof value === "object";
|
|
68
|
+
}
|
|
69
|
+
/** Retain module identity on every owned range before graph HIR is merged. */
|
|
70
|
+
function retainModuleSource(value, sourceId) {
|
|
71
|
+
if (isSourceRange(value)) return {
|
|
72
|
+
...value,
|
|
73
|
+
sourceId
|
|
74
|
+
};
|
|
75
|
+
if (Array.isArray(value)) return value.map((item) => retainModuleSource(item, sourceId));
|
|
76
|
+
if (!isNonNullObject(value)) return value;
|
|
77
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, retainModuleSource(item, sourceId)]));
|
|
78
|
+
}
|
|
79
|
+
/** Whether one statement itself declares an `await using` binding. */
|
|
80
|
+
function hirStatementDisposesAsynchronously(statement) {
|
|
81
|
+
if (statement.kind === "const") return statement.disposal === "async";
|
|
82
|
+
if (statement.kind === "for") return (statement.declarations ?? []).some((declaration) => declaration.kind === "binding" && declaration.disposal === "async");
|
|
83
|
+
return statement.kind === "for-of" && statement.target.kind === "declaration" && statement.target.disposal === "async";
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Whether one statement contains an await the module continuation
|
|
87
|
+
* transform can extract. A `for await` head awaits every iteration step
|
|
88
|
+
* without being such a point, because the loop stays in place, so this
|
|
89
|
+
* predicate must not report it: it drives both the extraction search and
|
|
90
|
+
* the rejection of awaits in positions the transform does not own. The
|
|
91
|
+
* `awaitedIteration` variant instead reports whether the statement makes
|
|
92
|
+
* its module's evaluation asynchronous at all, which the scheduler and
|
|
93
|
+
* the cycle check need.
|
|
94
|
+
*/
|
|
95
|
+
function hirStatementAwaits(statement, awaitedIteration) {
|
|
96
|
+
const recurse = (child) => hirStatementAwaits(child, awaitedIteration);
|
|
97
|
+
if (awaitedIteration && statement.kind === "for-of" && statement.awaited === true) return true;
|
|
98
|
+
if (awaitedIteration && hirStatementDisposesAsynchronously(statement)) return true;
|
|
99
|
+
if (statement.kind === "block") return statement.body.some(recurse);
|
|
100
|
+
if (statement.kind === "binding-init" || statement.kind === "const" || statement.kind === "let") return hirExpressionHasAwait(statement.initializer);
|
|
101
|
+
if (statement.kind === "binding-pattern") return hirExpressionHasAwait(statement.initializer) || hirBindingPatternHasAwait(statement.pattern);
|
|
102
|
+
if (statement.kind === "expression" || statement.kind === "throw") return hirExpressionHasAwait(statement.expression);
|
|
103
|
+
if (statement.kind === "return") return statement.expression != null && hirExpressionHasAwait(statement.expression);
|
|
104
|
+
if (statement.kind === "if") return hirExpressionHasAwait(statement.test) || recurse(statement.consequent) || statement.alternate != null && recurse(statement.alternate);
|
|
105
|
+
if (statement.kind === "try") return recurse(statement.block) || statement.handler != null && (statement.handler.pattern != null && hirBindingPatternHasAwait(statement.handler.pattern) || recurse(statement.handler.body)) || statement.finalizer != null && recurse(statement.finalizer);
|
|
106
|
+
if (statement.kind === "labeled") return recurse(statement.body);
|
|
107
|
+
if (statement.kind === "switch") return hirExpressionHasAwait(statement.discriminant) || statement.cases.some((switchCase) => switchCase.test != null && hirExpressionHasAwait(switchCase.test) || switchCase.body.some(recurse));
|
|
108
|
+
if (statement.kind === "for") return (statement.declarations ?? []).some((declaration) => hirExpressionHasAwait(declaration.initializer) || declaration.kind === "pattern" && hirBindingPatternHasAwait(declaration.pattern)) || statement.init != null && hirExpressionHasAwait(statement.init) || statement.test != null && hirExpressionHasAwait(statement.test) || statement.update != null && hirExpressionHasAwait(statement.update) || recurse(statement.body);
|
|
109
|
+
if (statement.kind === "for-of") return hirExpressionHasAwait(statement.iterable) || (statement.target.kind === "pattern-declaration" || statement.target.kind === "assignment-pattern") && hirBindingPatternHasAwait(statement.target.pattern) || statement.target.kind === "property" && (hirExpressionHasAwait(statement.target.object) || hirExpressionHasAwait(statement.target.key)) || recurse(statement.body);
|
|
110
|
+
if (statement.kind === "for-in") return hirExpressionHasAwait(statement.subject) || (statement.target.kind === "pattern-declaration" || statement.target.kind === "assignment-pattern") && hirBindingPatternHasAwait(statement.target.pattern) || statement.target.kind === "property" && (hirExpressionHasAwait(statement.target.object) || hirExpressionHasAwait(statement.target.key)) || statement.target.kind === "private" && hirExpressionHasAwait(statement.target.object) || recurse(statement.body);
|
|
111
|
+
return (statement.kind === "while" || statement.kind === "do-while") && (hirExpressionHasAwait(statement.test) || recurse(statement.body));
|
|
112
|
+
}
|
|
113
|
+
function hirStatementIsAsynchronous(statement) {
|
|
114
|
+
return hirStatementAwaits(statement, true);
|
|
115
|
+
}
|
|
116
|
+
function hirStatementsAreAsynchronous(statements) {
|
|
117
|
+
return statements.some(hirStatementIsAsynchronous);
|
|
118
|
+
}
|
|
119
|
+
function collectHirBindings(statements) {
|
|
120
|
+
const bindings = [];
|
|
121
|
+
const collect = (statement) => {
|
|
122
|
+
if (statement.kind === "const" || statement.kind === "let" || statement.kind === "function-init") bindings.push({
|
|
123
|
+
id: statement.bindingId,
|
|
124
|
+
name: statement.name
|
|
125
|
+
});
|
|
126
|
+
else if (statement.kind === "binding-pattern" && statement.mode === "declare" && statement.declarationKind !== "var") for (const binding of hirBindingIdentifiers(statement.pattern)) bindings.push({
|
|
127
|
+
id: binding.bindingId,
|
|
128
|
+
name: binding.name
|
|
129
|
+
});
|
|
130
|
+
else if (statement.kind === "block") statement.body.forEach(collect);
|
|
131
|
+
else if (statement.kind === "if") {
|
|
132
|
+
collect(statement.consequent);
|
|
133
|
+
if (statement.alternate != null) collect(statement.alternate);
|
|
134
|
+
} else if (statement.kind === "try") {
|
|
135
|
+
collect(statement.block);
|
|
136
|
+
if (statement.handler != null) {
|
|
137
|
+
if (statement.handler.pattern != null) for (const binding of hirBindingIdentifiers(statement.handler.pattern)) bindings.push({
|
|
138
|
+
id: binding.bindingId,
|
|
139
|
+
name: binding.name
|
|
140
|
+
});
|
|
141
|
+
collect(statement.handler.body);
|
|
142
|
+
}
|
|
143
|
+
if (statement.finalizer != null) collect(statement.finalizer);
|
|
144
|
+
} else if (statement.kind === "while" || statement.kind === "do-while" || statement.kind === "labeled") collect(statement.body);
|
|
145
|
+
else if (statement.kind === "for") {
|
|
146
|
+
for (const declaration of statement.declarations ?? []) {
|
|
147
|
+
if (declaration.declarationKind === "var") continue;
|
|
148
|
+
if (declaration.kind === "binding") bindings.push({
|
|
149
|
+
id: declaration.bindingId,
|
|
150
|
+
name: declaration.name
|
|
151
|
+
});
|
|
152
|
+
else for (const binding of hirBindingIdentifiers(declaration.pattern)) bindings.push({
|
|
153
|
+
id: binding.bindingId,
|
|
154
|
+
name: binding.name
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
collect(statement.body);
|
|
158
|
+
} else if (statement.kind === "for-of") {
|
|
159
|
+
if (statement.target.kind === "declaration" && statement.target.declarationKind !== "var") bindings.push({
|
|
160
|
+
id: statement.target.bindingId,
|
|
161
|
+
name: statement.target.name
|
|
162
|
+
});
|
|
163
|
+
else if (statement.target.kind === "pattern-declaration" && statement.target.declarationKind !== "var") for (const binding of hirBindingIdentifiers(statement.target.pattern)) bindings.push({
|
|
164
|
+
id: binding.bindingId,
|
|
165
|
+
name: binding.name
|
|
166
|
+
});
|
|
167
|
+
collect(statement.body);
|
|
168
|
+
} else if (statement.kind === "for-in") {
|
|
169
|
+
if (statement.target.kind === "declaration" && statement.target.declarationKind !== "var") bindings.push({
|
|
170
|
+
id: statement.target.bindingId,
|
|
171
|
+
name: statement.target.name
|
|
172
|
+
});
|
|
173
|
+
else if (statement.target.kind === "pattern-declaration" && statement.target.declarationKind !== "var") for (const binding of hirBindingIdentifiers(statement.target.pattern)) bindings.push({
|
|
174
|
+
id: binding.bindingId,
|
|
175
|
+
name: binding.name
|
|
176
|
+
});
|
|
177
|
+
collect(statement.body);
|
|
178
|
+
} else if (statement.kind === "switch") {
|
|
179
|
+
(statement.functionInits ?? []).forEach(collect);
|
|
180
|
+
for (const switchCase of statement.cases) switchCase.body.forEach(collect);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
statements.forEach(collect);
|
|
184
|
+
return bindings;
|
|
185
|
+
}
|
|
186
|
+
/** The link-time initializer of one module's namespace object cell. */
|
|
187
|
+
function moduleNamespaceInitializer(target, bindingId, range) {
|
|
188
|
+
return {
|
|
189
|
+
bindingId,
|
|
190
|
+
hint: void 0,
|
|
191
|
+
initializer: {
|
|
192
|
+
entries: target.exports.map((entry) => ({
|
|
193
|
+
bindingId: entry.cellId,
|
|
194
|
+
name: entry.exportedName
|
|
195
|
+
})),
|
|
196
|
+
kind: "module-namespace",
|
|
197
|
+
range
|
|
198
|
+
},
|
|
199
|
+
kind: "const",
|
|
200
|
+
name: `*namespace:${target.canonicalId}*`,
|
|
201
|
+
range
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function moduleUndefined(range) {
|
|
205
|
+
return {
|
|
206
|
+
kind: "undefined",
|
|
207
|
+
range
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function moduleNumber(value, range) {
|
|
211
|
+
return {
|
|
212
|
+
kind: "number",
|
|
213
|
+
range,
|
|
214
|
+
value
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function moduleFunctionExpression(functionValue) {
|
|
218
|
+
return {
|
|
219
|
+
functionId: functionValue.id,
|
|
220
|
+
functionKind: functionValue.functionKind,
|
|
221
|
+
functionLength: functionValue.functionLength,
|
|
222
|
+
kind: "function",
|
|
223
|
+
name: functionValue.name,
|
|
224
|
+
range: functionValue.range,
|
|
225
|
+
...includePropertiesWhen(() => {
|
|
226
|
+
if (functionValue.sourceText == null) return void 0;
|
|
227
|
+
return { sourceText: functionValue.sourceText };
|
|
228
|
+
})
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* A module reading a name that a Script's `with` statement may initialize
|
|
233
|
+
* could not observe the hidden fallback cell the Script writes.
|
|
234
|
+
*/
|
|
235
|
+
function withInitializingDiagnostic(script, names, role) {
|
|
236
|
+
return sourceDiagnostic(script.sourceId, script, `A ${role}Script whose with statement may initialize ${names.toSorted().join(", ")} is unsupported before a module graph.`);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Resolve the prelude Script after the graph's cells, so its IDs never
|
|
240
|
+
* collide with a module binding. A top-level `with` assignment that may
|
|
241
|
+
* write a hidden fallback cell is rejected, because a module reading the
|
|
242
|
+
* same name could not observe that cell.
|
|
243
|
+
*/
|
|
244
|
+
function resolveModulePrelude(prelude, nextBindingId) {
|
|
245
|
+
const resolved = buildSeededHir(prelude, {
|
|
246
|
+
fragmentMetadata: true,
|
|
247
|
+
nextBindingId
|
|
248
|
+
});
|
|
249
|
+
const failed = {
|
|
250
|
+
diagnostics: resolved.diagnostics,
|
|
251
|
+
nextBindingId: resolved.nextBindingId,
|
|
252
|
+
nextFunctionId: resolved.nextFunctionId
|
|
253
|
+
};
|
|
254
|
+
if (resolved.program == null || resolved.scriptBindings == null) return failed;
|
|
255
|
+
const initializing = resolved.initializingWithNames ?? [];
|
|
256
|
+
if (initializing.length > 0) return {
|
|
257
|
+
...failed,
|
|
258
|
+
diagnostics: [withInitializingDiagnostic(prelude, initializing, "prelude ")]
|
|
259
|
+
};
|
|
260
|
+
return {
|
|
261
|
+
...failed,
|
|
262
|
+
prelude: {
|
|
263
|
+
bindings: resolved.scriptBindings,
|
|
264
|
+
globalObjectBindingIds: (resolved.program.globalObjectBindings ?? []).map((binding) => binding.id),
|
|
265
|
+
hir: retainModuleSource(resolved.program, prelude.sourceId)
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* The module table order: the entry's static closure in startup
|
|
271
|
+
* evaluation order, then every module that only `import()` reaches, in
|
|
272
|
+
* discovery order. Startup never evaluates the latter; the driver
|
|
273
|
+
* evaluates each on its first request.
|
|
274
|
+
*/
|
|
275
|
+
function moduleTableOrder(linked) {
|
|
276
|
+
const scheduled = new Set(linked.evaluationOrder);
|
|
277
|
+
return [...linked.evaluationOrder, ...linked.modules.map((module) => module.canonicalId).filter((moduleId) => !scheduled.has(moduleId))];
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The module table index of every `import()` site one referrer makes,
|
|
281
|
+
* keyed by the call's start byte, as the HIR builder expects.
|
|
282
|
+
*/
|
|
283
|
+
function importCallTargetsFor(graph, moduleIndices, referrerId, referrerKind) {
|
|
284
|
+
const targets = /* @__PURE__ */ new Map();
|
|
285
|
+
for (const request of graph.requests ?? []) {
|
|
286
|
+
if (request.edgeKind !== "discovery" || request.referrer.id !== referrerId || request.referrer.kind !== referrerKind) continue;
|
|
287
|
+
const index = moduleIndices.get(request.targetId);
|
|
288
|
+
if (index == null) throw new Error(`Import target '${request.targetId}' is unavailable.`);
|
|
289
|
+
targets.set(request.byteRange.start, index);
|
|
290
|
+
}
|
|
291
|
+
return targets;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Resolve every module body of a linked graph and build its table. The
|
|
295
|
+
* `outer` Script's global declarations enclose each module environment.
|
|
296
|
+
*/
|
|
297
|
+
function compileModuleTable(graph, linked, tableOrder, moduleIndices, ids, outer) {
|
|
298
|
+
const nodes = new Map(graph.modules.map((module) => [module.canonicalId, module]));
|
|
299
|
+
const linkedModules = new Map(linked.modules.map((module) => [module.canonicalId, module]));
|
|
300
|
+
const cells = new Map(linked.cells.map((cell) => [cell.id, cell]));
|
|
301
|
+
const functionInitializers = [];
|
|
302
|
+
const functions = [];
|
|
303
|
+
const globalBindings = [];
|
|
304
|
+
const intrinsicInitializers = [];
|
|
305
|
+
const importMetaInitializers = [];
|
|
306
|
+
const jsonInitializers = [];
|
|
307
|
+
const moduleBodies = /* @__PURE__ */ new Map();
|
|
308
|
+
const varInitializers = [];
|
|
309
|
+
let { nextBindingId, nextFunctionId } = ids;
|
|
310
|
+
const namespaceBindings = /* @__PURE__ */ new Map();
|
|
311
|
+
const namespaceInitializers = [];
|
|
312
|
+
for (const module of linked.modules) for (const imported of module.imports) {
|
|
313
|
+
const targetId = imported.namespaceModuleId;
|
|
314
|
+
if (targetId == null || namespaceBindings.has(targetId)) continue;
|
|
315
|
+
const target = linkedModules.get(targetId);
|
|
316
|
+
const targetNode = nodes.get(targetId);
|
|
317
|
+
if (target == null || targetNode == null) throw new Error(`Namespace module '${targetId}' is unavailable.`);
|
|
318
|
+
const bindingId = imported.cellId;
|
|
319
|
+
if (bindingId == null) throw new Error("Module namespace cell is unavailable.");
|
|
320
|
+
namespaceBindings.set(targetId, bindingId);
|
|
321
|
+
const targetRange = retainModuleSource(moduleNodeRange(targetNode), targetId);
|
|
322
|
+
namespaceInitializers.push(moduleNamespaceInitializer(target, bindingId, targetRange));
|
|
323
|
+
}
|
|
324
|
+
for (const target of linked.modules) {
|
|
325
|
+
const bindingId = target.namespaceCellId;
|
|
326
|
+
if (bindingId == null || namespaceBindings.has(target.canonicalId)) continue;
|
|
327
|
+
const targetNode = nodes.get(target.canonicalId);
|
|
328
|
+
if (targetNode == null) throw new Error(`Namespace module '${target.canonicalId}' is unavailable.`);
|
|
329
|
+
namespaceBindings.set(target.canonicalId, bindingId);
|
|
330
|
+
namespaceInitializers.push(moduleNamespaceInitializer(target, bindingId, retainModuleSource(moduleNodeRange(targetNode), target.canonicalId)));
|
|
331
|
+
}
|
|
332
|
+
for (const moduleId of tableOrder) {
|
|
333
|
+
const node = nodes.get(moduleId);
|
|
334
|
+
const linkedModule = linkedModules.get(moduleId);
|
|
335
|
+
if (node == null || linkedModule == null) throw new Error(`Linked module '${moduleId}' is unavailable.`);
|
|
336
|
+
if (node.moduleKind === "builtin") {
|
|
337
|
+
const range = moduleNodeRange(node);
|
|
338
|
+
for (const cellId of linkedModule.cellIds) {
|
|
339
|
+
const cell = cells.get(cellId);
|
|
340
|
+
if (cell == null) throw new Error("Missing builtin export cell.");
|
|
341
|
+
const exportIndex = builtinModuleExports[node.builtin].indexOf(cell.localName);
|
|
342
|
+
if (exportIndex < 0) continue;
|
|
343
|
+
globalBindings.push({
|
|
344
|
+
id: cellId,
|
|
345
|
+
name: cell.localName
|
|
346
|
+
});
|
|
347
|
+
intrinsicInitializers.push({
|
|
348
|
+
kind: "const",
|
|
349
|
+
hint: void 0,
|
|
350
|
+
bindingId: cellId,
|
|
351
|
+
name: cell.localName,
|
|
352
|
+
range,
|
|
353
|
+
initializer: {
|
|
354
|
+
kind: "call",
|
|
355
|
+
target: { kind: "builtin-export" },
|
|
356
|
+
arguments: [moduleNumber(node.builtin === "node:process" ? 0 : 1, range), moduleNumber(exportIndex, range)],
|
|
357
|
+
range
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
moduleBodies.set(moduleId, []);
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
365
|
+
for (const imported of linkedModule.imports) {
|
|
366
|
+
if (imported.namespaceModuleId != null) {
|
|
367
|
+
const bindingId = namespaceBindings.get(imported.namespaceModuleId);
|
|
368
|
+
if (bindingId == null) throw new Error("Module namespace binding is unavailable.");
|
|
369
|
+
bindings.set(imported.localName, {
|
|
370
|
+
id: bindingId,
|
|
371
|
+
importedBinding: true,
|
|
372
|
+
mutable: false,
|
|
373
|
+
name: imported.localName
|
|
374
|
+
});
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
if (imported.cellId == null) continue;
|
|
378
|
+
bindings.set(imported.localName, {
|
|
379
|
+
id: imported.cellId,
|
|
380
|
+
importedBinding: true,
|
|
381
|
+
mutable: false,
|
|
382
|
+
name: imported.localName
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
for (const cellId of linkedModule.cellIds) {
|
|
386
|
+
const cell = cells.get(cellId);
|
|
387
|
+
if (cell == null) throw new Error(`Module cell '${cellId}' is missing.`);
|
|
388
|
+
if (bindings.has(cell.localName)) return { diagnostics: [sourceDiagnostic(moduleId, declarationNamed(moduleNodeSyntax(node).body, cell.localName) ?? { range: moduleNodeRange(node) }, `Duplicate declaration '${cell.localName}'.`)] };
|
|
389
|
+
bindings.set(cell.localName, {
|
|
390
|
+
id: cell.id,
|
|
391
|
+
mutable: false,
|
|
392
|
+
name: cell.localName,
|
|
393
|
+
pendingDeclaration: true
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
const result = buildSeededHir({
|
|
397
|
+
body: moduleProgramBody(moduleNodeSyntax(node)),
|
|
398
|
+
kind: "program",
|
|
399
|
+
range: moduleNodeRange(node),
|
|
400
|
+
sourceId: moduleId,
|
|
401
|
+
strict: true
|
|
402
|
+
}, {
|
|
403
|
+
bindings,
|
|
404
|
+
globalObjectBindingIds: outer?.globalObjectBindingIds ?? [],
|
|
405
|
+
importCallTargets: importCallTargetsFor(graph, moduleIndices, moduleId, "module"),
|
|
406
|
+
moduleBody: true,
|
|
407
|
+
nextBindingId,
|
|
408
|
+
nextFunctionId,
|
|
409
|
+
...includePropertiesWhen(() => {
|
|
410
|
+
if (outer == null) return void 0;
|
|
411
|
+
return { outerBindings: outer.bindings };
|
|
412
|
+
})
|
|
413
|
+
});
|
|
414
|
+
nextBindingId = result.nextBindingId;
|
|
415
|
+
nextFunctionId = result.nextFunctionId;
|
|
416
|
+
if (result.program == null) return { diagnostics: result.diagnostics };
|
|
417
|
+
const moduleBody = retainModuleSource(result.program.body, moduleId);
|
|
418
|
+
globalBindings.push(...result.program.globalBindings ?? []);
|
|
419
|
+
globalBindings.push(...collectHirBindings(moduleBody));
|
|
420
|
+
const evaluationBody = [];
|
|
421
|
+
const varNames = new Set(moduleNodeSyntax(node).varNames);
|
|
422
|
+
for (const statement of moduleBody) if (statement.kind === "let" && varNames.has(statement.name)) {
|
|
423
|
+
if (statement.initializer.kind !== "undefined") throw new Error(`Module var '${statement.name}' is not hoisted.`);
|
|
424
|
+
varInitializers.push(statement);
|
|
425
|
+
} else if (statement.kind === "const" && statement.bindingId === result.program.intrinsicGlobalObjectBindingId) intrinsicInitializers.push(statement);
|
|
426
|
+
else if (statement.kind === "const" && statement.bindingId === result.program.importMetaBindingId) importMetaInitializers.push(statement);
|
|
427
|
+
else if (statement.kind === "function-init") functionInitializers.push(statement);
|
|
428
|
+
else evaluationBody.push(statement);
|
|
429
|
+
if (node.moduleKind === "json") {
|
|
430
|
+
const declaration = evaluationBody[0];
|
|
431
|
+
if (evaluationBody.length !== 1 || declaration?.kind !== "const") throw new Error("JSON module must construct one default value.");
|
|
432
|
+
const dataId = nextBindingId;
|
|
433
|
+
nextBindingId += 1;
|
|
434
|
+
const dataName = `*json-data:${moduleId}*`;
|
|
435
|
+
globalBindings.push({
|
|
436
|
+
id: dataId,
|
|
437
|
+
name: dataName
|
|
438
|
+
});
|
|
439
|
+
jsonInitializers.push({
|
|
440
|
+
...declaration,
|
|
441
|
+
bindingId: dataId,
|
|
442
|
+
name: dataName
|
|
443
|
+
}, {
|
|
444
|
+
...declaration,
|
|
445
|
+
initializer: moduleUndefined(declaration.range),
|
|
446
|
+
kind: "let"
|
|
447
|
+
});
|
|
448
|
+
moduleBodies.set(moduleId, [{
|
|
449
|
+
expression: {
|
|
450
|
+
bindingId: declaration.bindingId,
|
|
451
|
+
kind: "binding-set",
|
|
452
|
+
mutable: true,
|
|
453
|
+
name: declaration.name,
|
|
454
|
+
range: declaration.range,
|
|
455
|
+
value: {
|
|
456
|
+
bindingId: dataId,
|
|
457
|
+
kind: "binding",
|
|
458
|
+
name: dataName,
|
|
459
|
+
range: declaration.range
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
kind: "expression",
|
|
463
|
+
range: declaration.range
|
|
464
|
+
}]);
|
|
465
|
+
} else moduleBodies.set(moduleId, evaluationBody);
|
|
466
|
+
functions.push(...retainModuleSource(result.program.functions, moduleId));
|
|
467
|
+
}
|
|
468
|
+
const tableArguments = [];
|
|
469
|
+
for (const moduleId of tableOrder) {
|
|
470
|
+
const node = nodes.get(moduleId);
|
|
471
|
+
const moduleBody = moduleBodies.get(moduleId);
|
|
472
|
+
if (node == null || moduleBody == null) throw new Error(`Module evaluation '${moduleId}' is unavailable.`);
|
|
473
|
+
const range = retainModuleSource(moduleNodeRange(node), moduleId);
|
|
474
|
+
const hasTopLevelAwait = hirStatementsAreAsynchronous(moduleBody);
|
|
475
|
+
const evaluator = {
|
|
476
|
+
body: [...moduleBody, {
|
|
477
|
+
expression: moduleUndefined(range),
|
|
478
|
+
kind: "return",
|
|
479
|
+
range
|
|
480
|
+
}],
|
|
481
|
+
functionLength: 0,
|
|
482
|
+
functionKind: hasTopLevelAwait ? "async-arrow" : "arrow",
|
|
483
|
+
id: nextFunctionId,
|
|
484
|
+
kind: "hir-function",
|
|
485
|
+
localBindingIds: [],
|
|
486
|
+
name: `*module:${moduleId}*`,
|
|
487
|
+
parameters: [],
|
|
488
|
+
range,
|
|
489
|
+
returnHints: [],
|
|
490
|
+
strict: true
|
|
491
|
+
};
|
|
492
|
+
nextFunctionId += 1;
|
|
493
|
+
functions.push(evaluator);
|
|
494
|
+
const requested = node.dependencies.map((dependency) => {
|
|
495
|
+
const index = moduleIndices.get(dependency.canonicalId);
|
|
496
|
+
if (index == null) throw new Error(`Module dependency '${dependency.canonicalId}' is unavailable.`);
|
|
497
|
+
return index;
|
|
498
|
+
});
|
|
499
|
+
const namespaceBinding = namespaceBindings.get(moduleId);
|
|
500
|
+
tableArguments.push(moduleFunctionExpression(evaluator), moduleNumber(hasTopLevelAwait ? 1 : 0, range), namespaceBinding == null ? moduleUndefined(range) : {
|
|
501
|
+
bindingId: namespaceBinding,
|
|
502
|
+
kind: "binding",
|
|
503
|
+
name: `*namespace:${moduleId}*`,
|
|
504
|
+
range
|
|
505
|
+
}, moduleNumber(requested.length, range), ...requested.map((index) => moduleNumber(index, range)));
|
|
506
|
+
}
|
|
507
|
+
return {
|
|
508
|
+
functions,
|
|
509
|
+
globalBindings,
|
|
510
|
+
instantiation: [
|
|
511
|
+
...intrinsicInitializers,
|
|
512
|
+
...importMetaInitializers,
|
|
513
|
+
...namespaceInitializers,
|
|
514
|
+
...jsonInitializers,
|
|
515
|
+
...varInitializers,
|
|
516
|
+
...functionInitializers
|
|
517
|
+
],
|
|
518
|
+
nextBindingId,
|
|
519
|
+
nextFunctionId,
|
|
520
|
+
tableArguments
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Link and lower a closed module graph to shared-cell scheduled MIR.
|
|
525
|
+
* A `prelude` Script runs to completion in the same realm before any
|
|
526
|
+
* module links or evaluates; modules resolve its global declarations as
|
|
527
|
+
* names of their outer global environment. The table does not exist
|
|
528
|
+
* while the prelude runs, so an `import()` in it keeps the profile
|
|
529
|
+
* diagnostic.
|
|
530
|
+
*
|
|
531
|
+
* The table holds every discovered module. A literal `import()` site
|
|
532
|
+
* lowers to the runtime import operation with its target's constant
|
|
533
|
+
* table index; modules that only such sites reach are linked and
|
|
534
|
+
* instantiated at startup but evaluate only on request.
|
|
535
|
+
*/
|
|
536
|
+
function compileModuleGraph(graph, options = {}, prelude) {
|
|
537
|
+
const linked = linkModuleGraph(graph);
|
|
538
|
+
if (linked.graph == null) return { diagnostics: linked.diagnostics };
|
|
539
|
+
const tableOrder = moduleTableOrder(linked.graph);
|
|
540
|
+
const moduleIndices = new Map(tableOrder.map((moduleId, index) => [moduleId, index]));
|
|
541
|
+
const entryIndex = moduleIndices.get(graph.entryId);
|
|
542
|
+
if (entryIndex == null) throw new Error("The module entry has no evaluation index.");
|
|
543
|
+
let nextBindingId = linked.graph.cells.length;
|
|
544
|
+
let nextFunctionId = 0;
|
|
545
|
+
let resolvedPrelude;
|
|
546
|
+
if (prelude != null) {
|
|
547
|
+
const result = resolveModulePrelude(prelude, nextBindingId);
|
|
548
|
+
if (result.prelude == null) return {
|
|
549
|
+
diagnostics: result.diagnostics,
|
|
550
|
+
graph: linked.graph
|
|
551
|
+
};
|
|
552
|
+
resolvedPrelude = result.prelude;
|
|
553
|
+
nextBindingId = result.nextBindingId;
|
|
554
|
+
nextFunctionId = result.nextFunctionId;
|
|
555
|
+
}
|
|
556
|
+
const parts = compileModuleTable(graph, linked.graph, tableOrder, moduleIndices, {
|
|
557
|
+
nextBindingId,
|
|
558
|
+
nextFunctionId
|
|
559
|
+
}, resolvedPrelude);
|
|
560
|
+
if ("diagnostics" in parts) return {
|
|
561
|
+
diagnostics: parts.diagnostics,
|
|
562
|
+
graph: linked.graph
|
|
563
|
+
};
|
|
564
|
+
const entry = graph.modules.find((module) => module.canonicalId === graph.entryId);
|
|
565
|
+
if (entry == null) throw new Error("The module entry is unavailable.");
|
|
566
|
+
const entryRange = retainModuleSource(moduleNodeRange(entry), graph.entryId);
|
|
567
|
+
const preludeHir = resolvedPrelude?.hir;
|
|
568
|
+
const hir = {
|
|
569
|
+
body: [
|
|
570
|
+
...preludeHir?.body ?? [],
|
|
571
|
+
...parts.instantiation,
|
|
572
|
+
{
|
|
573
|
+
expression: {
|
|
574
|
+
arguments: [
|
|
575
|
+
moduleNumber(entryIndex, entryRange),
|
|
576
|
+
moduleNumber(tableOrder.length, entryRange),
|
|
577
|
+
...parts.tableArguments
|
|
578
|
+
],
|
|
579
|
+
kind: "call",
|
|
580
|
+
range: entryRange,
|
|
581
|
+
target: { kind: "module-evaluate" }
|
|
582
|
+
},
|
|
583
|
+
kind: "return",
|
|
584
|
+
range: entryRange
|
|
585
|
+
}
|
|
586
|
+
],
|
|
587
|
+
functions: [...preludeHir?.functions ?? [], ...parts.functions],
|
|
588
|
+
globalBindings: [...preludeHir?.globalBindings ?? [], ...parts.globalBindings],
|
|
589
|
+
...includePropertiesWhen(() => {
|
|
590
|
+
if (preludeHir == null) return void 0;
|
|
591
|
+
return {
|
|
592
|
+
globalLexicalNames: preludeHir.globalLexicalNames ?? [],
|
|
593
|
+
globalObjectBindings: preludeHir.globalObjectBindings ?? []
|
|
594
|
+
};
|
|
595
|
+
}),
|
|
596
|
+
kind: "hir-program",
|
|
597
|
+
range: entryRange,
|
|
598
|
+
sourceId: graph.entryId,
|
|
599
|
+
strict: preludeHir == null || preludeHir.strict === true
|
|
600
|
+
};
|
|
601
|
+
return {
|
|
602
|
+
diagnostics: [],
|
|
603
|
+
graph: linked.graph,
|
|
604
|
+
hir,
|
|
605
|
+
mir: buildMir(hir, options)
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Resolve a Script whose `import()` calls name modules of `requests`.
|
|
610
|
+
* The program instantiates every module environment and installs the
|
|
611
|
+
* module table without evaluating any module, then runs the Script; each
|
|
612
|
+
* module evaluates on its first request. The modules resolve the
|
|
613
|
+
* Script's global declarations as names of their outer environment.
|
|
614
|
+
*/
|
|
615
|
+
function buildScriptGraphHir(script, requests, seed = {}) {
|
|
616
|
+
const graph = {
|
|
617
|
+
entryId: "",
|
|
618
|
+
kind: "module-graph",
|
|
619
|
+
modules: requests.closure.modules,
|
|
620
|
+
requests: requests.closure.requests
|
|
621
|
+
};
|
|
622
|
+
const linked = linkModuleGraph(graph);
|
|
623
|
+
if (linked.graph == null) return {
|
|
624
|
+
diagnostics: linked.diagnostics,
|
|
625
|
+
nextBindingId: 0,
|
|
626
|
+
nextFunctionId: 0
|
|
627
|
+
};
|
|
628
|
+
const tableOrder = moduleTableOrder(linked.graph);
|
|
629
|
+
if (tableOrder.length === 0) return buildSeededHir(script, seed);
|
|
630
|
+
const moduleIndices = new Map(tableOrder.map((moduleId, index) => [moduleId, index]));
|
|
631
|
+
const resolved = buildSeededHir(script, {
|
|
632
|
+
...seed,
|
|
633
|
+
fragmentMetadata: true,
|
|
634
|
+
importCallTargets: importCallTargetsFor(graph, moduleIndices, requests.canonicalId, "script"),
|
|
635
|
+
nextBindingId: linked.graph.cells.length
|
|
636
|
+
});
|
|
637
|
+
if (resolved.program == null || resolved.scriptBindings == null) return resolved;
|
|
638
|
+
const initializing = resolved.initializingWithNames ?? [];
|
|
639
|
+
if (initializing.length > 0) return {
|
|
640
|
+
diagnostics: [withInitializingDiagnostic(script, initializing, "")],
|
|
641
|
+
nextBindingId: resolved.nextBindingId,
|
|
642
|
+
nextFunctionId: resolved.nextFunctionId
|
|
643
|
+
};
|
|
644
|
+
const parts = compileModuleTable(graph, linked.graph, tableOrder, moduleIndices, {
|
|
645
|
+
nextBindingId: resolved.nextBindingId,
|
|
646
|
+
nextFunctionId: resolved.nextFunctionId
|
|
647
|
+
}, {
|
|
648
|
+
bindings: resolved.scriptBindings,
|
|
649
|
+
globalObjectBindingIds: (resolved.program.globalObjectBindings ?? []).map((binding) => binding.id),
|
|
650
|
+
hir: resolved.program
|
|
651
|
+
});
|
|
652
|
+
if ("diagnostics" in parts) return {
|
|
653
|
+
diagnostics: parts.diagnostics,
|
|
654
|
+
nextBindingId: resolved.nextBindingId,
|
|
655
|
+
nextFunctionId: resolved.nextFunctionId
|
|
656
|
+
};
|
|
657
|
+
const range = script.range;
|
|
658
|
+
return {
|
|
659
|
+
diagnostics: [],
|
|
660
|
+
nextBindingId: parts.nextBindingId,
|
|
661
|
+
nextFunctionId: parts.nextFunctionId,
|
|
662
|
+
program: {
|
|
663
|
+
...resolved.program,
|
|
664
|
+
body: [
|
|
665
|
+
...parts.instantiation,
|
|
666
|
+
{
|
|
667
|
+
expression: {
|
|
668
|
+
arguments: [
|
|
669
|
+
moduleNumber(tableOrder.length, range),
|
|
670
|
+
moduleNumber(tableOrder.length, range),
|
|
671
|
+
...parts.tableArguments
|
|
672
|
+
],
|
|
673
|
+
kind: "call",
|
|
674
|
+
range,
|
|
675
|
+
target: { kind: "module-evaluate" }
|
|
676
|
+
},
|
|
677
|
+
kind: "expression",
|
|
678
|
+
range
|
|
679
|
+
},
|
|
680
|
+
...resolved.program.body
|
|
681
|
+
],
|
|
682
|
+
functions: [...resolved.program.functions, ...parts.functions],
|
|
683
|
+
globalBindings: [...resolved.program.globalBindings ?? [], ...parts.globalBindings]
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
//#endregion
|
|
689
|
+
export { buildScriptGraphHir, compileModuleGraph };
|
|
690
|
+
//# sourceMappingURL=module-compile.js.map
|