@threenative/three-native 0.0.0-stage → 0.3.5
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/README.md +24 -2
- package/package.json +42 -3
- package/src/addons/buffer-geometry-utils.ts +14 -0
- package/src/addons/data-utils.ts +63 -0
- package/src/addons/decal-geometry.ts +25 -0
- package/src/addons/decal.ts +218 -0
- package/src/addons/gltf-loader-web.ts +323 -0
- package/src/addons/hdr-loader.ts +71 -0
- package/src/addons/hdr.ts +139 -0
- package/src/addons/lut-cube-loader-web.ts +17 -0
- package/src/addons/lut-cube-loader.ts +149 -0
- package/src/addons/merge-geometries.ts +374 -0
- package/src/addons/mesh-bvh-webgpu.ts +13 -0
- package/src/addons/mesh-bvh.ts +50 -0
- package/src/addons/post-effects-web.ts +22 -0
- package/src/addons/post-effects.ts +173 -0
- package/src/audio.ts +564 -0
- package/src/blend-mode.ts +33 -0
- package/src/browser-backend.ts +2156 -0
- package/src/browser-entry.ts +305 -0
- package/src/browser-math.ts +448 -0
- package/src/browser-renderer.ts +527 -0
- package/src/browser-tsl.ts +650 -0
- package/src/catalog.ts +497 -0
- package/src/fixture-format.ts +721 -0
- package/src/fixture-native.ts +426 -0
- package/src/fixture-protocol.ts +315 -0
- package/src/math-utils.ts +65 -0
- package/src/object-surface.ts +250 -0
- package/src/pass-node.ts +286 -0
- package/src/property-binding.ts +93 -0
- package/src/quad-mesh.ts +54 -0
- package/src/reflector.ts +89 -0
- package/src/refused.ts +27 -0
- package/src/render-target.ts +107 -0
- package/src/rtt.ts +158 -0
- package/src/shadow-map.ts +21 -0
- package/src/storage.ts +206 -0
- package/src/texture-sources.ts +252 -0
- package/src/tsl-extras.ts +51 -0
- package/src/tsl-struct.ts +20 -0
- package/src/tsl-uniforms.ts +234 -0
- package/src/update-ranges.ts +109 -0
- package/src/wgsl-fn.ts +55 -0
package/src/catalog.ts
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export type CatalogStatus =
|
|
5
|
+
| { readonly kind: "supported" }
|
|
6
|
+
| { readonly kind: "partial"; readonly gaps: readonly string[] }
|
|
7
|
+
| { readonly kind: "unsupported"; readonly diagnostic: string };
|
|
8
|
+
|
|
9
|
+
export interface ICatalogParameter {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly type: string;
|
|
12
|
+
readonly optional: boolean;
|
|
13
|
+
readonly default?: string;
|
|
14
|
+
readonly callback?: string;
|
|
15
|
+
readonly rest?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ICatalogOverload {
|
|
19
|
+
readonly parameters: readonly ICatalogParameter[];
|
|
20
|
+
readonly returns: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ICatalogField {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly type: string;
|
|
26
|
+
readonly mutable: boolean;
|
|
27
|
+
readonly lifetime: "owned" | "reference";
|
|
28
|
+
readonly accessor?: boolean;
|
|
29
|
+
readonly default?: string;
|
|
30
|
+
readonly note?: string;
|
|
31
|
+
readonly status?: CatalogStatus;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ICatalogMethod {
|
|
35
|
+
readonly name: string;
|
|
36
|
+
readonly async: boolean;
|
|
37
|
+
readonly overloads: readonly ICatalogOverload[];
|
|
38
|
+
readonly note?: string;
|
|
39
|
+
readonly status?: CatalogStatus;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ICatalogEntryBase {
|
|
43
|
+
readonly name: string;
|
|
44
|
+
readonly source: string;
|
|
45
|
+
readonly status: CatalogStatus;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ICatalogClassEntry extends ICatalogEntryBase {
|
|
49
|
+
readonly kind: "class";
|
|
50
|
+
readonly extends: string | null;
|
|
51
|
+
readonly constructor: readonly ICatalogParameter[];
|
|
52
|
+
readonly fields: readonly ICatalogField[];
|
|
53
|
+
readonly methods: readonly ICatalogMethod[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ICatalogTypeEntry extends ICatalogEntryBase {
|
|
57
|
+
readonly kind: "type";
|
|
58
|
+
readonly type: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ICatalogEnumEntry extends ICatalogEntryBase {
|
|
62
|
+
readonly kind: "enum";
|
|
63
|
+
readonly values: readonly { readonly name: string; readonly value: string | number }[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ICatalogConstantEntry extends ICatalogEntryBase {
|
|
67
|
+
readonly kind: "constant";
|
|
68
|
+
readonly type: "number" | "string";
|
|
69
|
+
readonly value: string | number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ICatalogFunctionEntry extends ICatalogEntryBase {
|
|
73
|
+
readonly kind: "function";
|
|
74
|
+
readonly parameters?: readonly ICatalogParameter[];
|
|
75
|
+
readonly returns?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type CatalogEntry =
|
|
79
|
+
| ICatalogClassEntry
|
|
80
|
+
| ICatalogTypeEntry
|
|
81
|
+
| ICatalogEnumEntry
|
|
82
|
+
| ICatalogConstantEntry
|
|
83
|
+
| ICatalogFunctionEntry;
|
|
84
|
+
|
|
85
|
+
export interface ICatalog {
|
|
86
|
+
readonly version: 1;
|
|
87
|
+
readonly reference: { readonly three: string; readonly types: string; readonly patch: string };
|
|
88
|
+
readonly abi: {
|
|
89
|
+
readonly engine: number;
|
|
90
|
+
readonly compatibilityContract: number;
|
|
91
|
+
readonly scene: number;
|
|
92
|
+
readonly shaderPackage: number;
|
|
93
|
+
};
|
|
94
|
+
readonly entries: readonly CatalogEntry[];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** TypeScript names a catalog type may use without being a catalog entry of its own. */
|
|
98
|
+
const TYPESCRIPT_NAMES: ReadonlySet<string> = new Set([
|
|
99
|
+
"Array",
|
|
100
|
+
"ArrayBuffer",
|
|
101
|
+
"ArrayBufferLike",
|
|
102
|
+
"ArrayBufferView",
|
|
103
|
+
"ArrayLike",
|
|
104
|
+
"BigInt",
|
|
105
|
+
"Boolean",
|
|
106
|
+
"Date",
|
|
107
|
+
"Exclude",
|
|
108
|
+
"Extract",
|
|
109
|
+
"Float32Array",
|
|
110
|
+
"Float64Array",
|
|
111
|
+
"Function",
|
|
112
|
+
"Int16Array",
|
|
113
|
+
"Int32Array",
|
|
114
|
+
"Int8Array",
|
|
115
|
+
"Map",
|
|
116
|
+
"NoInfer",
|
|
117
|
+
"NonNullable",
|
|
118
|
+
"Number",
|
|
119
|
+
"Omit",
|
|
120
|
+
"Partial",
|
|
121
|
+
"Pick",
|
|
122
|
+
"Promise",
|
|
123
|
+
"PromiseLike",
|
|
124
|
+
"Readonly",
|
|
125
|
+
"ReadonlyArray",
|
|
126
|
+
"Record",
|
|
127
|
+
"Set",
|
|
128
|
+
"String",
|
|
129
|
+
"Uint16Array",
|
|
130
|
+
"Uint32Array",
|
|
131
|
+
"Uint8Array",
|
|
132
|
+
"Uint8ClampedArray",
|
|
133
|
+
"WeakMap",
|
|
134
|
+
"WeakSet",
|
|
135
|
+
"any",
|
|
136
|
+
"boolean",
|
|
137
|
+
"false",
|
|
138
|
+
"keyof",
|
|
139
|
+
"never",
|
|
140
|
+
"null",
|
|
141
|
+
"number",
|
|
142
|
+
"object",
|
|
143
|
+
"readonly",
|
|
144
|
+
"self",
|
|
145
|
+
"string",
|
|
146
|
+
"this",
|
|
147
|
+
"true",
|
|
148
|
+
"undefined",
|
|
149
|
+
"unknown",
|
|
150
|
+
"void",
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
function record(value: unknown): Record<string, unknown> {
|
|
154
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
155
|
+
? (value as Record<string, unknown>)
|
|
156
|
+
: {};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function typeOf(value: unknown): string {
|
|
160
|
+
if (value === null) return "null";
|
|
161
|
+
if (Array.isArray(value)) return "array";
|
|
162
|
+
if (Number.isInteger(value)) return "integer";
|
|
163
|
+
return typeof value;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function matchesType(value: unknown, expected: string): boolean {
|
|
167
|
+
const actual = typeOf(value);
|
|
168
|
+
if (expected === "number") return actual === "number" || actual === "integer";
|
|
169
|
+
if (expected === "object") return actual === "object";
|
|
170
|
+
return actual === expected;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** JSON Schema keywords the catalog schema uses. Anything else in a schema is not supported. */
|
|
174
|
+
const SUPPORTED_KEYWORDS: ReadonlySet<string> = new Set([
|
|
175
|
+
"$defs",
|
|
176
|
+
"$id",
|
|
177
|
+
"$ref",
|
|
178
|
+
"$schema",
|
|
179
|
+
"additionalProperties",
|
|
180
|
+
"const",
|
|
181
|
+
"default",
|
|
182
|
+
"description",
|
|
183
|
+
"enum",
|
|
184
|
+
"items",
|
|
185
|
+
"minItems",
|
|
186
|
+
"minLength",
|
|
187
|
+
"minimum",
|
|
188
|
+
"oneOf",
|
|
189
|
+
"pattern",
|
|
190
|
+
"properties",
|
|
191
|
+
"required",
|
|
192
|
+
"title",
|
|
193
|
+
"type",
|
|
194
|
+
]);
|
|
195
|
+
|
|
196
|
+
function resolveRef(schema: Record<string, unknown>, root: Record<string, unknown>): unknown {
|
|
197
|
+
const reference = schema.$ref;
|
|
198
|
+
if (typeof reference !== "string" || !reference.startsWith("#/")) return undefined;
|
|
199
|
+
let value: unknown = root;
|
|
200
|
+
for (const key of reference.slice(2).split("/")) {
|
|
201
|
+
value = record(value)[key];
|
|
202
|
+
}
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Walks the schema subset `catalog.schema.json` uses and collects every disagreement. */
|
|
207
|
+
export function schemaErrors(
|
|
208
|
+
value: unknown,
|
|
209
|
+
schema: unknown,
|
|
210
|
+
path = "$",
|
|
211
|
+
root: unknown = schema,
|
|
212
|
+
): readonly string[] {
|
|
213
|
+
if (typeof schema !== "object" || schema === null) return [`${path}: schema is not an object`];
|
|
214
|
+
const node = record(schema);
|
|
215
|
+
const target = node.$ref === undefined ? node : record(resolveRef(node, record(root)));
|
|
216
|
+
const errors: string[] = [];
|
|
217
|
+
if (target !== node && Object.keys(target).length === 0)
|
|
218
|
+
return [`${path}: unresolved $ref ${String(node.$ref)}`];
|
|
219
|
+
if (node.$ref !== undefined && target !== node) return schemaErrors(value, target, path, root);
|
|
220
|
+
|
|
221
|
+
if (target.const !== undefined && JSON.stringify(value) !== JSON.stringify(target.const))
|
|
222
|
+
errors.push(
|
|
223
|
+
`${path}: expected ${JSON.stringify(target.const)}, found ${JSON.stringify(value)}`,
|
|
224
|
+
);
|
|
225
|
+
if (Array.isArray(target.enum) && !target.enum.some((option) => option === value))
|
|
226
|
+
errors.push(`${path}: ${JSON.stringify(value)} is not one of ${JSON.stringify(target.enum)}`);
|
|
227
|
+
if (typeof target.type === "string" && !matchesType(value, target.type))
|
|
228
|
+
errors.push(`${path}: expected ${target.type}, found ${typeOf(value)}`);
|
|
229
|
+
if (Array.isArray(target.type) && !target.type.some((option) => matchesType(value, option)))
|
|
230
|
+
errors.push(`${path}: expected one of ${target.type.join("|")}, found ${typeOf(value)}`);
|
|
231
|
+
if (typeof value === "string") {
|
|
232
|
+
if (typeof target.pattern === "string" && !new RegExp(target.pattern, "u").test(value))
|
|
233
|
+
errors.push(`${path}: ${JSON.stringify(value)} does not match ${target.pattern}`);
|
|
234
|
+
if (typeof target.minLength === "number" && value.length < target.minLength)
|
|
235
|
+
errors.push(`${path}: shorter than ${target.minLength}`);
|
|
236
|
+
}
|
|
237
|
+
if (typeof value === "number" && typeof target.minimum === "number" && value < target.minimum)
|
|
238
|
+
errors.push(`${path}: below minimum ${target.minimum}`);
|
|
239
|
+
|
|
240
|
+
if (Array.isArray(target.oneOf)) {
|
|
241
|
+
const branches = target.oneOf.map((branch) => schemaErrors(value, branch, path, root));
|
|
242
|
+
const passing = branches.filter((branch) => branch.length === 0).length;
|
|
243
|
+
if (passing === 0) {
|
|
244
|
+
// Report the branch the value came closest to matching, so the message names the real
|
|
245
|
+
// disagreement instead of the first branch's.
|
|
246
|
+
const closest = [...branches].sort((left, right) => left.length - right.length)[0];
|
|
247
|
+
errors.push(...(closest ?? [`${path}: matches no schema branch`]));
|
|
248
|
+
} else if (passing > 1) errors.push(`${path}: matches ${passing} schema branches`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const actual = record(value);
|
|
252
|
+
const properties = record(target.properties);
|
|
253
|
+
for (const key of (Array.isArray(target.required) ? target.required : []) as string[]) {
|
|
254
|
+
// `in` would be satisfied by Object.prototype for a key named `constructor`.
|
|
255
|
+
if (!Object.hasOwn(actual, key)) errors.push(`${path}: missing required property ${key}`);
|
|
256
|
+
}
|
|
257
|
+
if (target.additionalProperties === false) {
|
|
258
|
+
for (const key of Object.keys(actual)) {
|
|
259
|
+
if (!(key in properties)) errors.push(`${path}: unexpected property ${key}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
for (const [key, child] of Object.entries(properties)) {
|
|
263
|
+
if (Object.hasOwn(actual, key))
|
|
264
|
+
errors.push(...schemaErrors(actual[key], child, `${path}.${key}`, root));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (Array.isArray(value)) {
|
|
268
|
+
if (typeof target.minItems === "number" && value.length < target.minItems)
|
|
269
|
+
errors.push(`${path}: needs at least ${target.minItems} items, found ${value.length}`);
|
|
270
|
+
if (target.items !== undefined) {
|
|
271
|
+
value.forEach((item, index) =>
|
|
272
|
+
errors.push(...schemaErrors(item, target.items, `${path}[${index}]`, root)),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return errors;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The parameter signature that makes an overload ambiguous: same names, same types, same shape. */
|
|
280
|
+
function overloadSignature(overload: ICatalogOverload): string {
|
|
281
|
+
return overload.parameters
|
|
282
|
+
.map(
|
|
283
|
+
(parameter) => `${parameter.rest === true ? "..." : ""}${parameter.name}:${parameter.type}`,
|
|
284
|
+
)
|
|
285
|
+
.join(", ");
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function typeNames(text: string): readonly string[] {
|
|
289
|
+
const withoutLiterals = text.replace(/'[^']*'/gu, "''").replace(/"[^"]*"/gu, '""');
|
|
290
|
+
// Object literals, index signatures and a callback's own parameter list name no type.
|
|
291
|
+
// An object literal may nest another (`{ actions: { total: number } }`), so strip innermost first.
|
|
292
|
+
let withoutBlocks = withoutLiterals;
|
|
293
|
+
for (let previous = ""; previous !== withoutBlocks; ) {
|
|
294
|
+
previous = withoutBlocks;
|
|
295
|
+
withoutBlocks = withoutBlocks
|
|
296
|
+
.replace(/\{[^{}]*\}/gu, " ")
|
|
297
|
+
.replace(/\[[^\[\]]*\]/gu, " ")
|
|
298
|
+
.replace(/\([^()]*\)/gu, " ");
|
|
299
|
+
}
|
|
300
|
+
return [...new Set(withoutBlocks.match(/[A-Za-z_$][\w$]*/gu) ?? [])];
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function memberTypes(entry: ICatalogClassEntry): readonly string[] {
|
|
304
|
+
return [
|
|
305
|
+
...(entry.extends === null ? [] : [entry.extends]),
|
|
306
|
+
...entry.constructor.map((parameter) => parameter.type),
|
|
307
|
+
...entry.fields
|
|
308
|
+
.filter((field) => field.status?.kind !== "unsupported")
|
|
309
|
+
.map((field) => field.type),
|
|
310
|
+
...entry.methods
|
|
311
|
+
.filter((method) => method.status?.kind !== "unsupported")
|
|
312
|
+
.flatMap((method) =>
|
|
313
|
+
method.overloads.flatMap((overload) => [
|
|
314
|
+
overload.returns,
|
|
315
|
+
...overload.parameters.map((parameter) => parameter.type),
|
|
316
|
+
]),
|
|
317
|
+
),
|
|
318
|
+
];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** Rules the schema cannot express: one name per symbol, no ambiguous overloads, resolvable types. */
|
|
322
|
+
export function semanticErrors(catalog: ICatalog): readonly string[] {
|
|
323
|
+
const errors: string[] = [];
|
|
324
|
+
const byName = new Map<string, CatalogEntry>();
|
|
325
|
+
for (const entry of catalog.entries) {
|
|
326
|
+
if (byName.has(entry.name)) errors.push(`$.entries: duplicate entry ${entry.name}`);
|
|
327
|
+
byName.set(entry.name, entry);
|
|
328
|
+
}
|
|
329
|
+
const declared = (name: string): CatalogEntry | undefined => {
|
|
330
|
+
const entry = byName.get(name);
|
|
331
|
+
return entry === undefined || entry.status.kind === "unsupported" ? undefined : entry;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
for (const entry of catalog.entries) {
|
|
335
|
+
if (entry.kind !== "class") continue;
|
|
336
|
+
const members = [...entry.fields, ...entry.methods];
|
|
337
|
+
const names = new Set<string>();
|
|
338
|
+
for (const member of members) {
|
|
339
|
+
if (names.has(member.name))
|
|
340
|
+
errors.push(`$.entries[${entry.name}]: duplicate member ${member.name}`);
|
|
341
|
+
names.add(member.name);
|
|
342
|
+
}
|
|
343
|
+
for (const method of entry.methods) {
|
|
344
|
+
const seen = new Map<string, number>();
|
|
345
|
+
method.overloads.forEach((overload, index) => {
|
|
346
|
+
const signature = overloadSignature(overload);
|
|
347
|
+
const first = seen.get(signature);
|
|
348
|
+
if (first !== undefined)
|
|
349
|
+
errors.push(
|
|
350
|
+
`$.entries[${entry.name}].${method.name}: overload ${index + 1} repeats the parameter signature of overload ${first + 1}: ${JSON.stringify(signature)}`,
|
|
351
|
+
);
|
|
352
|
+
else seen.set(signature, index);
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
if (entry.extends !== null && declared(entry.extends) === undefined)
|
|
356
|
+
errors.push(
|
|
357
|
+
`$.entries[${entry.name}]: extends ${entry.extends}, which is not a published entry`,
|
|
358
|
+
);
|
|
359
|
+
for (const type of memberTypes(entry)) {
|
|
360
|
+
for (const name of typeNames(type)) {
|
|
361
|
+
if (TYPESCRIPT_NAMES.has(name) || declared(name) !== undefined) continue;
|
|
362
|
+
errors.push(
|
|
363
|
+
`$.entries[${entry.name}]: published type ${JSON.stringify(type)} names ${name}, which the catalog does not publish`,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const entry of catalog.entries) {
|
|
369
|
+
if (entry.kind !== "constant" || entry.status.kind === "unsupported") continue;
|
|
370
|
+
// Back ends hand this value to games (PRD-540), so it must parse as the type it declares.
|
|
371
|
+
if (constantValueError(entry) !== undefined)
|
|
372
|
+
errors.push(`$.entries[${entry.name}]: ${constantValueError(entry)}`);
|
|
373
|
+
}
|
|
374
|
+
for (const entry of catalog.entries) {
|
|
375
|
+
if (entry.kind !== "type" || entry.status.kind === "unsupported") continue;
|
|
376
|
+
for (const name of typeNames(entry.type)) {
|
|
377
|
+
if (TYPESCRIPT_NAMES.has(name) || declared(name) !== undefined) continue;
|
|
378
|
+
errors.push(
|
|
379
|
+
`$.entries[${entry.name}]: published type ${entry.type} names ${name}, which the catalog does not publish`,
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return errors;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function constantValueError(entry: ICatalogConstantEntry): string | undefined {
|
|
387
|
+
let value: unknown;
|
|
388
|
+
try {
|
|
389
|
+
value = typeof entry.value === "string" ? JSON.parse(entry.value) : entry.value;
|
|
390
|
+
} catch {
|
|
391
|
+
value = undefined;
|
|
392
|
+
}
|
|
393
|
+
const matches = entry.type === "number" ? typeof value === "number" : typeof value === "string";
|
|
394
|
+
return matches ? undefined : `value ${JSON.stringify(entry.value)} is not a ${entry.type}`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Validates a catalog against its schema and the rules JSON Schema cannot state.
|
|
399
|
+
*
|
|
400
|
+
* Fails closed: every disagreement is returned, and an empty array is the only pass.
|
|
401
|
+
*/
|
|
402
|
+
export function validateCatalog(catalog: unknown, schema: unknown): readonly string[] {
|
|
403
|
+
const structural = schemaErrors(catalog, schema);
|
|
404
|
+
return structural.length > 0 ? structural : semanticErrors(catalog as ICatalog);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export function formatErrors(errors: readonly string[]): string {
|
|
408
|
+
return errors.map((message) => ` ${message}`).join("\n");
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Keywords the hand-written validator ignores. A schema using one would pass unchecked. */
|
|
412
|
+
export function unsupportedSchemaKeywords(schema: unknown): readonly string[] {
|
|
413
|
+
const unknown: string[] = [];
|
|
414
|
+
const walk = (node: unknown): void => {
|
|
415
|
+
if (Array.isArray(node)) {
|
|
416
|
+
node.forEach(walk);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (typeof node !== "object" || node === null) return;
|
|
420
|
+
for (const [key, value] of Object.entries(node)) {
|
|
421
|
+
if (!SUPPORTED_KEYWORDS.has(key)) {
|
|
422
|
+
unknown.push(key);
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
// `properties` and `$defs` map names to schemas; their keys are not keywords.
|
|
426
|
+
if (key === "properties" || key === "$defs") {
|
|
427
|
+
for (const child of Object.values(record(value))) walk(child);
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
walk(value);
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
walk(schema);
|
|
434
|
+
return [...new Set(unknown)].sort();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export function catalogPaths(root: string): { catalog: string; schema: string } {
|
|
438
|
+
const directory = path.join(root, "packages", "three-native", "api");
|
|
439
|
+
return {
|
|
440
|
+
catalog: path.join(directory, "catalog.json"),
|
|
441
|
+
schema: path.join(directory, "catalog.schema.json"),
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
export function readCatalog(root: string): ICatalog {
|
|
446
|
+
const { catalog, schema } = catalogPaths(root);
|
|
447
|
+
return JSON.parse(readFileSync(catalog, "utf8")) as ICatalog;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function readCatalogSchema(root: string): unknown {
|
|
451
|
+
return JSON.parse(readFileSync(catalogPaths(root).schema, "utf8")) as unknown;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Loads and validates the committed catalog. Throws `TN_API_CATALOG_INVALID` when it disagrees. */
|
|
455
|
+
export function loadCatalog(root: string): ICatalog {
|
|
456
|
+
const catalog = readCatalog(root);
|
|
457
|
+
const errors = validateCatalog(catalog, readCatalogSchema(root));
|
|
458
|
+
if (errors.length > 0)
|
|
459
|
+
throw new Error(`TN_API_CATALOG_INVALID:\n${formatErrors(errors.slice(0, 20))}`);
|
|
460
|
+
return catalog;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** The published native surface: the entries `capabilities.json` must advertise for native. */
|
|
464
|
+
export function supportedNativeSymbols(
|
|
465
|
+
catalog: ICatalog,
|
|
466
|
+
): readonly { readonly symbol: string; readonly kind: string; readonly source: string }[] {
|
|
467
|
+
return catalog.entries
|
|
468
|
+
.filter((entry) => entry.status.kind === "supported")
|
|
469
|
+
.map((entry) => ({ symbol: entry.name, kind: entry.kind, source: entry.source }))
|
|
470
|
+
.sort((left, right) => (left.symbol < right.symbol ? -1 : left.symbol > right.symbol ? 1 : 0));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const FNV_1A_64_OFFSET = 0xcbf29ce484222325n;
|
|
474
|
+
const FNV_1A_64_PRIME = 0x100000001b3n;
|
|
475
|
+
const UINT64_MASK = 0xffffffffffffffffn;
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* The capability digest the version handshake compares: FNV-1a 64 over the UTF-8 bytes of
|
|
479
|
+
* `TN_CAPABILITY_DIGEST_PREFIX` followed by the supported entry names sorted by code unit and joined
|
|
480
|
+
* with `\n`, with no trailing newline.
|
|
481
|
+
*
|
|
482
|
+
* The prefix keeps a catalog with no supported entry from hashing to the FNV offset basis, and code
|
|
483
|
+
* unit order rather than `localeCompare` keeps two machines that built the same catalog on the same
|
|
484
|
+
* number. The engine recomputes it in C++ from `tn_engine_version()`'s names.
|
|
485
|
+
*/
|
|
486
|
+
export const TN_CAPABILITY_DIGEST_PREFIX = "tn/native/capability/v1:";
|
|
487
|
+
|
|
488
|
+
export function capabilityDigest(catalog: ICatalog): bigint {
|
|
489
|
+
const names = catalog.entries
|
|
490
|
+
.filter((entry) => entry.status.kind === "supported")
|
|
491
|
+
.map((entry) => entry.name)
|
|
492
|
+
.sort((left, right) => (left < right ? -1 : left > right ? 1 : 0));
|
|
493
|
+
const bytes = new TextEncoder().encode(`${TN_CAPABILITY_DIGEST_PREFIX}${names.join("\n")}`);
|
|
494
|
+
let hash = FNV_1A_64_OFFSET;
|
|
495
|
+
for (const byte of bytes) hash = ((hash ^ BigInt(byte)) * FNV_1A_64_PRIME) & UINT64_MASK;
|
|
496
|
+
return hash;
|
|
497
|
+
}
|