@carbonenginejs/runtime-utils 0.1.0
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 +21 -0
- package/NOTICE +23 -0
- package/README.md +56 -0
- package/THIRD-PARTY-NOTICES.md +38 -0
- package/docs/README.md +81 -0
- package/docs/architecture.md +101 -0
- package/docs/concepts/foundation-consolidation.md +86 -0
- package/docs/const-kb.md +92 -0
- package/docs/core-types/DECORATOR-TODOS.md +25 -0
- package/docs/core-types/README.md +203 -0
- package/docs/reference/api.md +70 -0
- package/docs/reference/classes/README.md +115 -0
- package/package.json +132 -0
- package/src/arrays.js +5 -0
- package/src/audio/audioFormats.js +34 -0
- package/src/audio/index.js +1 -0
- package/src/box3.js +1385 -0
- package/src/bytes.js +56 -0
- package/src/compression.js +56 -0
- package/src/constants/index.js +6 -0
- package/src/constants.js +15 -0
- package/src/curve.js +419 -0
- package/src/d3d/dxgiFormats.js +46 -0
- package/src/d3d/index.js +2 -0
- package/src/d3d/primitiveTopology.js +11 -0
- package/src/document/CjsCarbonDocument.js +212 -0
- package/src/document/CjsClassRegistry.js +373 -0
- package/src/document/CjsDocumentDehydrator.js +142 -0
- package/src/document/CjsDocumentHydrator.js +156 -0
- package/src/document/CjsStructRegistry.js +348 -0
- package/src/document/hydrationAdapter.js +129 -0
- package/src/document/index.js +6 -0
- package/src/geometry/box.js +137 -0
- package/src/geometry/cylinder.js +244 -0
- package/src/geometry/helpers/LICENSE +15 -0
- package/src/geometry/helpers/earcut.js +766 -0
- package/src/geometry/helpers/misc.js +103 -0
- package/src/geometry/index.js +8 -0
- package/src/geometry/json.js +165 -0
- package/src/geometry/lathe.js +172 -0
- package/src/geometry/octahedron.js +0 -0
- package/src/geometry/plane.js +81 -0
- package/src/geometry/shape.js +95 -0
- package/src/geometry/sphere.js +123 -0
- package/src/geometry/torus.js +96 -0
- package/src/graphics/colorSpaces.js +22 -0
- package/src/graphics/index.js +4 -0
- package/src/graphics/pixelFormats.js +158 -0
- package/src/graphics/textureDimensions.js +22 -0
- package/src/graphics/trinityEnums.js +87 -0
- package/src/index.js +58 -0
- package/src/is.js +524 -0
- package/src/json.js +23 -0
- package/src/lifecycle/CjsLifecycleState.js +77 -0
- package/src/lifecycle/index.js +1 -0
- package/src/lne3.js +497 -0
- package/src/lookup.js +48 -0
- package/src/mat3.js +131 -0
- package/src/mat4.js +699 -0
- package/src/math/index.js +25 -0
- package/src/math/scalar.js +63 -0
- package/src/media/index.js +1 -0
- package/src/media/mediaTypes.js +50 -0
- package/src/mesh.js +394 -0
- package/src/model/CjsEventEmitter.js +333 -0
- package/src/model/CjsModel.js +1544 -0
- package/src/model/CjsModelState.js +72 -0
- package/src/model/index.js +4 -0
- package/src/model/sourceRecordUtils.js +54 -0
- package/src/noise.js +310 -0
- package/src/num.js +827 -0
- package/src/path.js +21 -0
- package/src/pln.js +762 -0
- package/src/pool.js +160 -0
- package/src/quat.js +144 -0
- package/src/ray3.js +1085 -0
- package/src/renderContext/formats.js +145 -0
- package/src/renderContext/index.js +5 -0
- package/src/renderContext/presentation.js +125 -0
- package/src/renderContext/resources.js +27 -0
- package/src/renderContext/upscaling.js +22 -0
- package/src/renderContext/window.js +20 -0
- package/src/runtime/CjsRuntimeState.js +50 -0
- package/src/schema/CjsSchema.js +1009 -0
- package/src/schema/index.js +17 -0
- package/src/shader/index.js +1 -0
- package/src/shader/shaderStages.js +37 -0
- package/src/sph3.js +754 -0
- package/src/tangent.js +288 -0
- package/src/text.js +40 -0
- package/src/tri3.js +650 -0
- package/src/types/carbonTypes.js +635 -0
- package/src/types/index.js +2 -0
- package/src/utils.js +58 -0
- package/src/validation.js +46 -0
- package/src/vec2.js +229 -0
- package/src/vec3.js +1172 -0
- package/src/vec4.js +347 -0
- package/src/vertex.js +108 -0
- package/src/webgpu/index.js +1 -0
- package/src/webgpu/textureFormats.js +121 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
export const CARBON_DOCUMENT_SCHEMA = "carbon.document";
|
|
2
|
+
export const CARBON_DOCUMENT_VERSION = 1;
|
|
3
|
+
|
|
4
|
+
/** Represents one neutral Carbon document graph for hydration and dehydration. */
|
|
5
|
+
export class CjsCarbonDocument
|
|
6
|
+
{
|
|
7
|
+
constructor(options = {})
|
|
8
|
+
{
|
|
9
|
+
const normalized = CjsCarbonDocument.normalize(options);
|
|
10
|
+
|
|
11
|
+
this.schema = CARBON_DOCUMENT_SCHEMA;
|
|
12
|
+
this.version = CARBON_DOCUMENT_VERSION;
|
|
13
|
+
this.format = normalized.format;
|
|
14
|
+
this.roots = normalized.roots;
|
|
15
|
+
this.nodes = normalized.nodes;
|
|
16
|
+
|
|
17
|
+
if (normalized.refs) this.refs = normalized.refs;
|
|
18
|
+
if (normalized.metadata) this.metadata = normalized.metadata;
|
|
19
|
+
if (normalized.reports) this.reports = normalized.reports;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static create(options = {})
|
|
23
|
+
{
|
|
24
|
+
return new CjsCarbonDocument(options);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static createNode(options = {})
|
|
28
|
+
{
|
|
29
|
+
return CjsCarbonDocument.normalizeNode(options);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static createRef(id)
|
|
33
|
+
{
|
|
34
|
+
return { $ref: CjsCarbonDocument.normalizeId(id) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static isDocument(value)
|
|
38
|
+
{
|
|
39
|
+
return Boolean(value && typeof value === "object" && value.schema === CARBON_DOCUMENT_SCHEMA);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static isRef(value)
|
|
43
|
+
{
|
|
44
|
+
return Boolean(value && typeof value === "object" && Object.keys(value).length === 1 && Object.hasOwn(value, "$ref"));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static normalize(value)
|
|
48
|
+
{
|
|
49
|
+
if (!value || typeof value !== "object")
|
|
50
|
+
{
|
|
51
|
+
throw new TypeError("CarbonDocument must be an object");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (value.schema != null && !CjsCarbonDocument.isDocument(value))
|
|
55
|
+
{
|
|
56
|
+
throw new TypeError(`Unsupported document schema: ${String(value.schema || "unknown")}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return CjsCarbonDocument.normalizeOptions(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static normalizeOptions(options = {})
|
|
63
|
+
{
|
|
64
|
+
const result = {
|
|
65
|
+
schema: CARBON_DOCUMENT_SCHEMA,
|
|
66
|
+
version: CARBON_DOCUMENT_VERSION,
|
|
67
|
+
format: CjsCarbonDocument.normalizeFormat(options.format),
|
|
68
|
+
roots: CjsCarbonDocument.normalizeRoots(options.roots),
|
|
69
|
+
nodes: CjsCarbonDocument.normalizeNodes(options.nodes)
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const refs = CjsCarbonDocument.normalizeRefs(options.refs);
|
|
73
|
+
const metadata = CjsCarbonDocument.normalizeRecord(options.metadata);
|
|
74
|
+
const reports = CjsCarbonDocument.normalizeReports(options.reports);
|
|
75
|
+
|
|
76
|
+
if (!CjsCarbonDocument.isEmptyRecord(refs)) result.refs = refs;
|
|
77
|
+
if (!CjsCarbonDocument.isEmptyRecord(metadata)) result.metadata = metadata;
|
|
78
|
+
if (reports.length) result.reports = reports;
|
|
79
|
+
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static normalizeNodes(nodes = [])
|
|
84
|
+
{
|
|
85
|
+
return CjsCarbonDocument.normalizeList(nodes).map(CjsCarbonDocument.normalizeNode);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static normalizeRoots(roots = [])
|
|
89
|
+
{
|
|
90
|
+
return CjsCarbonDocument.normalizeList(roots).map((root, index) => CjsCarbonDocument.normalizeRoot(root, index));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static normalizeRefs(refs = {})
|
|
94
|
+
{
|
|
95
|
+
return CjsCarbonDocument.normalizeRecord(refs);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static normalizeMetadata(metadata = {})
|
|
99
|
+
{
|
|
100
|
+
return CjsCarbonDocument.normalizeRecord(metadata);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static normalizeReports(reports = [])
|
|
104
|
+
{
|
|
105
|
+
return CjsCarbonDocument.normalizeList(reports, report => ({ ...report }));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static normalizeId(id)
|
|
109
|
+
{
|
|
110
|
+
if (typeof id === "number" && Number.isInteger(id) && id > 0) return id;
|
|
111
|
+
|
|
112
|
+
const number = Number(id);
|
|
113
|
+
if (Number.isInteger(number) && number > 0) return number;
|
|
114
|
+
|
|
115
|
+
throw new TypeError(`Invalid CarbonDocument id: ${String(id)}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static normalizeNode(node)
|
|
119
|
+
{
|
|
120
|
+
const id = CjsCarbonDocument.normalizeId(node.id);
|
|
121
|
+
const kind = String(node.kind || node.type || "");
|
|
122
|
+
|
|
123
|
+
if (!kind)
|
|
124
|
+
{
|
|
125
|
+
throw new TypeError(`CarbonDocument node ${id} is missing a kind`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const result = {
|
|
129
|
+
id,
|
|
130
|
+
kind,
|
|
131
|
+
fields: CjsCarbonDocument.normalizeRecord(node.fields)
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const source = CjsCarbonDocument.normalizeRecord(node.source);
|
|
135
|
+
const meta = CjsCarbonDocument.normalizeRecord(node.meta);
|
|
136
|
+
const raw = CjsCarbonDocument.normalizeNullableRecord(node.raw);
|
|
137
|
+
|
|
138
|
+
if (!CjsCarbonDocument.isEmptyRecord(source)) result.source = source;
|
|
139
|
+
if (!CjsCarbonDocument.isEmptyRecord(meta)) result.meta = meta;
|
|
140
|
+
if (raw && !CjsCarbonDocument.isEmptyRecord(raw)) result.raw = raw;
|
|
141
|
+
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static normalizeRoot(root, index)
|
|
146
|
+
{
|
|
147
|
+
if (CjsCarbonDocument.isRef(root))
|
|
148
|
+
{
|
|
149
|
+
return { name: index === 0 ? "default" : `root${index}`, ref: CjsCarbonDocument.createRef(root.$ref) };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const name = String(root?.name || (index === 0 ? "default" : `root${index}`));
|
|
153
|
+
const ref = root?.ref || root?.value || root?.root;
|
|
154
|
+
|
|
155
|
+
if (!CjsCarbonDocument.isRef(ref))
|
|
156
|
+
{
|
|
157
|
+
throw new TypeError(`CarbonDocument root ${name} is missing a ref`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { name, ref: CjsCarbonDocument.createRef(ref.$ref) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static normalizeFormat(format)
|
|
164
|
+
{
|
|
165
|
+
if (typeof format === "string") return { id: format, version: 1 };
|
|
166
|
+
return {
|
|
167
|
+
id: String(format?.id || "unknown"),
|
|
168
|
+
version: format?.version ?? 1
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static clonePlainRecord(value)
|
|
173
|
+
{
|
|
174
|
+
return CjsCarbonDocument.normalizeRecord(value);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static normalizeRecord(value, defaults = {})
|
|
178
|
+
{
|
|
179
|
+
if (!CjsCarbonDocument.isObject(value) || Array.isArray(value))
|
|
180
|
+
{
|
|
181
|
+
return { ...defaults };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return { ...value, ...defaults };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static normalizeNullableRecord(value)
|
|
188
|
+
{
|
|
189
|
+
return value == null ? null : CjsCarbonDocument.normalizeRecord(value);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static normalizeList(value, map = null)
|
|
193
|
+
{
|
|
194
|
+
const list = CjsCarbonDocument.isArray(value) ? value : [];
|
|
195
|
+
return map ? list.map(map) : list.slice();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static isObject(value)
|
|
199
|
+
{
|
|
200
|
+
return value !== null && typeof value === "object";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
static isArray(value)
|
|
204
|
+
{
|
|
205
|
+
return Array.isArray(value);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static isEmptyRecord(value)
|
|
209
|
+
{
|
|
210
|
+
return !CjsCarbonDocument.isObject(value) || Array.isArray(value) || Object.keys(value).length === 0;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { CjsSchema } from "../schema/index.js";
|
|
2
|
+
|
|
3
|
+
/** Maps serialized Carbon class names to explicit runtime constructors. */
|
|
4
|
+
export class CjsClassRegistry
|
|
5
|
+
{
|
|
6
|
+
constructor(options = {})
|
|
7
|
+
{
|
|
8
|
+
const {
|
|
9
|
+
name = "registry",
|
|
10
|
+
family = "",
|
|
11
|
+
constructors = null,
|
|
12
|
+
sourceShapes = null,
|
|
13
|
+
modules = null,
|
|
14
|
+
entries = [],
|
|
15
|
+
aliases = null,
|
|
16
|
+
metadata = {}
|
|
17
|
+
} = options;
|
|
18
|
+
|
|
19
|
+
this.name = String(name || "registry");
|
|
20
|
+
this.family = String(family || "");
|
|
21
|
+
this.metadata = { ...metadata };
|
|
22
|
+
this.entries = new Map();
|
|
23
|
+
this.aliases = new Map();
|
|
24
|
+
this.reports = [];
|
|
25
|
+
|
|
26
|
+
if (constructors || sourceShapes || modules)
|
|
27
|
+
{
|
|
28
|
+
this.RegisterMaps({ constructors, sourceShapes, modules, aliases });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.RegisterMany(entries);
|
|
32
|
+
this.RegisterAliases(aliases);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get size()
|
|
36
|
+
{
|
|
37
|
+
return this.entries.size;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
RegisterMaps(options = {})
|
|
41
|
+
{
|
|
42
|
+
const {
|
|
43
|
+
constructors = {},
|
|
44
|
+
sourceShapes = {},
|
|
45
|
+
modules = {},
|
|
46
|
+
aliases = {}
|
|
47
|
+
} = options;
|
|
48
|
+
|
|
49
|
+
const names = new Set([
|
|
50
|
+
...Object.keys(constructors || {}).filter(name => typeof constructors[name] === "function"),
|
|
51
|
+
...Object.keys(sourceShapes || {}),
|
|
52
|
+
...Object.keys(modules || {})
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
for (const className of names)
|
|
56
|
+
{
|
|
57
|
+
const constructor = typeof constructors?.[className] === "function" ? constructors[className] : null;
|
|
58
|
+
const sourceShape = sourceShapes?.[className] || null;
|
|
59
|
+
const moduleEntry = modules?.[className] || null;
|
|
60
|
+
|
|
61
|
+
this.Register({
|
|
62
|
+
className,
|
|
63
|
+
constructor,
|
|
64
|
+
sourceShape,
|
|
65
|
+
folder: moduleEntry?.folder || "",
|
|
66
|
+
modulePath: moduleEntry?.modulePath || "",
|
|
67
|
+
registryEntry: moduleEntry,
|
|
68
|
+
aliases: aliasesForClassName(className, aliases),
|
|
69
|
+
family: sourceShape?.family || this.family
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
RegisterMany(entries = [])
|
|
77
|
+
{
|
|
78
|
+
const list = Array.isArray(entries) ? entries : Object.values(entries || {});
|
|
79
|
+
for (const entry of list)
|
|
80
|
+
{
|
|
81
|
+
this.Register(entry);
|
|
82
|
+
}
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
Register(entry)
|
|
87
|
+
{
|
|
88
|
+
const normalized = normalizeRegistryEntry(entry, this.family);
|
|
89
|
+
const existing = this.entries.get(normalized.className);
|
|
90
|
+
|
|
91
|
+
if (existing)
|
|
92
|
+
{
|
|
93
|
+
this.entries.set(normalized.className, mergeRegistryEntries(existing, normalized));
|
|
94
|
+
this.ReportEntryClashes(existing, normalized);
|
|
95
|
+
}
|
|
96
|
+
else
|
|
97
|
+
{
|
|
98
|
+
this.entries.set(normalized.className, normalized);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for (const alias of normalized.aliases)
|
|
102
|
+
{
|
|
103
|
+
this.RegisterAlias(alias, normalized.className);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
RegisterAliases(aliases = null)
|
|
110
|
+
{
|
|
111
|
+
if (!aliases) return this;
|
|
112
|
+
|
|
113
|
+
if (Array.isArray(aliases))
|
|
114
|
+
{
|
|
115
|
+
for (const item of aliases)
|
|
116
|
+
{
|
|
117
|
+
if (Array.isArray(item)) this.RegisterAlias(item[0], item[1]);
|
|
118
|
+
else this.RegisterAlias(item.alias, item.className || item.target);
|
|
119
|
+
}
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (const [key, value] of Object.entries(aliases))
|
|
124
|
+
{
|
|
125
|
+
if (Array.isArray(value))
|
|
126
|
+
{
|
|
127
|
+
for (const alias of value)
|
|
128
|
+
{
|
|
129
|
+
this.RegisterAlias(alias, key);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else
|
|
133
|
+
{
|
|
134
|
+
this.RegisterAlias(key, value);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
RegisterAlias(alias, className)
|
|
142
|
+
{
|
|
143
|
+
if (!alias || !className) return this;
|
|
144
|
+
|
|
145
|
+
const normalizedAlias = String(alias);
|
|
146
|
+
const normalizedClassName = String(className);
|
|
147
|
+
const existing = this.aliases.get(normalizedAlias);
|
|
148
|
+
|
|
149
|
+
if (existing && existing !== normalizedClassName)
|
|
150
|
+
{
|
|
151
|
+
this.reports.push({
|
|
152
|
+
level: "warning",
|
|
153
|
+
code: "alias-clash",
|
|
154
|
+
alias: normalizedAlias,
|
|
155
|
+
existingClassName: existing,
|
|
156
|
+
className: normalizedClassName,
|
|
157
|
+
message: `Alias ${normalizedAlias} already points to ${existing}`
|
|
158
|
+
});
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (this.entries.has(normalizedAlias) && normalizedAlias !== normalizedClassName)
|
|
163
|
+
{
|
|
164
|
+
this.reports.push({
|
|
165
|
+
level: "warning",
|
|
166
|
+
code: "alias-class-clash",
|
|
167
|
+
alias: normalizedAlias,
|
|
168
|
+
className: normalizedClassName,
|
|
169
|
+
message: `Alias ${normalizedAlias} also names a registered class`
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
this.aliases.set(normalizedAlias, normalizedClassName);
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
ResolveName(name)
|
|
178
|
+
{
|
|
179
|
+
let current = String(name || "");
|
|
180
|
+
const seen = new Set();
|
|
181
|
+
|
|
182
|
+
while (this.aliases.has(current) && !seen.has(current))
|
|
183
|
+
{
|
|
184
|
+
seen.add(current);
|
|
185
|
+
current = this.aliases.get(current);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return current;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
GetEntry(name)
|
|
192
|
+
{
|
|
193
|
+
return this.entries.get(this.ResolveName(name)) || null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
Has(name)
|
|
197
|
+
{
|
|
198
|
+
return Boolean(this.GetEntry(name));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
GetConstructor(name)
|
|
202
|
+
{
|
|
203
|
+
return this.GetEntry(name)?.constructor || null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
ResolveClass(name)
|
|
207
|
+
{
|
|
208
|
+
return this.GetConstructor(name);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
GetSourceShape(name)
|
|
212
|
+
{
|
|
213
|
+
return this.GetEntry(name)?.sourceShape || null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
GetModulePath(name)
|
|
217
|
+
{
|
|
218
|
+
return this.GetEntry(name)?.modulePath || null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
Create(name, values)
|
|
222
|
+
{
|
|
223
|
+
const ClassConstructor = this.GetConstructor(name);
|
|
224
|
+
return ClassConstructor ? new ClassConstructor(values) : null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
GetReports()
|
|
228
|
+
{
|
|
229
|
+
return this.reports.map(report => ({ ...report }));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
ToJSON()
|
|
233
|
+
{
|
|
234
|
+
return {
|
|
235
|
+
name: this.name,
|
|
236
|
+
family: this.family,
|
|
237
|
+
metadata: { ...this.metadata },
|
|
238
|
+
entries: Array.from(this.entries.values()).map(entry => ({
|
|
239
|
+
className: entry.className,
|
|
240
|
+
family: entry.family,
|
|
241
|
+
folder: entry.folder,
|
|
242
|
+
modulePath: entry.modulePath,
|
|
243
|
+
aliases: [...entry.aliases],
|
|
244
|
+
hasConstructor: Boolean(entry.constructor),
|
|
245
|
+
hasSourceShape: Boolean(entry.sourceShape)
|
|
246
|
+
})),
|
|
247
|
+
aliases: Object.fromEntries(this.aliases),
|
|
248
|
+
reports: this.GetReports()
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
[Symbol.iterator]()
|
|
253
|
+
{
|
|
254
|
+
return this.entries.values();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
ReportEntryClashes(existing, incoming)
|
|
258
|
+
{
|
|
259
|
+
if (existing.constructor && incoming.constructor && existing.constructor !== incoming.constructor)
|
|
260
|
+
{
|
|
261
|
+
this.reports.push(createEntryClash("constructor-clash", existing, incoming));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (existing.sourceShape && incoming.sourceShape && existing.sourceShape !== incoming.sourceShape)
|
|
265
|
+
{
|
|
266
|
+
this.reports.push(createEntryClash("source-shape-clash", existing, incoming));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (existing.modulePath && incoming.modulePath && existing.modulePath !== incoming.modulePath)
|
|
270
|
+
{
|
|
271
|
+
this.reports.push(createEntryClash("module-path-clash", existing, incoming));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
static from(options = {})
|
|
276
|
+
{
|
|
277
|
+
return options instanceof CjsClassRegistry ? options : new CjsClassRegistry(options);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
static fromMaps(options = {})
|
|
281
|
+
{
|
|
282
|
+
return new CjsClassRegistry(options);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function normalizeRegistryEntry(entry, defaultFamily)
|
|
287
|
+
{
|
|
288
|
+
if (!entry || typeof entry !== "object")
|
|
289
|
+
{
|
|
290
|
+
throw new TypeError("Class registry entry must be an object");
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const entryConstructor = Object.hasOwn(entry, "constructor") ? entry.constructor : null;
|
|
294
|
+
const constructor = typeof entryConstructor === "function" ? entryConstructor : null;
|
|
295
|
+
const sourceShape = entry.sourceShape || null;
|
|
296
|
+
const className = String(entry.className || entry.name || sourceShape?.className || (constructor ? CjsSchema.getClassName(constructor) : null) || "");
|
|
297
|
+
|
|
298
|
+
if (!className)
|
|
299
|
+
{
|
|
300
|
+
throw new TypeError("Class registry entry is missing a className");
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
className,
|
|
305
|
+
constructor,
|
|
306
|
+
sourceShape,
|
|
307
|
+
folder: String(entry.folder || ""),
|
|
308
|
+
modulePath: String(entry.modulePath || ""),
|
|
309
|
+
registryEntry: entry.registryEntry || null,
|
|
310
|
+
aliases: normalizeAliases(entry.aliases),
|
|
311
|
+
family: String(entry.family || sourceShape?.family || defaultFamily || ""),
|
|
312
|
+
metadata: { ...(entry.metadata || {}) }
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function mergeRegistryEntries(existing, incoming)
|
|
317
|
+
{
|
|
318
|
+
return {
|
|
319
|
+
className: existing.className,
|
|
320
|
+
constructor: existing.constructor || incoming.constructor,
|
|
321
|
+
sourceShape: existing.sourceShape || incoming.sourceShape,
|
|
322
|
+
folder: existing.folder || incoming.folder,
|
|
323
|
+
modulePath: existing.modulePath || incoming.modulePath,
|
|
324
|
+
registryEntry: existing.registryEntry || incoming.registryEntry,
|
|
325
|
+
aliases: Array.from(new Set([...existing.aliases, ...incoming.aliases])),
|
|
326
|
+
family: existing.family || incoming.family,
|
|
327
|
+
metadata: { ...existing.metadata, ...incoming.metadata }
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function normalizeAliases(aliases)
|
|
332
|
+
{
|
|
333
|
+
if (!aliases) return [];
|
|
334
|
+
if (Array.isArray(aliases)) return aliases.map(String).filter(Boolean);
|
|
335
|
+
return [String(aliases)].filter(Boolean);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function aliasesForClassName(className, aliases)
|
|
339
|
+
{
|
|
340
|
+
if (!aliases || Array.isArray(aliases)) return [];
|
|
341
|
+
|
|
342
|
+
const direct = aliases[className];
|
|
343
|
+
if (Array.isArray(direct)) return direct;
|
|
344
|
+
|
|
345
|
+
const matched = [];
|
|
346
|
+
for (const [alias, target] of Object.entries(aliases))
|
|
347
|
+
{
|
|
348
|
+
if (target === className) matched.push(alias);
|
|
349
|
+
}
|
|
350
|
+
return matched;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function createEntryClash(code, existing, incoming)
|
|
354
|
+
{
|
|
355
|
+
return {
|
|
356
|
+
level: "warning",
|
|
357
|
+
code,
|
|
358
|
+
className: existing.className,
|
|
359
|
+
message: `${existing.className} has conflicting registry metadata`,
|
|
360
|
+
existing: summarizeEntry(existing),
|
|
361
|
+
incoming: summarizeEntry(incoming)
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function summarizeEntry(entry)
|
|
366
|
+
{
|
|
367
|
+
return {
|
|
368
|
+
className: entry.className,
|
|
369
|
+
modulePath: entry.modulePath,
|
|
370
|
+
hasConstructor: Boolean(entry.constructor),
|
|
371
|
+
hasSourceShape: Boolean(entry.sourceShape)
|
|
372
|
+
};
|
|
373
|
+
}
|