1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { runtimeToString, RuntimeSpecMap } = require("./util/runtime");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class CodeGenerationResults {
|
16 | constructor() {
|
17 |
|
18 | this.map = new Map();
|
19 | }
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | get(module, runtime) {
|
27 | const entry = this.map.get(module);
|
28 | if (entry === undefined) {
|
29 | throw new Error(
|
30 | `No code generation entry for ${module.identifier()} (existing entries: ${Array.from(
|
31 | this.map.keys(),
|
32 | m => m.identifier()
|
33 | ).join(", ")})`
|
34 | );
|
35 | }
|
36 | if (runtime === undefined) {
|
37 | const results = new Set(entry.values());
|
38 | if (results.size !== 1) {
|
39 | throw new Error(
|
40 | `No unique code generation entry for unspecified runtime for ${module.identifier()} (existing runtimes: ${Array.from(
|
41 | entry.keys(),
|
42 | r => runtimeToString(r)
|
43 | ).join(", ")}).
|
44 | Caller might not support runtime-dependent code generation (opt-out via optimization.usedExports: "global").`
|
45 | );
|
46 | }
|
47 | return results.values().next().value;
|
48 | } else {
|
49 | const result = entry.get(runtime);
|
50 | if (result === undefined) {
|
51 | throw new Error(
|
52 | `No code generation entry for runtime ${runtimeToString(
|
53 | runtime
|
54 | )} for ${module.identifier()} (existing runtimes: ${Array.from(
|
55 | entry.keys(),
|
56 | r => runtimeToString(r)
|
57 | ).join(", ")})`
|
58 | );
|
59 | }
|
60 | return result;
|
61 | }
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | getSource(module, runtime, sourceType) {
|
71 | return this.get(module, runtime).sources.get(sourceType);
|
72 | }
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | getRuntimeRequirements(module, runtime) {
|
80 | return this.get(module, runtime).runtimeRequirements;
|
81 | }
|
82 |
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | getData(module, runtime, key) {
|
90 | const data = this.get(module, runtime).data;
|
91 | return data === undefined ? undefined : data.get(key);
|
92 | }
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | add(module, runtime, result) {
|
101 | const map = this.map.get(module);
|
102 | if (map !== undefined) {
|
103 | map.set(runtime, result);
|
104 | } else {
|
105 | const newMap = new RuntimeSpecMap();
|
106 | newMap.set(runtime, result);
|
107 | this.map.set(module, newMap);
|
108 | }
|
109 | }
|
110 | }
|
111 |
|
112 | module.exports = CodeGenerationResults;
|