hclang 0.20.1 → 0.20.2

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.
@@ -63,6 +63,27 @@ export class MemoryStore {
63
63
  return { ok: true, content };
64
64
  }
65
65
  }
66
+ /**
67
+ * The copies `canonicalEntries` made. None escapes this module except through
68
+ * `ResourceMapStore.overlay`, which hands it only back to the constructor, so a
69
+ * member is already checked and no caller can change it.
70
+ */
71
+ const CANONICAL = new WeakSet();
72
+ /** Checks every key is a canonical nonempty path, and copies the entries. */
73
+ function canonicalEntries(entries) {
74
+ if (CANONICAL.has(entries))
75
+ return entries;
76
+ const copied = new Map();
77
+ for (const [key, content] of entries) {
78
+ const normal = normalizeReferencePath(key);
79
+ if (key === "" || !normal.ok || normal.path !== key) {
80
+ throw new TypeError(`ResourceMapStore key must be a canonical nonempty path: ${JSON.stringify(key)}`);
81
+ }
82
+ copied.set(key, content);
83
+ }
84
+ CANONICAL.add(copied);
85
+ return copied;
86
+ }
66
87
  /**
67
88
  * A read-only overlay of fixed entries, keyed by canonical root-relative path,
68
89
  * on another store.
@@ -73,6 +94,8 @@ export class MemoryStore {
73
94
  * path the root plane itself would produce can be installed; a key that fails is
74
95
  * a harness defect, and the constructor throws rather than refusing. The entries
75
96
  * are copied, so the caller's map cannot change what the overlay serves.
97
+ * `overlay` checks and copies once for a harness that lays the same entries over
98
+ * many stores.
76
99
  */
77
100
  export class ResourceMapStore {
78
101
  constructor(underlying, entries) {
@@ -83,15 +106,17 @@ export class ResourceMapStore {
83
106
  value: underlying
84
107
  });
85
108
  _ResourceMapStore_entries.set(this, void 0);
86
- const copied = new Map();
87
- for (const [key, content] of entries) {
88
- const normal = normalizeReferencePath(key);
89
- if (key === "" || !normal.ok || normal.path !== key) {
90
- throw new TypeError(`ResourceMapStore key must be a canonical nonempty path: ${JSON.stringify(key)}`);
91
- }
92
- copied.set(key, content);
93
- }
94
- __classPrivateFieldSet(this, _ResourceMapStore_entries, copied, "f");
109
+ __classPrivateFieldSet(this, _ResourceMapStore_entries, canonicalEntries(entries), "f");
110
+ }
111
+ /**
112
+ * Checks and copies fixed entries once, for laying over any number of stores.
113
+ *
114
+ * @param entries The fixed entries, by canonical root-relative path.
115
+ * @returns A function that lays those entries over a store.
116
+ */
117
+ static overlay(entries) {
118
+ const fixed = canonicalEntries(entries);
119
+ return (underlying) => new ResourceMapStore(underlying, fixed);
95
120
  }
96
121
  describe() {
97
122
  return this.underlying.describe();
@@ -14,23 +14,17 @@
14
14
  * gains these reads and nothing else.
15
15
  *
16
16
  * The sources are compiled into the program from the generated
17
- * `lib/modules/standard-sources.ts` (`deno task modules:embed`), so the CLI, the
18
- * compiled binary, the npm package, `HCLang`, and the browser playground serve
19
- * the same text without reading a file at run time.
17
+ * `lib/modules/standard-sources.ts` (`deno task modules:embed`, checked by
18
+ * `deno task modules:check`), so the CLI, the compiled binary, the npm package,
19
+ * `HCLang`, and the browser playground serve the same text without reading a
20
+ * file at run time.
20
21
  *
21
22
  * @module
22
23
  */
23
24
  import { ResourceMapStore, } from "../frames/resource-store.js";
24
25
  import { STANDARD_SOURCES } from "./standard-sources.js";
25
- const MODULES = STANDARD_SOURCES;
26
- /**
27
- * The standard modules by canonical key.
28
- *
29
- * @returns Each module's source under its key.
30
- */
31
- export function standardModules() {
32
- return MODULES;
33
- }
26
+ /** Checked and copied once, so a host start or `reset()` only wraps a store. */
27
+ const overlayStandardModules = ResourceMapStore.overlay(STANDARD_SOURCES);
34
28
  /**
35
29
  * Lays the standard modules over a store as read-only entries.
36
30
  *
@@ -38,5 +32,5 @@ export function standardModules() {
38
32
  * @returns A store that serves the standard modules and delegates the rest.
39
33
  */
40
34
  export function withStandardModules(store) {
41
- return new ResourceMapStore(store, MODULES);
35
+ return overlayStandardModules(store);
42
36
  }