123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- "use strict";
- const { getOrInsert } = require("./util/MapHelpers");
- const { first } = require("./util/SetHelpers");
- const createHash = require("./util/createHash");
- const { runtimeToString, RuntimeSpecMap } = require("./util/runtime");
- class CodeGenerationResults {
-
- constructor(hashFunction = "md4") {
-
- this.map = new Map();
- this._hashFunction = hashFunction;
- }
-
- get(module, runtime) {
- const entry = this.map.get(module);
- if (entry === undefined) {
- throw new Error(
- `No code generation entry for ${module.identifier()} (existing entries: ${Array.from(
- this.map.keys(),
- m => m.identifier()
- ).join(", ")})`
- );
- }
- if (runtime === undefined) {
- if (entry.size > 1) {
- const results = new Set(entry.values());
- if (results.size !== 1) {
- throw new Error(
- `No unique code generation entry for unspecified runtime for ${module.identifier()} (existing runtimes: ${Array.from(
- entry.keys(),
- r => runtimeToString(r)
- ).join(", ")}).
- Caller might not support runtime-dependent code generation (opt-out via optimization.usedExports: "global").`
- );
- }
- return first(results);
- }
- return entry.values().next().value;
- }
- const result = entry.get(runtime);
- if (result === undefined) {
- throw new Error(
- `No code generation entry for runtime ${runtimeToString(
- runtime
- )} for ${module.identifier()} (existing runtimes: ${Array.from(
- entry.keys(),
- r => runtimeToString(r)
- ).join(", ")})`
- );
- }
- return result;
- }
-
- has(module, runtime) {
- const entry = this.map.get(module);
- if (entry === undefined) {
- return false;
- }
- if (runtime !== undefined) {
- return entry.has(runtime);
- } else if (entry.size > 1) {
- const results = new Set(entry.values());
- return results.size === 1;
- } else {
- return entry.size === 1;
- }
- }
-
- getSource(module, runtime, sourceType) {
- return this.get(module, runtime).sources.get(sourceType);
- }
-
- getRuntimeRequirements(module, runtime) {
- return this.get(module, runtime).runtimeRequirements;
- }
-
- getData(module, runtime, key) {
- const data = this.get(module, runtime).data;
- return data === undefined ? undefined : data.get(key);
- }
-
- getHash(module, runtime) {
- const info = this.get(module, runtime);
- if (info.hash !== undefined) return info.hash;
- const hash = createHash(this._hashFunction);
- for (const [type, source] of info.sources) {
- hash.update(type);
- source.updateHash(hash);
- }
- if (info.runtimeRequirements) {
- for (const rr of info.runtimeRequirements) hash.update(rr);
- }
- return (info.hash = (hash.digest("hex")));
- }
-
- add(module, runtime, result) {
- const map = getOrInsert(this.map, module, () => new RuntimeSpecMap());
- map.set(runtime, result);
- }
- }
- module.exports = CodeGenerationResults;
|